Search in sources :

Example 1 with PreparedBatch

use of org.jdbi.v3.core.statement.PreparedBatch in project jdbi by jdbi.

the class TestArgumentFactory method testOnPreparedBatch.

@Test
public void testOnPreparedBatch() throws Exception {
    Handle h = dbRule.getSharedHandle();
    PreparedBatch batch = h.prepareBatch("insert into something (id, name) values (:id, :name)");
    batch.registerArgument(new NameAF());
    batch.bind("id", 1).bind("name", new Name("Brian", "McCallister")).add();
    batch.bind("id", 2).bind("name", new Name("Henning", "S")).add();
    batch.execute();
    List<String> rs = h.createQuery("select name from something order by id").mapTo(String.class).list();
    assertThat(rs.get(0)).isEqualTo("Brian McCallister");
    assertThat(rs.get(1)).isEqualTo("Henning S");
}
Also used : PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 2 with PreparedBatch

use of org.jdbi.v3.core.statement.PreparedBatch in project jdbi by jdbi.

the class SqlBatchHandler method invoke.

@Override
public Object invoke(Object target, Object[] args, HandleSupplier h) {
    final Handle handle = h.getHandle();
    final String sql = locateSql(handle);
    final int chunkSize = batchChunkSize.call(args);
    final Iterator<Object[]> batchArgs = zipArgs(getMethod(), args);
    ResultIterator<Object> result;
    if (batchArgs.hasNext()) {
        result = new ResultIterator<Object>() {

            ResultIterator<?> batchResult;

            boolean closed = false;

            {
                // Ensure our batchResult is prepared, so we can get its context
                hasNext();
            }

            @Override
            public boolean hasNext() {
                if (closed) {
                    throw new IllegalStateException("closed");
                }
                // first, any elements already buffered?
                if (batchResult != null) {
                    if (batchResult.hasNext()) {
                        return true;
                    }
                    // no more in this chunk, release resources
                    batchResult.close();
                }
                // more chunks?
                if (!batchArgs.hasNext()) {
                    return false;
                }
                // execute a single chunk and buffer
                PreparedBatch batch = handle.prepareBatch(sql);
                for (int i = 0; i < chunkSize && batchArgs.hasNext(); i++) {
                    applyCustomizers(batch, batchArgs.next());
                    batch.add();
                }
                batchResult = executeBatch(handle, batch);
                // recurse to ensure we actually got elements
                return hasNext();
            }

            @Override
            public Object next() {
                if (closed) {
                    throw new IllegalStateException("closed");
                }
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }
                return batchResult.next();
            }

            @Override
            public StatementContext getContext() {
                return batchResult.getContext();
            }

            @Override
            public void close() {
                closed = true;
                batchResult.close();
            }
        };
    } else {
        PreparedBatch dummy = handle.prepareBatch(sql);
        result = new ResultIterator<Object>() {

            @Override
            public void close() {
            // no op
            }

            @Override
            public StatementContext getContext() {
                return dummy.getContext();
            }

            @Override
            public boolean hasNext() {
                return false;
            }

            @Override
            public Object next() {
                throw new NoSuchElementException();
            }
        };
    }
    ResultIterable<Object> iterable = ResultIterable.of(result);
    return magic.mappedResult(iterable, result.getContext());
}
Also used : Handle(org.jdbi.v3.core.Handle) StatementContext(org.jdbi.v3.core.statement.StatementContext) PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) NoSuchElementException(java.util.NoSuchElementException)

Example 3 with PreparedBatch

use of org.jdbi.v3.core.statement.PreparedBatch in project jdbi by jdbi.

the class TestPreparedBatchGenerateKeys method testBatchInsertWithKeyGeneration.

@Test
public void testBatchInsertWithKeyGeneration() throws Exception {
    Jdbi db = Jdbi.create("jdbc:hsqldb:mem:jdbi-batch-keys-test", "sa", "");
    try (Handle h = db.open()) {
        h.execute("create table something (id integer not null generated by default as identity (start with 10000), name varchar(50) )");
        PreparedBatch batch = h.prepareBatch("insert into something (name) values (?)");
        batch.add("Brian");
        batch.add("Thom");
        List<Integer> ids = batch.executeAndReturnGeneratedKeys().mapTo(int.class).list();
        assertThat(ids).containsExactly(10000, 10001);
        List<Something> somethings = h.createQuery("select id, name from something").mapToBean(Something.class).list();
        assertThat(somethings).containsExactly(new Something(10000, "Brian"), new Something(10001, "Thom"));
    }
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 4 with PreparedBatch

use of org.jdbi.v3.core.statement.PreparedBatch in project jdbi by jdbi.

the class TestPreparedBatchGenerateKeysPostgres method testBatchInsertWithKeyGenerationAndExplicitColumnNames.

@Test
public void testBatchInsertWithKeyGenerationAndExplicitColumnNames() {
    PreparedBatch batch = h.prepareBatch("insert into something (name) values (?) ");
    batch.add("Brian");
    batch.add("Thom");
    List<Integer> ids = batch.executeAndReturnGeneratedKeys("id").mapTo(Integer.class).list();
    assertThat(ids).containsExactly(1, 2);
    List<Something> somethings = h.createQuery("select id, name from something").mapToBean(Something.class).list();
    assertThat(somethings).containsExactly(new Something(1, "Brian"), new Something(2, "Thom"));
}
Also used : Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 5 with PreparedBatch

use of org.jdbi.v3.core.statement.PreparedBatch in project jdbi by jdbi.

the class StatementsTest method testBatch.

@Test
public void testBatch() throws Exception {
    // tag::batch[]
    PreparedBatch batch = handle.prepareBatch("INSERT INTO user(id, name) VALUES(:id, :name)");
    for (int i = 100; i < 5000; i++) {
        batch.bind("id", i).bind("name", "User:" + i).add();
    }
    int[] counts = batch.execute();
    // end::batch[]
    int[] expected = new int[4900];
    Arrays.fill(expected, 1);
    assertThat(counts).isEqualTo(expected);
}
Also used : PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)6 Handle (org.jdbi.v3.core.Handle)4 PreparedBatch (org.jdbi.v3.core.statement.PreparedBatch)4 Something (org.jdbi.v3.core.Something)2 NoSuchElementException (java.util.NoSuchElementException)1 Jdbi (org.jdbi.v3.core.Jdbi)1 StatementContext (org.jdbi.v3.core.statement.StatementContext)1