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.
  • Constructor Details

    • SQLStatementHelper

      private SQLStatementHelper()
  • Method Details

    • 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 expression
      ec - execution context
      mconn - The connection to use
      resultSetType - 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 statement
      ec - ExecutionContext
      parameters - The parameters
      paramNameByPosition - Optional map of parameter names keyed by the position
      paramValuesByName - 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 object
      index - Index of the PK field whose value we need to return
      cmd - Metadata for the class
      storeMgr - Store manager
      clr - 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 statement
      sqlTbl - 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 statement
      mappingDefinition - Mapping definition for result columns
      candidateCmd - 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 statement
      mappingDefinition - Mapping definition for result columns
      candidateCmd - The candidate class meta-data
      fetchPlan - FetchPlan in use
      maxFetchDepth - 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 statement
      mappingDefinition - Mapping definition for result columns (populated with column positions of any selected mappings if provided as input)
      fetchPlan - FetchPlan in use
      sourceSqlTbl - SQLTable for the source class that we select from
      sourceCmd - Meta-data for the source class
      maxFetchDepth - 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 statement
      mappingDefinition - Mapping definition for result columns (populated with column positions of any selected mappings if provided as input)
      fetchPlan - FetchPlan in use
      sourceSqlTbl - SQLTable for the source class that we select from
      sourceCmd - Meta-data for the source class
      maxFetchDepth - 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 statement
      mappingDefinition - Mapping definition for the results (will be populated by any selected mappings if provided as input)
      fetchPlan - FetchPlan
      sourceSqlTbl - 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 selecting
      clr - ClassLoader resolver
      maxFetchPlanLimit - 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)
    • selectFetchPlanFieldsOfFKRelatedObject

      private static boolean selectFetchPlanFieldsOfFKRelatedObject(SelectStatement stmt, StatementClassMapping mappingDefinition, org.datanucleus.FetchPlan fetchPlan, SQLTable sourceSqlTbl, org.datanucleus.metadata.AbstractMemberMetaData mmd, org.datanucleus.ClassLoaderResolver clr, int maxFetchPlanLimit, JavaTypeMapping m, String tableGroupName, StatementMappingIndex stmtMapping, SQLTable sqlTbl, SQLJoin.JoinType inputJoinType)
      Convenience method to join to and select all required FP fields of a related object where linked via an FK at this side. Note that the FetchPlan can have null passed in here (27/04/2021) so cater for it.
      Returns:
      Whether the caller should select the FK themselves (i.e we haven't selected anything)
    • 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 SQLStatement
      sourceMapping - Mapping of the relation in the source table
      sourceSqlTbl - Source table in the SQLStatement
      targetMapping - Mapping of the relation in the target table
      targetTable - Target table in the datastore
      targetAlias - Alias for target table to use in SQLStatement
      discrimValues - 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 table
      sourceSqlTbl - Source table in the SQLStatement
      joinType - 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 updated
      className - The class name
      dismd - MetaData for the discriminator
      discriminatorMapping - Mapping for the discriminator
      discrimSqlTbl - SQLTable for the table with the discriminator
      clr - 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 - NucleusContext
      className - The class name
      dismd - MetaData for the discriminator
      discriminatorMapping - Mapping for the discriminator
      clr - 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 class
      discMapping - The discriminator mapping
      storeMgr - StoreManager
      clr - ClassLoader resolver
      Returns:
      The possible discriminator values