org.apache.empire.db
Class DBCommand

java.lang.Object
  extended by org.apache.empire.db.DBObject
      extended by org.apache.empire.db.DBExpr
          extended by org.apache.empire.db.DBCommandExpr
              extended by org.apache.empire.db.DBCommand
All Implemented Interfaces:
Serializable, Cloneable
Direct Known Subclasses:
DBCommandOracle, DBDatabaseDriverDerby.DBCommandDerby, DBDatabaseDriverH2.DBCommandH2, DBDatabaseDriverHSql.DBCommandHSql, DBDatabaseDriverMSSQL.DBCommandMSSQL, DBDatabaseDriverMySQL.DBCommandMySQL, DBDatabaseDriverPostgreSQL.DBCommandPostreSQL, DBDatabaseDriverSQLite.DBCommandSQLite

public abstract class DBCommand
extends DBCommandExpr
implements Cloneable

This abstract class handles the creation of the SQL-Commands. There are methods to create SQL-Commands, like update, insert, delete and select.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class org.apache.empire.db.DBCommandExpr
DBCommandExpr.DBCmdColumn, DBCommandExpr.DBCmdQuery
 
Field Summary
protected  Vector<DBCmdParam> cmdParams
           
protected  List<DBColumnExpr> groupBy
           
protected  List<DBCompareExpr> having
           
protected  List<DBJoinExpr> joins
           
protected static org.slf4j.Logger log
           
protected  List<DBColumnExpr> select
           
protected  boolean selectDistinct
           
protected  List<DBSetExpr> set
           
protected  List<DBCompareExpr> where
           
 
Fields inherited from class org.apache.empire.db.DBCommandExpr
cmdQuery, orderBy
 
Fields inherited from class org.apache.empire.db.DBExpr
CTX_ALIAS, CTX_ALL, CTX_DEFAULT, CTX_FULLNAME, CTX_NAME, CTX_NOPARENTHESES, CTX_VALUE
 
Constructor Summary
protected DBCommand(DBDatabase db)
          Constructs a new DBCommand object and set the specified DBDatabase object.
 
Method Summary
protected  void addFrom(StringBuilder buf)
           
protected  void addGrouping(StringBuilder buf)
           
 void addJoins(List<DBJoinExpr> joinExprList)
          Adds a list of join expressions to the command.
protected  void addOrder(StringBuilder buf)
           
 DBCmdParam addParam()
          Adds an command parameter which will be used in a prepared statement.
 DBCmdParam addParam(DataType type, Object value)
          Adds an command parameter which will be used in a prepared statement.
 DBCmdParam addParam(DBColumnExpr colExpr, Object value)
          Adds an command parameter which will be used in a prepared statement.
 DBCmdParam addParam(Object value)
          Adds an command parameter which will be used in a prepared statement.
 void addReferencedColumns(Set<DBColumn> list)
          Adds Columns
protected  void addSelect(StringBuilder buf)
           
protected  void addWhere(StringBuilder buf)
           
protected  void addWhere(StringBuilder buf, long context)
           
 void addWhereConstraints(List<DBCompareExpr> constraints)
          Adds a list of constraints to the command.
 void clear()
          Clears the entire command object.
 void clearGroupBy()
          Clears the list of group by constraints.
 void clearHaving()
          Clears the list of having constraints.
 void clearJoin()
          Clears the list of join expressions.
 void clearSelect()
          Clears the list of selected columns.
 void clearSelectDistinct()
          Clears the select distinct option.
 void clearSet()
          Clears the list of set expressions.
 void clearWhere()
          Clears the list of where constraints.
 DBCommand clone()
          Creates a clone of this class.
 DBDatabase getDatabase()
          Returns the current DBDatabase object.
 String getDelete(DBTable table)
          Creates the delete SQL-Command.
 String getInsert()
          Creates the insert SQL-Command.
 List<DBJoinExpr> getJoins()
          Returns a copy of the defined joins.
 Object[] getParamValues()
          Returns an array of parameter values for a prepared statement.
protected  List<DBRowSet> getRowSetList()
          Gets a list of all tables referenced by the query.
 void getSelect(StringBuilder buf)
           
 DBColumnExpr[] getSelectExprList()
          Returns a array of all select DBColumnExpr for this command
 String getUpdate()
          Creates the update SQL-Command.
 List<DBCompareExpr> getWhereConstraints()
          Returns a copy of the defined where clauses.
 void groupBy(DBColumnExpr... exprs)
          Adds a list of columns to the group by phrase of an sql statement.
 boolean hasConstraintOn(DBRowSet rowset)
          Returns true if the command has a constraint on the given table or false otherwise.
 boolean hasJoinOn(DBColumn column)
          Returns true if the command has a join on the given column or false otherwise.
 boolean hasJoinOn(DBRowSet rowset)
          Returns true if the command has a join on the given table or false otherwise.
 boolean hasSelectExpr()
           
