Search in sources :

Example 1 with BatchInsertCommand

use of org.seasar.doma.jdbc.command.BatchInsertCommand in project doma by domaframework.

the class EntityqlBatchInsertStatement method createCommand.

@Override
protected Command<BatchResult<ENTITY>> createCommand() {
    EntityType<ENTITY> entityType = entityMetamodel.asType();
    AutoBatchInsertQuery<ENTITY> query = config.getQueryImplementors().createAutoBatchInsertQuery(EXECUTE_METHOD, entityType);
    query.setMethod(EXECUTE_METHOD);
    query.setConfig(config);
    query.setEntities(entities);
    query.setCallerClassName(getClass().getName());
    query.setCallerMethodName(EXECUTE_METHOD_NAME);
    query.setQueryTimeout(settings.getQueryTimeout());
    query.setBatchSize(settings.getBatchSize());
    query.setSqlLogType(settings.getSqlLogType());
    query.setIncludedPropertyNames();
    query.setExcludedPropertyNames();
    query.setMessage(settings.getComment());
    query.prepare();
    BatchInsertCommand command = config.getCommandImplementors().createBatchInsertCommand(EXECUTE_METHOD, query);
    return new Command<BatchResult<ENTITY>>() {

        @Override
        public Query getQuery() {
            return query;
        }

        @Override
        public BatchResult<ENTITY> execute() {
            int[] counts = command.execute();
            query.complete();
            return new BatchResult<>(counts, query.getEntities());
        }
    };
}
Also used : BatchInsertCommand(org.seasar.doma.jdbc.command.BatchInsertCommand) Command(org.seasar.doma.jdbc.command.Command) BatchInsertCommand(org.seasar.doma.jdbc.command.BatchInsertCommand) BatchResult(org.seasar.doma.jdbc.BatchResult)

Example 2 with BatchInsertCommand

use of org.seasar.doma.jdbc.command.BatchInsertCommand in project doma by domaframework.

the class BatchInsertExecutor method execute.

/**
 * Executes SQL INSERT statements.
 *
 * @param <P> the parameter type
 * @param params the parameters
 * @param buildConsumer the code block that builds SQL statements
 * @return the array whose each element contains affected rows count. The array length is equal to
 *     the {@code parameter} size.
 * @throws DomaNullPointerException if {@code params} or {@code buildConsumer} is {@code null}
 * @throws UniqueConstraintException if an unique constraint violation occurs
 * @throws JdbcException if a JDBC related error occurs
 */
public <P> int[] execute(Iterable<P> params, BiConsumer<P, BatchBuilder> buildConsumer) {
    if (params == null) {
        throw new DomaNullPointerException("params");
    }
    if (buildConsumer == null) {
        throw new DomaNullPointerException("buildConsumer");
    }
    if (query.getMethodName() == null) {
        query.setCallerMethodName("execute");
    }
    BatchBuilder builder = BatchBuilder.newInstance(query);
    for (P p : params) {
        buildConsumer.accept(p, builder);
        builder = builder.fixSql();
    }
    return builder.execute(() -> new BatchInsertCommand(query));
}
Also used : BatchInsertCommand(org.seasar.doma.jdbc.command.BatchInsertCommand) DomaNullPointerException(org.seasar.doma.DomaNullPointerException)

Example 3 with BatchInsertCommand

use of org.seasar.doma.jdbc.command.BatchInsertCommand in project doma by domaframework.

the class BatchInsertExecutorTest method testBuilder.

@Test
public void testBuilder() {
    SqlBatchInsertQuery query = mockQuery();
    BatchBuilder builder = BatchBuilder.newInstance(query);
    builder.sql("insert into Emp");
    builder.sql("(name, salary)");
    builder.sql("values (");
    builder.param(String.class, "SMITH").sql(", ");
    builder.param(int.class, 100).sql(")");
    builder = builder.fixSql();
    builder.sql("insert into Emp");
    builder.sql("(name, salary)");
    builder.sql("values (");
    builder.param(String.class, "ALLEN").sql(", ");
    builder.param(int.class, 200).sql(")");
    builder = builder.fixSql();
    builder.execute(() -> new BatchInsertCommand(query));
}
Also used : BatchInsertCommand(org.seasar.doma.jdbc.command.BatchInsertCommand) SqlBatchInsertQuery(org.seasar.doma.jdbc.query.SqlBatchInsertQuery) Test(org.junit.jupiter.api.Test)

Example 4 with BatchInsertCommand

use of org.seasar.doma.jdbc.command.BatchInsertCommand in project doma by domaframework.

the class BatchInsertExecutorTest method testNotEqualParamCall.

@Test
public void testNotEqualParamCall() {
    SqlBatchInsertQuery query = mockQuery();
    BatchBuilder builder = BatchBuilder.newInstance(query);
    builder.sql("insert into Emp");
    builder.sql("(name, salary)");
    builder.sql("values (");
    builder.param(String.class, "SMITH").sql(", ");
    builder.param(int.class, 100).sql(")");
    builder = builder.fixSql();
    builder.sql("insert into Emp");
    builder.sql("(name, salary)");
    builder.sql("values (");
    builder.param(String.class, "ALLEN").sql(")");
    builder = builder.fixSql();
    try {
        builder.execute(() -> new BatchInsertCommand(query));
    } catch (AssertionError e) {
        return;
    }
    fail();
}
Also used : BatchInsertCommand(org.seasar.doma.jdbc.command.BatchInsertCommand) SqlBatchInsertQuery(org.seasar.doma.jdbc.query.SqlBatchInsertQuery) Test(org.junit.jupiter.api.Test)

Aggregations

BatchInsertCommand (org.seasar.doma.jdbc.command.BatchInsertCommand)4 Test (org.junit.jupiter.api.Test)2 SqlBatchInsertQuery (org.seasar.doma.jdbc.query.SqlBatchInsertQuery)2 DomaNullPointerException (org.seasar.doma.DomaNullPointerException)1 BatchResult (org.seasar.doma.jdbc.BatchResult)1 Command (org.seasar.doma.jdbc.command.Command)1