@Override public void beforeTemplating(PreparedStatement stmt, StatementContext ctx) throws SQLException { final Set<String> alreadyDefined = ctx.getAttributes().keySet(); final Binding binding = ctx.getBinding(); binding.getNames().stream() .filter(name -> !alreadyDefined.contains(name)) .forEach(name -> binding.findForName(name, ctx).ifPresent( a -> ctx.define(name, a instanceof NullArgument ? false : true))); } }
private static void bindNamed(ParsedParameters params, Binding binding, PreparedStatement statement, StatementContext context) { List<String> paramNames = params.getParameterNames(); // best effort: compare empty to non-empty because we can't list the individual binding names (unless we expose a method to do so) boolean argumentsProvidedButNoneDeclared = paramNames.isEmpty() && !binding.isEmpty(); if (argumentsProvidedButNoneDeclared && !context.getConfig(SqlStatements.class).isUnusedBindingAllowed()) { throw new UnableToCreateStatementException(String.format( "Superfluous named parameters provided while the query " + "declares none: '%s'. This check may be disabled by calling " + "getConfig(SqlStatements.class).setUnusedBindingAllowed(true) " + "or using @AllowUnusedBindings in SQL object.", binding), context); } for (int i = 0; i < paramNames.size(); i++) { final String name = paramNames.get(i); try { binding.findForName(name, context) .orElseThrow(() -> new UnableToCreateStatementException(String.format("Missing named parameter '%s'.", name), context)) .apply(i + 1, statement, context); } catch (SQLException e) { throw new UnableToCreateStatementException(String.format("Exception while binding named parameter '%s'", name), e, context); } } } }
@Override public void logBeforeExecution(StatementContext context) { context.getBinding().findForPosition(0).ifPresent(value -> positional = Objects.toString(value)); context.getBinding().findForName(NAME, context).ifPresent(value -> named = Objects.toString(value)); } });
@Override public void logBeforeExecution(StatementContext ctx) { String toString = ctx.getBinding() .findForName(name, ctx) .orElseThrow(AssertionError::new) .toString(); insertedTimestamp = OffsetDateTime.parse(toString); insertedSqlTimestamp = Timestamp.from(insertedTimestamp.toInstant()); }
@Test public void testContextGetsBinding() { try { h.prepareBatch("insert into something (id, name) values (:id, :name)") .bind("id", 0) .bind("name", "alice") .add() .bind("id", 0) .bind("name", "bob") .add() .execute(); fail("expected exception"); } catch (UnableToExecuteStatementException e) { final StatementContext ctx = e.getStatementContext(); assertThat(ctx.getBinding().findForName("name", ctx).toString()).contains("bob"); } }
private static void bindNamed(List<String> parameterNames, Binding binding, PreparedStatement statement, StatementContext context) { for (int i = 0; i < parameterNames.size(); i++) { String param = parameterNames.get(i); try { binding.findForName(param, context) .orElseThrow(() -> new UnableToExecuteStatementException( String.format("Unable to execute, no named parameter matches '%s'.", param), context)) .apply(i + 1, statement, context); } catch (SQLException e) { throw new UnableToCreateStatementException( String.format("Exception while binding named parameter '%s'", param), e, context); } } } }
@Override public void logBeforeExecution(StatementContext ctx) { String toString = ctx.getBinding() .findForName(name, ctx) .orElseThrow(AssertionError::new) .toString(); timestamp = OffsetDateTime.parse(toString); sqlTimestamp = Timestamp.from(timestamp.toInstant()); }