Class BatchUpdateExecutor

java.lang.Object
org.seasar.doma.jdbc.builder.BatchUpdateExecutor

public class BatchUpdateExecutor extends Object
An executor that execute SQL UPDATE statements in batches.

This is not thread safe.

Java

 List<Employee> employees = Arrays
         .asList(new Employee[] { new Employee(10, "SMITH", new BigDecimal("1000")),
                 new Employee(20, "ALLEN", new BigDecimal("2000")) });
 BatchUpdateExecutor executor = BatchUpdateExecutor.newInstance(config);
 executor.batchSize(10);
 executor.execute(employees, (emp, builder) -> {
     builder.sql("update Emp");
     builder.sql("set");
     builder.sql("name = ").param(String.class, emp.name).sql(",");
     builder.sql("salary = ").param(BigDecimal.class, emp.salary);
     builder.sql("where");
     builder.sql("id = ").param(int.class, emp.id);
 });
 

built SQLs

 update Emp
 set
 name = 'SMITH',
 salary = 1000
 where
 id = 10

 update Emp
 set
 name = 'ALLEN',
 salary = 2000
 where
 id = 20
 
  • Method Details

    • newInstance

      public static BatchUpdateExecutor newInstance(Config config)
      Creates a new instance.
      Parameters:
      config - the configuration
      Returns:
      a executor
      Throws:
      DomaNullPointerException - if config is null
    • queryTimeout

      public void queryTimeout(int queryTimeout)
      Sets the query timeout limit in seconds.

      If not specified, the value of Config.getQueryTimeout() is used.

      Parameters:
      queryTimeout - the query timeout limit in seconds
      See Also:
    • sqlLogType

      public void sqlLogType(SqlLogType sqlLogType)
      Sets the SQL log format.
      Parameters:
      sqlLogType - the SQL log format type
    • batchSize

      public void batchSize(int batchSize)
      Sets the batch size.

      If not specified, the value of Config.getBatchSize() is used.

      Parameters:
      batchSize - the batch size
    • callerClassName

      public void callerClassName(String className)
      Sets the caller class name.

      If not specified, the class name of this instance is used.

      Parameters:
      className - the caller class name
      Throws:
      DomaNullPointerException - if className is null
    • callerMethodName

      public void callerMethodName(String methodName)
      Sets the caller method name.

      if not specified, execute is used.

      Parameters:
      methodName - the caller method name
      Throws:
      DomaNullPointerException - if methodName is null
    • execute

      public <P> int[] execute(Iterable<P> params, BiConsumer<P,BatchBuilder> buildConsumer)
      Executes SQL UPDATE statements.
      Type Parameters:
      P - the parameter type
      Parameters:
      params - the parameters
      buildConsumer - the code block that builds SQL statements
      Returns:
      the array whose each element contains affected rows count. The array length is equal to the parameter size.
      Throws:
      DomaNullPointerException - if params or buildConsumer is null
      UniqueConstraintException - if an unique constraint violation occurs
      JdbcException - if a JDBC related error occurs
    • getSqls

      public List<? extends Sql<?>> getSqls()
      Returns the built SQL.
      Returns:
      the built SQL