Class BatchInsertExecutor

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

public class BatchInsertExecutor extends Object
An executor that execute SQL INSERT statements in batches.

This is not thread safe.

Java

 List<Employee> employees = Arrays
         .asList(new Employee[] { new Employee("SMITH", 100), new Employee("ALLEN", 200) });
 BatchInsertExecutor executor = BatchInsertExecutor.newInstance(config);
 executor.batchSize(10);
 executor.execute(employees, (emp, builder) -> {
     builder.sql("insert into Emp");
     builder.sql("(name, salary)");
     builder.sql("values (");
     builder.param(String.class, emp.name).sql(", ");
     builder.param(int.class, emp.salary).sql(")");
 });
 

built SQLs

 insert into Emp
 (name, salary)
 values('SMITH', 100)

 insert into Emp
 (name, salary)
 values('ALLEN', 200)
 
  • Method Details

    • newInstance

      public static BatchInsertExecutor 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 INSERT 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