Query
- Super Interfaces:
Serializable
Query
interface allows applications to obtain persistent instances, values, and aggregate data from the data store. The PersistenceManager is the factory for Query
instances. There may be many Query
instances associated with a PersistenceManager
. Multiple queries might be executed simultaneously by different threads, but the implementation might choose to execute them serially. In either case, the implementation must be thread safe. There are three required elements in a Query
: the class of the results, the candidate collection of instances, and the filter.
There are optional elements: parameter declarations, variable declarations, import statements, ordering and grouping specifications, result and result class, the range of results, and flags indicating whether the query result is unique and whether the query can be modified.
The query namespace is modeled after methods in Java:
setClass
corresponds to the class definitiondeclareParameters
corresponds to formal parameters of a methoddeclareVariables
corresponds to local variables of a methodsetFilter
andsetOrdering
correspond to the method body
There are two namespaces in queries. Type names have their own namespace that is separate from the namespace for fields, variables and parameters.
The method setClass
introduces the name of the candidate class in the type namespace. The method declareImports
introduces the names of the imported class or interface types in the type namespace. Imported type names must be unique. When used (e.g. in a parameter declaration, cast expression, etc.) a type name must be the name of the candidate class, the name of a class or interface imported by method declareImports
, or denote a class or interface from the same package as the candidate class.
The method setClass
introduces the names of the candidate class fields.
The method declareParameters
introduces the names of the parameters. A name introduced by declareParameters
hides the name of a candidate class field of the same name. Parameter names must be unique.
The method declareVariables
introduces the names of the variables. A name introduced by declareVariables
hides the name of a candidate class field if equal. Variable names must be unique and must not conflict with parameter names.
The result of the query by default is a list of result class instances, but might be specified via setResult
. The class of the result by default is the candidate class, but might be specified via setResultClass
.
A hidden field may be accessed using the 'this' qualifier: this.fieldName
.
The Query
interface provides methods which execute the query based on the parameters given. They return a single instance or a List
of result class instances which the user can iterate to get results. The signature of the execute
methods specifies that they return an Object
which must be cast to the appropriate result by the user.
Any parameters passed to the execute
methods are used only for this execution, and are not remembered for future execution.
- Since:
- JDO 1.0
Public Methods
- Parameters:
key
- the key of the extensionvalue
- the value of the extension
- Since:
- JDO 2.0
- Parameters:
sub
- the subquery to add to this QueryvariableDeclaration
- the name of the variable in the outer query to bind the results of the subquerycandidateCollectionExpression
- the candidate collection of the subquery as an expression using terms of the outer query
- See Also:
addSubquery(Query sub,String variableDeclaration,String candidateCollectionExpression,String... parameters)
- Since:
- JDO 2.1
- Parameters:
sub
- the subquery to add to this QueryvariableDeclaration
- the name of the variable to be used in this QuerycandidateCollectionExpression
- the candidate collection to apply to the subqueryparameters
- the expressions from the outer query to bind the parameter in the subquery
- See Also:
addSubquery(Query sub,String variableDeclaration,String candidateCollectionExpression,String... parameters)
- Since:
- JDO 2.1
- Parameters:
sub
- the subquery to add to this QueryvariableDeclaration
- the name of the variable to be used in this QuerycandidateCollectionExpression
- the candidate collection to apply to the subqueryparameter
- the expression from the outer query to bind the parameter in the subquery
- See Also:
addSubquery(Query sub,String variableDeclaration,String candidateCollectionExpression,String... parameters)
- Since:
- JDO 2.1
- Parameters:
sub
- the subquery to add to this QueryvariableDeclaration
- the name of the variable in the outer query to bind the results of the subquerycandidateCollectionExpression
- the candidate collection of the subquery as an expression using terms of the outer queryparameters
- the expressions from the outer query to bind the parameters in the subquery
- Since:
- JDO 2.1
execute(...)
and might have iterators open on it. Iterators associated with the query result are invalidated: they return false
to hasNext()
and throw NoSuchElementException
to next()
. - Parameters:
queryResult
- the result ofexecute(...)
on thisQuery
instance.
- Since:
- JDO 1.0
false
to hasNext()
and throw NoSuchElementException
to next()
. - Since:
- JDO 1.0
- Since:
- JDO 1.0
String
with semicolon-separated statements. The String
parameter to this method follows the syntax of the import statement of the Java language.
- Parameters:
imports
- import statements separated by semicolons.
- Since:
- JDO 1.0
String
containing one or more query parameter declarations separated with commas. Each parameter named in the parameter declaration must be bound to a value when the query is executed. The String
parameter to this method follows the syntax for formal parameters in the Java language.
- Parameters:
parameters
- the list of parameters separated by commas.
- Since:
- JDO 1.0
String
containing one or more unbound variable declarations separated with semicolons. It follows the syntax for local variables in the Java language. - Parameters:
variables
- the variables separated by semicolons.
- Since:
- JDO 1.0
Dirty instances of affected classes in the cache are first flushed to the datastore. Instances in the cache or brought into the cache as a result of executing one of the deletePersistentAll
methods undergo life cycle changes as if deletePersistent
were called on them.
Specifically, if the class of deleted instances implements the delete callback interface, the corresponding callback methods are called on the deleted instances. Similarly, if there are lifecycle listeners registered for delete events on affected classes, the listener is called for each appropriate deleted instance.
Before returning control to the application, instances of affected classes in the cache are refreshed to reflect whether they were deleted from the datastore.
- Return:
- the number of instances of the candidate class that were deleted
- Since:
- JDO 2.0
- Parameters:
parameters
- for the query
- Return:
- the number of instances of the candidate class that were deleted
- See Also:
deletePersistentAll()
- Since:
- JDO 2.0
- Parameters:
parameters
- for the query
- Return:
- the number of instances of the candidate class that were deleted
- See Also:
deletePersistentAll()
- Since:
- JDO 2.0
- Parameters:
p1
- the value of the first parameter declared.
- Return:
- the filtered
Collection
.
- See Also:
executeWithArray(Object[] parameters)
- Since:
- JDO 1.0
- Parameters:
p1
- the value of the first parameter declared.p2
- the value of the second parameter declared.
- Return:
- the filtered
Collection
.
- See Also:
executeWithArray(Object[] parameters)
- Since:
- JDO 1.0
- Parameters:
p1
- the value of the first parameter declared.p2
- the value of the second parameter declared.p3
- the value of the third parameter declared.
- Return:
- the filtered
Collection
.
- See Also:
executeWithArray(Object[] parameters)
- Since:
- JDO 1.0
The execution of the query obtains the values of the parameters and matches them against the declared parameters in order. The names of the declared parameters are ignored. The type of the declared parameters must match the type of the passed parameters, except that the passed parameters might need to be unwrapped to get their primitive values.
The filter, import, declared parameters, declared variables, and ordering statements are verified for consistency.
Each element in the candidate Collection
is examined to see that it is assignment compatible to the Class
of the query. It is then evaluated by the Boolean expression of the filter. The element passes the filter if there exist unique values for all variables for which the filter expression evaluates to true
.
- Parameters:
parameters
- theObject
array with all of the parameters.
- Return:
- the filtered
Collection
.
- Since:
- JDO 1.0
Map
values. Each Map
entry consists of a key which is the name of the parameter in the declareParameters
method, and a value which is the value used in the execute
method. The keys in the Map
and the declared parameters must exactly match or a JDOUserException
is thrown. - Parameters:
parameters
- theMap
containing all of the parameters.
- Return:
- the filtered
Collection
.
- See Also:
executeWithArray(Object[] parameters)
- Since:
- JDO 1.0
PersistenceManager
's FetchPlan
to be modified. - Return:
- the fetch plan used by this query
- Since:
- JDO 2.0
If this Query
was restored from a serialized form, it has no PersistenceManager
, and this method returns null
.
- Return:
- the
PersistenceManager
associated with thisQuery
.
- Since:
- JDO 1.0
- Return:
- the current setting of the flag
- Since:
- JDO 2.0
- Parameters:
pcs
- the candidateCollection
.
- Since:
- JDO 1.0
- Parameters:
pcs
- the candidateExtent
.
- Since:
- JDO 1.0
The class specifies the class of the candidates of the query. Elements of the candidate collection that are of the specified class are filtered before being put into the result Collection
.
- Parameters:
cls
- theClass
of the candidate instances.
- Since:
- JDO 1.0
- Parameters:
extensions
- the map of extensions
- See Also:
addExtension
- Since:
- JDO 2.0
The filter specification is a String
containing a Boolean expression that is to be evaluated for each of the instances in the candidate collection. If the filter is not specified, then it defaults to "true", which has the effect of filtering the input Collection
only for class type.
An element of the candidate collection is returned in the result if:
- it is assignment compatible to the candidate
Class
of theQuery
; and - for all variables there exists a value for which the filter expression evaluates to
true
.
The user may denote uniqueness in the filter expression by explicitly declaring an expression (for example, e1 != e2
).
Rules for constructing valid expressions follow the Java language, except for these differences:
- Equality and ordering comparisons between primitives and instances of wrapper classes are valid.
- Equality and ordering comparisons of
Date
fields andDate
parameters are valid. - White space (non-printing characters space, tab, carriage return, and line feed) is a separator and is otherwise ignored.
- The assignment operators
=
,+=
, etc. and pre- and post-increment and -decrement are not supported. Therefore, there are no side effects from evaluation of any expressions. - Methods, including object construction, are not supported, except for
Collection.contains(Object o)
,Collection.isEmpty()
,String.startsWith(String s)
, andString.endsWith(String e)
. Implementations might choose to support non-mutating method calls as non-standard extensions. - Navigation through a
null
-valued field, which would throwNullPointerException
, is treated as if the filter expression returnedfalse
for the evaluation of the current set of variable values. Other values for variables might still qualify the candidate instance for inclusion in the result set. - Navigation through multi-valued fields (
Collection
types) is specified using a variable declaration and theCollection.contains(Object o)
method.
Identifiers in the expression are considered to be in the name space of the specified class, with the addition of declared imports, parameters and variables. As in the Java language, this
is a reserved word which means the element of the collection being evaluated.
Navigation through single-valued fields is specified by the Java language syntax of field_name.field_name....field_name
.
A JDO implementation is allowed to reorder the filter expression for optimization purposes.
- Parameters:
filter
- the query filter.
- Since:
- JDO 1.0
- Parameters:
group a
- comma-delimited list of expressions, optionally followed by the "having" keyword and a boolean expression
- Since:
- JDO 2.0
PersistenceManagerFactory
or the PersistenceManager
used to create this Query
. The ignoreCache option setting specifies whether the query should execute entirely in the back end, instead of in the cache. If this flag is set to true
, an implementation might be able to optimize the query execution by ignoring changed values in the cache. For optimistic transactions, this can dramatically improve query response times. - Parameters:
ignoreCache
- the setting of the ignoreCache option.
- Since:
- JDO 1.0
String
containing one or more ordering declarations separated by commas. Each ordering declaration is the name of the field on which to order the results followed by one of the following words: "ascending
" or "descending
".
The field must be declared in the candidate class or must be a navigation expression starting with a field in the candidate class.
Valid field types are primitive types except boolean
; wrapper types except Boolean
; BigDecimal
; BigInteger
; String
; and Date
.
- Parameters:
ordering
- the ordering specification.
- Since:
- JDO 1.0
setRange("50, 70");
or setRange(":from, :to");
or setRange("50, :to");
. The execution of the query is modified to return only a subset of results. If the filter would normally return 100 instances, and fromIncl is set to 50, and toExcl is set to 70, then the first 50 results that would have been returned are skipped, the next 20 results are returned and the remaining 30 results are ignored. An implementation should execute the query such that the range algorithm is done at the data store. - Parameters:
fromInclToExcl
- comma-separated fromIncl and toExcl values
- See Also:
setRange
- Since:
- JDO 2.0
- Parameters:
fromIncl
- 0-based inclusive start indextoExcl
- 0-based exclusive end index, or {@link Long#MAX_VALUE} for no limit.
- Since:
- JDO 2.0
null
, this query returns instances of the query's candidate class. If set, this query will return expressions, including field values (projections) and aggregate function results. - Parameters:
data
- a comma-delimited list of expressions (fields, functions on fields, or aggregate functions) to return from this query
- Since:
- JDO 2.0
If there are multiple result expressions, the result class must be able to hold all elements of the result specification or a JDOUserException is thrown.
If there is only one result expression, the result class must be assignable from the type of the result expression or must be able to hold all elements of the result specification. A single value must be able to be coerced into the specified result class (treating wrapper classes as equivalent to their unwrapped primitive types) or by matching. If the result class does not satisfy these conditions, a JDOUserException is thrown.
A constructor of a result class specified in the setResult method will be used if the results specification matches the parameters of the constructor by position and type. If more than one constructor satisfies the requirements, the JDO implementation chooses one of them. If no constructor satisfies the results requirements, or if the result class is specified via the setResultClass method, the following requirements apply:
- A user-defined result class must have a no-args constructor and one or more public
set
orput
methods or fields. - Each result expression must match one of:
- a public field that matches the name of the result expression and is of the type (treating wrapper types equivalent to primitive types) of the result expression;
- or if no public field matches the name and type, a public
setor if neither of the above applies,a public method must be found with the signature
void put(Object, Object)
. During processing of the results, the first argument is the name of the result expression and the second argument is the value from the query result.
set
- Parameters:
cls
- the result class
- Since:
- JDO 2.0
- Parameters:
unique
- if true, only one element is returned
- Since:
- JDO 2.0
- Since:
- JDO 2.0