@Override public String toJson(Type type, Object value, ConfigRegistry config) { throw new UnableToCreateStatementException(NO_IMPL_INSTALLED); }
@Override public ParsedSql parse(String sql, StatementContext ctx) { try { return cache.computeIfAbsent(sql, this::internalParse); } catch (IllegalArgumentException e) { throw new UnableToCreateStatementException("Exception parsing for named parameter replacement", e, ctx); } }
@Override public ParsedSql parse(String sql, StatementContext ctx) { try { return cache.computeIfAbsent(sql, this::internalParse); } catch (IllegalArgumentException e) { throw new UnableToCreateStatementException("Exception parsing for named parameter replacement", e, ctx); } }
protected Object invokeMethod(Method method, StatementContext ctx) { try { return method.invoke(obj); } catch (IllegalAccessException e) { throw new UnableToCreateStatementException(String.format("Access exception invoking " + "method [%s] on [%s]", method.getName(), obj), e, ctx); } catch (InvocationTargetException e) { throw new UnableToCreateStatementException(String.format("Invocation target exception invoking " + "method [%s] on [%s]", method.getName(), obj), e, ctx); } } }
static RowReducer<?, ?> rowReducerFor(UseRowReducer annotation) { Class<? extends RowReducer<?, ?>> reducerClass = annotation.value(); try { return reducerClass.getConstructor().newInstance(); } catch (Exception e) { throw new UnableToCreateStatementException("Could not create reducer " + reducerClass.getName(), e, null); } }
static RowMapper<?> rowMapperFor(UseRowMapper annotation) { Class<? extends RowMapper<?>> mapperClass = annotation.value(); try { return mapperClass.getConstructor().newInstance(); } catch (Exception e) { throw new UnableToCreateStatementException("Could not create mapper " + mapperClass.getName(), e, null); } }
public PojoPropertyArguments(String prefix, Object obj, StatementContext ctx) { this(prefix, obj, ctx.getConfig(PojoTypes.class).findFor(obj.getClass()) .orElseThrow(() -> new UnableToCreateStatementException("Couldn't find pojo type of " + obj.getClass(), ctx)), ctx); }
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 final Optional<Argument> find(String name, StatementContext ctx) { if (name.startsWith(prefix)) { final String actualName = name.substring(prefix.length()); int separator = actualName.indexOf('.'); if (separator != -1) { String parentName = actualName.substring(0, separator); String childName = actualName.substring(separator + 1); return childArgumentFinders .computeIfAbsent(parentName.endsWith("?") ? parentName.substring(0, parentName.length() - 1) : parentName, pn -> getValue(pn, ctx).map(typedValue -> getValueNested(typedValue, parentName, childName))) .flatMap(arg -> arg.find(childName, ctx)); } return getValue(actualName, ctx) .map(tv -> ctx.findArgumentFor(tv.type, tv.value) .orElseThrow(() -> new UnableToCreateStatementException( String.format("No argument factory registered for type [%s] for element [%s] on [%s]", tv.type, name, obj), ctx))); } return Optional.empty(); }
@Override protected Optional<TypedValue> getValue(String name, StatementContext ctx) { Field field = fields.get(name); if (field == null) { return Optional.empty(); } try { QualifiedType<?> type = QualifiedType.of(field.getGenericType()) .with(getQualifiers(field)); Object value = field.get(obj); return Optional.of(new TypedValue(type, value)); } catch (IllegalAccessException e) { throw new UnableToCreateStatementException(String.format("Access exception getting field for " + "bean property [%s] on [%s]", name, obj), e, ctx); } }
names.append(':').append(name); final Argument argument = beanMethods.find(methodName, ctx) .orElseThrow(() -> new UnableToCreateStatementException("Unable to get " + methodName + " argument for " + bean, ctx)); bind(name, argument);
private static void bindPositional(ParsedParameters params, Binding binding, PreparedStatement statement, StatementContext context) { // best effort: just try +1 (unless we expose a method to get the full binding count) boolean moreArgumentsProvidedThanDeclared = binding.findForPosition(params.getParameterCount()).isPresent(); if (moreArgumentsProvidedThanDeclared && !context.getConfig(SqlStatements.class).isUnusedBindingAllowed()) { throw new UnableToCreateStatementException("Superfluous positional param at (0 based) position " + params.getParameterCount(), context); } for (int i = 0; i < params.getParameterCount(); i++) { final int index = i; try { binding.findForPosition(i) .orElseThrow(() -> new UnableToCreateStatementException("Missing positional param at (0 based) position " + index, context)) .apply(i + 1, statement, context); } catch (SQLException e) { throw new UnableToCreateStatementException("Exception while binding positional param at (0 based) position " + i, e, context); } } }
throw new UnableToCreateStatementException("@SqlBatch method has no Iterable or array parameters," + " did you mean @SqlQuery?", null, null);
names.append(':').append(name); Argument argument = beanProperties.find(propertyName, ctx) .orElseThrow(() -> new UnableToCreateStatementException("Unable to get " + propertyName + " argument for " + bean, ctx)); bind(name, argument);
@Override public Optional<Argument> build(Type type, Object value, ConfigRegistry config) { if (String.class.equals(type)) { return Optional.empty(); } String json = value == null ? null : config.get(JsonConfig.class).getJsonMapper().toJson(type, value, config); Arguments a = config.get(Arguments.class); // look for specialized json support first, revert to simple String binding if absent return Optional.of(JdbiOptionals.findFirstPresent( () -> a.findFor(QualifiedType.of(String.class).with(Json.class), json), () -> a.findFor(String.class, json)) .orElseThrow(() -> new UnableToCreateStatementException(JSON_NOT_STORABLE))); } }
Object value = ctx.getAttribute(key); if (value == null) { throw new UnableToCreateStatementException("Undefined attribute for token '" + text + "'", ctx); throw new UnableToCreateStatementException("Error rendering SQL template: '" + template + "'", e, ctx);
getConfig(SqlStatements.class).customize(stmt); } catch (SQLException e) { throw new UnableToCreateStatementException(e, getContext());
getConfig(SqlStatements.class).customize(stmt); } catch (SQLException e) { throw new UnableToCreateStatementException(e, ctx);
getConfig(SqlStatements.class).customize(stmt); } catch (SQLException e) { throw new UnableToCreateStatementException(e, getContext());
@Override public ParsedSql parse(String sql, StatementContext ctx) { try { return cache.computeIfAbsent(sql, this::internalParse); } catch (IllegalArgumentException e) { throw new UnableToCreateStatementException("Exception parsing for named parameter replacement", e, ctx); } }