Search in sources :

Example 1 with BatchDeleteCommand

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

the class EntityqlBatchDeleteStatement method createCommand.

@Override
protected Command<BatchResult<ENTITY>> createCommand() {
    EntityType<ENTITY> entityType = entityMetamodel.asType();
    AutoBatchDeleteQuery<ENTITY> query = config.getQueryImplementors().createAutoBatchDeleteQuery(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.setOptimisticLockExceptionSuppressed(settings.getSuppressOptimisticLockException());
    query.setMessage(settings.getComment());
    query.prepare();
    BatchDeleteCommand command = config.getCommandImplementors().createBatchDeleteCommand(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 : BatchDeleteCommand(org.seasar.doma.jdbc.command.BatchDeleteCommand) Command(org.seasar.doma.jdbc.command.Command) BatchDeleteCommand(org.seasar.doma.jdbc.command.BatchDeleteCommand) BatchResult(org.seasar.doma.jdbc.BatchResult)

Example 2 with BatchDeleteCommand

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

the class BatchDeleteExecutor method execute.

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

Example 3 with BatchDeleteCommand

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

the class BatchDeleteExecutorTest method testNotEqualParamCall.

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

Example 4 with BatchDeleteCommand

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

the class BatchDeleteExecutorTest method testBuilder.

@Test
public void testBuilder() {
    SqlBatchDeleteQuery query = mockQuery();
    BatchBuilder builder = BatchBuilder.newInstance(query);
    builder.sql("delete from Emp");
    builder.sql("where");
    builder.sql("name = ").param(String.class, "SMITH");
    builder.sql("and");
    builder.sql("salary = ").param(BigDecimal.class, new BigDecimal("1000"));
    builder = builder.fixSql();
    builder.sql("delete from Emp");
    builder.sql("where");
    builder.sql("name = ").param(String.class, "ALLEN");
    builder.sql("and");
    builder.sql("salary = ").param(BigDecimal.class, new BigDecimal("2000"));
    builder = builder.fixSql();
    builder.execute(() -> new BatchDeleteCommand(query));
}
Also used : BatchDeleteCommand(org.seasar.doma.jdbc.command.BatchDeleteCommand) SqlBatchDeleteQuery(org.seasar.doma.jdbc.query.SqlBatchDeleteQuery) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Aggregations

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