/** * Bind arguments from a Map, add the binding as a saved batch, * then clear the current binding. * * @param args map to bind arguments from for named parameters on the statement * @return this */ public PreparedBatch add(Map<String, ?> args) { bindMap(args); add(); return this; }
/** * Bind arguments positionally, add the binding as a saved batch, and * then clear the current binding. * @param args the positional arguments to bind * @return this */ public PreparedBatch add(Object... args) { for (int i = 0; i < args.length; i++) { bind(i, args[i]); } add(); return this; }
@Test public void testBindMaps() { PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); b.add(ImmutableMap.of("id", 0, "name", "Keith")); b.add(ImmutableMap.of("id", 1, "name", "Eric")); b.add(ImmutableMap.of("id", 2, "name", "Brian")); b.execute(); List<Something> r = h.createQuery("select * from something order by id").mapToBean(Something.class).list(); assertThat(r).hasSize(3); assertThat(r.get(2).getName()).isEqualTo("Brian"); }
@Test public void testBindProperties() { PreparedBatch b = h.prepareBatch("insert into something (id, name) values (?, ?)"); b.add(0, "Keith"); b.add(1, "Eric"); b.add(2, "Brian"); b.execute(); List<Something> r = h.createQuery("select * from something order by id").mapToBean(Something.class).list(); assertThat(r).hasSize(3); assertThat(r.get(2).getName()).isEqualTo("Brian"); }
@Test public void testCollectList() { h.prepareBatch("insert into something (id, name) values (?, ?)") .add(1, "Brian") .add(2, "Keith") .execute(); List<String> rs = h.createQuery("select name from something order by id") .mapTo(String.class) .collect(toList()); assertThat(rs).containsExactly("Brian", "Keith"); }
@Test public void testBatchInsertWithKeyGenerationAndExplicitSeveralColumnNames() { PreparedBatch batch = h.prepareBatch("insert into something (name) values (?) "); batch.add("Brian"); batch.add("Thom"); List<IdCreateTime> ids = batch.executeAndReturnGeneratedKeys("id", "create_time") .map((r, ctx) -> new IdCreateTime(r.getInt("id"), r.getDate("create_time"))) .list(); assertThat(ids).hasSize(2); assertThat(ids).extracting(ic -> ic.id).containsExactly(1, 2); assertThat(ids).extracting(ic -> ic.createTime).doesNotContainNull(); }
@Test public void testFold() { h.prepareBatch("insert into something (id, name) values (?, ?)") .add(1, "Brian") .add(2, "Keith") .execute(); Map<String, Integer> rs = h.createQuery("select id, name from something") .<Entry<String, Integer>>map((r, ctx) -> Maps.immutableEntry(r.getString("name"), r.getInt("id"))) .collect(toMap(Entry::getKey, Entry::getValue)); assertThat(rs).containsOnly(entry("Brian", 1), entry("Keith", 2)); }
@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")); }
@Test public void testBindBatch() { PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); b.bind("id", 1).bind("name", "Eric").add(); b.bind("id", 2).bind("name", "Brian").add(); b.bind("id", 3).bind("name", "Keith").add(); b.execute(); List<Something> r = h.createQuery("select * from something order by id").mapToBean(Something.class).list(); assertThat(r).hasSize(3); assertThat(r.get(2).getName()).isEqualTo("Keith"); }
@Test public void testBatch() { // 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); }
@Test public void testPreparedBatch() { PreparedBatch b = dbRule.openHandle().prepareBatch("insert into something (id, name) values (?,?)"); b.add(0, "a"); b.add(0, "a"); assertThatExceptionOfType(UnableToExecuteStatementException.class) .isThrownBy(b::execute) .satisfies(e -> assertSuppressions(e.getCause())); }
@Test public void testBigishBatch() { PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); int count = 100; for (int i = 0; i < count; ++i) { b.bind("id", i).bind("name", "A Name").add(); } b.execute(); int rowCount = h.createQuery("select count(id) from something").mapTo(int.class).findOnly(); assertThat(rowCount).isEqualTo(count); }
@Test public void testMixedModeBatch() { PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); Map<String, Object> one = ImmutableMap.of("id", 0); b.bind("name", "Keith").add(one); b.execute(); List<Something> r = h.createQuery("select * from something order by id").mapToBean(Something.class).list(); assertThat(r).extracting(Something::getName).containsExactly("Keith"); }
@Test public void testOnPreparedBatch() { 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).containsExactly("Brian McCallister", "Henning S"); }
@Test public void testForgotFinalAdd() { PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); b.bind("id", 1); b.bind("name", "Jeff"); b.add(); b.bind("id", 2); b.bind("name", "Tom"); // forgot to add() here but we fix it up b.execute(); assertThat(h.createQuery("select name from something order by id").mapTo(String.class).list()) .containsExactly("Jeff", "Tom"); }
@Test public void testMultipleExecuteBindBean() { final PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); b.bindBean(new Something(1, "Eric")).add(); b.bindBean(new Something(2, "Brian")).add(); b.execute(); // bindings should be cleared after execute() b.bindBean(new Something(3, "Keith")).add(); b.execute(); final List<Something> r = h.createQuery("select * from something order by id").mapToBean(Something.class).list(); assertThat(r).extracting(Something::getId, Something::getName) .containsExactly(tuple(1, "Eric"), tuple(2, "Brian"), tuple(3, "Keith")); }
@Test public void testMultipleExecuteBind() { final PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); b.bind("id", 1).bind("name", "Eric").add(); b.bind("id", 2).bind("name", "Brian").add(); b.execute(); // bindings should be cleared after execute() b.bind("id", 3).bind("name", "Keith").add(); b.execute(); final List<Something> r = h.createQuery("select * from something order by id").mapToBean(Something.class).list(); assertThat(r).extracting(Something::getId, Something::getName) .containsExactly(tuple(1, "Eric"), tuple(2, "Brian"), tuple(3, "Keith")); }
@Test public void testMultipleExecuteBindFields() { h.registerRowMapper(ConstructorMapper.factory(PublicSomething.class)); final PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); b.bindFields(new PublicSomething(1, "Eric")).add(); b.bindFields(new PublicSomething(2, "Brian")).add(); b.execute(); // bindings should be cleared after execute() b.bindFields(new PublicSomething(3, "Keith")).add(); b.execute(); final List<PublicSomething> r = h.createQuery("select * from something order by id").mapTo(PublicSomething.class).list(); assertThat(r).extracting(s -> s.id, s -> s.name).containsExactly(tuple(1, "Eric"), tuple(2, "Brian"), tuple(3, "Keith")); }
@Test public void testPositionalBinding() { PreparedBatch b = h.prepareBatch("insert into something (id, name) values (?, ?)"); b.bind(0, 0).bind(1, "Keith").add().execute(); List<Something> r = h.createQuery("select * from something order by id").mapToBean(Something.class).list(); assertThat(r).extracting(Something::getName).containsExactly("Keith"); }