Package org.apache.empire.db
Class DBQuery
java.lang.Object
org.apache.empire.db.DBObject
org.apache.empire.db.DBExpr
org.apache.empire.db.DBRowSet
org.apache.empire.db.DBQuery
- All Implemented Interfaces:
Entity
This class can be used to wrap a query from a DBCommand and use it like a DBRowSet.
You may use this class for two purposes:
You may use this class for two purposes:
- In oder to define subqueries simply define a command object with the subquery and wrap it inside a DBQuery. Then in a second command object you can reference this Query to join with your other tables and views. In order to join other columns with your query use column(DBColumnExpr expr) to get the query column object for a given column expression in the underlying select clause.
- With a key supplied you can have an updateable query that will update several records at once.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.empire.db.DBRowSet
DBRowSet.FieldInitMode, DBRowSet.PartialMode -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Stringprotected final DBCommandExprprotected final DBColumn[]protected final DBColumnExpr[]protected booleanFields inherited from class org.apache.empire.db.DBRowSet
attributes, beanType, columnReferences, columns, comment, db, entityName, log, timestampColumnFields inherited from class org.apache.empire.db.DBExpr
CTX_ALIAS, CTX_ALL, CTX_DEFAULT, CTX_FULLNAME, CTX_NAME, CTX_NOPARENTHESIS, CTX_VALUE -
Constructor Summary
ConstructorsConstructorDescriptionDBQuery(DBCommandExpr cmd) Creates a DBQuery object from a given command object.DBQuery(DBCommandExpr cmd, String alias) Creates a DBQuery object from a given command object.DBQuery(DBCommandExpr cmd, DBColumn keyColumn) Constructs a new DBQuery object initialize the query object.DBQuery(DBCommandExpr cmd, DBColumn[] keyColumns) Constructor initializes the query object.DBQuery(DBCommandExpr cmd, DBColumn[] keyColumns, String alias) Constructor initializes the query object.DBQuery(DBCommandExpr cmd, DBColumn keyColumn, String alias) Constructs a new DBQuery object initialize the query object.Creates a copy of an existing DBQuery object. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanaddJoinRestriction(DBCommand cmd, DBColumn updCol, DBColumn joinCol, Object[] key, DBRecordBase record) Adds join restrictions to the supplied command object.voidaddSQL(DBSQLBuilder sql, long context) Adds the select SQL Command of this object to the specified StringBuilder object.protected DBColumncloneColumn(DBRowSet clone, DBColumn scourceColumn) Clones a RowSet columncolumn(int columnIndex) Returns the query column for a particular index Same as getColumn(columnIndex) but throws an exception if index is out of rangeReturns the query column for a given column name in the underlying querycolumn(DBColumnExpr expr) Returns the query column for a underlying column expression Use this function to map table columns to query columnsprotected DBQueryColumncreateQueryColumn(DBColumnExpr expr, int index) factory method for column expressions in order to allow overridesvoidcreateRecord(DBRecordBase record, Object[] initalKey, boolean deferredInit) Returns an error, because it is not possible to add a record to a query.voiddeleteRecord(Object[] key, DBContext context) Deletes a record identified by its primary key from the database.findColumn(String name) Deprecated.findColumn(DBColumnExpr expr) Deprecated.protected StringgenerateAlias(String prefix) Automatically generates a new alias for this ObjectgetAlias()not applicable - returns nullprotected 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 underlying command expressionprotected DBCommandreturns the command from the underlying command expression or throws an exceptionDBColumn[]getName()not applicable - returns nullgetQueryColumnExpr(int columnIndex) Returns the query column expression at a given indexReturns the select expressions of the underlying queryintgetQueryColumnIndex(ColumnExpr columnExpr) Returns the index of a column in the underlying queryDeprecated.protected Object[]getRecordKey(DBRecordBase record) Returns the record key for a given record.voidinitRecord(DBRecordBase record, RecordData recData, boolean newRecord) Add rowset databooleanReturns whether or not the table supports record updates.voidreadRecord(DBRecordBase record, DBCompareExpr whereConstraints) Creates a select SQL-Command of the query call the InitRecord method to execute the SQL-Command.voidsetUpdateable(boolean updateable) Sets whether or not records for this query can be updated Initially DBQuery records are not updateable For a query to be updateable it must have key columnsvoidupdateRecord(DBRecordBase record) Updates a query record by creating individual update commands for each table.Methods inherited from class org.apache.empire.db.DBRowSet
addColumnReference, addReferencedColumns, checkParamRecord, completeInitRecord, count, createRecordCommand, deleteAllReferences, deleteRecord, deleteReferenceRecords, equals, getAllColumns, getAttribute, getAttributes, getBeanType, getColumn, getColumn, getColumnCount, getColumnReferences, getColumns, getComment, getDatabase, getEntityName, getFullName, getIdentifier, getKeyConstraints, getRenameTablePhrase, getRowsetData, getTimestampColumn, hashCode, initClonedFields, initRecord, initRecord, initRecordDefaultValues, isColumnReadOnly, isKeyColumn, isSame, key, on, on, prepareInitRecord, readRecord, readRecord, recordExists, recordExists, setAttribute, setBeanType, setBeanType, setComment, setEntityName, setRowsetData, setTimestampColumnMethods inherited from class org.apache.empire.db.DBObject
checkParamNull
-
Field Details
-
cmdExpr
-
queryColumns
-
keyColumns
-
alias
-
updateable
protected boolean updateable
-
-
Constructor Details
-
DBQuery
Constructor initializes the query object. Saves the queryColumns and the primary key of this query.- Parameters:
cmd- the SQL-CommandkeyColumns- an array of the primary key queryColumnsalias- the query alias
-
DBQuery
Constructor initializes the query object. Saves the queryColumns and the primary key of this query.- Parameters:
cmd- the SQL-CommandkeyColumns- an array of the primary key queryColumns
-
DBQuery
Constructs a new DBQuery object initialize the query object. Save the queryColumns and the primary key of this query.- Parameters:
cmd- the SQL-CommandkeyColumn- the primary key columnalias- the query alias
-
DBQuery
Constructs a new DBQuery object initialize the query object. Save the queryColumns and the primary key of this query.- Parameters:
cmd- the SQL-CommandkeyColumn- the primary key column
-
DBQuery
Creates a DBQuery object from a given command object.- Parameters:
cmd- the command object representing an SQL-Command.alias- the query alias
-
DBQuery
Creates a DBQuery object from a given command object.- Parameters:
cmd- the command object representing an SQL-Command.
-
DBQuery
Creates a copy of an existing DBQuery object.- Parameters:
other- the query which to copynewAlias- the new alias for the cloned query
-
-
Method Details
-
generateAlias
Automatically generates a new alias for this Object- Parameters:
prefix- the alias prefix- Returns:
- an alias consisting of the prefix and a unique number
-
getCommandExpr
returns the underlying command expression- Returns:
- the command used for this query
-
getName
not applicable - returns null -
getAlias
not applicable - returns null -
isUpdateable
public boolean isUpdateable()Returns whether or not the table supports record updates.- Specified by:
isUpdateablein classDBRowSet- Returns:
- true if the query allows record updates
-
setUpdateable
public void setUpdateable(boolean updateable) Sets whether or not records for this query can be updated Initially DBQuery records are not updateable For a query to be updateable it must have key columns- Parameters:
updateable- flag whether or not the column can be updated
-
column
Returns the query column for a underlying column expression Use this function to map table columns to query columns- Parameters:
expr- the DBColumnExpr object- Returns:
- the query column
-
column
Returns the query column for a given column name in the underlying query- Parameters:
columnName- the column name in the underlying query- Returns:
- the query column
-
column
Returns the query column for a particular index Same as getColumn(columnIndex) but throws an exception if index is out of range- Parameters:
columnIndex- the index of the query column- Returns:
- the located column
-
findColumn
Deprecated.Returns the query column for a underlying column expression Deprecated: use column(expr) instead- Parameters:
expr- the DBColumnExpr object- Returns:
- the query column
-
findColumn
Deprecated.Returns the query column for a given column name Deprecated: use column(name) instead- Parameters:
name- the column name- Returns:
- the query column
-
getQueryColumns
Deprecated.Gets all columns of this rowset (e.g. for cmd.select()). Deprecated: use getColumns() or getAllColumns() instead- Returns:
- all columns of this rowset
-
getQueryColumnExprs
Returns the select expressions of the underlying query- Returns:
- the select expressions of the underlying query
-
getQueryColumnExpr
Returns the query column expression at a given index- Parameters:
columnIndex- the column index- Returns:
- the query column expression at that index
-
getQueryColumnIndex
Returns the index of a column in the underlying query- Parameters:
columnExpr- the column to find- Returns:
- the index of the column
-
getKeyColumns
- Specified by:
getKeyColumnsin interfaceEntity- Specified by:
getKeyColumnsin classDBRowSet- Returns:
- the key columns
-
getRecordKey
Returns the record key for a given record.- Parameters:
record- a record for this DBQuery- Returns:
- the record key
-
addSQL
Adds the select SQL Command of this object to the specified StringBuilder object. -
initRecord
Add rowset data- Overrides:
initRecordin classDBRowSet- 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
-
createRecord
Returns an error, because it is not possible to add a record to a query.- Specified by:
createRecordin classDBRowSet- Parameters:
record- the DBRecord object, contains all fields and the field propertiesinitalKey- the initial record keydeferredInit- flag whether to defer record initialization- Throws:
NotImplementedException- because this is not implemented
-
readRecord
Creates a select SQL-Command of the query call the InitRecord method to execute the SQL-Command.- Overrides:
readRecordin classDBRowSet- Parameters:
record- the DBRecord object, contains all fields and the field propertieswhereConstraints- the constraint for querying the record
-
updateRecord
Updates a query record by creating individual update commands for each table.- Overrides:
updateRecordin classDBRowSet- Parameters:
record- the DBRecord object. contains all fields and the field properties
-
deleteRecord
Deletes a record identified by its primary key from the database.- Specified by:
deleteRecordin classDBRowSet- Parameters:
key- array of primary key valuescontext- the database context
-
addJoinRestriction
protected boolean addJoinRestriction(DBCommand cmd, DBColumn updCol, DBColumn joinCol, Object[] key, DBRecordBase record) Adds join restrictions to the supplied command object.- Parameters:
cmd- the commandupdCol- the update columnjoinCol- the join columnkey- the record keyrecord- the record- Returns:
- flag whether the join restriction could be added
-
getCommandFromExpression
returns the command from the underlying command expression or throws an exception- Returns:
- the command used for this query
-
createQueryColumn
factory method for column expressions in order to allow overrides- Parameters:
expr- the column expression for the query columnindex- the column index- Returns:
- the query column
-
getColumnIndex
Gets the index of a particular column expression.- Overrides:
getColumnIndexin classDBRowSet- Parameters:
columnExpr- the column expression for which to get the index of- Returns:
- the position of a column expression
-
getColumnIndex
Description copied from class:DBRowSetGets the index of a particular column expression.- Overrides:
getColumnIndexin classDBRowSet- Parameters:
column- column the DBColumn to get the index for- Returns:
- the position of a column expression
-
getColumnExprAt
Description copied from class:DBRowSetReturns the column expression at a given column index Allow overrides in derived classes- Overrides:
getColumnExprAtin classDBRowSet- Parameters:
index- the index of the desired expression- Returns:
- the column expression
-
cloneColumn
Description copied from class:DBRowSetClones a RowSet column- Specified by:
cloneColumnin classDBRowSet- Parameters:
clone- the cloned rowsetscourceColumn- the source column- Returns:
- the cloned column
-