org.tentackle.db
Class LegacyDbObject

java.lang.Object
  extended by org.tentackle.db.LegacyDbObject

public abstract class LegacyDbObject
extends Object

Abstract class defining some essential basics for objects *not* derived from DbObject.
Useful to access non-tentackle-based database tables.

Author:
harald

Constructor Summary
LegacyDbObject(Db db)
          Creates a legacy db object.
 
Method Summary
 boolean alwaysPrepare()
          Determines whether prepared statements of this class should always be prepared each time when the statement used.
 boolean delete()
          Removes this object from the database.
 boolean exists()
          Tests if an object exists in the database.
Because LegacyDbObjects have no serial and no id, there is no generic way to implement isNew().
 String getClassBaseName()
          Gets the basename of the class of this object.
The basename is the class name without the package name.
 Db getDb()
          Get the logical db connection for this object.
protected  int getDeleteStatementId()
          Gets the prepared statement id for delete().
protected  int getExistsStatementId()
          Gets the prepared statement id for exists().
 int getFieldCount()
          Gets the number of db-columns.
Will perform a dummy select if not known so far.
abstract  boolean getFields(ResultSetWrapper rs)
          Retrieves the values of all fields (all columns of the database table) in the given ResultSetWrapper and stores them in the object's attributes.
protected  int getInsertStatementId()
          Gets the prepared statement id for insert().
abstract  LegacyDbObjectClassVariables getLegacyDbObjectClassVariables()
          Gets the some attributes and variables common to all objects of the same class.
 String getMultiName()
          Gets the human readable name for multiple (> 1) objects of this class.
protected  int getSelectAllStatementId()
          Gets the prepared statement id for selectAll().
 String getSingleName()
          Gets the human readable name for one (1) object of this class.
 String getSqlPrefix()
          Column names may be preceeded by a prefix.
 String getSqlPrefixWithDot()
          Gets the prefix with a trailing dot or "" if no prefix.
 String getSqlSelectAllFields()
          Gets the SQL-string to select all columns of this class (may be for joined multiple tables)
 String getTableName()
          Gets the database table name for the class of this object.
protected  int getUpdateStatementId()
          Gets the prepared statement id for update().
 boolean insert()
          Inserts this object into the database.
 boolean isGettingFieldCount()
          as getFieldCount() is invoked from getFields() we must prevent loops and heap overflow.
 boolean isPartial()
          Returns true if object is only a partial implementation.
 LegacyDbObject newObject()
          Creates a new object with the same class and same logical db connection as this object.
 void prepareDeleteStatement()
          Prepares the delete statement.
The default implementation throws a DbRuntimeException.
 void prepareExistsStatement()
          Prepare the exists statement.
The default implementation throws a DbRuntimeException.
 void prepareInsertStatement()
          Prepares the insert statement.
The default implementation throws a DbRuntimeException.
 void prepareUpdateStatement()
          Prepares the update statement.
The default implementation throws a DbRuntimeException.
 boolean readFromResultSetWrapper(ResultSetWrapper rs)
          Reads from a result-set into 'this' object.
 List<? extends LegacyDbObject> selectAll()
          Selects all objects of this class.
 ResultSetWrapper selectAllResultSet()
          Selects all objects and returns the ResultSetWrapper.
 LegacyDbObject selectNext(ResultSetWrapper rs)
          Selects the next object from the resultset.
 void setAlwaysPrepare(boolean alwaysPrepare)
          Sets the always prepare flag.
 void setDb(Db db)
          Sets the logical db connection for this object.
protected  void setDeleteStatementId(int id)
          Sets the prepared statement id for delete().
protected  void setExistsStatementId(int id)
          Sets the prepared statement id for exists().
 int setFields(PreparedStatementWrapper st)
          Sets the values of all fields (all columns of the database table) in the given PreparedStatementWrapper from the object's attributes.
protected  void setInsertStatementId(int id)
          Sets the prepared statement id for insert().
 int setKeyFields(PreparedStatementWrapper st, int ndx)
          Sets the unique keys.
