org.apache.empire.db
Class DBCommand

java.lang.Object
  extended by org.apache.empire.commons.ErrorObject
      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:
java.lang.Cloneable, ErrorInfo
Direct Known Subclasses:
DBCommandOracle, DBDatabaseDriverDerby.DBCommandDerby, DBDatabaseDriverH2.DBCommandH2, DBDatabaseDriverHSql.DBCommandHSql, DBDatabaseDriverMSSQL.DBCommandMSSQL, DBDatabaseDriverMySQL.DBCommandMySQL, DBDatabaseDriverPostgreSQL.DBCommandPostreSQL

public abstract class DBCommand
extends DBCommandExpr
implements java.lang.Cloneable

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


Nested Class Summary
static class DBCommand.DBCmdParameter
           
 
Nested classes/interfaces inherited from class org.apache.empire.db.DBCommandExpr
DBCommandExpr.DBCmdColumn, DBCommandExpr.DBCmdQuery
 
Nested classes/interfaces inherited from class org.apache.empire.commons.ErrorObject
ErrorObject.ObjectErrorInfo
 
Field Summary
protected  java.util.Vector<java.lang.Object> cmdParams
           
protected  java.util.List<DBColumnExpr> groupBy
           
protected  java.util.List<DBCompareExpr> having
           
protected  java.util.List<DBJoinExpr> joins
           
protected  java.util.List<DBColumnExpr> select
           
protected  boolean selectDistinct
           
protected  java.util.List<DBSetExpr> set
           
protected  java.util.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, log
 
Constructor Summary
protected DBCommand(DBDatabase db)
          Constructs a new DBCommand object and set the specified DBDatabase object.
 
Method Summary
 DBCommand.DBCmdParameter addCmdParam(java.lang.Object item)
          Adds an object to in the list of Parameters and returns a parameter object.
protected  void addFrom(java.lang.StringBuilder buf)
           
protected  void addGrouping(java.lang.StringBuilder buf)
           
 void addJoins(java.util.List<DBJoinExpr> joinExprList)
          Adds a list of join expressions to the command.
protected  void addOrder(java.lang.StringBuilder buf)
           
 void addReferencedColumns(java.util.Set<DBColumn> list)
          Adds Columns
protected  void addSelect(java.lang.StringBuilder buf)
           
