|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.datanucleus.store.rdbms.sql.SQLStatement
public class SQLStatement
Class providing an API for generating SQL statements. Caller builds up the statement using the various methods, and accesses the SQL statement using getStatement(). Generated statement is of the form :-
SELECT {expr}, {expr}, ...
FROM {tblExpr} [joinInfo {tblExpr} ON ...] ...
WHERE {boolExpr} [AND|OR] {boolExpr} ...
GROUP BY {expr}, {expr}
HAVING {boolExpr}
ORDER BY {expr} [ASC|DESC], {expr} [ASC|DESC], ...
and also supports UNIONs between SQLStatements, and having sub-queries of other SQLStatements.
Alternatively, for an UPDATE,
UPDATE {tbl}
SET {expr}={val}, {expr}={val}, ...
WHERE {boolExpr} [AND|OR] {boolExpr} ...
The generated SQL is cached. Any use of a mutating method, changing the composition of the statement will clear the cached SQL, and it will be regenerated when
getStatementis called next.
| Field Summary | |
|---|---|
protected boolean |
aggregated
whether there is an aggregate expression present in the select |
protected java.lang.String |
candidateClassName
Name of class that this statement selects (optional, only typically for unioned statements). |
protected boolean |
distinct
Whether the statement is distinct. |
protected java.util.HashMap<java.lang.String,java.lang.Object> |
extensions
Map of extensions for use in generating the SQL, keyed by the extension name. |
protected java.util.List<SQLExpression> |
groupingExpressions
Expression(s) for the GROUP BY clause. |
protected BooleanExpression |
having
Having clause. |
protected java.util.List<SQLJoin> |
joins
List of joins for this statement. |
protected static org.datanucleus.util.Localiser |
LOCALISER
Localisation for messages. |
protected boolean[] |
orderingDirections
Directions for any ORDER BY expressions (1 for each orderingExpressions entry). |
protected SQLExpression[] |
orderingExpressions
Expressions for any ORDER BY clause. |
protected SQLStatement |
parent
Parent statement, if this is a subquery. |
protected SQLTable |
primaryTable
Primary table for this statement. |
protected QueryGenerator |
queryGenerator
Context of any query generation. |
protected long |
rangeCount
The number of records to be retrieved in any range restriction. |
protected long |
rangeOffset
The offset for any range restriction. |
protected RDBMSManager |
rdbmsMgr
Manager for the RDBMS datastore. |
protected java.util.List<java.lang.String> |
selects
List of select objects. |
protected SQLText |
sql
Cached SQL statement, generated by getStatement(). |
protected java.util.Map<java.lang.String,SQLTableGroup> |
tableGroups
Map of table groups keyed by the group name. |
protected static java.util.Map<java.lang.String,SQLTableNamer> |
tableNamerByName
Map of SQLTable naming instance keyed by the name of the naming scheme. |
protected java.util.Map<java.lang.String,SQLTable> |
tables
Map of tables referenced in this statement, keyed by their alias. |
protected java.util.List<SQLStatement> |
unions
List of unioned SQLStatements (if any). |
protected SQLExpression[] |
updates
Array of update expressions when the statement is an UPDATE. |
protected BooleanExpression |
where
Where clause. |
| Constructor Summary | |
|---|---|
SQLStatement(RDBMSManager rdbmsMgr,
org.datanucleus.store.mapped.DatastoreContainerObject table,
org.datanucleus.store.mapped.DatastoreIdentifier alias,
java.lang.String tableGroupName)
Constructor for an SQL statement. |
|
SQLStatement(SQLStatement parentStmt,
RDBMSManager rdbmsMgr,
org.datanucleus.store.mapped.DatastoreContainerObject table,
org.datanucleus.store.mapped.DatastoreIdentifier alias,
java.lang.String tableGroupName)
Constructor for an SQL statement that is a subquery of another statement. |
|
| Method Summary | |
|---|---|
void |
addExtension(java.lang.String key,
java.lang.Object value)
Method to define an extension for this query statement allowing control over its behaviour in generating a query. |
void |
addGroupingExpression(SQLExpression expr)
Method to add a grouping expression to the query. |
protected void |
addOrderingColumnsToSelect()
Convenience method to add any necessary columns to the SELECT that are needed by the ordering constraint. |
SQLTable |
crossJoin(org.datanucleus.store.mapped.DatastoreContainerObject target,
java.lang.String targetAlias,
java.lang.String tableGrpName)
Method to form a right outer join to the specified table using the provided mappings. |
protected SQLText |
generateOrderingStatement()
Convenience method to generate the ordering statement to add to the overall query statement. |
protected java.lang.String |
generateTableAlias(org.datanucleus.store.mapped.DatastoreContainerObject tbl,
java.lang.String groupName)
Method to generate the alias to be used for a joined table. |
java.lang.String |
getCandidateClassName()
|
DatabaseAdapter |
getDatabaseAdapter()
|
int |
getNumberOfSelects()
Accessor for the number of selected items in the SELECT clause. |
int |
getNumberOfTables()
Accessor for the number of tables defined for this statement. |
int |
getNumberOfUnions()
|
SQLTable |
getPrimaryTable()
Accessor for the primary table of the statement. |
QueryGenerator |
getQueryGenerator()
|
RDBMSManager |
getRDBMSManager()
|
SQLText |
getSelectStatement()
Accessor for the SQL SELECT statement. |
SQLExpressionFactory |
getSQLExpressionFactory()
|
SQLTable |
getTable(org.datanucleus.store.mapped.DatastoreContainerObject table,
java.lang.String groupName)
Accessor for the SQLTable object for the specified table (if defined for this statement) in the specified table group. |
SQLTable |
getTable(java.lang.String alias)
Accessor for the SQLTable object with the specified alias (if defined for this statement). |
SQLTableGroup |
getTableGroup(java.lang.String groupName)
Accessor for the table group with this name. |
java.util.List<SQLStatement> |
getUnions()
Accessor for the unioned statements. |
java.lang.Object |
getValueForExtension(java.lang.String key)
Accessor for the value for an extension. |
SQLTable |
innerJoin(SQLTable sourceTable,
org.datanucleus.store.mapped.mapping.JavaTypeMapping sourceMapping,
org.datanucleus.store.mapped.DatastoreContainerObject target,
java.lang.String targetAlias,
org.datanucleus.store.mapped.mapping.JavaTypeMapping targetMapping,
java.lang.Object[] discrimValues,
java.lang.String tableGrpName)
Method to form an inner join to the specified table using the provided mappings. |
protected void |
invalidateStatement()
Method to uncache the generated SQL (because some condition has changed). |
protected void |
join(SQLJoin.JoinType joinType,
SQLTable sourceTable,
org.datanucleus.store.mapped.mapping.JavaTypeMapping sourceMapping,
SQLTable targetTable,
org.datanucleus.store.mapped.mapping.JavaTypeMapping targetMapping,
java.lang.Object[] discrimValues)
Internal method to form a join to the specified table using the provided mappings. |
SQLTable |
leftOuterJoin(SQLTable sourceTable,
org.datanucleus.store.mapped.mapping.JavaTypeMapping sourceMapping,
org.datanucleus.store.mapped.DatastoreContainerObject target,
java.lang.String targetAlias,
org.datanucleus.store.mapped.mapping.JavaTypeMapping targetMapping,
java.lang.Object[] discrimValues,
java.lang.String tableGrpName)
Method to form a left outer join to the specified table using the provided mappings. |
void |
log(org.datanucleus.util.NucleusLogger logger)
Method to dump the statement to the supplied log (debug level). |
SQLTable |
rightOuterJoin(SQLTable sourceTable,
org.datanucleus.store.mapped.mapping.JavaTypeMapping sourceMapping,
org.datanucleus.store.mapped.DatastoreContainerObject target,
java.lang.String targetAlias,
org.datanucleus.store.mapped.mapping.JavaTypeMapping targetMapping,
java.lang.Object[] discrimValues,
java.lang.String tableGrpName)
Method to form a right outer join to the specified table using the provided mappings. |
int |
select(SQLExpression expr,
java.lang.String alias)
Select an expression. |
int |
select(SQLTable table,
org.datanucleus.store.mapped.DatastoreField column,
java.lang.String alias)
Add a select clause for the specified column. |
int[] |
select(SQLTable table,
org.datanucleus.store.mapped.mapping.JavaTypeMapping mapping,
java.lang.String alias)
Add a select clause for the specified field (via its mapping). |
protected int |
selectSQLExpressionInternal(SQLExpression expr,
java.lang.String alias)
Convenience method for selecting columns when generating the SQL text. |
void |
setCandidateClassName(java.lang.String name)
|
void |
setDistinct(boolean distinct)
Mutator for whether the query returns distinct results. |
void |
setHaving(BooleanExpression expr)
Mutator for the "having" expression. |
void |
setOrdering(SQLExpression[] exprs,
boolean[] descending)
Mutator for the ordering criteria. |
void |
setParent(SQLStatement parentStmt)
Sets the parent SQL statement of this statement. |
void |
setQueryGenerator(QueryGenerator gen)
|
void |
setRange(long offset,
long count)
Method to add a range constraint on any SELECT. |
void |
setUpdates(SQLExpression[] exprs)
Method to set the UPDATE clause of the statement. |
void |
union(SQLStatement stmt)
Method to union this SQL statement with another SQL statement. |
void |
whereAnd(BooleanExpression expr,
boolean applyToUnions)
Method to add an AND condition to the WHERE clause. |
void |
whereOr(BooleanExpression expr,
boolean applyToUnions)
Method to add an OR condition to the WHERE clause. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected static final org.datanucleus.util.Localiser LOCALISER
protected static java.util.Map<java.lang.String,SQLTableNamer> tableNamerByName
protected SQLText sql
protected RDBMSManager rdbmsMgr
protected QueryGenerator queryGenerator
protected java.lang.String candidateClassName
protected boolean distinct
protected java.util.HashMap<java.lang.String,java.lang.Object> extensions
protected SQLStatement parent
protected java.util.List<SQLStatement> unions
protected java.util.List<java.lang.String> selects
protected SQLExpression[] updates
protected boolean aggregated
protected SQLTable primaryTable
protected java.util.List<SQLJoin> joins
protected java.util.Map<java.lang.String,SQLTable> tables
protected java.util.Map<java.lang.String,SQLTableGroup> tableGroups
protected BooleanExpression where
protected java.util.List<SQLExpression> groupingExpressions
protected BooleanExpression having
protected SQLExpression[] orderingExpressions
protected boolean[] orderingDirections
protected long rangeOffset
protected long rangeCount
| Constructor Detail |
|---|
public SQLStatement(RDBMSManager rdbmsMgr,
org.datanucleus.store.mapped.DatastoreContainerObject table,
org.datanucleus.store.mapped.DatastoreIdentifier alias,
java.lang.String tableGroupName)
rdbmsMgr - The datastore managertable - The primary tablealias - Alias for this tabletableGroupName - Name of candidate table-group (if any). Uses "Group0" if not provided
public SQLStatement(SQLStatement parentStmt,
RDBMSManager rdbmsMgr,
org.datanucleus.store.mapped.DatastoreContainerObject table,
org.datanucleus.store.mapped.DatastoreIdentifier alias,
java.lang.String tableGroupName)
parentStmt - Parent statementrdbmsMgr - The datastore managertable - The primary tablealias - Alias for this tabletableGroupName - Name of candidate table-group (if any). Uses "Group0" if not provided| Method Detail |
|---|
public RDBMSManager getRDBMSManager()
public void setCandidateClassName(java.lang.String name)
public java.lang.String getCandidateClassName()
public QueryGenerator getQueryGenerator()
public void setQueryGenerator(QueryGenerator gen)
public SQLExpressionFactory getSQLExpressionFactory()
public DatabaseAdapter getDatabaseAdapter()
public void addExtension(java.lang.String key,
java.lang.Object value)
key - Extension keyvalue - Value for the keypublic java.lang.Object getValueForExtension(java.lang.String key)
key - Key for the extension
public void union(SQLStatement stmt)
stmt - The other SQL statement to unionpublic int getNumberOfUnions()
public java.util.List<SQLStatement> getUnions()
public void setParent(SQLStatement parentStmt)
SELECT 1 FROM PARENT WHERE EXISTS (SELECT 1 FROM THIS)
The parent SQLStatement is the outer SELECT, and this SQLStatement is the inner SELECT.
parentStmt - the parent of this statementpublic void setDistinct(boolean distinct)
distinct - Whether to return distinctpublic int getNumberOfSelects()
public int select(SQLExpression expr,
java.lang.String alias)
expr - The expression to add to the select statementalias - Optional alias for this selected expression
public int[] select(SQLTable table,
org.datanucleus.store.mapped.mapping.JavaTypeMapping mapping,
java.lang.String alias)
table - The SQLTable to select from (null implies the primary table)mapping - The mapping for the fieldalias - optional alias
public int select(SQLTable table,
org.datanucleus.store.mapped.DatastoreField column,
java.lang.String alias)
table - The SQLTable to select from (null implies the primary table)column - The columnalias - Optional alias
public void setUpdates(SQLExpression[] exprs)
exprs - The update clause expressionpublic SQLTable getPrimaryTable()
public SQLTable getTable(java.lang.String alias)
alias - Alias
public SQLTable getTable(org.datanucleus.store.mapped.DatastoreContainerObject table,
java.lang.String groupName)
table - The tablegroupName - Name of the table group where we should look for this table
public SQLTableGroup getTableGroup(java.lang.String groupName)
groupName - Name of the group
public int getNumberOfTables()
public SQLTable innerJoin(SQLTable sourceTable,
org.datanucleus.store.mapped.mapping.JavaTypeMapping sourceMapping,
org.datanucleus.store.mapped.DatastoreContainerObject target,
java.lang.String targetAlias,
org.datanucleus.store.mapped.mapping.JavaTypeMapping targetMapping,
java.lang.Object[] discrimValues,
java.lang.String tableGrpName)
sourceTable - SQLTable for the source (null implies primaryTable)sourceMapping - Mapping in this table to join fromtarget - Table to join totargetAlias - Alias for the target table (if known)targetMapping - Mapping in the other table to join to (also defines the table to join to)discrimValues - Any discriminator values to apply for the joined table (null if not)tableGrpName - Name of the table group for the target (null implies a new group)
public SQLTable leftOuterJoin(SQLTable sourceTable,
org.datanucleus.store.mapped.mapping.JavaTypeMapping sourceMapping,
org.datanucleus.store.mapped.DatastoreContainerObject target,
java.lang.String targetAlias,
org.datanucleus.store.mapped.mapping.JavaTypeMapping targetMapping,
java.lang.Object[] discrimValues,
java.lang.String tableGrpName)
sourceTable - SQLTable for the source (null implies primaryTable)sourceMapping - Mapping in this table to join fromtarget - Table to join totargetAlias - Alias for the target table (if known)targetMapping - Mapping in the other table to join to (also defines the table to join to)discrimValues - Any discriminator values to apply for the joined table (null if not)tableGrpName - Name of the table group for the target (null implies a new group)
public SQLTable rightOuterJoin(SQLTable sourceTable,
org.datanucleus.store.mapped.mapping.JavaTypeMapping sourceMapping,
org.datanucleus.store.mapped.DatastoreContainerObject target,
java.lang.String targetAlias,
org.datanucleus.store.mapped.mapping.JavaTypeMapping targetMapping,
java.lang.Object[] discrimValues,
java.lang.String tableGrpName)
sourceTable - SQLTable for the source (null implies primaryTable)sourceMapping - Mapping in this table to join fromtarget - Table to join totargetAlias - Alias for the target table (if known)targetMapping - Mapping in the other table to join to (also defines the table to join to)discrimValues - Any discriminator values to apply for the joined table (null if not)tableGrpName - Name of the table group for the target (null implies a new group)
public SQLTable crossJoin(org.datanucleus.store.mapped.DatastoreContainerObject target,
java.lang.String targetAlias,
java.lang.String tableGrpName)
target - Table to join totargetAlias - Alias for the target table (if known)tableGrpName - Name of the table group for the target (null implies a new group)
protected void join(SQLJoin.JoinType joinType,
SQLTable sourceTable,
org.datanucleus.store.mapped.mapping.JavaTypeMapping sourceMapping,
SQLTable targetTable,
org.datanucleus.store.mapped.mapping.JavaTypeMapping targetMapping,
java.lang.Object[] discrimValues)
joinType - Type of join (INNER, LEFT OUTER, RIGHT OUTER, CROSS, NON-ANSI)sourceTable - SQLTable to join fromsourceMapping - Mapping in this table to join fromtargetTable - SQLTable to join totargetMapping - Mapping in the other table to join to (also defines the table to join to)discrimValues - Any discriminator values to apply for the joined table (null if not)
protected java.lang.String generateTableAlias(org.datanucleus.store.mapped.DatastoreContainerObject tbl,
java.lang.String groupName)
tbl - Table objectgroupName - Name of the table group
public void whereAnd(BooleanExpression expr,
boolean applyToUnions)
expr - The condition
public void whereOr(BooleanExpression expr,
boolean applyToUnions)
expr - The conditionapplyToUnions - Whether to apply to unionspublic void addGroupingExpression(SQLExpression expr)
expr - The expressionpublic void setHaving(BooleanExpression expr)
expr - Boolean expression for the having clause
public void setOrdering(SQLExpression[] exprs,
boolean[] descending)
exprs - The expressions to order bydescending - Whether each expression is ascending/descending
public void setRange(long offset,
long count)
offset - The offset to start fromcount - The number of records to returnpublic SQLText getSelectStatement()
protected SQLText generateOrderingStatement()
protected void addOrderingColumnsToSelect()
protected int selectSQLExpressionInternal(SQLExpression expr,
java.lang.String alias)
expr - The expressionalias - The alias to use
protected void invalidateStatement()
public void log(org.datanucleus.util.NucleusLogger logger)
logger - The logger
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||