If update(), delete() or exists() are implemented this method must be overridden to set the unique key-value(s).
protected  void setSelectAllStatementId(int id)
          Sets the prepared statement id for selectAll().
protected  void setUpdateStatementId(int id)
          Sets the prepared statement id for update().
 boolean update()
          Updates this object to the database.
protected  void updateFieldCount(int columnIndex)
          Update fieldCount if index greater than current fieldcount.
 boolean updateOrInsert()
          Updates the object if it exists, else inserts it.
Because transactions abort on unique violations or update failures and there is no generic concept for isNew(), we cannot provide a generic method save().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LegacyDbObject

public LegacyDbObject(Db db)
Creates a legacy db object.

Parameters:
db - the db connection
Method Detail

setDb

public void setDb(Db db)
Sets the logical db connection for this object.

Parameters:
db - the db connection

getDb

public Db getDb()
Get the logical db connection for this object.

Returns:
the db connection

getClassBaseName

public String getClassBaseName()
Gets the basename of the class of this object.
The basename is the class name without the package name.

Returns:
the basename of the Objects class

getLegacyDbObjectClassVariables

public abstract LegacyDbObjectClassVariables getLegacyDbObjectClassVariables()
Gets the some attributes and variables common to all objects of the same class. Class variables for classes derived from LegacyDbObject are kept in an instance of LegacyDbObjectClassVariables.

Returns:
the class variables

getSqlPrefix

public String getSqlPrefix()
Column names may be preceeded by a prefix. This is especially useful for joined tables auch as "table-per-class". The prefix must contain a trailing dot, just the name. The default is null.

Returns:
the SQL prefix

getSqlPrefixWithDot

public String getSqlPrefixWithDot()
Gets the prefix with a trailing dot or "" if no prefix.

Returns:
the SQL prefix

isPartial

public boolean isPartial()
Returns true if object is only a partial implementation. This is used in conjunction with PartialAppDbObject and the wurblet AppDbSuper for abstract super classes in a table-per-class mapping.

Returns:
true if partial implementation

getFields

public abstract boolean getFields(ResultSetWrapper rs)
Retrieves the values of all fields (all columns of the database table) in the given ResultSetWrapper and stores them in the object's attributes.

Parameters:
rs - the result set
Returns:
true if all fields retrieved

setFields

public int setFields(PreparedStatementWrapper st)
Sets the values of all fields (all columns of the database table) in the given PreparedStatementWrapper from the object's attributes.

Parameters:
st - the statement
Returns:
the number of fields set, -1 if error.

prepareInsertStatement

public void prepareInsertStatement()
Prepares the insert statement.
The default implementation throws a DbRuntimeException.


prepareUpdateStatement

public void prepareUpdateStatement()
Prepares the update statement.
The default implementation throws a DbRuntimeException.


prepareDeleteStatement

public void prepareDeleteStatement()
Prepares the delete statement.
The default implementation throws a DbRuntimeException.


prepareExistsStatement

public void prepareExistsStatement()
Prepare the exists statement.
The default implementation throws a DbRuntimeException.


alwaysPrepare

public boolean alwaysPrepare()
Determines whether prepared statements of this class should always be prepared each time when the statement used.

Returns:
true if always prepare

setAlwaysPrepare

public void setAlwaysPrepare(boolean alwaysPrepare)
Sets the always prepare flag.

Parameters:
alwaysPrepare - true if always prepare

getSelectAllStatementId

protected int getSelectAllStatementId()
Gets the prepared statement id for selectAll().

Returns:
the statement id

setSelectAllStatementId

protected void setSelectAllStatementId(int id)
Sets the prepared statement id for selectAll().

Parameters:
id - the statement id

getInsertStatementId

protected int getInsertStatementId()
Gets the prepared statement id for insert().

Returns:
the statement id

setInsertStatementId

protected void setInsertStatementId(int id)
Sets the prepared statement id for insert().

Parameters:
id - the statement id

getUpdateStatementId

protected int getUpdateStatementId()
Gets the prepared statement id for update().

Returns:
the statement id

