Package org.datanucleus.store.rdbms.sql
Class SQLStatementHelper
- java.lang.Object
-
- org.datanucleus.store.rdbms.sql.SQLStatementHelper
-
public class SQLStatementHelper extends Object
Series of convenience methods to help the process of generating SQLStatements.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static SQLTable
addJoinForOneToOneRelation(SQLStatement stmt, JavaTypeMapping sourceMapping, SQLTable sourceSqlTbl, JavaTypeMapping targetMapping, Table targetTable, String targetAlias, Object[] discrimValues, String targetTablegroupName, SQLJoin.JoinType joinType)
Convenience method to add a join across a 1-1 relation to the provided SQLStatement.static void
applyParametersToStatement(PreparedStatement ps, org.datanucleus.ExecutionContext ec, List<SQLStatementParameter> parameters, Map<Integer,String> paramNameByPosition, Map paramValuesByName)
Convenience method to apply parameter values to the provided statement.static Object
getDiscriminatorValueForClass(org.datanucleus.NucleusContext nucleusCtx, String className, org.datanucleus.metadata.DiscriminatorMetaData dismd, JavaTypeMapping discriminatorMapping, org.datanucleus.ClassLoaderResolver clr)
Convenience method to generate a BooleanExpression for the associated discriminator value for the specified class.static List
getDiscriminatorValuesForMember(String className, JavaTypeMapping discMapping, RDBMSStoreManager storeMgr, org.datanucleus.ClassLoaderResolver clr)
Method to return all possible discriminator values for the supplied class and its subclasses.static BooleanExpression
getExpressionForDiscriminatorForClass(SQLStatement stmt, String className, org.datanucleus.metadata.DiscriminatorMetaData dismd, JavaTypeMapping discriminatorMapping, SQLTable discrimSqlTbl, org.datanucleus.ClassLoaderResolver clr)
Convenience method to generate a BooleanExpression for the associated discriminator value for the specified class.static SQLJoin.JoinType
getJoinTypeForOneToOneRelationJoin(JavaTypeMapping sourceMapping, SQLTable sourceSqlTbl, SQLJoin.JoinType joinType)
Convenience method to return the join type to use for the specified 1-1 relation.static PreparedStatement
getPreparedStatementForSQLStatement(SQLStatement sqlStmt, org.datanucleus.ExecutionContext ec, org.datanucleus.store.connection.ManagedConnection mconn, String resultSetType, String resultSetConcurrency)
Convenience method to return a PreparedStatement for an SQLStatement.static SQLTable
getSQLTableForMappingOfTable(SQLStatement stmt, SQLTable sqlTbl, JavaTypeMapping mapping)
Method to return the SQLTable where the specified mapping (in the same table group as the provided SQLTable) is defined.static Object
getValueForPrimaryKeyIndexOfObjectUsingReflection(Object value, int index, org.datanucleus.metadata.AbstractClassMetaData cmd, RDBMSStoreManager storeMgr, org.datanucleus.ClassLoaderResolver clr)
Convenience method to use reflection to extract the value of a PK field of the provided object.static void
selectFetchPlanOfCandidateInStatement(SelectStatement stmt, StatementClassMapping mappingDefinition, org.datanucleus.metadata.AbstractClassMetaData candidateCmd, org.datanucleus.FetchPlan fetchPlan, int maxFetchDepth)
Method to select all fetch plan members for the candidate class.static void
selectFetchPlanOfSourceClassInStatement(SelectStatement stmt, StatementClassMapping mappingDefinition, org.datanucleus.FetchPlan fetchPlan, SQLTable sourceSqlTbl, org.datanucleus.metadata.AbstractClassMetaData sourceCmd, int maxFetchDepth)
Method to select all fetch plan members for the "source" class.static void
selectFetchPlanOfSourceClassInStatement(SelectStatement stmt, StatementClassMapping mappingDefinition, org.datanucleus.FetchPlan fetchPlan, SQLTable sourceSqlTbl, org.datanucleus.metadata.AbstractClassMetaData sourceCmd, int maxFetchDepth, SQLJoin.JoinType inputJoinType)
Method to select all fetch plan members for the "source" class.static void
selectIdentityOfCandidateInStatement(SelectStatement stmt, StatementClassMapping mappingDefinition, org.datanucleus.metadata.AbstractClassMetaData candidateCmd)
Method to select the identity for the candidate class.static void
selectMemberOfSourceInStatement(SelectStatement stmt, StatementClassMapping mappingDefinition, org.datanucleus.FetchPlan fetchPlan, SQLTable sourceSqlTbl, org.datanucleus.metadata.AbstractMemberMetaData mmd, org.datanucleus.ClassLoaderResolver clr, int maxFetchPlanLimit, SQLJoin.JoinType inputJoinType)
Method to select the specified member (field/property) of the source table in the passed SQL statement.
-
-
-
Method Detail
-
getPreparedStatementForSQLStatement
public static PreparedStatement getPreparedStatementForSQLStatement(SQLStatement sqlStmt, org.datanucleus.ExecutionContext ec, org.datanucleus.store.connection.ManagedConnection mconn, String resultSetType, String resultSetConcurrency) throws SQLException
Convenience method to return a PreparedStatement for an SQLStatement.- Parameters:
sqlStmt
- The query expressionec
- execution contextmconn
- The connection to useresultSetType
- Type of result set (if any)resultSetConcurrency
- result-set concurrency (if any)- Returns:
- The PreparedStatement
- Throws:
SQLException
- If an error occurs in creation
-
applyParametersToStatement
public static void applyParametersToStatement(PreparedStatement ps, org.datanucleus.ExecutionContext ec, List<SQLStatementParameter> parameters, Map<Integer,String> paramNameByPosition, Map paramValuesByName)
Convenience method to apply parameter values to the provided statement.- Parameters:
ps
- The prepared statementec
- ExecutionContextparameters
- The parametersparamNameByPosition
- Optional map of parameter names keyed by the positionparamValuesByName
- Value of parameter keyed by name (or position)
-
getValueForPrimaryKeyIndexOfObjectUsingReflection
public static Object getValueForPrimaryKeyIndexOfObjectUsingReflection(Object value, int index, org.datanucleus.metadata.AbstractClassMetaData cmd, RDBMSStoreManager storeMgr, org.datanucleus.ClassLoaderResolver clr)
Convenience method to use reflection to extract the value of a PK field of the provided object.- Parameters:
value
- The value of the overall objectindex
- Index of the PK field whose value we need to returncmd
- Metadata for the classstoreMgr
- Store managerclr
- ClassLoader resolver- Returns:
- Value of this index of the PK field
-
getSQLTableForMappingOfTable
public static SQLTable getSQLTableForMappingOfTable(SQLStatement stmt, SQLTable sqlTbl, JavaTypeMapping mapping)
Method to return the SQLTable where the specified mapping (in the same table group as the provided SQLTable) is defined. If the statement doesn't currently join to the required table then a join will be added. If the required table is a superclass table then the join will be INNER. If the required table is a secondary table then the join will be defined by the meta-data for the secondary table. If this table group is NOT the candidate table group then LEFT OUTER JOIN will be used.- Parameters:
stmt
- The statementsqlTbl
- SQLTable to start from for the supplied mapping (may be in super-table, or secondary-table of this)mapping
- The mapping- Returns:
- The SQLTable for this mapping (may have been added to the statement during this method)
-
selectIdentityOfCandidateInStatement
public static void selectIdentityOfCandidateInStatement(SelectStatement stmt, StatementClassMapping mappingDefinition, org.datanucleus.metadata.AbstractClassMetaData candidateCmd)
Method to select the identity for the candidate class. The supplied statement and mapping definition are updated during this method. Selects the datastore id (if using datastore id) as "DN_DATASTOREID", the version (if present) as "DN_VERSION", the discriminator (if present) as "DN_DISCRIM", and the application id (if using application id) as "DN_APPID_{i}"- Parameters:
stmt
- The statementmappingDefinition
- Mapping definition for result columnscandidateCmd
- The candidate class meta-data
-
selectFetchPlanOfCandidateInStatement
public static void selectFetchPlanOfCandidateInStatement(SelectStatement stmt, StatementClassMapping mappingDefinition, org.datanucleus.metadata.AbstractClassMetaData candidateCmd, org.datanucleus.FetchPlan fetchPlan, int maxFetchDepth)
Method to select all fetch plan members for the candidate class. The supplied statement and mapping definition are updated during this method. Shortcut to calling "selectFetchPlanOfSourceClassInStatement".- Parameters:
stmt
- The statementmappingDefinition
- Mapping definition for result columnscandidateCmd
- The candidate class meta-datafetchPlan
- FetchPlan in usemaxFetchDepth
- Max fetch depth from this point to select (0 implies no other objects)
-
selectFetchPlanOfSourceClassInStatement
public static void selectFetchPlanOfSourceClassInStatement(SelectStatement stmt, StatementClassMapping mappingDefinition, org.datanucleus.FetchPlan fetchPlan, SQLTable sourceSqlTbl, org.datanucleus.metadata.AbstractClassMetaData sourceCmd, int maxFetchDepth)
Method to select all fetch plan members for the "source" class. If the passed FetchPlan is null then the default fetch group fields will be selected. The source class is defined by the supplied meta-data, and the SQLTable that we are selecting from. The supplied statement and mapping definition are updated during this method.- Parameters:
stmt
- The statementmappingDefinition
- Mapping definition for result columns (populated with column positions of any selected mappings if provided as input)fetchPlan
- FetchPlan in usesourceSqlTbl
- SQLTable for the source class that we select fromsourceCmd
- Meta-data for the source classmaxFetchDepth
- Max fetch depth from this point to select (0 implies no other objects)
-
selectFetchPlanOfSourceClassInStatement
public static void selectFetchPlanOfSourceClassInStatement(SelectStatement stmt, StatementClassMapping mappingDefinition, org.datanucleus.FetchPlan fetchPlan, SQLTable sourceSqlTbl, org.datanucleus.metadata.AbstractClassMetaData sourceCmd, int maxFetchDepth, SQLJoin.JoinType inputJoinType)
Method to select all fetch plan members for the "source" class. If the passed FetchPlan is null then the default fetch group fields will be selected. The source class is defined by the supplied meta-data, and the SQLTable that we are selecting from. The supplied statement and mapping definition are updated during this method.- Parameters:
stmt
- The statementmappingDefinition
- Mapping definition for result columns (populated with column positions of any selected mappings if provided as input)fetchPlan
- FetchPlan in usesourceSqlTbl
- SQLTable for the source class that we select fromsourceCmd
- Meta-data for the source classmaxFetchDepth
- Max fetch depth from this point to select (0 implies no other objects)inputJoinType
- Optional join type to use for subobjects (otherwise decide join type internally)
-
selectMemberOfSourceInStatement
public static void selectMemberOfSourceInStatement(SelectStatement stmt, StatementClassMapping mappingDefinition, org.datanucleus.FetchPlan fetchPlan, SQLTable sourceSqlTbl, org.datanucleus.metadata.AbstractMemberMetaData mmd, org.datanucleus.ClassLoaderResolver clr, int maxFetchPlanLimit, SQLJoin.JoinType inputJoinType)
Method to select the specified member (field/property) of the source table in the passed SQL statement. This populates the mappingDefinition with the column details for this member.- Parameters:
stmt
- The SQL statementmappingDefinition
- Mapping definition for the results (will be populated by any selected mappings if provided as input)fetchPlan
- FetchPlansourceSqlTbl
- Table that has the member (or a super-table/secondary-table of this table)mmd
- Meta-data for the field/property in the source that we are selectingclr
- ClassLoader resolvermaxFetchPlanLimit
- Max fetch depth from this point to select (0 implies no other objects)inputJoinType
- Optional join type to use for subobjects (otherwise decide join type internally)
-
addJoinForOneToOneRelation
public static SQLTable addJoinForOneToOneRelation(SQLStatement stmt, JavaTypeMapping sourceMapping, SQLTable sourceSqlTbl, JavaTypeMapping targetMapping, Table targetTable, String targetAlias, Object[] discrimValues, String targetTablegroupName, SQLJoin.JoinType joinType)
Convenience method to add a join across a 1-1 relation to the provided SQLStatement. Returns the SQLTable for the other side of the relation. If the join type isn't provided and ID is in the target and FK in the source and the FK is not nullable then an "inner join" is used, otherwise a "left outer join" is used to cater for null values. If the join type is supplied then it is respected.- Parameters:
stmt
- The SQLStatementsourceMapping
- Mapping of the relation in the source tablesourceSqlTbl
- Source table in the SQLStatementtargetMapping
- Mapping of the relation in the target tabletargetTable
- Target table in the datastoretargetAlias
- Alias for target table to use in SQLStatementdiscrimValues
- Any discriminator values to apply to restrict the target side (if any)targetTablegroupName
- Name of the tablegroup that the target SQLTable should be in (if known)joinType
- Type of join to use (if known). If not known will use based on whether nullable- Returns:
- The SQLTable for the target once the join is added
-
getJoinTypeForOneToOneRelationJoin
public static SQLJoin.JoinType getJoinTypeForOneToOneRelationJoin(JavaTypeMapping sourceMapping, SQLTable sourceSqlTbl, SQLJoin.JoinType joinType)
Convenience method to return the join type to use for the specified 1-1 relation. If the join type isn't provided and ID is in the target and FK in the source and the FK is not nullable then an "inner join" is used, otherwise a "left outer join" is used to cater for null values.- Parameters:
sourceMapping
- Mapping of the relation in the source tablesourceSqlTbl
- Source table in the SQLStatementjoinType
- Join type to use if already known; will be returned if not null- Returns:
- Join type that should be used for this 1-1 relation
-
getExpressionForDiscriminatorForClass
public static BooleanExpression getExpressionForDiscriminatorForClass(SQLStatement stmt, String className, org.datanucleus.metadata.DiscriminatorMetaData dismd, JavaTypeMapping discriminatorMapping, SQLTable discrimSqlTbl, org.datanucleus.ClassLoaderResolver clr)
Convenience method to generate a BooleanExpression for the associated discriminator value for the specified class.- Parameters:
stmt
- The Query Statement to be updatedclassName
- The class namedismd
- MetaData for the discriminatordiscriminatorMapping
- Mapping for the discriminatordiscrimSqlTbl
- SQLTable for the table with the discriminatorclr
- ClassLoader resolver- Returns:
- Boolean expression for this discriminator value
-
getDiscriminatorValueForClass
public static Object getDiscriminatorValueForClass(org.datanucleus.NucleusContext nucleusCtx, String className, org.datanucleus.metadata.DiscriminatorMetaData dismd, JavaTypeMapping discriminatorMapping, org.datanucleus.ClassLoaderResolver clr)
Convenience method to generate a BooleanExpression for the associated discriminator value for the specified class.- Parameters:
nucleusCtx
- NucleusContextclassName
- The class namedismd
- MetaData for the discriminatordiscriminatorMapping
- Mapping for the discriminatorclr
- ClassLoader resolver- Returns:
- The discriminator value for this class
-
getDiscriminatorValuesForMember
public static List getDiscriminatorValuesForMember(String className, JavaTypeMapping discMapping, RDBMSStoreManager storeMgr, org.datanucleus.ClassLoaderResolver clr)
Method to return all possible discriminator values for the supplied class and its subclasses.- Parameters:
className
- Name of the classdiscMapping
- The discriminator mappingstoreMgr
- StoreManagerclr
- ClassLoader resolver- Returns:
- The possible discriminator values
-
-