Package com.storedobject.core
Class QueryBuilder<T extends StoredObject>
java.lang.Object
com.storedobject.core.QueryBuilder<T>
- Type Parameters:
T
- The type ofStoredObject
this builder targets.
A builder class for constructing queries and retrieving
StoredObject
instances based on various criteria.
This class provides methods to specify conditions, sorting and other query parameters. This can be used for
querying and/or retrieving StoredObject
instances, their link instances and master instances.- Author:
- Syam
-
Method Summary
Modifier and TypeMethodDescriptionany()
Enables the "any" condition so that all subclasses to be included in the output.any
(boolean any) Sets the "any" condition for the query, a boolean value indicating whether to retrieve all subclasses or not.Sets the "any" condition for the query, a boolean value indicating whether to retrieve all subclasses or not.Sets the column names to be retrieved or operated upon in the query being built.int
count()
Counts the number of records that match the query constructed using the current configurations of theQueryBuilder
.int
countLinks
(Id parentId) Counts the number of links associated with the given parent object.int
countLinks
(Id parentId, int linkType) Counts the number of links of a specified type for a given parent object.int
countLinks
(StoredObject parent) Counts the number of links associated with the given parent object.int
countLinks
(StoredObject parent, int linkType) Counts the number of links of a specified type for a given parent object.int
countMasters
(Id link) Counts the number of master objects associated with the given link.int
countMasters
(Id linkId, int linkType) Counts the number of master objects associated with a given link and link type.int
countMasters
(StoredObject link) Counts the number of master objects associated with the given link.int
countMasters
(StoredObject link, int linkType) Counts the number of master objects associated with a given link and link type.distinctColumns
(int... columnNumbers) Specifies the columns by their indexes to be considered as distinct in the query.boolean
exists()
Check whether any entry exists or not.boolean
existsLinks
(Id parentId) Check whether link objects exists associated with the specified parent object.boolean
existsLinks
(Id parentId, int linkType) Check whether link objects exists associated with the specified parent object and of a specific link type.boolean
existsLinks
(StoredObject parent) Check whether link objects exists associated with the specified parent object.boolean
existsLinks
(StoredObject parent, int linkType) Check whether link objects exists associated with the specified parent object and of a specific link type.boolean
existsMasters
(Id linkId) Check whether master objects exists related to the given stored object link.boolean
existsMasters
(Id link, int linkType) Check whether master objects exists associated with a given stored object link and link type.boolean
existsMasters
(StoredObject link) Check whether master objects exists related to the given stored object link.boolean
existsMasters
(StoredObject link, int linkType) Check whether master objects exists associated with a given stored object link and link type.static <O extends StoredObject>
QueryBuilder<O> Creates a newQueryBuilder
instance for the specified class type.get()
Get an object instance.static <O extends StoredObject>
StringgetExistsCondition
(Class<O> objectClass, String attributeName) Create an "exists condition" that can be used for retrieving or queryingStoredObject
instances.static <O extends StoredObject>
StringgetExistsCondition
(Class<O> objectClass, String attributeName, String condition) Create an "exists condition" that can be used for retrieving or queryingStoredObject
instances.static <O extends StoredObject>
StringgetInCondition
(String attributeName, Class<O> objectClass) static <O extends StoredObject>
StringgetInCondition
(String attributeName, Class<O> objectClass, boolean any) static <O extends StoredObject>
StringgetInCondition
(String attributeName, Class<O> objectClass, String condition) static <O extends StoredObject>
StringgetInCondition
(String attributeName, Class<O> objectClass, String condition, boolean any) Get the first link object.Get the first link object.getLink
(StoredObject master) Get the first link object.getLink
(StoredObject master, int linkType) Get the first link object.Get the first master object.Get the first master object.getMaster
(StoredObject link) Get the first master object.getMaster
(StoredObject link, int linkType) Get the first master object.static <O extends StoredObject>
StringgetNotExistsCondition
(Class<O> objectClass, String attributeName) Create a "not exists condition" that can be used for retrieving or queryingStoredObject
instances.static <O extends StoredObject>
StringgetNotExistsCondition
(Class<O> objectClass, String attributeName, String condition) Create a "not exists condition" that can be used for retrieving or queryingStoredObject
instances.static <O extends StoredObject>
StringgetNotInCondition
(String attributeName, Class<O> objectClass) static <O extends StoredObject>
StringgetNotInCondition
(String attributeName, Class<O> objectClass, boolean any) static <O extends StoredObject>
StringgetNotInCondition
(String attributeName, Class<O> objectClass, String condition) static <O extends StoredObject>
StringgetNotInCondition
(String attributeName, Class<O> objectClass, String condition, boolean any) Retrieves the class type of the object represented by this instance.limit
(int limit) Sets a limit on the number of records to be retrieved by the query.list()
Retrieves an iterator containing the results that match the query constructed with the current configurations ofQueryBuilder
.Returns an iterator over the linked objects of the specified parent object.Retrieves an iterator over objects that are linked to the given parent object with the specified link type.listLinks
(StoredObject parent) Returns an iterator over the linked objects of the specified parent object.listLinks
(StoredObject parent, int linkType) Retrieves an iterator over objects that are linked to the given parent object with the specified link type.listMasters
(Id linkId) Retrieves an iterator over master objects linked to the specified stored object.listMasters
(Id linkId, int linkType) Retrieves an iterator over the master objects linked to the specified stored object.listMasters
(StoredObject link) Retrieves an iterator over master objects linked to the specified stored object.listMasters
(StoredObject link, int linkType) Retrieves an iterator over the master objects linked to the specified stored object.Sets the "order by" clause for the query being built.query()
Executes the query built using the current configurations of theQueryBuilder
.queryLinks
(Id parentId) Constructs a query to retrieve the links associated with the specified parent object.queryLinks
(Id parentId, int linkType) Constructs a query to retrieve the links associated with the specified parent object and of a specific link type.queryLinks
(StoredObject parent) Constructs a query to retrieve the links associated with the specified parent object.queryLinks
(StoredObject parent, int linkType) Constructs a query to retrieve the links associated with the specified parent object and of a specific link type.queryMasters
(Id linkId) Queries and retrieves master objects related to the given stored object link.queryMasters
(Id linkId, int linkType) Queries the master objects associated with a given stored object link and link type.queryMasters
(StoredObject link) Queries and retrieves master objects related to the given stored object link.queryMasters
(StoredObject link, int linkType) Queries the master objects associated with a given stored object link and link type.querySQL()
Constructs and returns an SQL query string based on the given parameters and object class.skip
(int skip) Sets an offset to skip a specified number of records or results in the query being built.transaction
(Transaction transaction) Sets the transaction for the query being built.Sets the "where" clause for the query being built.
-
Method Details
-
from
Creates a newQueryBuilder
instance for the specified class type.- Type Parameters:
O
- The type of objects that extendStoredObject
.- Parameters:
objectClass
- The class of the object for which the query builder is to be created.- Returns:
- A new
QueryBuilder
instance for the specified object type.
-
getObjectClass
-
where
Sets the "where" clause for the query being built.- Parameters:
where
- The condition to filter the query results. It should be a valid query condition string.- Returns:
- The current
QueryBuilder
instance with the specified "where" clause applied.
-
orderBy
Sets the "order by" clause for the query being built.- Parameters:
orderBy
- The column or columns by which the query results should be ordered. It should be a valid column name or a comma-separated list of column names.- Returns:
- The current
QueryBuilder
instance with the specified "order by" clause applied.
-
limit
Sets a limit on the number of records to be retrieved by the query.- Parameters:
limit
- The maximum number of results the query should return.- Returns:
- The current
QueryBuilder
instance with the specified limit applied.
-
skip
Sets an offset to skip a specified number of records or results in the query being built.- Parameters:
skip
- The number of records to skip in the result set.- Returns:
- The current
QueryBuilder
instance with the specified skip value applied.
-
any
Enables the "any" condition so that all subclasses to be included in the output.- Returns:
- The current
QueryBuilder
instance with the "any" condition applied.
-
any
Sets the "any" condition for the query, a boolean value indicating whether to retrieve all subclasses or not.- Parameters:
any
- A value of true means all subclasses to be included. If any is null, it is considered as false.- Returns:
- the updated QueryBuilder instance.
-
any
Sets the "any" condition for the query, a boolean value indicating whether to retrieve all subclasses or not.- Parameters:
any
- A value of true means all subclasses to be included.- Returns:
- the updated QueryBuilder instance.
-
columns
Sets the column names to be retrieved or operated upon in the query being built.- Parameters:
columns
- A string representing the column names to include in the query. The column names should be separated by commas if specifying multiple columns. Since the SO platform supports dot (.) notation to retrieve related class instances, it may be used to obtain attributes of related class instances.- Returns:
- The current
QueryBuilder
instance with the specified columns applied.
-
distinctColumns
Specifies the columns by their indexes to be considered as distinct in the query.- Parameters:
columnNumbers
- The indexes of the columns to be included as distinct in the query.- Returns:
- The current
QueryBuilder
instance with the specified distinct columns applied.
-
transaction
Sets the transaction for the query being built.- Parameters:
transaction
- The transaction to be associated with the query.- Returns:
- The current
QueryBuilder
instance with the specified transaction applied.
-
get
Get an object instance. The first item is retrieved.- Returns:
- An object instance or null.
-
getMaster
Get the first master object.- Parameters:
link
- Link object.- Returns:
- A master instance or null.
-
getMaster
Get the first master object.- Parameters:
link
- Link object.linkType
- Link type.- Returns:
- A master instance or null.
-
getMaster
-
getMaster
-
getLink
Get the first link object.- Parameters:
master
- Master object.- Returns:
- A master instance or null.
-
getLink
Get the first link object.- Parameters:
master
- Master object.linkType
- Link type.- Returns:
- A master instance or null.
-
getLink
-
getLink
-
exists
public boolean exists()Check whether any entry exists or not.- Returns:
- True/false
-
query
Executes the query built using the current configurations of theQueryBuilder
.- Returns:
- The
Query
object representing the query configured with the specified parameters such as object class, columns, where clause, order by clause, distinct columns, transaction, and limits.
-
list
Retrieves an iterator containing the results that match the query constructed with the current configurations ofQueryBuilder
.- Returns:
- An
ObjectIterator
containing objects of typeT
that satisfy the query criteria, including filters, ordering, limits, and other specified parameters.
-
count
public int count()Counts the number of records that match the query constructed using the current configurations of theQueryBuilder
.- Returns:
- The number of records that match the specified query criteria.
-
queryLinks
Constructs a query to retrieve the links associated with the specified parent object.- Parameters:
parent
- The parent object for which linked objects are to be queried.- Returns:
- A
Query
object representing the links associated with the given parent object.
-
queryLinks
-
queryLinks
Constructs a query to retrieve the links associated with the specified parent object and of a specific link type.- Parameters:
parent
- The parent object for which linked objects of the specified link type are to be queried.linkType
- The type of links to filter the query results.- Returns:
- A
Query
object representing the links of the given type associated with the specified parent object.
-
queryLinks
Constructs a query to retrieve the links associated with the specified parent object and of a specific link type.- Parameters:
parentId
- The parent object Id for which linked objects of the specified link type are to be queried.linkType
- The type of links to filter the query results.- Returns:
- A
Query
object representing the links of the given type associated with the specified parent object.
-
existsLinks
Check whether link objects exists associated with the specified parent object.- Parameters:
parent
- The parent object for which linked objects are to be queried.- Returns:
- True/false.
-
existsLinks
Check whether link objects exists associated with the specified parent object.- Parameters:
parentId
- The parent object Id for which linked objects are to be queried.- Returns:
- True/false.
-
existsLinks
Check whether link objects exists associated with the specified parent object and of a specific link type.- Parameters:
parent
- The parent object for which linked objects of the specified link type are to be queried.linkType
- The type of links to filter the query results.- Returns:
- True/false.
-
existsLinks
Check whether link objects exists associated with the specified parent object and of a specific link type.- Parameters:
parentId
- The parent object Id for which linked objects of the specified link type are to be queried.linkType
- The type of links to filter the query results.- Returns:
- True/false.
-
listLinks
Returns an iterator over the linked objects of the specified parent object.- Parameters:
parent
- the parent StoredObject whose linked objects are to be listed- Returns:
- an iterator over the linked objects of the specified parent
-
listLinks
Returns an iterator over the linked objects of the specified parent object.- Parameters:
parentId
- the parent object Id whose linked objects are to be listed- Returns:
- an iterator over the linked objects of the specified parent
-
listLinks
Retrieves an iterator over objects that are linked to the given parent object with the specified link type.- Parameters:
parent
- the parent object whose links are to be listedlinkType
- the type of links to filter the results- Returns:
- an iterator over the linked objects of the specified type
-
listLinks
Retrieves an iterator over objects that are linked to the given parent object with the specified link type.- Parameters:
parentId
- the parent object Id whose links are to be listedlinkType
- the type of links to filter the results- Returns:
- an iterator over the linked objects of the specified type
-
countLinks
Counts the number of links associated with the given parent object.- Parameters:
parent
- the parent object for which the links are to be counted- Returns:
- the total number of links associated with the specified parent
-
countLinks
Counts the number of links associated with the given parent object.- Parameters:
parentId
- the parent object Id for which the links are to be counted- Returns:
- the total number of links associated with the specified parent
-
countLinks
Counts the number of links of a specified type for a given parent object.- Parameters:
parent
- the parent object whose links are to be countedlinkType
- the type of links to be counted- Returns:
- the count of links of the specified type for the given parent object
-
countLinks
Counts the number of links of a specified type for a given parent object.- Parameters:
parentId
- the parent object Id whose links are to be countedlinkType
- the type of links to be counted- Returns:
- the count of links of the specified type for the given parent object
-
existsMasters
Check whether master objects exists related to the given stored object link.- Parameters:
link
- the StoredObject that serves as a reference for querying master objects- Returns:
- True/false.
-
existsMasters
Check whether master objects exists related to the given stored object link.- Parameters:
linkId
- the link object that serves as a reference for querying master objects- Returns:
- True/false.
-
existsMasters
Check whether master objects exists associated with a given stored object link and link type.- Parameters:
link
- the stored object link to query masters forlinkType
- the type of link defining the relationship between the object and its masters- Returns:
- True/false.
-
existsMasters
Check whether master objects exists associated with a given stored object link and link type.- Parameters:
link
- the stored object link to query masters forlinkType
- the type of link defining the relationship between the object and its masters- Returns:
- True/false.
-
queryMasters
Queries and retrieves master objects related to the given stored object link.- Parameters:
link
- the StoredObject that serves as a reference for querying master objects- Returns:
- a Query object containing the result set of master objects matching the criteria
-
queryMasters
-
queryMasters
Queries the master objects associated with a given stored object link and link type.- Parameters:
link
- the stored object link to query masters forlinkType
- the type of link defining the relationship between the object and its masters- Returns:
- a Query object containing the results of the master objects query
-
queryMasters
Queries the master objects associated with a given stored object link and link type.- Parameters:
linkId
- the stored object link Id to query masters forlinkType
- the type of link defining the relationship between the object and its masters- Returns:
- a Query object containing the results of the master objects query
-
listMasters
Retrieves an iterator over master objects linked to the specified stored object.- Parameters:
link
- the stored object for which the master objects are to be listed- Returns:
- an iterator over the master objects associated with the given stored object
-
listMasters
Retrieves an iterator over master objects linked to the specified stored object.- Parameters:
linkId
- the link object Id for which the master objects are to be listed- Returns:
- an iterator over the master objects associated with the given stored object
-
listMasters
Retrieves an iterator over the master objects linked to the specified stored object.- Parameters:
link
- The stored object for which to list the master objects.linkType
- The type of the link used to filter the master objects.- Returns:
- An iterator over the master objects linked to the specified stored object.
-
listMasters
Retrieves an iterator over the master objects linked to the specified stored object.- Parameters:
linkId
- The link object Id for which to list the master objects.linkType
- The type of the link used to filter the master objects.- Returns:
- An iterator over the master objects linked to the specified stored object.
-
querySQL
Constructs and returns an SQL query string based on the given parameters and object class. This method uses the StoredObjectUtility class to create an SQL statement that includes the specified column selections, filtering conditions, ordering, and other query attributes.- Returns:
- a String representing the constructed SQL query based on the provided parameters.
-
countMasters
Counts the number of master objects associated with the given link.- Parameters:
link
- The link object representing the link for which the count of masters is required.- Returns:
- The count of master objects associated with the provided link.
-
countMasters
Counts the number of master objects associated with the given link.- Parameters:
link
- The link object Id representing the link for which the count of masters is required.- Returns:
- The count of master objects associated with the provided link.
-
countMasters
Counts the number of master objects associated with a given link and link type.- Parameters:
link
- the link object representing the link.linkType
- an integer representing the type of link.- Returns:
- the count of master objects associated with the specified link and link type.
-
countMasters
Counts the number of master objects associated with a given link and link type.- Parameters:
linkId
- the link object Id representing the link.linkType
- an integer representing the type of link.- Returns:
- the count of master objects associated with the specified link and link type.
-
getExistsCondition
public static <O extends StoredObject> String getExistsCondition(Class<O> objectClass, String attributeName) Create an "exists condition" that can be used for retrieving or queryingStoredObject
instances.- Type Parameters:
O
- Type of this object class.- Parameters:
objectClass
- The object class against which the existence should be checked.attributeName
- Attribute name that matches theId
of the object that is going to be retrieved or queried.- Returns:
- Condition string suitable for retrieving or querying another type of
StoredObject
instances where this object is related.
-
getExistsCondition
public static <O extends StoredObject> String getExistsCondition(Class<O> objectClass, String attributeName, String condition) Create an "exists condition" that can be used for retrieving or queryingStoredObject
instances.- Type Parameters:
O
- Type of this object class.- Parameters:
objectClass
- The object class against which the existence should be checked.attributeName
- Attribute name that matches theId
of the object that is going to be retrieved or queried.condition
- Condition to be applied to this object class. Null should be passed if no condition needs to be applied.- Returns:
- Condition string suitable for retrieving or querying another type of
StoredObject
instances where this object is related.
-
getNotExistsCondition
public static <O extends StoredObject> String getNotExistsCondition(Class<O> objectClass, String attributeName) Create a "not exists condition" that can be used for retrieving or queryingStoredObject
instances.- Type Parameters:
O
- Type of this object class.- Parameters:
objectClass
- The object class against which the existence should be checked.attributeName
- Attribute name that matches theId
of the object that is going to be retrieved or queried.- Returns:
- Condition string suitable for retrieving or querying another type of
StoredObject
instances where this object is related.
-
getNotExistsCondition
public static <O extends StoredObject> String getNotExistsCondition(Class<O> objectClass, String attributeName, String condition) Create a "not exists condition" that can be used for retrieving or queryingStoredObject
instances.- Type Parameters:
O
- Type of this object class.- Parameters:
objectClass
- The object class against which the existence should be checked.attributeName
- Attribute name that matches theId
of the object that is going to be retrieved or queried.condition
- Condition to be applied to this object class. Null should be passed if no condition needs to be applied.- Returns:
- Condition string suitable for retrieving or querying another type of
StoredObject
instances where this object is related.
-
getInCondition
public static <O extends StoredObject> String getInCondition(String attributeName, Class<O> objectClass) -
getInCondition
public static <O extends StoredObject> String getInCondition(String attributeName, Class<O> objectClass, boolean any) -
getInCondition
public static <O extends StoredObject> String getInCondition(String attributeName, Class<O> objectClass, String condition) -
getInCondition
public static <O extends StoredObject> String getInCondition(String attributeName, Class<O> objectClass, String condition, boolean any) -
getNotInCondition
public static <O extends StoredObject> String getNotInCondition(String attributeName, Class<O> objectClass) -
getNotInCondition
public static <O extends StoredObject> String getNotInCondition(String attributeName, Class<O> objectClass, boolean any) -
getNotInCondition
public static <O extends StoredObject> String getNotInCondition(String attributeName, Class<O> objectClass, String condition) -
getNotInCondition
public static <O extends StoredObject> String getNotInCondition(String attributeName, Class<O> objectClass, String condition, boolean any)
-