For IntelliJ IDEA and
Android Studio


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() throws Exception { 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 testBigishBatch() throws Exception { 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 row_count = h.createQuery("select count(id) from something").mapTo(int.class).findOnly(); assertThat(row_count).isEqualTo(count); }
/** * 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 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"); }
@Test public void testPositionalBinding() throws Exception { 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 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).hasSize(3); assertThat(r.get(0)).extracting(Something::getId, Something::getName).containsSequence(1, "Eric"); assertThat(r.get(1)).extracting(Something::getId, Something::getName).containsSequence(2, "Brian"); assertThat(r.get(2)).extracting(Something::getId, Something::getName).containsSequence(3, "Keith"); }
@Test public void testForgotFinalAdd() throws Exception { 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 testMixedModeBatch() throws Exception { 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 testBindBatch() throws Exception { 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 testMixedModeBatch() throws Exception { 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 testPositionalBinding() throws Exception { 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 testBigishBatch() throws Exception { 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 row_count = h.createQuery("select count(id) from something").mapTo(int.class).findOnly(); assertThat(row_count).isEqualTo(count); }
@Test public void testForgotFinalAdd() throws Exception { 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"); }
/** * 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; }