Package org.apache.empire.db
Class DBRowSet
java.lang.Object
org.apache.empire.db.DBObject
org.apache.empire.db.DBExpr
org.apache.empire.db.DBRowSet
- All Implemented Interfaces:
Entity
- Direct Known Subclasses:
DBCommandExpr.DBCmdQuery,DBQuery,DBTable,DBView
This class is the base class for all the DBTable,
DBView and DBQuery classes this class contains all the columns of the
tables, views or queries
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumstatic enum -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Attributesprotected Class<?>protected Stringprotected final DBDatabaseprotected Stringprotected static final org.slf4j.Loggerprotected DBColumnFields inherited from class org.apache.empire.db.DBExpr
CTX_ALIAS, CTX_ALL, CTX_DEFAULT, CTX_FULLNAME, CTX_NAME, CTX_NOPARENTHESIS, CTX_VALUE -
Constructor Summary
ConstructorsConstructorDescriptionDBRowSet(DBDatabase db) Constructs a DBRecord object set the current database object. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddColumnReference(DBColumn source, DBColumn target) Adds a column reference to the list of table references.voidaddReferencedColumns(Set<DBColumn> list) Internal function to obtain all DBColumnExpr-objects used by this expression.protected voidcheckParamRecord(DBRecordBase record, boolean checkValid) Internally used for parameter checkingprotected abstract DBColumncloneColumn(DBRowSet clone, DBColumn scourceColumn) Clones a RowSet columnprotected voidcompleteInitRecord(DBRecordBase record) Completes the record initialization.
Override this function to do post initialization processing.count()Returns a new DBCountExpr object.abstract voidcreateRecord(DBRecordBase record, Object[] initalKey, boolean deferredInit) protected DBCommandcreateRecordCommand(DBContext context) Creates a new command object for record handing May use Prepared statements even if disabled in contextprotected voiddeleteAllReferences(Object[] key, DBContext context) Deletes all records which reference this table.final voiddeleteRecord(long id, DBContext context) Deletes a single record from the database.abstract voiddeleteRecord(Object[] key, DBContext context) protected voiddeleteReferenceRecords(DBRelation.DBReference[] refs, Object[] parentKey, DBContext context) Deletes all records which are referenced by a particular relation.booleanabstract StringgetAlias()DBColumn[]Returns an array of all columns of this rowsetgetAttribute(String name) Returns a named attribute for this tableReturns all metadata attributes.Class<?>returns the bean type for this rowsetgetColumn(int iColumn) Returns a DBColumn object by a specified index value.Gets the column Expression with a particular name.intReturns the number of columns in this rowsetprotected DBColumnExprgetColumnExprAt(int index) Returns the column expression at a given column index Allow overrides in derived classesintgetColumnIndex(ColumnExpr columnExpr) Gets the index of a particular column expression.intgetColumnIndex(DBColumn column) Gets the index of a particular column expression.Returns the a list of column references.Returns a list of all columns of this rowset (e.g. for cmd.select()).Returns the current DBDatabase object.Returns the entity name creating qualified names.Returns the full qualified name of the rowset.Gets an identifier for this RowSet Objectabstract DBColumn[]protected DBCompareExprgetKeyConstraints(Object[] key) Set the constraints for a single record from a supplied keyabstract StringgetName()protected StringReturns the sql phrase for renaming tables.protected final ObjectgetRowsetData(DBRecordBase record) Returns additional data stored on a record by the RowSetinthashCode()Custom serialization for transient database.protected <T extends DBRowSet>
voidinitClonedFields(T clone) Clone columnsprotected voidinitRecord(DBRecordBase record, Object[] key, DBRowSet.FieldInitMode fieldInitMode, boolean newRecord) Initializes a DBRecord for this RowSet and sets primary key values.final voidinitRecord(DBRecordBase record, RecordData recData) Initializes a DBRecord for this rowset for an existing recordvoidinitRecord(DBRecordBase record, RecordData recData, boolean newRecord) Initializes a DBRecord for this rowset using the record data provided (i.e. from a DBReader)
The record may then be modified and updated.
At least all primary key columns must be supplied.
We strongly recommend to supply the value of the update timestamp column in order to detect concurrent changes.protected voidinitRecordDefaultValues(DBRecordBase record, DBRowSet.FieldInitMode fieldInitMode) initializes the Record Default ValuesbooleanisColumnReadOnly(DBColumn col) Checks whether a column is read only or writable.booleanisKeyColumn(DBColumn column) Checks whether a given column is part of the primary key for this RowSetbooleanCompares the rowset to another oneabstract booleanstatic DBColumn[]varArgs to ArrayCreates a cross join expressionon(DBCompareExpr cmp) Creates a join expression based on a compare expressionprotected voidprepareInitRecord(DBRecordBase record, boolean newRecord) Initialize this DBRowSet object and sets it's initial state.protected voidreadRecord(DBRecordBase record, DBCommand cmd) Reads a single record from the database using the given command object.
If a record is found the DBRecord object will hold all record data.voidreadRecord(DBRecordBase record, DBCompareExpr whereConstraints) Reads a record from the databasevoidreadRecord(DBRecordBase record, DBCompareExpr whereConstraints, DBRowSet.PartialMode mode, DBColumn... columns) Reads the partial record for a given primary key from the databasebooleanrecordExists(Object[] key, DBContext context) Returns true if the record exists in the database or false otherwise.final booleanrecordExists(Object id, DBContext context) Returns true if the record exists in the database or false otherwise.<T extends DBTable>
TsetAttribute(String name, Object value) Sets the value of a attribute.voidsetBeanType(Class<?> beanType) sets the bean type for this rowset<T> voidsetBeanType(Class<T> beanType, DBBeanListFactory<T> factory) sets the bean type for this rowsetvoidsetComment(String comment) protected voidsetEntityName(String entityName) sets the entity name for creating qualified names.protected final voidsetRowsetData(DBRecordBase record, Object rowsetData) May be used by a Rowset to store additional data on a recordvoidsetTimestampColumn(DBColumn timestampColumn) <R extends DBRecordBase>
voidupdateRecord(R record) Updates or Inserts a record in the database.
Whether an update or insert is performed depends on the record state.
Only modified fields will be inserted or updated in the database.Methods inherited from class org.apache.empire.db.DBObject
checkParamNull
-
Field Details
-
log
protected static final org.slf4j.Logger log -
db
-
comment
-
entityName
-
timestampColumn
-
columnReferences
-
columns
-
attributes
-
beanType
-
-
Constructor Details
-
DBRowSet
Constructs a DBRecord object set the current database object.- Parameters:
db- the database object
-
-
Method Details
-
checkParamRecord
Internally used for parameter checking- Parameters:
record- the record to checkcheckValid- flag whether to check the validity
-
key
varArgs to Array- Parameters:
parts- the parts- Returns:
- the key columns array
-
getIdentifier
Gets an identifier for this RowSet Object- Returns:
- the rowset identifier
-
getAttribute
Returns a named attribute for this table- Parameters:
name- the attribute name- Returns:
- the attribute value
-
getAttributes
Returns all metadata attributes.- Returns:
- set of metadata attributes
-
setAttribute
Sets the value of a attribute.- Type Parameters:
T- the table type- Parameters:
name- the attribute namevalue- the value of the attribute- Returns:
- returns self (this)
-
hashCode
public int hashCode()Custom serialization for transient database. private void writeObject(ObjectOutputStream strm) throws IOException { // Database strm.writeObject(db.getIdentifier()); // write the rest strm.defaultWriteObject(); } private void readObject(ObjectInputStream strm) throws IOException, ClassNotFoundException { // Database String dbid = String.valueOf(strm.readObject()); // find database DBDatabase dbo = DBDatabase.findByIdentifier(dbid); if (dbo==null) throw new ItemNotFoundException(dbid); // set final field ClassUtils.setPrivateFieldValue(DBRowSet.class, this, "db", dbo); // read the rest strm.defaultReadObject(); } -
equals
-
isSame
Compares the rowset to another one- Parameters:
other- the rowset to compare this rowset with- Returns:
- true if it is the same rowset (but maybe a different instance)
-
getName
-
getAlias
-
isUpdateable
public abstract boolean isUpdateable() -
createRecord
-
deleteRecord
-
getKeyColumns
- Specified by:
getKeyColumnsin interfaceEntity
-
getFullName
Returns the full qualified name of the rowset.- Returns:
- the full qualified name
-
getEntityName
Returns the entity name creating qualified names. This is usually the same as "getName()" but it may be overridden to return singular instead of plural- Specified by:
getEntityNamein interfaceEntity- Returns:
- the entity name
-
setEntityName
sets the entity name for creating qualified names. This is usefull if the table or view name is plural but you want to qualifiy names in singular- Parameters:
entityName- the entity name
-
getBeanType
returns the bean type for this rowset- Specified by:
getBeanTypein interfaceEntity- Returns:
- the bean type
-
setBeanType
sets the bean type for this rowset- Parameters:
beanType- the bean type for this rowset
-
setBeanType
sets the bean type for this rowset- Type Parameters:
T- the bean type- Parameters:
beanType- the bean type for this rowsetfactory- the bean factory
-
addReferencedColumns
Description copied from class:DBExprInternal function to obtain all DBColumnExpr-objects used by this expression.- Specified by:
addReferencedColumnsin classDBExpr- Parameters:
list- list to which all used column expressions must be added- See Also:
-
getDatabase
Returns the current DBDatabase object.- Specified by:
getDatabasein classDBObject- Returns:
- the current DBDatabase object
-
getColumnCount
public int getColumnCount()Returns the number of columns in this rowset- Returns:
- the column count
-
getColumns
Returns a list of all columns of this rowset (e.g. for cmd.select()).- Specified by:
getColumnsin interfaceEntity- Returns:
- all columns of this rowset
-
getAllColumns
Returns an array of all columns of this rowset- Returns:
- all columns of this rowset
-
getColumnIndex
Gets the index of a particular column expression.- Parameters:
column- column the DBColumn to get the index for- Returns:
- the position of a column expression
-
getColumnIndex
Gets the index of a particular column expression.- Parameters:
columnExpr- the Column to get the index for- Returns:
- the position of a column expression
-
getColumn
Returns a DBColumn object by a specified index value.- Parameters:
iColumn- the index to get the DBColumn for- Returns:
- the index value
-
getColumn
Gets the column Expression with a particular name. -
isColumnReadOnly
Checks whether a column is read only or writable. Only the timestamp column is read only by default. The primary is read only if the column is of type.- Parameters:
col- the column object- Returns:
- a new DBCountExpr object
-
isKeyColumn
Checks whether a given column is part of the primary key for this RowSet- Parameters:
column- the column to check- Returns:
- true if the column is part of the primary key or false otherwise
-
getComment
- Returns:
- Returns the comment.
-
setComment
- Parameters:
comment- The comment to set.
-
getTimestampColumn
- Returns:
- Returns the timestampColumn.
-
setTimestampColumn
- Parameters:
timestampColumn- The timestampColumn to set.
-
getColumnReferences
Returns the a list of column references.- Returns:
- a list of references
-
addColumnReference
Adds a column reference to the list of table references. This method is internally called from DBDatabase.addReleation().- Parameters:
source- a column reference for one of this table's columntarget- the target column to which the source column references
-
count
Returns a new DBCountExpr object.- Returns:
- a new DBCountExpr object
-
getRenameTablePhrase
Returns the sql phrase for renaming tables. usually just a space character ' '- Returns:
- the table rename phrase
-
getColumnExprAt
Returns the column expression at a given column index Allow overrides in derived classes- Parameters:
index- the index of the desired expression- Returns:
- the column expression
-
on
Creates a join expression based on a compare expression- Parameters:
cmp- the compare expression for the join- Returns:
- the join expression
-
on
Creates a cross join expression- Parameters:
right- the rowset which to cross join- Returns:
- the cross join expression
-
initRecord
protected void initRecord(DBRecordBase record, Object[] key, DBRowSet.FieldInitMode fieldInitMode, boolean newRecord) Initializes a DBRecord for this RowSet and sets primary key values. The record may then be modified and updated.
- Parameters:
record- the Record objectkey- an array of the values for the primary keyfieldInitMode- indicates how to initializes the record fieldsnewRecord- true if the record is new or false if it is an existing record
-
initRecord
Initializes a DBRecord for this rowset using the record data provided (i.e. from a DBReader)
The record may then be modified and updated.
At least all primary key columns must be supplied.
We strongly recommend to supply the value of the update timestamp column in order to detect concurrent changes.
- Parameters:
record- the record objectrecData- the record data from which to initialized the recordnewRecord- true if the record is new or false if it is an existing record
-
initRecord
Initializes a DBRecord for this rowset for an existing record- Parameters:
record- the record objectrecData- the record data from which to initialized the record- See Also:
-
initRecordDefaultValues
initializes the Record Default Values- Parameters:
record- the recordfieldInitMode- the field initialization mode
-
prepareInitRecord
Initialize this DBRowSet object and sets it's initial state.- Parameters:
record- the DBRecord object to initialize this DBRowSet objectnewRecord- flag whether the record is new (non-existing) in the database
-
completeInitRecord
Completes the record initialization.
Override this function to do post initialization processing.- Parameters:
record- the DBRecord object to initialize
-
getKeyConstraints
Set the constraints for a single record from a supplied key- Parameters:
key- the record key- Returns:
- the primary key constraint expression
-
readRecord
Reads a single record from the database using the given command object.
If a record is found the DBRecord object will hold all record data.- Parameters:
record- the DBRecord object which holds the record datacmd- the SQL-Command used to query the record
-
readRecord
Reads a record from the database- Parameters:
record- the DBRecord object which will hold the record datawhereConstraints- the constraint for querying the record
-
readRecord
public void readRecord(DBRecordBase record, DBCompareExpr whereConstraints, DBRowSet.PartialMode mode, DBColumn... columns) Reads the partial record for a given primary key from the database- Parameters:
record- the DBRecord object which will hold the record datawhereConstraints- a compare expression for querying the recordmode- flag whether to include only the given columns or whether to add all but the given columnscolumns- the columns to include or exclude (depending on mode)
-
recordExists
Returns true if the record exists in the database or false otherwise.- Parameters:
key- an array of the primary key columnscontext- the DBContext- Returns:
- true if the record exists or false otherwise
-
recordExists
Returns true if the record exists in the database or false otherwise.- Parameters:
id- id of the recordcontext- the DBContext- Returns:
- true if the record exists or false otherwise
-
updateRecord
Updates or Inserts a record in the database.
Whether an update or insert is performed depends on the record state.
Only modified fields will be inserted or updated in the database.
If a timestamp-column is set for this RowSet then a constraint will be added in the update statement in order to detect concurrent changes.
If the record has been modified by another user, an error of type DBErrors.RecordUpdateFailed will be set.- Type Parameters:
R- the record type- Parameters:
record- the DBRecord object. contains all fields and the field properties
-
deleteRecord
Deletes a single record from the database.
- Parameters:
id- the record's primary keycontext- the DBContext
-
deleteAllReferences
Deletes all records which reference this table.- Parameters:
key- the key the record to be deletedcontext- the DBContext
-
deleteReferenceRecords
protected void deleteReferenceRecords(DBRelation.DBReference[] refs, Object[] parentKey, DBContext context) Deletes all records which are referenced by a particular relation.- Parameters:
refs- the reference columns belonging to the relationparentKey- the key of the parent elementcontext- the DBContext
-
createRecordCommand
Creates a new command object for record handing May use Prepared statements even if disabled in context- Parameters:
context- the db context- Returns:
- the command object
-
getRowsetData
Returns additional data stored on a record by the RowSet- Parameters:
record- the record- Returns:
- the rowset data
-
setRowsetData
May be used by a Rowset to store additional data on a record- Parameters:
record- the recordrowsetData- the extra data to set on this record
-
initClonedFields
Clone columns- Type Parameters:
T- the rowset type- Parameters:
clone- the cloned rowset- Throws:
CloneNotSupportedException- exception if cloning the fields fails
-
cloneColumn
Clones a RowSet column- Parameters:
clone- the cloned rowsetscourceColumn- the source column- Returns:
- the cloned column
-