Package com.storedobject.core
Class Query
java.lang.Object
com.storedobject.core.Query
The Query class provides a mechanism to iterate over rows in a SQL result set.
It implements
Iterator<ResultSet>, Iterable<ResultSet>, and Closeable
interfaces to facilitate traversing and managing the lifecycle of database query results using for/for each/while
loops.
Query objects allow navigation through the result set, provide means to map results into other data types, and support optional transformation functions to manipulate individual rows.
- Author:
- Syam
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the resources associated with this query.booleaneoq()Determines if the end of the query has been reached.Retrieves the current result set of the query.booleanhasNext()Checks if there are additional results available in the query.Provides an iterable over the query results, using the default column index.idIterator(int column) Provides an iterable ofIdobjects by mapping the result set from a specific column index of a database query to instances ofId.Creates an iterable that iterates over the first column of the result set, mapping each row to an integer value.integerIterator(int column) Creates an iterable over integers retrieved from a specified column of a database result set.iterator()Returns an iterator for traversing the results of the query.limit(long limit) Limits the number of results returned by the query.next()Retrieves the next result set from the query.<T> Iterable<T> objectIterator(Function<ResultSet, T> objectMapper) Creates anIterablethat maps eachResultSetentry from the query into an object of typeTusing the providedobjectMapperfunction.voidremove()Removes the current element from the underlying data structure.skip(long skip) Skips a specified number of rows in the result set.Provides an iterable sequence of string values retrieved from the query's result set, defaulting to the first column (column index 1).stringIterator(int column) Provides an iterable over a specific column in the result set, extracting its values as strings.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface java.util.Iterator
forEachRemaining
-
Method Details
-
close
public void close()Closes the resources associated with this query. This method ensures that any open database resources, represented by thesqlobject, are properly cleaned up. After the cleanup, thesqlfield is set tonullto indicate that the associated resources have been released. Repeated calls to this method are safe and effectively have no additional effect once the resources have already been closed andsqlis null.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
iterator
-
getResultSet
Retrieves the current result set of the query.- Returns:
- the
ResultSetobject obtained from the underlying SQL query execution.
-
next
Retrieves the next result set from the query. If no result set is available or the query is invalid, this method will throw aNoSuchElementException. It skips over the current result set if it has already been read before retrieving the next result.- Specified by:
nextin interfaceIterator<ResultSet>- Returns:
- the next
ResultSetretrieved from the query - Throws:
NoSuchElementException- if there are no more results available or the query is invalid
-
hasNext
public boolean hasNext()Checks if there are additional results available in the query. This method determines whether there are more elements to process in the query. It manages conditions such as end-of-query state, ensures the query is properly closed when no further results are available, and skips the current result if necessary. -
remove
public void remove()Removes the current element from the underlying data structure. This method is unsupported for the current implementation and will always throw anUnsupportedOperationException. It is included to fulfill theIteratorinterface contract.- Specified by:
removein interfaceIterator<ResultSet>- Throws:
UnsupportedOperationException- always, as this operation is not supported.
-
stringIterator
-
stringIterator
Provides an iterable over a specific column in the result set, extracting its values as strings.- Parameters:
column- the column index (1-based) to retrieve string values from- Returns:
- an iterable of strings corresponding to the specified column in the result set
-
integerIterator
-
integerIterator
Creates an iterable over integers retrieved from a specified column of a database result set. The method maps the values obtained from the given column index of the result set into Integer objects. If an SQL exception occurs during the retrieval of a value, it will returnnullfor that entry.- Parameters:
column- the column index of the result set from which integers are to be retrieved- Returns:
- an Iterable of integers corresponding to the specified column's values in the result set
-
idIterator
-
idIterator
Provides an iterable ofIdobjects by mapping the result set from a specific column index of a database query to instances ofId.- Parameters:
column- the column index to extractIdvalues from the result set- Returns:
- an
Iterable<Id>that iterates over theIdobjects from the specified column
-
objectIterator
Creates anIterablethat maps eachResultSetentry from the query into an object of typeTusing the providedobjectMapperfunction.- Type Parameters:
T- the type of the object to be created from each row of theResultSet- Parameters:
objectMapper- aFunctionthat maps aResultSetto an object of typeT- Returns:
- an
Iterableover objects of typeT, representing the mapped entries from the query
-
eoq
public boolean eoq()Determines if the end of the query has been reached. This method is a utility to check if there are no more results available in the query being processed. It internally calls thehasNext()method to verify if additional elements exist and negates its result.- Returns:
trueif the end of the query has been reached (no more results);falseotherwise.
-
skip
Skips a specified number of rows in the result set.- Parameters:
skip- the number of rows to skip- Returns:
- the current Query instance, for method chaining
-
limit
Limits the number of results returned by the query.- Parameters:
limit- the maximum number of results to be returned- Returns:
- a new Query instance with the specified limit applied
-