Class ModifyCommand<QUERY extends ModifyQuery>
- Type Parameters:
QUERY- the type of query this command executes
- Direct Known Subclasses:
DeleteCommand,InsertCommand,UpdateCommand
This class provides common functionality for executing SQL statements that modify data, including connection management, statement preparation, parameter binding, and execution. It also handles error conditions such as SQL exceptions, unique constraint violations, and optimistic lock failures.
Subclasses must implement the executeInternal(PreparedStatement) method to define the
specific execution behavior.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final QUERYThe query object that contains the SQL to be executed and related configuration.protected final PreparedSqlThe prepared SQL statement that will be executed. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidbindParameters(PreparedStatement preparedStatement) execute()Executes the SQL command and returns the number of rows affected.protected abstract intexecuteInternal(PreparedStatement preparedStatement) Executes the internal implementation of the SQL command.protected intexecuteUpdate(PreparedStatement preparedStatement) getQuery()Returns the query associated with this command.protected voidlog()protected PreparedStatementprepareStatement(Connection connection) protected voidsetupOptions(PreparedStatement preparedStatement) protected voidvalidateRows(int rows)
-
Field Details
-
query
The query object that contains the SQL to be executed 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.
-
-
Constructor Details
-
ModifyCommand
-
-
Method Details
-
getQuery
Description copied from interface:CommandReturns the query associated with this command.The query contains the SQL statement and parameters that will be executed by this command.
- Specified by:
getQueryin interfaceCommand<QUERY extends ModifyQuery>- Returns:
- the query object that this command will execute
-
execute
Executes the SQL command and returns the number of rows affected.This method handles the entire execution process including:
- Checking if the query is executable
- Obtaining a database connection
- Preparing the statement
- Setting up statement options
- Binding parameters
- Executing the statement
- Handling exceptions
- Ensuring proper resource cleanup
- Specified by:
executein interfaceCommand<QUERY extends ModifyQuery>- Returns:
- the number of rows affected by the execution, or 0 if the query is not executable
- Throws:
SqlExecutionException- if a database access error occurs
-
prepareStatement
-
executeInternal
Executes the internal implementation of the SQL command.This abstract method must be implemented by subclasses to define the specific behavior for executing the SQL statement. It is called by the
execute()method after the connection is established and the statement is prepared.- Parameters:
preparedStatement- the prepared statement to execute- Returns:
- the number of rows affected by the execution
- Throws:
SQLException- if a database access error occurs
-
log
protected void log() -
setupOptions
- Throws:
SQLException
-
bindParameters
- Throws:
SQLException
-
executeUpdate
- Throws:
SQLException
-
validateRows
protected void validateRows(int rows)
-