Class ModifyCommand<QUERY extends ModifyQuery>

java.lang.Object
org.seasar.doma.jdbc.command.ModifyCommand<QUERY>
Type Parameters:
QUERY - the type of query this command executes
All Implemented Interfaces:
Command<Integer>
Direct Known Subclasses:
DeleteCommand, InsertCommand, UpdateCommand

public abstract class ModifyCommand<QUERY extends ModifyQuery> extends Object implements Command<Integer>
An abstract base class for commands that modify database data (INSERT, UPDATE, DELETE).

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 Details

    • query

      protected final QUERY extends ModifyQuery 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

      protected final PreparedSql 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

      protected ModifyCommand(QUERY query)
  • Method Details

    • getQuery

      public QUERY getQuery()
      Description copied from interface: Command
      Returns the query associated with this command.

      The query contains the SQL statement and parameters that will be executed by this command.

      Specified by:
      getQuery in interface Command<QUERY extends ModifyQuery>
      Returns:
      the query object that this command will execute
    • execute

      public Integer 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:
      execute in interface Command<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

      protected PreparedStatement prepareStatement(Connection connection)
    • executeInternal

      protected abstract int executeInternal(PreparedStatement preparedStatement) throws SQLException
      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

      protected void setupOptions(PreparedStatement preparedStatement) throws SQLException
      Throws:
      SQLException
    • bindParameters

      protected void bindParameters(PreparedStatement preparedStatement) throws SQLException
      Throws:
      SQLException
    • executeUpdate

      protected int executeUpdate(PreparedStatement preparedStatement) throws SQLException
      Throws:
      SQLException
    • validateRows

      protected void validateRows(int rows)