setUpdateStatementId

protected void setUpdateStatementId(int id)
Sets the prepared statement id for update().

Parameters:
id - the statement id

getDeleteStatementId

protected int getDeleteStatementId()
Gets the prepared statement id for delete().

Returns:
the statement id

setDeleteStatementId

protected void setDeleteStatementId(int id)
Sets the prepared statement id for delete().

Parameters:
id - the statement id

getExistsStatementId

protected int getExistsStatementId()
Gets the prepared statement id for exists().

Returns:
the statement id

setExistsStatementId

protected void setExistsStatementId(int id)
Sets the prepared statement id for exists().

Parameters:
id - the statement id

getTableName

public String getTableName()
Gets the database table name for the class of this object.

Returns:
the table name

getSingleName

public String getSingleName()
Gets the human readable name for one (1) object of this class.

Returns:
the name

getMultiName

public String getMultiName()
Gets the human readable name for multiple (> 1) objects of this class.

Returns:
the name

newObject

public LegacyDbObject newObject()
Creates a new object with the same class and same logical db connection as this object. Must be overridden if further setup is necessary.

Returns:
the new object

getSqlSelectAllFields

public String getSqlSelectAllFields()
Gets the SQL-string to select all columns of this class (may be for joined multiple tables)

Returns:
the sql-string for "SELECT * FROM ..."

readFromResultSetWrapper

public boolean readFromResultSetWrapper(ResultSetWrapper rs)
Reads from a result-set into 'this' object. For compatability with wurblets.

Parameters:
rs - is the result set (wrapper)
Returns:
true if values read, false if some error

selectAllResultSet

public ResultSetWrapper selectAllResultSet()
Selects all objects and returns the ResultSetWrapper.

Returns:
the result set

selectNext

public LegacyDbObject selectNext(ResultSetWrapper rs)
Selects the next object from the resultset.

Parameters:
rs - the result set
Returns:
null if end of set (app should close rs hereafter!)

selectAll

public List<? extends LegacyDbObject> selectAll()
Selects all objects of this class.

Returns:
the list of objects

insert

public boolean insert()
Inserts this object into the database.

Returns:
true if inserted, false if duplicate or not saveable.

setKeyFields

public int setKeyFields(PreparedStatementWrapper st,
                        int ndx)
Sets the unique keys.
If update(), delete() or exists() are implemented this method must be overridden to set the unique key-value(s). Furthermore, the DbUpdate-wurblet must reflect these keys in a where-clause with options --maponly --append=" WHERE ... "

Parameters:
st - the prepared statement
ndx - the starting index in st
Returns:
the new index in st

update

public boolean update()
Updates this object to the database.

Returns:
true if done, false if duplicate or object is not saveable.

delete

public boolean delete()
Removes this object from the database.

Returns:
true if removed, false if failed

getFieldCount

public int getFieldCount()
Gets the number of db-columns.
Will perform a dummy select if not known so far. (assumes getFields() will compute the number of columns!)

Returns:
the number of db-columns

isGettingFieldCount

public boolean isGettingFieldCount()
as getFieldCount() is invoked from getFields() we must prevent loops and heap overflow.

Returns:
true if within getFieldCount

updateFieldCount

protected void updateFieldCount(int columnIndex)
Update fieldCount if index greater than current fieldcount.

Parameters:
columnIndex - is the result-set column index (>=1)

exists

public boolean exists()
Tests if an object exists in the database.
Because LegacyDbObjects have no serial and no id, there is no generic way to implement isNew(). However, we can query the database to check if the object exists. For this method to work, prepareExistsStatement() and setKeyFields() must be implemented.

Returns:
true if object exists

updateOrInsert

public boolean updateOrInsert()
Updates the object if it exists, else inserts it.
Because transactions abort on unique violations or update failures and there is no generic concept for isNew(), we cannot provide a generic method save(). But we can check for existance and updateOrInsert().

Returns:
true if updated or inserter


Copyright © 2001-2008 Harald Krake, Bergstr. 48, 78098 Triberg, Germany, harald@krake.de