Search in sources :

Example 1 with BatchUpdateCommand

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

the class EntityqlBatchUpdateStatement method createCommand.

@Override
protected Command<BatchResult<ENTITY>> createCommand() {
    EntityType<ENTITY> entityType = entityMetamodel.asType();
    AutoBatchUpdateQuery<ENTITY> query = config.getQueryImplementors().createAutoBatchUpdateQuery(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.setVersionIgnored(settings.getIgnoreVersion());
    query.setIncludedPropertyNames();
    query.setExcludedPropertyNames();
    query.setOptimisticLockExceptionSuppressed(settings.getSuppressOptimisticLockException());
    query.setMessage(settings.getComment());
    query.prepare();
    BatchUpdateCommand command = config.getCommandImplementors().createBatchUpdateCommand(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 : BatchUpdateCommand(org.seasar.doma.jdbc.command.BatchUpdateCommand) Command(org.seasar.doma.jdbc.command.Command) BatchUpdateCommand(org.seasar.doma.jdbc.command.BatchUpdateCommand) BatchResult(org.seasar.doma.jdbc.BatchResult)

Example 2 with BatchUpdateCommand

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

the class BatchUpdateExecutorTest method testBuilder.

@Test
public void testBuilder() {
    SqlBatchUpdateQuery query = mockQuery();
    BatchBuilder builder = BatchBuilder.newInstance(query);
    builder.sql("update Emp");
    builder.sql("set");
    builder.sql("name = ").param(String.class, "SMITH").sql(",");
    builder.sql("salary = ").param(BigDecimal.class, new BigDecimal("1000"));
    builder.sql("where");
    builder.sql("ID = ").param(int.class, 10);
    builder = builder.fixSql();
    builder.sql("update Emp");
    builder.sql("set");
    builder.sql("name = ").param(String.class, "ALLEN").sql(",");
    builder.sql("salary = ").param(BigDecimal.class, new BigDecimal("2000"));
    builder.sql("where");
    builder.sql("ID = ").param(int.class, 20);
    builder = builder.fixSql();
    builder.execute(() -> new BatchUpdateCommand(query));
}
Also used : SqlBatchUpdateQuery(org.seasar.doma.jdbc.query.SqlBatchUpdateQuery) BatchUpdateCommand(org.seasar.doma.jdbc.command.BatchUpdateCommand) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 3 with BatchUpdateCommand

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

the class BatchUpdateExecutor method execute.

/**
 * Executes SQL UPDATE 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 BatchUpdateCommand(query));
}
Also used : DomaNullPointerException(org.seasar.doma.DomaNullPointerException) BatchUpdateCommand(org.seasar.doma.jdbc.command.BatchUpdateCommand)

Example 4 with BatchUpdateCommand

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

the class BatchUpdateExecutorTest method testNotEqualParamCall.

@Test
public void testNotEqualParamCall() {
    SqlBatchUpdateQuery query = mockQuery();
    BatchBuilder builder = BatchBuilder.newInstance(query);
    builder.sql("update Emp");
    builder.sql("set");
    builder.sql("name = ").param(String.class, "SMITH").sql(",");
    builder.sql("salary = ").param(BigDecimal.class, new BigDecimal("1000"));
    builder.sql("where");
    builder.sql("ID = ").param(int.class, 10);
    builder = builder.fixSql();
    builder.sql("update Emp");
    builder.sql("set");
    builder.sql("name = ").param(String.class, "ALLEN").sql(",");
    builder.sql("salary = ").param(BigDecimal.class, new BigDecimal("2000"));
    builder = builder.fixSql();
    try {
        builder.execute(() -> new BatchUpdateCommand(query));
    } catch (AssertionError e) {
        return;
    }
    fail();
}
Also used : SqlBatchUpdateQuery(org.seasar.doma.jdbc.query.SqlBatchUpdateQuery) BatchUpdateCommand(org.seasar.doma.jdbc.command.BatchUpdateCommand) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Aggregations

BatchUpdateCommand (org.seasar.doma.jdbc.command.BatchUpdateCommand)4 BigDecimal (java.math.BigDecimal)2 Test (org.junit.jupiter.api.Test)2 SqlBatchUpdateQuery (org.seasar.doma.jdbc.query.SqlBatchUpdateQuery)2 DomaNullPointerException (org.seasar.doma.DomaNullPointerException)1 BatchResult (org.seasar.doma.jdbc.BatchResult)1 Command (org.seasar.doma.jdbc.command.Command)1