public static Update getBoundStatement(Handle handle, String dbPrefix, String tableParameterName, String tableName, String queryName) { return handle.createStatement(dbPrefix + queryName).define(tableParameterName, tableName); }
@Override public Boolean inTransaction(Handle handle, TransactionStatus arg1) throws Exception { try { handle.createStatement(prefix + "table_exists").define("table_name", tableName).execute(); return true; } catch (UnableToExecuteStatementException e) { // expected in missing case } catch (UnableToCreateStatementException e) { // expected in missing case } return false; } });
@Override public Object withHandle(final Handle h) throws Exception { h.createStatement("create table <name> (id int primary key, name text)") .define("name", "something") .execute(); h.execute("insert into something (id, name) values (3, 'Carlos')"); return null; } });
@Override public Integer withHandle(final Handle handle) throws SQLException { final SqlScript cleanScript = migratoryContext.getDbSupport().createCleanScript(handle); setSchema(migratoryContext.getDbSupport().getCurrentSchema(handle)); for (SqlStatement sqlStatement : cleanScript.getSqlStatements()) { try { if (!MigratoryOption.containsOption(MigratoryOption.DRY_RUN, options)) { handle.createStatement(sqlStatement.getSql()) .define(sqlStatement.getDefines()) .execute(); } } catch (DBIException e) { LOG.warn("While executing {}", sqlStatement, e); } } return 0; } });
@Override public Object withHandle(final Handle h) throws Exception { h.execute("createSomething"); h.createStatement("insert") .define("table", "something") .bind("id", 1) .bind("name", "Ven") .execute(); final Something s = h.createQuery("findById") .bind("id", 1) .map(new ResultSetMapper<Something>() { @Override public Something map(final int index, final ResultSet r, final StatementContext ctx) throws SQLException { return new Something(r.getInt("id"), r.getString("name")); } }) .first(); assertThat(s).isEqualTo(new Something(1, "Ven")); return null; } });
@Test public void testFoo() throws Exception { Handle h = openHandle(); h.setStatementLocator(new StatementLocator() { @Override public String locate(String name, StatementContext ctx) throws Exception { return name.replaceAll("<table>", String.valueOf(ctx.getAttribute("table"))); } }); final int inserted = h.createStatement("insert into <table> (id, name) values (:id, :name)") .bind("id", 7) .bind("name", "Martin") .define("table", "something") .execute(); assertEquals(1, inserted); } }
@Test public void testFoo() throws Exception { Handle h = openHandle(); h.setStatementLocator(new StatementLocator() { @Override public String locate(String name, StatementContext ctx) throws Exception { return name.replaceAll("<table>", String.valueOf(ctx.getAttribute("table"))); } }); final int inserted = h.createStatement("insert into <table> (id, name) values (:id, :name)") .bind("id", 7) .bind("name", "Martin") .define("table", "something") .execute(); assertEquals(1, inserted); } }
.define("name", "something") .execute();