@Override public Void withHandle(Handle handle) throws Exception { final PreparedBatch batch = handle.prepareBatch( StringUtils.format( "INSERT INTO %1$s (id, dataSource, created_date, start, %2$send%2$s, partitioned, version, used, payload) " + "VALUES (:id, :dataSource, :created_date, :start, :end, :partitioned, :version, :used, :payload)", tableName, connector.getQuoteString() ) ); for (final DataSegment segment : segments) { batch.add( new ImmutableMap.Builder<String, Object>() .put("id", segment.getId().toString()) .put("dataSource", segment.getDataSource()) .put("created_date", DateTimes.nowUtc().toString()) .put("start", segment.getInterval().getStart().toString()) .put("end", segment.getInterval().getEnd().toString()) .put("partitioned", (segment.getShardSpec() instanceof NoneShardSpec) ? false : true) .put("version", segment.getVersion()) .put("used", true) .put("payload", mapper.writeValueAsBytes(segment)) .build() ); log.info("Published %s", segment.getId()); } batch.execute(); return null; } }
handle.prepareBatch(String.format( "INSERT INTO %1$s (id, dataSource, created_date, start, \"end\", partitioned, version, used, payload) " + "VALUES (:id, :dataSource, :created_date, :start, :end, :partitioned, :version, :used, :payload)",
public PreparedBatch getInsertBatch() { if (insertBatch == null) { insertBatch = dbi.prepareBatch("INSERT into jsondb (path, value, ovalue, idx) values (:path, :value, :ovalue, :idx)"); } return insertBatch; }
/** * @see Handle#prepareBatch(String) */ public PreparedBatch createPreparedBatch(final Handle handle) { return handle.prepareBatch(queryName); }
public PreparedBatch getInsertBatch() { if (insertBatch == null) { insertBatch = dbi.prepareBatch("INSERT into jsondb (path, value, kind, idx) values (:path, :value, :kind, :idx)"); } return insertBatch; }
@Override public void deleteDepartments(List<Department> departmentsToDelete) { try (Handle h = dbi.open()) { PreparedBatch preparedBatch = h.prepareBatch("DELETE FROM department WHERE pid = :pid"); for (Department department : departmentsToDelete) { preparedBatch.bind("pid", department.getPid()).add(); } preparedBatch.execute(); } }
@Test public void testPreparedBatch() throws Exception { String sql = "insert into something (id, name) values (?, ?)"; h.prepareBatch(sql).add(1, "Eric").add(2, "Keith").execute(); assertEquals(1, logged.size()); assertEquals(String.format("%d:%s", 2, sql), logged.get(0)); }
@Test public void testPreparedBatch() throws Exception { String sql = "insert into something (id, name) values (?, ?)"; h.prepareBatch(sql).add(1, "Eric").add(2, "Keith").execute(); assertEquals(1, logged.size()); assertEquals(String.format("%d:%s", 2, sql), logged.get(0)); }
@Override public void insertDepartments(List<Department> departmentsToInsert) { try (Handle h = dbi.open()) { PreparedBatch preparedBatch = h.prepareBatch("INSERT INTO department (company_pid, name) VALUES (:company_pid, :name)"); for (Department department : departmentsToInsert) { preparedBatch .bind("company_pid", department.getCompanyPid()) .bind("name", department.getName()) .add(); } preparedBatch.execute(); } }
@Test public void testMixedModeBatch() throws Exception { Handle h = openHandle(); PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); Map<String, Object> one = DerbyHelper.map("id", 0); b.add(one).bind("name", "Keith"); b.execute(); List<Something> r = h.createQuery("select * from something order by id").map(Something.class).list(); assertEquals(1, r.size()); assertEquals("Keith", r.get(0).getName()); }
@Test public void testBatchInsertWithKeyGenerationAndExplicitColumnNames() { PreparedBatch batch = h.prepareBatch("insert into something (name) values (?) "); batch.add("Brian"); batch.add("Thom"); List<Integer> ids = batch.executeAndGenerateKeys(IntegerColumnMapper.WRAPPER, "id").list(); assertEquals(Arrays.asList(1, 2), ids); List<Something> somethings = h.createQuery("select id, name from something") .map(Something.class) .list(); assertEquals(Arrays.asList(new Something(1, "Brian"), new Something(2, "Thom")), somethings); }
@Test public void testMixedModeBatch() throws Exception { Handle h = openHandle(); PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); Map<String, Object> one = DerbyHelper.map("id", 0); b.add(one).bind("name", "Keith"); b.execute(); List<Something> r = h.createQuery("select * from something order by id").map(Something.class).list(); assertEquals(1, r.size()); assertEquals("Keith", r.get(0).getName()); }
@Test public void testPositionalBinding() throws Exception { Handle h = openHandle(); PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); b.add().bind(0, 0).bind(1, "Keith").submit().execute(); List<Something> r = h.createQuery("select * from something order by id").map(Something.class).list(); assertEquals(1, r.size()); assertEquals("Keith", r.get(0).getName()); }
@Test public void testBindProperties() throws Exception { Handle h = openHandle(); 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").map(Something.class).list(); assertEquals(3, r.size()); assertEquals("Brian", r.get(2).getName()); }
@Test public void testBindProperties() throws Exception { Handle h = openHandle(); 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").map(Something.class).list(); assertEquals(3, r.size()); assertEquals("Brian", r.get(2).getName()); }
@Test public void testBindMaps() throws Exception { Handle h = openHandle(); PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); Map<String, Object> one = DerbyHelper.map("id", 0).add("name", "Keith"); b.add(one); b.add(DerbyHelper.map("id", Integer.parseInt("1")).add("name", "Eric")); b.add(DerbyHelper.map("id", Integer.parseInt("2")).add("name", "Brian")); b.execute(); List<Something> r = h.createQuery("select * from something order by id").map(Something.class).list(); assertEquals(3, r.size()); assertEquals("Brian", r.get(2).getName()); }
@Test public void testPositionalBinding() throws Exception { Handle h = openHandle(); PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); b.add().bind(0, 0).bind(1, "Keith").submit().execute(); List<Something> r = h.createQuery("select * from something order by id").map(Something.class).list(); assertEquals(1, r.size()); assertEquals("Keith", r.get(0).getName()); }
@Test public void testMixedBatchSetting() throws Exception { Handle h = openHandle(); PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); b.bind("id", 1); b.add().bind("name", "Jeff"); b.bind("id", 2); b.add().bind("name", "Tom"); b.execute(); assertEquals(h.createQuery("select name from something order by id").map(StringMapper.FIRST).list(), Arrays.asList("Jeff", "Tom")); } }
@Test public void testDesignApi() throws Exception { Handle h = openHandle(); PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); PreparedBatchPart p = b.add(); p = p.bind("id", 1).bind("name", "Eric").next(); p.bind("id", 2).bind("name", "Brian").next().bind("id", 3).bind("name", "Keith"); b.execute(); List<Something> r = h.createQuery("select * from something order by id").map(Something.class).list(); assertEquals(3, r.size()); assertEquals("Keith", r.get(2).getName()); }
@Test public void testDesignApi() throws Exception { Handle h = openHandle(); PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); PreparedBatchPart p = b.add(); p = p.bind("id", 1).bind("name", "Eric").next(); p.bind("id", 2).bind("name", "Brian").next().bind("id", 3).bind("name", "Keith"); b.execute(); List<Something> r = h.createQuery("select * from something order by id").map(Something.class).list(); assertEquals(3, r.size()); assertEquals("Keith", r.get(2).getName()); }