Package org.datanucleus.store.query
Class QueryUtils
- java.lang.Object
-
- org.datanucleus.store.query.QueryUtils
-
public class QueryUtils extends java.lang.Object
Utilities for use in queries.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static java.lang.Class[]
MAP_PUT_METHOD_ARG_TYPES
Convenience Class[] for parameter types in getMethod call.
-
Constructor Summary
Constructors Constructor Description QueryUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
compareExpressionValues(java.lang.Object left, java.lang.Object right, Expression.Operator op)
Convenience method to compare two expression values against the specified operator.static java.lang.Object
createResultObjectUsingArgumentedConstructor(java.lang.Class resultClass, java.lang.Object[] fieldValues, java.lang.Class[] fieldTypes)
Convenience method to create an instance of the result class with the provided field values, using a constructor taking the arguments.static java.lang.Object
createResultObjectUsingArgumentedConstructor(java.lang.reflect.Constructor ctr, java.lang.Object[] fieldValues)
Convenience method to create an instance of the result class with the specified arg constructor and with the provided field values.static java.lang.Object
createResultObjectUsingDefaultConstructorAndSetters(java.lang.Class resultClass, java.lang.String[] resultFieldNames, java.lang.reflect.Field[] resultFields, java.lang.Object[] fieldValues)
Convenience method to create an instance of the result class with the provided field values, using the default constructor and setting the fields using either public fields, or setters, or a put method.static boolean
expressionHasNotOperator(Expression expr)
Convenience method to return if there is a NOT operator in the expression.static boolean
expressionHasOrOperator(Expression expr)
Convenience method to return if there is an OR operator in the expression.static java.lang.String[]
getExpressionsFromString(java.lang.String str)
Convenience method to split an expression string into its constituent parts where separated by commas.static java.lang.String
getKeyForQueryResultsCache(Query query, java.util.Map params)
Convenience method to generate the "key" for storing the query results of a query with parameters.static ParameterExpression
getParameterExpressionForPosition(Expression rootExpr, int pos)
Convenience method to return the ParameterExpression for the specified position if found in the expression tree starting atstatic java.lang.reflect.Method
getPublicPutMethodForResultClass(java.lang.Class resultClass)
Convenience method to return the put(Object, Object method for the result class.static java.lang.reflect.Method
getPublicSetMethodForFieldOfResultClass(java.lang.Class resultClass, java.lang.String fieldName, java.lang.Class fieldType)
Convenience method to return the setXXX method for a field of the result class.static java.lang.reflect.Constructor
getResultClassConstructorForArguments(java.lang.Class resultClass, java.lang.Class[] fieldTypes, java.lang.Object[] fieldValues)
Convenience method to obtain the constructor for the result class taking in the specified argument types.static java.lang.String
getStringValue(java.lang.Object obj)
Convenience method to get the String value for an Object.static java.lang.String
getStringValueForExpression(Expression expr, java.util.Map parameters)
Convenience method to get the String value for an Expression.static java.lang.Object
getValueForParameterExpression(java.util.Map parameterValues, ParameterExpression paramExpr)
Convenience method to get the value for a ParameterExpression.static java.util.List
orderCandidates(java.util.List candidates, java.lang.Class type, java.lang.String ordering, ExecutionContext ec, ClassLoaderResolver clr)
Convenience method to in-memory order the candidates, using the ordering supplied.static java.util.List
orderCandidates(java.util.List candidates, java.lang.Class type, java.lang.String ordering, ExecutionContext ec, ClassLoaderResolver clr, java.lang.String queryLanguage)
Convenience method to in-memory order the candidates, using the ordering supplied.static java.util.List
orderCandidates(java.util.List candidates, Expression[] ordering, java.util.Map state, java.lang.String candidateAlias, ExecutionContext ec, ClassLoaderResolver clr, java.util.Map parameterValues, Imports imports, java.lang.String queryLanguage)
Convenience method to order the input List of objects to the ordering defined by the compilation.static boolean
queryParameterTypesAreCompatible(java.lang.Class cls1, java.lang.Class cls2)
static boolean
queryReturnsSingleRow(Query query)
Convenience method to return whether the query should return a single row.static boolean
resultClassIsSimple(java.lang.String className)
Utility to return if the passed result class is a simple type with a single value.static boolean
resultClassIsUserType(java.lang.String className)
Utility to return if the passed result class is a user-type, and so requires fields matching up.static boolean
resultHasOnlyAggregates(java.lang.String result)
Convenience method to return if the "result" clause from a java string-based query language includes only aggregates.private static boolean
setFieldForResultObject(java.lang.Object obj, java.lang.String fieldName, java.lang.reflect.Field field, java.lang.Object value)
Method to set a field of an object, using set/put methods, or via a public field.
-
-
-
Method Detail
-
resultClassIsUserType
public static boolean resultClassIsUserType(java.lang.String className)
Utility to return if the passed result class is a user-type, and so requires fields matching up.- Parameters:
className
- the class name looked for- Returns:
- Whether it is a user class
-
resultClassIsSimple
public static boolean resultClassIsSimple(java.lang.String className)
Utility to return if the passed result class is a simple type with a single value. Checks the class name against the supported "simple" query result-class types.- Parameters:
className
- the class name looked for- Returns:
- Whether the result class is "simple".
-
resultHasOnlyAggregates
public static boolean resultHasOnlyAggregates(java.lang.String result)
Convenience method to return if the "result" clause from a java string-based query language includes only aggregates. This provides useful information when determining if the results will be a single row.- Parameters:
result
- The result required- Returns:
- Whether it has only aggregates
-
queryReturnsSingleRow
public static boolean queryReturnsSingleRow(Query query)
Convenience method to return whether the query should return a single row.- Parameters:
query
- The query- Returns:
- Whether it represents a unique row
-
getResultClassConstructorForArguments
public static java.lang.reflect.Constructor getResultClassConstructorForArguments(java.lang.Class resultClass, java.lang.Class[] fieldTypes, java.lang.Object[] fieldValues)
Convenience method to obtain the constructor for the result class taking in the specified argument types.- Parameters:
resultClass
- Result classfieldTypes
- Argument types (if known)fieldValues
- Argument values (if types not provided)- Returns:
- The constructor (or null if none found)
-
createResultObjectUsingArgumentedConstructor
public static java.lang.Object createResultObjectUsingArgumentedConstructor(java.lang.reflect.Constructor ctr, java.lang.Object[] fieldValues)
Convenience method to create an instance of the result class with the specified arg constructor and with the provided field values.- Parameters:
ctr
- Argumented constructorfieldValues
- The field values- Returns:
- The result class object
-
createResultObjectUsingArgumentedConstructor
public static java.lang.Object createResultObjectUsingArgumentedConstructor(java.lang.Class resultClass, java.lang.Object[] fieldValues, java.lang.Class[] fieldTypes)
Convenience method to create an instance of the result class with the provided field values, using a constructor taking the arguments. If the returned object is null there is no constructor with the correct signature. Tries to find a constructor taking the required arguments. Uses the fieldTypes first (if specified), then (if not specified) uses the type of the fieldValues, otherwise uses Object as the argument type.- Parameters:
resultClass
- The class of results that need creatingfieldValues
- The field valuesfieldTypes
- The field types (optional). If specified needs same number as fieldValues- Returns:
- The result class object
-
createResultObjectUsingDefaultConstructorAndSetters
public static java.lang.Object createResultObjectUsingDefaultConstructorAndSetters(java.lang.Class resultClass, java.lang.String[] resultFieldNames, java.lang.reflect.Field[] resultFields, java.lang.Object[] fieldValues)
Convenience method to create an instance of the result class with the provided field values, using the default constructor and setting the fields using either public fields, or setters, or a put method. If one of these parts is not found in the result class the returned object is null.- Parameters:
resultClass
- Result class that we need to create an object ofresultFieldNames
- Names of the fields in the resultsresultFields
- (java.lang.reflect.)Field objects for the fields in the resultsfieldValues
- The field values- Returns:
- The result class object
-
setFieldForResultObject
private static boolean setFieldForResultObject(java.lang.Object obj, java.lang.String fieldName, java.lang.reflect.Field field, java.lang.Object value)
Method to set a field of an object, using set/put methods, or via a public field. See JDO spec [14.6.12] describing the 3 ways of setting the field values after creating the result object using the default constructor.- Parameters:
obj
- The object to update the field forfieldName
- Name of the fieldfield
- The fieldvalue
- The value to apply- Returns:
- Whether it was updated
-
getPublicSetMethodForFieldOfResultClass
public static java.lang.reflect.Method getPublicSetMethodForFieldOfResultClass(java.lang.Class resultClass, java.lang.String fieldName, java.lang.Class fieldType)
Convenience method to return the setXXX method for a field of the result class.- Parameters:
resultClass
- The result classfieldName
- Name of the fieldfieldType
- The type of the field being set- Returns:
- The setter method
-
getPublicPutMethodForResultClass
public static java.lang.reflect.Method getPublicPutMethodForResultClass(java.lang.Class resultClass)
Convenience method to return the put(Object, Object method for the result class.- Parameters:
resultClass
- The result class- Returns:
- The put(Object, Object) method
-
getExpressionsFromString
public static java.lang.String[] getExpressionsFromString(java.lang.String str)
Convenience method to split an expression string into its constituent parts where separated by commas. This is used in the case of, for example, a result specification, to get the column definitions.- Parameters:
str
- The expression string- Returns:
- The expression parts
-
getValueForParameterExpression
public static java.lang.Object getValueForParameterExpression(java.util.Map parameterValues, ParameterExpression paramExpr)
Convenience method to get the value for a ParameterExpression. If the parameterValues is supplied as named then it will be like {[name1, val1], [name2, val2], ...} and paramExpr as id="name1", pos=0. If the parameterValues is supplied as positional then it will be like {[0, val1], [1, val2], ...} and paramExpr as id="name1", pos=0 (JDOQL) or id="1", pos=0 (JPQL)- Parameters:
parameterValues
- Input parameter values keyed by the parameter name/positionparamExpr
- Expression- Returns:
- The value in the object for this expression
-
getStringValue
public static java.lang.String getStringValue(java.lang.Object obj)
Convenience method to get the String value for an Object. Currently String, Character and Number are supported.- Parameters:
obj
- Object- Returns:
- The String value for the Object
-
getStringValueForExpression
public static java.lang.String getStringValueForExpression(Expression expr, java.util.Map parameters)
Convenience method to get the String value for an Expression. Currently only ParameterExpression and Literal are supported.- Parameters:
expr
- Expressionparameters
- Input parameters- Returns:
- The String value in the object for this expression
-
compareExpressionValues
public static boolean compareExpressionValues(java.lang.Object left, java.lang.Object right, Expression.Operator op)
Convenience method to compare two expression values against the specified operator. Returns true if "left {operator} right" is true. The operator can be <, >, ≥, ≤, ==, !=.- Parameters:
left
- Left objectright
- Right objectop
- Operator- Returns:
- Whether the comparison is true
- Throws:
NucleusException
- if the comparison is impossible
-
expressionHasOrOperator
public static boolean expressionHasOrOperator(Expression expr)
Convenience method to return if there is an OR operator in the expression. Allows for hierarchical expressions, navigating down through the expression tree.- Parameters:
expr
- The expression- Returns:
- Whether there is an OR
-
expressionHasNotOperator
public static boolean expressionHasNotOperator(Expression expr)
Convenience method to return if there is a NOT operator in the expression. Allows for hierarchical expressions, navigating down through the expression tree.- Parameters:
expr
- The expression- Returns:
- Whether there is a NOT
-
getParameterExpressionForPosition
public static ParameterExpression getParameterExpressionForPosition(Expression rootExpr, int pos)
Convenience method to return the ParameterExpression for the specified position if found in the expression tree starting atrootExpr
- Parameters:
rootExpr
- The expressionpos
- The position- Returns:
- The ParameterExpression (if found)
-
queryParameterTypesAreCompatible
public static boolean queryParameterTypesAreCompatible(java.lang.Class cls1, java.lang.Class cls2)
-
getKeyForQueryResultsCache
public static java.lang.String getKeyForQueryResultsCache(Query query, java.util.Map params)
Convenience method to generate the "key" for storing the query results of a query with parameters. The key will be of the formJDOQL:SELECT FROM myClass WHERE myFilter:123456
where "123456" is the hashCode of the parameters for the query- Parameters:
query
- The queryparams
- The params- Returns:
- The key
-
orderCandidates
public static java.util.List orderCandidates(java.util.List candidates, java.lang.Class type, java.lang.String ordering, ExecutionContext ec, ClassLoaderResolver clr)
Convenience method to in-memory order the candidates, using the ordering supplied. Assumes that the query is JDOQL.- Parameters:
candidates
- The candidatestype
- Candidate typeordering
- The ordering clauseec
- Execution Contextclr
- ClassLoader resolver- Returns:
- The ordered list
-
orderCandidates
public static java.util.List orderCandidates(java.util.List candidates, java.lang.Class type, java.lang.String ordering, ExecutionContext ec, ClassLoaderResolver clr, java.lang.String queryLanguage)
Convenience method to in-memory order the candidates, using the ordering supplied. Assumes that the query is JDOQL.- Parameters:
candidates
- The candidatestype
- Candidate typeordering
- The ordering clauseec
- Execution Contextclr
- ClassLoader resolverqueryLanguage
- The query language in use- Returns:
- The ordered list. Note that this typically returns Arrays$List which may not be what you want
-
orderCandidates
public static java.util.List orderCandidates(java.util.List candidates, Expression[] ordering, java.util.Map state, java.lang.String candidateAlias, ExecutionContext ec, ClassLoaderResolver clr, java.util.Map parameterValues, Imports imports, java.lang.String queryLanguage)
Convenience method to order the input List of objects to the ordering defined by the compilation.- Parameters:
candidates
- Candidatesordering
- Ordering expression(s)state
- Map of state information (see JavaQueryEvaluator)candidateAlias
- Candidate aliasec
- ExecutionContextclr
- ClassLoader resolverparameterValues
- Any parameter values (maybe used by the ordering clause)imports
- Imports for the queryqueryLanguage
- The language of this query (JDOQL, JPQL etc)- Returns:
- The ordered List of candidates
-
-