|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.triactive.jdo.store.sql.expr.QueryStatement
An SQL SELECT statement that can be constructed programmatically.
This class allows the components parts of a SELECT statement (the columns to
be selected, the expression making up the WHERE clause, etc.) to be
successively built up.
The toStatementText()
method then produces the resulting statement
text.
Once the text is generated the statement can no longer be modified. Primarily this prevents the statement from inadvertently modifying itself as its text is being produced.
QueryStatement objects should not be constructed directly.
Instead, DatabaseAdapter.newQueryStatement(StoreManager,Table)
should
be called so that an appropriate adapter subclass can be substituted if
necessary.
Nested Class Summary | |
static class |
QueryStatement.QueryColumn
Represents a column in one of the table expressions in a query statement. |
Field Summary | |
protected CorrelationName |
defaultRangeVar
The range variable for the default table expression, ie "THIS". |
protected boolean |
distinct
true if results must be DISTINCT. |
protected StatementText |
groupByList
The content of the GROUP BY clause, if any. |
protected BooleanExpression |
havingExpr
The content of the HAVING clause, if any. |
protected TableExpression |
initialTableExpr
The initial table expression, ie the first one in the FROM clause. |
protected java.util.List |
joins
A list containing all the additional table expression joins to be added to the FROM clause, in the order in which they should be joined. |
protected StatementText |
orderByList
The content of the ORDER BY clause, if any. |
protected java.util.List |
selected
A list of StatementText objects representing the result expressions that have been selected. |
protected StoreManager |
storeMgr
The associated store manager. |
protected QueryStatement |
superquery
The surrounding statement if this is a subquery, otherwise null . |
protected java.util.Map |
tableExprsByRangeVar
A map of CorrelationName to TableExpression containing all table expressions in the statement. |
protected BooleanExpression |
whereExpr
The content of the WHERE clause, if any. |
Constructor Summary | |
QueryStatement(StoreManager storeMgr,
Table initialTable,
CorrelationName initialRangeVar,
QueryStatement superquery)
Constructs a new query statement. |
Method Summary | |
void |
andHavingCondition(BooleanExpression condition)
ANDs the given condition into the HAVING clause for this statement. |
void |
andWhereCondition(BooleanExpression condition)
ANDs the given condition into the WHERE clause for this statement. |
protected void |
assertNotFrozen()
Asserts that the statement text has not yet been generated. |
boolean |
contains(QueryStatement qs)
Tests whether the given statement is a subquery of this statement. |
void |
embedIn(QueryStatement superquery)
Establishes the query statement to which this statement belongs. |
QueryStatement.QueryColumn |
getColumn(Column col)
Returns a query column object for the specified column in the default table expression. |
QueryStatement.QueryColumn |
getColumn(CorrelationName rangeVar,
Column col)
Returns a query column object for the specified column in the specified table expression. |
java.util.Set |
getCorrelatedColumns()
Returns a set containing all the correlated (ie superquery) columns referenced in this statement. |
DatabaseAdapter |
getDatabaseAdapter()
Convenience method to return the associated store manager's database adapter. |
TableExpression |
getDefaultTableExpression()
Returns the "default" table expression in the statement. |
protected BooleanExpression |
getJoinConditions()
Returns the join conditions to be prefixed on the WHERE clause. |
QueryStatement |
getQueryStatement()
Returns the query statement to which this statement belongs, if any. |
StoreManager |
getStoreManager()
Returns the associated store manager. |
TableExpression |
getTableExpression(CorrelationName rangeVar)
Returns the table expression associated with the range variable rangeVar. |
java.util.Collection |
getTableExpressions()
Returns all the table expressions in the statement. |
QueryStatement.QueryColumn[] |
join(JoinType type,
Table table,
CorrelationName rangeVar,
QueryStatement.QueryColumn[] from,
Column[] to)
Performs a join on the equality of two lists of columns. |
QueryStatement.QueryColumn |
join(JoinType type,
Table table,
CorrelationName rangeVar,
QueryStatement.QueryColumn from,
Column to)
Performs a join on the equality of two columns. |
protected Join |
newJoin(JoinType type,
TableExpression tableExpr,
QueryStatement.QueryColumn[] from,
Column[] to)
Constructs a new Join object for this statement. |
QueryStatement |
newSubqueryStatement(Table table,
CorrelationName rangeVar)
Produces a new subquery of this query, such as might be used in an SQL EXISTS() function, etc. |
int |
numResultColumns()
Returns the number of result columns SELECTed in this statement. |
int |
select(QueryStatement.QueryColumn qsc)
SELECTs the given query column. |
int |
select(SqlExpression expr)
SELECTs the given SQL expression as a result column. |
void |
setDistinct(boolean distinct)
Sets the distinct mode flag, indicating whether or not results must be DISTINCT (unique). |
void |
setGrouping(java.util.List groupExprs)
Establishes the GROUP BY clause for this statement. |
void |
setOrdering(java.util.List orderingSpecs)
Establishes the ORDER BY clause for this statement. |
StatementText |
toStatementText()
Generates and returns the statement text for this query statement. |
java.lang.String |
toString()
Returns the text of this statement. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
protected final StoreManager storeMgr
protected final CorrelationName defaultRangeVar
protected final TableExpression initialTableExpr
protected QueryStatement superquery
null
.
protected java.util.Map tableExprsByRangeVar
protected boolean distinct
true
if results must be DISTINCT.
protected java.util.List selected
protected java.util.List joins
protected BooleanExpression whereExpr
protected StatementText groupByList
protected BooleanExpression havingExpr
protected StatementText orderByList
Constructor Detail |
public QueryStatement(StoreManager storeMgr, Table initialTable, CorrelationName initialRangeVar, QueryStatement superquery)
DatabaseAdapter
object.
storeMgr
- The store managerinitialTable
- The initial table from which the query will select.initialRangeVar
- The range variable to use for the initial table. If
null
the SQL identifier THIS will be used.superquery
- If this is a subquery, the surrounding query; otherwise
null
Method Detail |
protected void assertNotFrozen()
java.lang.IllegalStateException
- If the statement text has already been generated.public StoreManager getStoreManager()
public DatabaseAdapter getDatabaseAdapter()
public QueryStatement getQueryStatement()
null
if this is not a subquery.
public void embedIn(QueryStatement superquery)
superquery
- the query statement to which this statement belongspublic QueryStatement newSubqueryStatement(Table table, CorrelationName rangeVar)
table
- The initial table from which the query will select.rangeVar
- The range variable to use for the initial table. If
null
the SQL identifier THIS will be used.public boolean contains(QueryStatement qs)
true
if the given statement is a subquery of this
statement, or is this statement itself; false
otherwise.public java.util.Set getCorrelatedColumns()
QueryStatement.QueryColumn
objects.public TableExpression getDefaultTableExpression()
public TableExpression getTableExpression(CorrelationName rangeVar)
null
if no such range variable is in-use in the
statement.
public java.util.Collection getTableExpressions()
public void setDistinct(boolean distinct)
distinct
- true
if results must be distinct,
false
to allow duplicates.public int select(QueryStatement.QueryColumn qsc)
qsc
- The query column to SELECT. The QueryColumn object indicates both
the TableExpression and the Column within that expression.
java.lang.IllegalArgumentException
- If the given query column belongs to a different query statement.
java.lang.IllegalStateException
- If the statement text has already been generated.public int select(SqlExpression expr)
expr
- The expression to SELECT.
java.lang.IllegalArgumentException
- If the given expression belongs to a different query statement.
java.lang.IllegalStateException
- If the statement text has already been generated.public int numResultColumns()
public QueryStatement.QueryColumn getColumn(Column col)
col
- The column, which must belong to a table in the default table
expression.
public QueryStatement.QueryColumn getColumn(CorrelationName rangeVar, Column col)
rangeVar
- The range variable of the table expression.col
- The column, which must belong to a table in the specified table
expression.
java.lang.IllegalArgumentException
- If no such range variable exists in the statement.public QueryStatement.QueryColumn join(JoinType type, Table table, CorrelationName rangeVar, QueryStatement.QueryColumn from, Column to)
type
- The type of join to perform.table
- The table to be joined.rangeVar
- The range variable (i.e. alias) to use for the table expression.from
- The source (left) query column. Must be null for
JoinType.CROSS.to
- The target column in the table being joined. Must be null
for JoinType.CROSS.
null
for JoinType.CROSS.
java.lang.IllegalArgumentException
- If the to is not a column in table, or
if rangeVar is already in use.
java.lang.IllegalStateException
- If the statement text has already been generated.public QueryStatement.QueryColumn[] join(JoinType type, Table table, CorrelationName rangeVar, QueryStatement.QueryColumn[] from, Column[] to)
type
- The type of join to perform.table
- The table to be joined.rangeVar
- The range variable (i.e. alias) to use for the table expression.from
- The source (left) query columns. Must be zero-length for
JoinType.CROSS.to
- The target columns in the table being joined. Must be zero-length
for JoinType.CROSS.
java.lang.IllegalArgumentException
- If a to column is not a column in table, or
if rangeVar is already in use, or
if the two column lists are not the same size.
java.lang.IllegalStateException
- If the statement text has already been generated.protected Join newJoin(JoinType type, TableExpression tableExpr, QueryStatement.QueryColumn[] from, Column[] to)
StandardJoin
object.
Vendor-specific subclasses could override it and return a customized join
object.
protected BooleanExpression getJoinConditions()
public void andWhereCondition(BooleanExpression condition)
condition
- A boolean SQL expression representing the test condition to be
ANDed.
java.lang.IllegalStateException
- If the statement text has already been generated.public void setGrouping(java.util.List groupExprs)
If required by the underlying DBMS, any columns referenced in the GROUP BY expression(s) will also be SELECTed in the statement.
groupExprs
- A list of SqlExpression objects representing the contents of the
GROUP BY clause.
java.lang.IllegalStateException
- If the statement text has already been generated.public void andHavingCondition(BooleanExpression condition)
condition
- A boolean SQL expression representing the test condition to be
ANDed.
java.lang.IllegalStateException
- If the statement text has already been generated.public void setOrdering(java.util.List orderingSpecs)
If required by the underlying DBMS, any columns referenced in the ORDER BY expression(s) will also be SELECTed in the statement.
orderingSpecs
- A list of OrderingSpec objects representing the contents of the
ORDER BY clause.
java.lang.IllegalStateException
- If the statement text has already been generated.public StatementText toStatementText()
toStatementText
in interface StatementTextGenerator
public java.lang.String toString()
toStatementText()
and returns the resulting string.
toString
in interface StatementTextGenerator
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |