- Type Parameters:
RESULT- the type of result returned by this command
- All Implemented Interfaces:
Command<RESULT>
This class handles the execution of database queries that retrieve data, including connection
management, statement preparation, parameter binding, and result set processing. It uses a ResultSetHandler to convert the JDBC result set into the desired return type.
The command supports both eager and lazy fetching of results, with proper resource management for each approach. When using lazy fetching with streams, resources are automatically closed when the stream is closed.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final SelectQueryThe query object that contains the SELECT SQL statement and related configuration.protected final ResultSetHandler<RESULT>The handler responsible for processing the result set returned by the query execution.protected final PreparedSqlThe prepared SQL statement that will be executed. -
Constructor Summary
ConstructorsConstructorDescriptionSelectCommand(SelectQuery query, ResultSetHandler<RESULT> resultSetHandler) Constructs a new SelectCommand with the specified query and result set handler. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidbindParameters(PreparedStatement preparedStatement) Binds the SQL parameters to the prepared statement.protected voidDeprecated, for removal: This API element is subject to removal in a future version.Defers the execution of a runnable until the result is consumed.execute()Executes the SELECT SQL query and processes the result set.executeQuery(PreparedStatement preparedStatement) Executes the SQL query and processes the result set.getQuery()Returns the query object associated with this command.handleResultSet(ResultSet resultSet) Processes the result set using the configured result set handler.protected voidlog()Logs the SQL statement that will be executed.protected voidsetupOptions(PreparedStatement preparedStatement) Sets up the options for the prepared statement based on the query configuration.
-
Field Details
-
query
The query object that contains the SELECT SQL statement and related configuration. This member is initialized in the constructor and remains unchanged throughout the command's lifecycle. -
sql
The prepared SQL statement that will be executed. This is extracted from the query object and contains both the SQL string and its parameters. -
resultSetHandler
The handler responsible for processing the result set returned by the query execution. It converts the JDBC ResultSet into the desired return type specified by the RESULT generic parameter.
-
-
Constructor Details
-
SelectCommand
Constructs a new SelectCommand with the specified query and result set handler.- Parameters:
query- the query to execute, which contains the SQL statement and configurationresultSetHandler- the handler that processes the result set and converts it to the desired type
-
-
Method Details
-
getQuery
Returns the query object associated with this command. -
execute
Executes the SELECT SQL query and processes the result set.This method handles the entire lifecycle of a database query:
- Obtaining a database connection
- Preparing the SQL statement
- Setting up statement options (fetch size, max rows, timeout)
- Binding parameters to the statement
- Executing the query
- Processing the result set
- Properly closing all resources
For lazy fetching with streams, resources are automatically closed when the stream is closed. Statistics are recorded if enabled in the configuration.
- Specified by:
executein interfaceCommand<RESULT>- Returns:
- the result of the query execution, as processed by the result set handler
- Throws:
SqlExecutionException- if a database access error occurs
-
log
protected void log()Logs the SQL statement that will be executed.This method uses the JDBC logger configured in the query to log the SQL statement along with the class name and method name that initiated the query.
-
setupOptions
Sets up the options for the prepared statement based on the query configuration.This method configures the following options if they are specified in the query:
- Fetch size - controls how many rows are fetched from the database at once
- Max rows - limits the maximum number of rows that can be returned
- Query timeout - sets the maximum time in seconds that a query can execute
- Parameters:
preparedStatement- the prepared statement to configure- Throws:
SQLException- if a database access error occurs
-
bindParameters
Binds the SQL parameters to the prepared statement.This method uses a
PreparedSqlParameterBinderto set the parameter values in the prepared statement according to their types and positions.- Parameters:
preparedStatement- the prepared statement to bind parameters to- Throws:
SQLException- if a database access error occurs or if a parameter value is incompatible with the designated SQL type
-
executeQuery
Executes the SQL query and processes the result set.This method executes the prepared statement and obtains a result set. It then delegates the processing of the result set to the
handleResultSet(ResultSet)method. The result set is properly closed when the returned supplier is consumed, or immediately if eager fetching is used.- Parameters:
preparedStatement- the prepared statement to execute- Returns:
- a supplier that provides the processed result
- Throws:
SQLException- if a database access error occurs during query execution
-
handleResultSet
Processes the result set using the configured result set handler.This method delegates the processing of the result set to the result set handler that was provided in the constructor. It also checks if the result set is empty when a result is required, and throws a
NoResultExceptionin that case.- Parameters:
resultSet- the result set to process- Returns:
- a supplier that provides the processed result
- Throws:
SQLException- if a database access error occurs during result set processingNoResultException- if no result is found and a result is required by the query
-
close
Deprecated, for removal: This API element is subject to removal in a future version.Usedefer(Supplier, Runnable)instead. -
defer
Defers the execution of a runnable until the result is consumed.This method is used for resource management. When lazy fetching is enabled and the result is a Stream, the runnable (typically a resource cleanup operation) is registered as an onClose handler for the stream, ensuring that resources are properly closed when the stream is closed. For non-stream results or eager fetching, the runnable is executed immediately.
- Parameters:
supplier- the supplier of the resultrunnable- the operation to defer or execute immediately- Returns:
- the original supplier or a new supplier that wraps the result with the deferred operation
-
defer(Supplier, Runnable)instead.