protected  boolean hasSetExprOn(DBColumn column)
          Checks whether a column is in the list of set expressions
 boolean hasWhereConstraints()
          Returns true if the command has constraints or false if not.
 void having(DBCompareExpr expr)
          adds a constraint to the having clause.
protected  boolean isPreparedStatementsEnabled()
          returns true if prepared statements are enabled for this database
 boolean isSelectDistinct()
          Returns whether or not the select statement will be distinct or not.
 boolean isValid()
           
 boolean isValidQuery()
          Returns whether the command object can produce a select sql-statement.
 boolean isValidUpdate()
          Returns whether the command object can produce a update sql-statement.
 DBJoinExpr join(DBColumnExpr left, DBColumn right)
          Adds an inner join based on two columns to the list of join expressions.
 DBJoinExpr join(DBColumnExpr left, DBColumn right, DBJoinType joinType)
          Adds a join based on two columns to the list of join expressions.
 void join(DBJoinExpr join)
          Adds a join to the list of join expressions.
 DBJoinExpr join(DBRowSet rowset, DBCompareExpr cmp)
          Adds an inner join based on a compare expression to the command.
 DBJoinExpr join(DBRowSet rowset, DBCompareExpr cmp, DBJoinType joinType)
          Adds a join based on a compare expression to the command.
protected  void notifyParamUsage(DBCmdParam param)
          internally used to reorder the command params to match their order of occurance
protected  void removeConstraintOn(List<DBCompareExpr> list, DBColumnExpr col)
          removes a constraint on a particular column to the 'where' or 'having' collections
 void removeHavingConstraintOn(DBColumnExpr col)
          removes a constraint on a particular column from the where clause
 boolean removeJoinsOn(DBColumn column)
          removes all joins to a given column
 boolean removeJoinsOn(DBRowSet rowset)
          removes all joins to a given table or view
 void removeWhereConstraintOn(DBColumnExpr col)
          removes a constraint on a particular column from the where clause
protected  void resetParamUsage()
          internally used to reset the command param usage count.
 void select(Collection<? extends DBColumnExpr> columns)
          Adds a collection of columns to the select phrase of an sql statement.
 void select(DBColumnExpr... exprs)
          Adds a list of columns to the select phrase of an sql statement.
 void select(DBColumnExpr expr)
          Adds a DBColumnExpr object to the Vector: 'select'.
 void selectDistinct()
          Sets whether or not the select statement should contain the distinct directive .
 void set(DBSetExpr expr)
          Inserts DBSetExpr objects to the Vector 'set'.
protected  void setConstraint(List<DBCompareExpr> list, DBCompareExpr expr)
          adds a constraint to the 'where' or 'having' collections
protected  boolean useCmdParam(DBColumn col, Object value)
          returns true if a cmdParam should be used for the given column or false otherwise
 void where(DBCompareExpr expr)
          Adds a compare expression to the list of constraints.
 
Methods inherited from class org.apache.empire.db.DBCommandExpr
addListExpr, addSQL, clearLimit, clearOrderBy, getCmdColumn, getInsertInto, getInsertInto, getInsertInto, getSelect, intersect, limitRows, orderBy, orderBy, orderBy, skipRows, union
 
Methods inherited from class org.apache.empire.db.DBExpr
getObjectValue, getValueClass
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static final org.slf4j.Logger log

selectDistinct

protected boolean selectDistinct

select

protected List<DBColumnExpr> select

set

protected List<DBSetExpr> set

joins

protected List<DBJoinExpr> joins

where

protected List<DBCompareExpr> where

having

protected List<DBCompareExpr> having

groupBy

protected List<DBColumnExpr> groupBy

cmdParams

protected Vector<DBCmdParam> cmdParams
Constructor Detail

DBCommand

protected DBCommand(DBDatabase db)
Constructs a new DBCommand object and set the specified DBDatabase object.

Parameters:
db - the current database object
Method Detail

resetParamUsage

protected void resetParamUsage()
internally used to reset the command param usage count. Note: Only one thread my generate an SQL statement


notifyParamUsage

protected void notifyParamUsage(DBCmdParam param)
internally used to reorder the command params to match their order of occurance


clone

public DBCommand clone()
Creates a clone of this class.

Overrides:
clone in class Object

getDatabase

public DBDatabase getDatabase()
Returns the current DBDatabase object.

Specified by:
getDatabase in class DBObject
Returns:
the current DBDatabase object

isValid

public boolean isValid()
Specified by:
isValid in class DBCommandExpr

isValidQuery

public boolean isValidQuery()
Returns whether the command object can produce a select sql-statement.

Returns:
true if at least one select expression has been set

