@Override void configureReturner(Script stmt, SqlObjectStatementConfiguration cfg) { cfg.setReturner(() -> stmt.execute()); }
/** * Creates a Script from the given SQL script. * * @param sql the SQL script * * @return the created Script */ public Script createScript(String sql) { return new Script(this, sql); }
private List<String> splitToStatements(String script) { final List<String> statements = new ArrayList<>(); String lastStatement = new SqlScriptParser((t, sb) -> { addStatement(sb.toString(), statements); sb.setLength(0); }).parse(new ANTLRStringStream(script)); addStatement(lastStatement, statements); return statements; }
/** * Locate the Script and split it into statements. * @return the split statements */ public List<String> getStatements() { return splitToStatements(getConfig(SqlStatements.class).getTemplateEngine().render(sql, getContext())); }
/** * Execute this script as a set of separate statements */ public void executeAsSeparateStatements() { for (String s : getStatements()) { handle.execute(s); } }
/** * Locate the Script and split it into statements. * @return the split statements */ public List<String> getStatements() { return splitToStatements(sql); }
@Test public void testFuzzyScript() { Handle h = dbRule.openHandle(); Script script = h.createScript(getResourceOnClasspath("script/fuzzy-script.sql")); script.executeAsSeparateStatements(); List<Map<String, Object>> rows = h.select("select id, name from something order by id").mapToMap().list(); assertThat(rows).isEqualTo(ImmutableList.of( ImmutableMap.of("id", 1L, "name", "eric"), ImmutableMap.of("id", 2L, "name", "sally;ann"), ImmutableMap.of("id", 3L, "name", "bob"), ImmutableMap.of("id", 12L, "name", "sally;ann;junior"))); }
/** * 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 testScriptAsSetOfSeparateStatements() { assertThatExceptionOfType(StatementException.class) .isThrownBy(() -> { Handle h = dbRule.openHandle(); Script script = h.createScript(getResourceOnClasspath("script/malformed-sql-script.sql")); script.executeAsSeparateStatements(); }) .satisfies(e -> assertThat(e.getStatementContext().getRawSql().trim()) .isEqualTo("insert into something(id, name) values (2, eric)")); } }
@Test public void testScript() { // tag::script[] int[] results = handle.createScript( "INSERT INTO user VALUES(3, 'Charlie');" + "UPDATE user SET name='Bobby Tables' WHERE id=2;") .execute(); assertThat(results).containsExactly(1, 1); // end::script[] }
@Override Script createStatement(Handle handle, String locatedSql) { return new Script(handle, locatedSql); } }
/** * Execute this script as a set of separate statements */ public void executeAsSeparateStatements() { for (String s : getStatements()) { handle.execute(s); } }
private List<String> splitToStatements(String script) { final List<String> statements = new ArrayList<>(); String lastStatement = new SqlScriptParser((t, sb) -> { addStatement(sb.toString(), statements); sb.setLength(0); }).parse(new ANTLRStringStream(script)); addStatement(lastStatement, statements); return statements; }
@Test public void testParamRight() { FooParamRight sqlObject = handle.attach(FooParamRight.class); handle.createScript(CREATE_INSERT).execute(); assertThat(sqlObject.bar(ROWS)).hasSize(ROWS); }
/** * Creates a Script from the given SQL script. * * @param sql the SQL script * * @return the created Script */ public Script createScript(String sql) { return new Script(this, sql); }
/** * 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 testControlGroup() { NoMaxRows sqlObject = handle.attach(NoMaxRows.class); handle.createScript(CREATE_INSERT).execute(); assertThat(sqlObject.bar()).hasSize(3); }
@Test public void testMethodRight() { FooMethodRight sqlObject = handle.attach(FooMethodRight.class); handle.createScript(CREATE_INSERT).execute(); assertThat(sqlObject.bar()).hasSize(ROWS); }
@Test public void testParamNonsense() { FooParamRight sqlObject = handle.attach(FooParamRight.class); handle.createScript(CREATE_INSERT).execute(); assertThatThrownBy(() -> sqlObject.bar(0)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("is 0, which is negative or 0"); }
@Test public void testFetchSize() { h.createScript(findSqlOnClasspath("default-data")).execute(); ResultIterable<Something> ri = h.createQuery("select id, name from something order by id") .setFetchSize(1) .mapToBean(Something.class); ResultIterator<Something> r = ri.iterator(); assertThat(r.hasNext()).isTrue(); r.next(); assertThat(r.hasNext()).isTrue(); r.next(); assertThat(r.hasNext()).isFalse(); }