protected  void addWhere(java.lang.StringBuilder buf)
           
 void addWhereConstraints(java.util.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 clearLimit()
          Clears a limit or offset set by calling limit() or offset()
 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.
 java.lang.Object[] getCmdParams()
          Returns the list of parameter values for a prepared statement.
protected  java.lang.Object getCmdParamValue(DBColumn col, java.lang.Object value)
           
 DBDatabase getDatabase()
          Returns the current DBDatabase object.
 java.lang.String getDelete(DBTable table)
          Creates the delete SQL-Command.
 java.lang.String getInsert()
          Creates the insert SQL-Command.
 java.util.List<DBJoinExpr> getJoins()
          Returns a copy of the defined joins.
 boolean getSelect(java.lang.StringBuilder buf)
           
 DBColumnExpr[] getSelectExprList()
          Returns a array of all select DBColumnExpr for this command
protected  java.util.List<DBRowSet> getTableList()
          Gets a list of all tables referenced by the query.
 java.lang.String getUpdate()
          Creates the update SQL-Command.
 java.util.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 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 constaints or false if not.
 void having(DBCompareExpr expr)
          adds a constraint to the having clause.
 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.
 boolean limitRows(int numRows)
          set the maximum number of rows to return when executing a query command A negative value will remove the limit.
protected  void removeConstraintOn(java.util.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
 void removeWhereConstraintOn(DBColumnExpr col)
          removes a constraint on a particular column from the where clause
 void select(java.util.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'.
 boolean setCmdParam(int index, java.lang.Object item)
          Sets a object to in the list of Parameters.
protected  void setConstraint(java.util.List<DBCompareExpr> list, DBCompareExpr expr)
          adds a constraint to the 'where' or 'having' collections
 boolean skipRows(int numRows)
          sets the offset of the first row to return when executing a query command.
protected  boolean useCmdParam(DBColumn col)
           
 void where(DBCompareExpr expr)
          Adds a compare expression to the list of constraints.
 
Methods inherited from class org.apache.empire.db.DBCommandExpr
addListExpr, addSQL, clearOrderBy, getCmdColumn, getCmdColumn, getInsertInto, getInsertInto, getInsertInto, getSelect, intersect, orderBy, orderBy, orderBy, union
 
Methods inherited from class org.apache.empire.db.DBExpr
getObjectValue, getValueClass
 
Methods inherited from class org.apache.empire.db.DBObject
error, error
 
Methods inherited from class org.apache.empire.commons.ErrorObject
clearError, clearErrorInfo, error, error, error, error, error, error, error, getErrorInfo, getErrorMessage, getErrorParams, getErrorSource, getErrorType, getMessage, hasError, internalSetError, isExceptionsEnabled, setExceptionsEnabled, success
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

selectDistinct

protected boolean selectDistinct

select

protected java.util.List<DBColumnExpr> select

set

protected java.util.List<DBSetExpr> set

joins

protected java.util.List<DBJoinExpr> joins

where

protected java.util.List<DBCompareExpr> where

having

protected java.util.List<DBCompareExpr> having

groupBy

protected java.util.List<DBColumnExpr> groupBy

cmdParams

protected java.util.Vector<java.lang.Object> 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

clone

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

Overrides:
clone in class java.lang.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(java.util.Collection<? extends DBColumnExpr> columns)
Adds a collection of columns to the select phrase of an sql statement.

Parameters:
columns - the column expressions to add

useCmdParam

protected boolean useCmdParam(DBColumn col)

getCmdParamValue

protected java.lang.Object getCmdParamValue(DBColumn col,
                                            java.lang.Object value)

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

setCmdParam

public boolean setCmdParam(int index,
                           java.lang.Object item)
Sets a object to in the list of Parameters.

Parameters:
index - the parameter index
item - the parameter value
Returns:
true on succes

addCmdParam

public DBCommand.DBCmdParameter addCmdParam(java.lang.Object item)
Adds an object to in the list of Parameters and returns a parameter object.

Parameters:
item - the parameter value
Returns:
the 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(java.util.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

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 constaints or false if not.

Returns:
true if constraints have been set on the command

getWhereConstraints

public java.util.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 java.util.List<DBJoinExpr> getJoins()
Returns a copy of the defined joins.

Returns:
vector of joins

addWhereConstraints

public void addWhereConstraints(java.util.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

limitRows

public boolean limitRows(int numRows)
set the maximum number of rows to return when executing a query command A negative value will remove the limit.

Returns:
true if the database supports a limit or false otherwise

skipRows

public boolean skipRows(int numRows)
sets the offset of the first row to return when executing a query command. A negative value will remove the offset.

Returns:
true if the database supports an offset or false otherwise

clearLimit

public void clearLimit()
Clears a limit or offset set by calling limit() or offset()


hasSelectExpr

public boolean hasSelectExpr()

getSelect

public boolean getSelect(java.lang.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(java.util.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(java.util.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

getTableList

protected java.util.List<DBRowSet> getTableList()
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(java.util.Set<DBColumn> list)
Adds Columns

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

getCmdParams

public java.lang.Object[] getCmdParams()
Returns the list of parameter values for a prepared statement.

Returns:
the list of parameter values for a prepared statement

getUpdate

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

Returns:
the update SQL-Command

getInsert

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

Returns:
the insert SQL-Command

getDelete

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

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

addSelect

protected void addSelect(java.lang.StringBuilder buf)

addFrom

protected void addFrom(java.lang.StringBuilder buf)

addWhere

protected void addWhere(java.lang.StringBuilder buf)

addGrouping

protected void addGrouping(java.lang.StringBuilder buf)

addOrder

protected void addOrder(java.lang.StringBuilder buf)


Copyright © 2008-2010 Apache Software Foundation. All Rights Reserved.