/** * 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; }
Type type = columns.get(column).getType(); if (BOOLEAN.equals(type)) { batch.bind(column, cursor.getBoolean(column)); batch.bind(column, cursor.getLong(column)); batch.bind(column, (int) cursor.getLong(column)); batch.bind(column, cursor.getDouble(column)); batch.bind(column, cursor.getSlice(column).toStringUtf8()); batch.bind(column, new Date(localMillis));
@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 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 testSomeQueriesWorkCorrectly() { try (Handle h = dbRule.openHandle()) { h.prepareBatch("insert into something (id, name) values (:id, :name)") .bind("id", 1).bind("name", "Brian").add() .bind("id", 2).bind("name", "Robert").add() .bind("id", 3).bind("name", "Patrick").add() .bind("id", 4).bind("name", "Maniax").add() .execute(); SomeQueries sq = h.attach(SomeQueries.class); assertThat(sq.findName(2)).isEqualTo("Robert"); assertThat(sq.findNamesBetween(1, 4)).containsExactly("Robert", "Patrick"); Iterator<String> names = sq.findAllNames(); assertThat(names).containsExactly("Brian", "Robert", "Patrick", "Maniax"); } }
@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 testBatch() { String insert = "insert into something (id, name) values (:id, :name)"; h.prepareBatch(insert) .bind("id", 1).bind("name", "Eric").add() .bind("id", 2).bind("name", "Brian").add() .execute(); String select = "select * from something order by id"; List<Something> r = h.createQuery(select).mapToBean(Something.class).list(); assertThat(r.stream().map(Something::getName)).containsExactly("Eric", "Brian"); assertThat(tc.getRawStatements()).containsExactly(insert, select); assertThat(tc.getRenderedStatements()).containsExactly(insert, select); assertThat(tc.getParsedStatements()).extracting("sql").containsExactly( "insert into something (id, name) values (?, ?)", select); }
@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 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 testPreparedBatch() { h.execute(CREATE); h.prepareBatch(INSERT_PREPARED).bind(0, 1).execute(); assertThat(logger.getRawSql()).containsExactly(CREATE, CREATE, INSERT_PREPARED, INSERT_PREPARED); assertThat(logger.getTimings()).hasSize(2).allMatch(IS_POSITIVE); assertThat(logger.getExceptions()).isEmpty(); }
@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 testContextGetsBinding() { try { h.prepareBatch("insert into something (id, name) values (:id, :name)") .bind("id", 0) .bind("name", "alice") .add() .bind("id", 0) .bind("name", "bob") .add() .execute(); fail("expected exception"); } catch (UnableToExecuteStatementException e) { final StatementContext ctx = e.getStatementContext(); assertThat(ctx.getBinding().findForName("name", ctx).toString()).contains("bob"); } }
@Before public void setUp() { h = dbRule.getSharedHandle(); h.execute("CREATE TABLE user (" + "uid INTEGER NOT NULL," + "name VARCHAR NOT NULL" + ")"); h.execute("CREATE TABLE article (" + "aid INTEGER NOT NULL," + "title VARCHAR NOT NULL" + ")"); h.execute("CREATE TABLE author (" + "uid INTEGER NOT NULL," + "aid INTEGER NOT NULL" + ")"); IntStream.rangeClosed(1, 3).forEach(u -> h.execute("INSERT INTO user (uid, name) VALUES (?, ?)", u, "u" + u)); IntStream.rangeClosed(1, 3).forEach(a -> h.execute("INSERT INTO article (aid, title) VALUES (?, ?)", a, "a" + a)); h.prepareBatch("INSERT INTO author (uid, aid) VALUES (?,?)") .bind(0, 1).bind(1, 2).add() .bind(0, 3).bind(1, 1).add() .bind(0, 3).bind(1, 3).add() .execute(); // tag::mapperSetup[] h.registerRowMapper(ConstructorMapper.factory(User.class)); h.registerRowMapper(ConstructorMapper.factory(Article.class)); // end::mapperSetup[] }
@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"); }
@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 testPreparedBatch() { h.configure(SqlStatements.class, c -> c.setSqlLogger(SqlLogger.NOP_SQL_LOGGER)); h.createUpdate(CREATE).define("x", "foo").execute(); h.configure(SqlStatements.class, c -> c.setSqlLogger(logger)); int id = 0; h.prepareBatch("insert into <x>(bar) values(?)") .define("x", "foo") .bind(0, new Argument() { @Override public void apply(int position, PreparedStatement statement, StatementContext ctx) throws SQLException { statement.setInt(1, id); } @Override public String toString() { return Objects.toString(id); } }) .execute(); assertThat(logger.getAttributes()) .hasSize(2) .allMatch(x -> x.get("x").equals("foo")) .allMatch(x -> x.size() == 1); assertThat(logger.getBindings()).containsExactly(String.valueOf(id), String.valueOf(id)); }
/** * 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; }
@Override public List<Identifiable> saveIdentifiables(UUID identifiablesContainerUuid, List<Identifiable> identifiables) { dbi.withHandle(h -> h.createUpdate("DELETE FROM webpage_identifiables WHERE webpagee_uuid = :uuid") .bind("uuid", identifiablesContainerUuid).execute()); PreparedBatch batch = dbi.withHandle(h -> h.prepareBatch("INSERT INTO webpage_identifiables(webpage_uuid, identifiable_uuid, sortIndex) VALUES(:uuid, :identifiableUuid, :sortIndex)")); for (Identifiable identifiable : identifiables) { batch.bind("uuid", identifiablesContainerUuid) .bind("identifiableUuid", identifiable.getUuid()) .bind("sortIndex", identifiables.indexOf(identifiable)) .add(); } int[] counts = batch.execute(); return getIdentifiables(identifiablesContainerUuid); } }
@Override public List<Identifiable> saveIdentifiables(UUID identifiablesContainerUuid, List<Identifiable> identifiables) { dbi.withHandle(h -> h.createUpdate("DELETE FROM article_identifiables WHERE article_uuid = :uuid") .bind("uuid", identifiablesContainerUuid).execute()); PreparedBatch batch = dbi.withHandle(h -> h.prepareBatch("INSERT INTO article_identifiables(article_uuid, identifiable_uuid, sortIndex) VALUES(:uuid, :identifiableUuid, :sortIndex)")); for (Identifiable identifiable : identifiables) { batch.bind("uuid", identifiablesContainerUuid) .bind("identifiableUuid", identifiable.getUuid()) .bind("sortIndex", identifiables.indexOf(identifiable)) .add(); } int[] counts = batch.execute(); return getIdentifiables(identifiablesContainerUuid); } }