isValidUpdate

public boolean isValidUpdate()
Returns whether the command object can produce a update sql-statement.

Returns:
true if a set expression has been set.

selectDistinct

public void selectDistinct()
Sets whether or not the select statement should contain the distinct directive .


isSelectDistinct

public boolean isSelectDistinct()
Returns whether or not the select statement will be distinct or not.

Returns:
true if the select will contain the distinct directive or false otherwise.

select

public void select(DBColumnExpr expr)
Adds a DBColumnExpr object to the Vector: 'select'.

Parameters:
expr - the DBColumnExpr object

select

public void select(DBColumnExpr... exprs)
Adds a list of columns to the select phrase of an sql statement.

Parameters:
exprs - an vararg of DBColumnExpr's to select

select

public void select(Collection<? extends DBColumnExpr> columns)
Adds a collection of columns to the select phrase of an sql statement.

Parameters:
columns - the column expressions to add

isPreparedStatementsEnabled

protected boolean isPreparedStatementsEnabled()
returns true if prepared statements are enabled for this database


useCmdParam

protected boolean useCmdParam(DBColumn col,
                              Object value)
returns true if a cmdParam should be used for the given column or false otherwise


set

public void set(DBSetExpr expr)
Inserts DBSetExpr objects to the Vector 'set'.

Parameters:
expr - the DBSetExpr object(s)

hasSetExprOn

protected boolean hasSetExprOn(DBColumn column)
Checks whether a column is in the list of set expressions

Parameters:
column -
Returns:
true if there is a set expression

addParam

public DBCmdParam addParam(DataType type,
                           Object value)
Adds an command parameter which will be used in a prepared statement. The command parameter returned may be used to alter the value.

Parameters:
type - the data type of the parameter
value - the initial parameter value
Returns:
the command parameter object

addParam

public final DBCmdParam addParam(DBColumnExpr colExpr,
                                 Object value)
Adds an command parameter which will be used in a prepared statement. The initial value of the command parameter is null but can be modified using the setValue method.

Parameters:
colExpr - the column expression for which to create the parameter
value - the initial parameter value
Returns:
the command parameter object

addParam

public final DBCmdParam addParam(Object value)
Adds an command parameter which will be used in a prepared statement. The initial value of the command parameter is null but can be modified using the setValue method.

Returns:
the command parameter object

addParam

public final DBCmdParam addParam()
Adds an command parameter which will be used in a prepared statement. The initial value of the command parameter is null but can be modified using the setValue method.

Returns:
the command parameter object

join

public void join(DBJoinExpr join)
Adds a join to the list of join expressions.

Parameters:
join - the join expression

join

public DBJoinExpr join(DBColumnExpr left,
                       DBColumn right,
                       DBJoinType joinType)
Adds a join based on two columns to the list of join expressions.

Parameters:
left - the left join value
right - the right join
joinType - type of join (DBJoinType.INNER, DBJoinType.LEFT, DBJoinType.RIGHT)
Returns:
the join expression

join

public DBJoinExpr join(DBColumnExpr left,
                       DBColumn right)
Adds an inner join based on two columns to the list of join expressions.

Parameters:
left - the left join value
right - the right join
Returns:
the join expresion

join

public DBJoinExpr join(DBRowSet rowset,
                       DBCompareExpr cmp,
                       DBJoinType joinType)
Adds a join based on a compare expression to the command.

Parameters:
rowset - table or view to join
cmp - the compare expression with wich to join the table
joinType - type of join (DBJoinType.INNER, DBJoinType.LEFT, DBJoinType.RIGHT)
Returns:
the join expresion

join

public DBJoinExpr join(DBRowSet rowset,
                       DBCompareExpr cmp)
Adds an inner join based on a compare expression to the command.

Parameters:
rowset - table of view which to join
cmp - the compare expression with wich to join the table
Returns:
the join expresion

addJoins

public void addJoins(List<DBJoinExpr> joinExprList)
Adds a list of join expressions to the command.

Parameters:
joinExprList - list of join expressions

hasJoinOn

public boolean hasJoinOn(DBRowSet rowset)
Returns true if the command has a join on the given table or false otherwise.

Parameters:
rowset - rowset table or view to join
Returns:
true if the command has a join on the given table or false otherwise

hasConstraintOn

public boolean hasConstraintOn(DBRowSet rowset)
Returns true if the command has a constraint on the given table or false otherwise.

Parameters:
rowset - rowset table or view to join
Returns:
true if the command has a join on the given table or false otherwise

hasJoinOn

public boolean hasJoinOn(DBColumn column)
Returns true if the command has a join on the given column or false otherwise.

Parameters:
column - the column to test
Returns:
true if the command has a join on the given column or false otherwise

removeJoinsOn

