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");
}
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());
}
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"));
}
}
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"));
}
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);
}
Aggregations