org.datanucleus.store.rdbms.sql.expression
Class ExpressionUtils

java.lang.Object
  extended by org.datanucleus.store.rdbms.sql.expression.ExpressionUtils

public class ExpressionUtils
extends Object

Utility methods for working with SQL expressions.


Field Summary
protected static org.datanucleus.util.Localiser LOCALISER
          Localiser for messages
 
Constructor Summary
ExpressionUtils()
           
 
Method Summary
static void checkAndCorrectExpressionMappingsForBooleanComparison(SQLExpression expr1, SQLExpression expr2)
          Convenience method that compares the mappings used by the two expressions for compatibility for use in a boolean comparison (eq, noteq, gt, gteq, lt, lteq) and, if necessary, updates the mapping if one of them is a SQLLiteral and is deemed inconsistent with the other expression.
protected static void checkAndCorrectLiteralForConsistentMappingsForBooleanComparison(SQLLiteral lit, SQLExpression expr)
           
static BooleanExpression getAppIdEqualityExpression(Object id, SQLExpression expr, org.datanucleus.store.mapped.MappedStoreManager storeMgr, org.datanucleus.ClassLoaderResolver clr, org.datanucleus.metadata.AbstractClassMetaData acmd, Integer index, BooleanExpression bExpr)
          Create an equality expression "(expr == id)" for an application identity using reflection to retrieve values and generate the mappings.
static BooleanExpression getEqualityExpressionForObjectExpressions(ObjectExpression expr1, ObjectExpression expr2, boolean equals)
          Method to generate an equality/inequality expression between two ObjectExpressions.
static SQLExpression getEscapedPatternExpression(SQLExpression patternExpr)
          The pattern string for representing one character.
static SQLExpression getLiteralForOne(SQLStatement stmt)
          Convenience accessor for a literal for the number 1.
static NumericExpression getNumericExpression(SQLExpression expr)
          Method to return a numeric expression for the supplied SQL expression.
static StringExpression getStringExpression(SQLExpression expr)
          Method to convert the provided expression into a String-based expression using the datastore.
static Object getValueForMemberOfObject(org.datanucleus.store.ExecutionContext ec, org.datanucleus.metadata.AbstractMemberMetaData mmd, Object object)
          Get the value of a managed field/property in the provided object.
static int populatePrimaryKeyMappingsValuesForPCMapping(org.datanucleus.store.mapped.mapping.JavaTypeMapping[] pkMappings, Object[] pkFieldValues, int position, org.datanucleus.store.mapped.mapping.PersistableMapping pcMapping, org.datanucleus.metadata.AbstractClassMetaData cmd, org.datanucleus.metadata.AbstractMemberMetaData mmd, Object fieldValue, org.datanucleus.store.mapped.MappedStoreManager storeMgr, org.datanucleus.ClassLoaderResolver clr)
          Convenience method to populate PK mappings/values allowing for recursion where a PK field is itself a PCMapping, that itself has PK mappings, which in turn may include PCMappings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOCALISER

protected static final org.datanucleus.util.Localiser LOCALISER
Localiser for messages

Constructor Detail

ExpressionUtils

public ExpressionUtils()
Method Detail

getNumericExpression

public static NumericExpression getNumericExpression(SQLExpression expr)
Method to return a numeric expression for the supplied SQL expression. Makes use of the RDBMS function to convert to a numeric.

Parameters:
expr - The expression
Returns:
The numeric expression for this SQL expression

getLiteralForOne

public static SQLExpression getLiteralForOne(SQLStatement stmt)
Convenience accessor for a literal for the number 1.

Parameters:
stmt - The SQL statement
Returns:
The literal

getStringExpression

public static StringExpression getStringExpression(SQLExpression expr)
Method to convert the provided expression into a String-based expression using the datastore. Generates SQL like :-
 CAST({expr} AS VARCHAR(4000))
 
TODO Move this to a method so we can have datastore differences. e.g MySQL wants to cast as CHAR

Parameters:
expr - The expression
Returns:
The StringExpression

getEscapedPatternExpression

