/** * Bind an argument positionally * * @param position position to bind the parameter at, starting at 0 * @param value to bind * * @return the same Query instance */ public final This bind(int position, Character value) { return bind(position, toArgument(Character.class, value)); }
private Argument toArgument(QualifiedType<?> type, Object value) { return getConfig(Arguments.class).findFor(type, value) .orElseThrow(() -> factoryNotFound(type, value)); }
void afterExecution() { callCustomizers(c -> c.afterExecution(stmt, getContext())); } }
PreparedStatement internalExecute() { final StatementContext ctx = getContext(); beforeTemplating(); String renderedSql = getConfig(SqlStatements.class) .getTemplateEngine() .render(sql, ctx); ctx.setRenderedSql(renderedSql); ParsedSql parsedSql = getConfig(SqlStatements.class) .getSqlParser() .parse(renderedSql, ctx); addCleanable(() -> handle.getStatementBuilder().close(handle.getConnection(), this.sql, stmt)); getConfig(SqlStatements.class).customize(stmt); } catch (SQLException e) { throw new UnableToCreateStatementException(e, ctx); beforeBinding(); ArgumentBinder.bind(parsedSql.getParameters(), getBinding(), stmt, ctx); beforeExecution(); SqlLoggerUtil.wrap(stmt::execute, ctx, getConfig(SqlStatements.class).getSqlLogger()); } catch (SQLException e) { try { afterExecution();
names.append(','); String paramName = getConfig().get(SqlStatements.class).getSqlParser().nameParameter(name, getContext()); names.append(paramName); bind(name, values.get(i)); return define(key, names.toString());
final StatementContext ctx = getContext(); for (int valueIndex = 0; valueIter.hasNext(); valueIndex++) { if (valueIndex > 0) { final Argument argument = beanMethods.find(methodName, ctx) .orElseThrow(() -> new UnableToCreateStatementException("Unable to get " + methodName + " argument for " + bean, ctx)); bind(name, argument); return define(key, names.toString());
String renderedSql = getConfig(SqlStatements.class) .getTemplateEngine() .render(sql, getContext()); getContext().setRenderedSql(renderedSql); ParsedSql parsedSql = getConfig(SqlStatements.class) .getSqlParser() .parse(renderedSql, getContext()); String sql = parsedSql.getSql(); ParsedParameters parsedParameters = parsedSql.getParameters(); getContext().setParsedSql(sql); stmt = handle.getStatementBuilder().createCall(handle.getConnection(), sql, getContext()); stmt = handle.getStatementBuilder().create(handle.getConnection(), sql, getContext()); throw new UnableToCreateStatementException(e, getContext()); addCleanable(() -> handle.getStatementBuilder().close(handle.getConnection(), this.sql, stmt)); getContext().setStatement(stmt); ArgumentBinder.bind(parsedParameters, getBinding(), stmt, getContext()); beforeExecution(stmt); final long elapsedTime = System.nanoTime() - start; LOG.trace("Execute SQL \"{}\" in {}ms", sql, elapsedTime / 1000000L); getConfig(SqlStatements.class)
@Override public SqlStatementCustomizer createForMethod(Annotation annotation, Class<?> sqlObjectType, Method method) { final String parameterName = ((Timestamped) annotation).value(); return stmt -> { ZoneId zone = stmt.getConfig(TimestampedConfig.class).getTimezone(); stmt.bind(parameterName, OffsetDateTime.now(timeSource.apply(zone))); }; }
/** * Bind NULL to be set for a given argument. * * @param position position to bind NULL to, starting at 0 * @param sqlType The sqlType must be set and is a value from <code>java.sql.Types</code> * * @return the same statement instance */ public final This bindNull(int position, int sqlType) { return bind(position, new NullArgument(sqlType)); }
/** * Binds named parameters from JavaBean properties on the argument. * * @param bean source of named parameter values to use as arguments * * @return modified statement */ @SuppressWarnings("deprecation") public This bindBean(Object bean) { return bindNamedArgumentFinder(new BeanPropertyArguments(null, bean)); }
/** * Bind an argument dynamically by the generic type passed in. * * @param position position to bind the parameter at, starting at 0 * @param value to bind * @param argumentType type token for value argument * * @return the same Query instance */ public final This bindByType(int position, Object value, GenericType<?> argumentType) { return bindByType(position, value, argumentType.getType()); }
RowMapper<?> mapperForType(Type type) { return getConfig(Mappers.class).findFor(type) .orElseThrow(() -> new UnsupportedOperationException("No mapper registered for " + type)); }
@Override public SqlStatementParameterCustomizer createForParameter(Annotation annotation, Class<?> sqlObjectType, Method method, Parameter param, int index, Type type) { return (stmt, arg) -> { final QualifiedType<?> mapTo; if (arg instanceof QualifiedType) { mapTo = (QualifiedType<?>) arg; } else if (arg instanceof GenericType) { mapTo = QualifiedType.of(((GenericType<?>) arg).getType()); } else if (arg instanceof Type) { mapTo = QualifiedType.of((Type) arg); } else { throw new UnsupportedOperationException("@MapTo must take a GenericType, QualifiedType, or Type, but got a " + arg.getClass().getName()); } ResultReturner returner = ResultReturner.forMethod(sqlObjectType, method); stmt.getConfig(SqlObjectStatementConfiguration.class).setReturner( () -> returner.mappedResult(((ResultBearing) stmt).mapTo(mapTo), stmt.getContext())); }; } }
/** * Binds named parameters from object properties on the bean argument, with the given prefix. * The type must have been registered with pojo type mapping functionality first, usually * by a plugin or configuration. * * @param prefix a prefix to apply to all property names. * @param pojo source of named parameter values to use as arguments * * @return modified statement * @see JdbiImmutables an example method of registering a type */ @Beta public This bindPojo(String prefix, Object pojo) { return bindNamedArgumentFinder(new PojoPropertyArguments(prefix, pojo, getContext())); }
protected Binding getBinding() { return getContext().getBinding(); }
.collect(joining(", ")); return define(key, value);
/** * Bind a parameter for each value in the given vararg array, and defines an attribute as the comma-separated list * of parameter references (using colon prefix). * <p> * Examples: * <pre> * handle.createUpdate("insert into things (<columnNames>) values (<values>)") * .defineList("columnNames", "id", "name", "created_on") * .bindList("values", 1, "Alice", LocalDate.now()) * .execute(); * * List<Thing> things = handle.createQuery("select * from things where id in (<ids>)") * .bindList("ids", 1, 2, 3) * .mapTo(Contact.class) * .list(); * </pre> * * @param key attribute name * @param values vararg values that will be comma-spliced into the defined attribute value. * @return this * @throws IllegalArgumentException if the vararg array is empty. */ public final This bindList(String key, Object... values) { if (values.length == 0) { throw new IllegalArgumentException( getClass().getSimpleName() + ".bindList was called with no vararg values."); } return bindList(key, Arrays.asList(values)); }
return defineList(key, Arrays.asList(values));
/** * Set the query timeout, in seconds, on the prepared statement * * @param seconds number of seconds before timing out * * @return the same Query instance */ public This setQueryTimeout(final int seconds) { return addCustomizer(StatementCustomizers.statementTimeout(seconds)); }