@Test public void testBasics() { Handle h = dbRule.openHandle(); Batch b = h.createBatch(); b.add("insert into something (id, name) values (0, 'Keith')"); b.add("insert into something (id, name) values (1, 'Eric')"); b.add("insert into something (id, name) values (2, 'Brian')"); b.execute(); List<Something> r = h.createQuery("select * from something order by id").mapToBean(Something.class).list(); assertThat(r).hasSize(3); }
/** * Create a non-prepared (no bound parameters, but different SQL) batch statement. * @return empty batch * @see Handle#prepareBatch(String) */ public Batch createBatch() { return new Batch(this); }
try { try { stmt = getHandle().getStatementBuilder().create(getHandle().getConnection(), getContext()); addCleanable(stmt::close); getConfig(SqlStatements.class).customize(stmt); } catch (SQLException e) { throw new UnableToCreateStatementException(e, getContext()); final String sql = getConfig(SqlStatements.class).getTemplateEngine().render(part, getContext()); LOG.trace(" {}", sql); stmt.addBatch(sql); throw new UnableToExecuteStatementException("Unable to configure JDBC statement", e, getContext()); return SqlLoggerUtil.wrap(stmt::executeBatch, getContext(), getConfig(SqlStatements.class).getSqlLogger()); } catch (SQLException e) { throw new UnableToExecuteStatementException(mungeBatchException(e), getContext()); close();
/** * Execute this script in a batch statement * * @return an array of ints which are the results of each statement in the script */ public int[] execute() { final List<String> statements = getStatements(); Batch b = handle.createBatch(); statements.forEach(b::add); return b.execute(); }
@Test public void testBatchException() { h.execute(CREATE); Throwable e = catchThrowable(h.createBatch().add(INSERT_NULL)::execute); // unfortunately... assertThat(logger.getRawSql()).containsExactly(CREATE, CREATE, null, null); assertThat(logger.getTimings()).hasSize(2).allMatch(IS_POSITIVE); assertThat(logger.getExceptions()).containsExactly((SQLException) e.getCause()); }
throw new UnableToExecuteStatementException(Batch.mungeBatchException(e), getContext());
/** * Execute this script in a batch statement * * @return an array of ints which are the results of each statement in the script */ public int[] execute() { final List<String> statements = getStatements(); Batch b = handle.createBatch(); statements.forEach(b::add); return b.execute(); }
@Test public void testSimpleBatch() { Batch b = dbRule.openHandle().createBatch(); b.add("insert into something (id, name) values (0, 'Keith')"); b.add("insert into something (id, name) values (0, 'Keith')"); assertThatExceptionOfType(UnableToExecuteStatementException.class) .isThrownBy(b::execute) .satisfies(e -> assertSuppressions(e.getCause())); }
throw new UnableToExecuteStatementException(Batch.mungeBatchException(e), getContext());
stmt = getHandle().getStatementBuilder().create(getHandle().getConnection(), getContext()); addCleanable(stmt::close); throw new UnableToCreateStatementException(e, getContext()); final String sql = getConfig(SqlStatements.class).getTemplateEngine().render(part, getContext()); LOG.trace(" {}", sql); stmt.addBatch(sql); throw new UnableToExecuteStatementException("Unable to configure JDBC statement", e, getContext()); LOG.trace("] executed in {}ms", elapsedTime / 1000000L); getConfig(SqlStatements.class).getTimingCollector().collect(elapsedTime, getContext()); return rs; throw new UnableToExecuteStatementException(mungeBatchException(e), getContext()); close();
@Test public void testSimpleBatch() { // tag::simpleBatch[] Batch batch = handle.createBatch(); batch.add("INSERT INTO fruit VALUES(0, 'apple')"); batch.add("INSERT INTO fruit VALUES(1, 'banana')"); int[] rowsModified = batch.execute(); // end::simpleBatch[] assertThat(rowsModified).containsExactly(1, 1); assertThat(handle.createQuery("SELECT count(1) FROM fruit") .mapTo(int.class) .findOnly() .intValue()) .isEqualTo(2); }
if (value == addButtonType) { return new Batch() { { setCode(code.getText()); setName(name.getText()); } }; }
/** * Create a non-prepared (no bound parameters, but different SQL) batch statement. * @return empty batch * @see Handle#prepareBatch(String) */ public Batch createBatch() { return new Batch(this); }
public void process(File file) throws Exception { BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1")); String batchParent = input.readLine(); // read the first line in the file, this is a new batch String batchChild; do { currentBatch = new Batch(batchParent); do { batchChild = input.readLine(); } while (addToBatchIfKeysMatch(batchParent, batchChild)); // if we break out of the inner loop, that means batchChild is a new parent // assign it to batchParent and continue the outer loop batchParent = batchChild; } while (batchParent != null); } private boolean addToBatchIfKeysMatch(final String batchParent, final String batchChild) { if (batchChild != null && keysMatch(batchParent, batchChild)) { currentBatch.add(batchChild); return true; } else { return false; } }