public boolean removeJoinsOn(DBRowSet rowset)
removes all joins to a given table or view

Parameters:
rowset - the table or view for which to remove all joins
Returns:
true if any joins have been removed or false otherwise

removeJoinsOn

public boolean removeJoinsOn(DBColumn column)
removes all joins to a given column

Parameters:
column - the column for which to remove all joins
Returns:
true if any joins have been removed or false otherwise

where

public void where(DBCompareExpr expr)
Adds a compare expression to the list of constraints. If another restriction already exists for the same column it will be replaced.

Parameters:
expr - the DBCompareExpr object

hasWhereConstraints

public boolean hasWhereConstraints()
Returns true if the command has constraints or false if not.

Returns:
true if constraints have been set on the command

getWhereConstraints

public List<DBCompareExpr> getWhereConstraints()
Returns a copy of the defined where clauses.

Returns:
vector of where clauses

removeWhereConstraintOn

public void removeWhereConstraintOn(DBColumnExpr col)
removes a constraint on a particular column from the where clause

Parameters:
col - the column expression for which to remove the constraint

getJoins

public List<DBJoinExpr> getJoins()
Returns a copy of the defined joins.

Returns:
vector of joins

addWhereConstraints

public void addWhereConstraints(List<DBCompareExpr> constraints)
Adds a list of constraints to the command.

Parameters:
constraints - list of constraints

having

public void having(DBCompareExpr expr)
adds a constraint to the having clause.

Parameters:
expr - the DBCompareExpr object

removeHavingConstraintOn

public void removeHavingConstraintOn(DBColumnExpr col)
removes a constraint on a particular column from the where clause

Parameters:
col - the column expression for which to remove the constraint

groupBy

public void groupBy(DBColumnExpr... exprs)
Adds a list of columns to the group by phrase of an sql statement.

Parameters:
exprs - vararg of columns by which to group the rows

hasSelectExpr

public boolean hasSelectExpr()

getSelect

public void getSelect(StringBuilder buf)
Specified by:
getSelect in class DBCommandExpr

getSelectExprList

public DBColumnExpr[] getSelectExprList()
Returns a array of all select DBColumnExpr for this command

Specified by:
getSelectExprList in class DBCommandExpr
Returns:
a array of all DBColumnExpr objects or null if there are no selects

clearSelectDistinct

public void clearSelectDistinct()
Clears the select distinct option.


clearSelect

public void clearSelect()
Clears the list of selected columns.


clearSet

public void clearSet()
Clears the list of set expressions.


clearJoin

public void clearJoin()
Clears the list of join expressions.


clearWhere

public void clearWhere()
Clears the list of where constraints.


clearHaving

public void clearHaving()
Clears the list of having constraints.


clearGroupBy

public void clearGroupBy()
Clears the list of group by constraints.


clear

public void clear()
Clears the entire command object.


setConstraint

protected void setConstraint(List<DBCompareExpr> list,
                             DBCompareExpr expr)
adds a constraint to the 'where' or 'having' collections

Parameters:
list - the 'where' or 'having' list
expr - the DBCompareExpr object

removeConstraintOn

protected void removeConstraintOn(List<DBCompareExpr> list,
                                  DBColumnExpr col)
removes a constraint on a particular column to the 'where' or 'having' collections

Parameters:
list - the 'where' or 'having' list
col - the column expression for which to remove the constraint

getRowSetList

protected List<DBRowSet> getRowSetList()
Gets a list of all tables referenced by the query.

Returns:
list of all rowsets (tables or views) used by the query

addReferencedColumns

public void addReferencedColumns(Set<DBColumn> list)
Adds Columns

Specified by:
addReferencedColumns in class DBExpr
Parameters:
list - list to which all used column expressions must be added

getParamValues

public Object[] getParamValues()
Returns an array of parameter values for a prepared statement. To ensure that all values are in the order of their occurrence, getSelect() should be called first.

Specified by:
getParamValues in class DBCommandExpr
Returns:
an array of parameter values for a prepared statement

getUpdate

public String getUpdate()
Creates the update SQL-Command.

Returns:
the update SQL-Command

getInsert

public String getInsert()
Creates the insert SQL-Command.

Returns:
the insert SQL-Command

getDelete

public String getDelete(DBTable table)
Creates the delete SQL-Command.

Parameters:
table - the table object
Returns:
the delete SQL-Command

addSelect

protected void addSelect(StringBuilder buf)

addFrom

protected void addFrom(StringBuilder buf)

addWhere

protected void addWhere(StringBuilder buf,
                        long context)

addWhere

protected final void addWhere(StringBuilder buf)

addGrouping

protected void addGrouping(StringBuilder buf)

addOrder

protected void addOrder(StringBuilder buf)


Copyright © 2008–2014 Apache Software Foundation. All rights reserved.