public int executeUpdate(DSLContext dsl, String statement, Context context) { Connection c = dsl.configuration().connectionProvider().acquire(); try (PreparedStatement ps = c.prepareStatement(statement)) { return ps.executeUpdate(); } catch (SQLException ex) { throw errorHandler.handleException(context, ex); } finally { dsl.configuration().connectionProvider().release(c); } }
public Result<Record> executeStatementWithResult( DSLContext dsl, String statement, Context context, SetupPreparedStatement statementSetup) { Connection c = dsl.configuration().connectionProvider().acquire(); try (PreparedStatement ps = c.prepareStatement(statement)) { statementSetup.accept(ps); try (ResultSet resultSet = ps.executeQuery()) { return dsl.fetch(resultSet); } } catch (SQLException ex) { throw errorHandler.handleException(context, ex); } finally { dsl.configuration().connectionProvider().release(c); } }
@Test public void testCustomSQL() throws Exception { final Field<Integer> IDx2 = new CustomField<Integer>(TBook_ID().getName(), TBook_ID().getDataType()) { private static final long serialVersionUID = 1L; @Override public void toSQL(RenderContext context) { context.configuration().data("Foo-Field", "Baz"); if (context.inline()) { context.sql(TBook_ID().getName() + " * 2"); } // Firebird is the only dialect that cannot handle type inferral // When multiplying an INT by a bind value else if (context.configuration().dialect() == FIREBIRD) { context.sql(TBook_ID().getName() + " * cast (? as int)"); } else { context.sql(TBook_ID().getName() + " * ?"); } } @Override public void bind(BindContext context) { try { context.statement().setInt(context.nextIndex(), 2); } catch (SQLException e) { throw translate(null, e); } } }; Condition c = new CustomCondition() { private static final long serialVersionUID = -629253722638033620L; @Override public void toSQL(RenderContext context) { context.configuration().data("Foo-Condition", "Baz"); context.sql(IDx2); context.sql(" > "); if (context.inline()) { context.sql("3"); } else { context.sql("?"); } } @Override public void bind(BindContext context) { try { context.bind(IDx2); context.statement().setInt(context.nextIndex(), 3); } catch (SQLException e) { throw translate(null, e); } } }; // [#1169] Some additional checks to see if custom data is correctly // passed on to custom QueryParts DSLContext create = create(); create.configuration().data("Foo-Field", "Bar"); create.configuration().data("Foo-Condition", "Bar"); Result<Record2<Integer, Integer>> result = create .select(TBook_ID(), IDx2) .from(TBook()) .where(c) .orderBy(IDx2) .fetch(); assertEquals(3, result.size()); assertEquals(Integer.valueOf(2), result.getValue(0, TBook_ID())); assertEquals(Integer.valueOf(3), result.getValue(1, TBook_ID())); assertEquals(Integer.valueOf(4), result.getValue(2, TBook_ID())); assertEquals(Integer.valueOf(4), result.getValue(0, IDx2)); assertEquals(Integer.valueOf(6), result.getValue(1, IDx2)); assertEquals(Integer.valueOf(8), result.getValue(2, IDx2)); // [#1169] Check again assertEquals("Baz", create.configuration().data("Foo-Field")); assertEquals("Baz", create.configuration().data("Foo-Condition")); }