public static SQLExpression getEscapedPatternExpression(SQLExpression patternExpr)
The pattern string for representing one character. Most of databases will use the underscore character.

Parameters:
patternExpr - The expression that represents one character for a matcher/parser in the database
Returns:
the pattern string.

populatePrimaryKeyMappingsValuesForPCMapping

public static int populatePrimaryKeyMappingsValuesForPCMapping(org.datanucleus.store.mapped.mapping.JavaTypeMapping[] pkMappings,
                                                               Object[] pkFieldValues,
                                                               int position,
                                                               org.datanucleus.store.mapped.mapping.PersistableMapping pcMapping,
                                                               org.datanucleus.metadata.AbstractClassMetaData cmd,
                                                               org.datanucleus.metadata.AbstractMemberMetaData mmd,
                                                               Object fieldValue,
                                                               org.datanucleus.store.mapped.MappedStoreManager storeMgr,
                                                               org.datanucleus.ClassLoaderResolver clr)
Convenience method to populate PK mappings/values allowing for recursion where a PK field is itself a PCMapping, that itself has PK mappings, which in turn may include PCMappings. The pkMappings/pkFieldValues arrays are already created and we populate from "position".

Parameters:
pkMappings - The array of pk mappings to be populated
pkFieldValues - The array of pk field values to be populated
position - The current position needing populating
pcMapping - The PC mapping we are processing
cmd - ClassMetaData for the owning class with this PCMapping field
mmd - Field metadata for the field that this PCMapping represents
fieldValue - The value for the PCMapping field in the owning object
storeMgr - Store Manager
clr - ClassLoader resolver
Returns:
The current position (after our processing)

getValueForMemberOfObject

public static Object getValueForMemberOfObject(org.datanucleus.store.ExecutionContext ec,
                                               org.datanucleus.metadata.AbstractMemberMetaData mmd,
                                               Object object)
Get the value of a managed field/property in the provided object.

Parameters:
ec - execution context
mmd - metadata for the field/property
object - the pc object
Returns:
The field value

getAppIdEqualityExpression

public static BooleanExpression getAppIdEqualityExpression(Object id,
                                                           SQLExpression expr,
                                                           org.datanucleus.store.mapped.MappedStoreManager storeMgr,
                                                           org.datanucleus.ClassLoaderResolver clr,
                                                           org.datanucleus.metadata.AbstractClassMetaData acmd,
                                                           Integer index,
                                                           BooleanExpression bExpr)
Create an equality expression "(expr == id)" for an application identity using reflection to retrieve values and generate the mappings.

Parameters:
id - the identity to compare against
expr - the object expression
storeMgr - the StoreManager
clr - the ClassLoaderResolver
acmd - MetaData for the class the object id represents
index - the current index in the source expression (internal)
bExpr - the boolean equals expression (internal)
Returns:
the equality expression

getEqualityExpressionForObjectExpressions

public static BooleanExpression getEqualityExpressionForObjectExpressions(ObjectExpression expr1,
                                                                          ObjectExpression expr2,
                                                                          boolean equals)
Method to generate an equality/inequality expression between two ObjectExpressions. Either or both of the expressions can be ObjectLiterals.

Parameters:
expr1 - First expression
expr2 - Second expression
equals - Whether it is equality (otherwise inequality)
Returns:
The expression

checkAndCorrectExpressionMappingsForBooleanComparison

public static void checkAndCorrectExpressionMappingsForBooleanComparison(SQLExpression expr1,
                                                                         SQLExpression expr2)
Convenience method that compares the mappings used by the two expressions for compatibility for use in a boolean comparison (eq, noteq, gt, gteq, lt, lteq) and, if necessary, updates the mapping if one of them is a SQLLiteral and is deemed inconsistent with the other expression. Additionally, if both sides of the comparison are parameters, this will swap one to be its literal value.

Parameters:
expr1 - First expression
expr2 - Second expression

checkAndCorrectLiteralForConsistentMappingsForBooleanComparison

protected static void checkAndCorrectLiteralForConsistentMappingsForBooleanComparison(SQLLiteral lit,
                                                                                      SQLExpression expr)


Copyright © 2012. All Rights Reserved.