Search in sources :

Example 1 with UnableToCreateStatementException

use of org.jdbi.v3.core.statement.UnableToCreateStatementException in project jdbi by jdbi.

the class DefinedAttributeTemplateEngine method render.

@Override
public String render(String template, StatementContext ctx) {
    StringBuilder b = new StringBuilder();
    DefineStatementLexer lexer = new DefineStatementLexer(new ANTLRStringStream(template));
    try {
        Token t = lexer.nextToken();
        while (t.getType() != EOF) {
            switch(t.getType()) {
                case COMMENT:
                case LITERAL:
                case QUOTED_TEXT:
                case DOUBLE_QUOTED_TEXT:
                    b.append(t.getText());
                    break;
                case DEFINE:
                    String text = t.getText();
                    String key = text.substring(1, text.length() - 1);
                    Object value = ctx.getAttribute(key);
                    if (value == null) {
                        throw new UnableToCreateStatementException("Undefined attribute for token '" + text + "'", ctx);
                    }
                    b.append(value);
                    break;
                case ESCAPED_TEXT:
                    b.append(t.getText().substring(1));
                    break;
                default:
                    break;
            }
            t = lexer.nextToken();
        }
        return b.toString();
    } catch (RuntimeException e) {
        throw new UnableToCreateStatementException("Error rendering SQL template: '" + template + "'", e, ctx);
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) DefineStatementLexer(org.jdbi.v3.core.internal.lexer.DefineStatementLexer) Token(org.antlr.runtime.Token)

Example 2 with UnableToCreateStatementException

use of org.jdbi.v3.core.statement.UnableToCreateStatementException in project jdbi by jdbi.

the class SqlStatement method bindBeanList.

/**
 * Bind a parameter for each value in the given list * number of property names,
 * and defines an attribute as the comma-separated list of parameter references (using colon prefix).
 *
 * Used to create query similar to:
 * select * from things where (id, foo) in ((1,'abc'),(2,'def'),(3,'ghi'))
 * <p>
 * Examples:
 * <pre>
 *
 * List&lt;ThingKey&gt; thingKeys = ...
 * List&lt;Thing&gt; things = handle.createQuery("select * from things where (id, foo) in (&lt;thingKeys&gt;)")
 *     .bindBeanList("thingKeys", thingKeys, Arrays.asList("id", "foo"))
 *     .mapTo(Contact.class)
 *     .list();
 * </pre>
 *
 * @param key    attribute name
 * @param values list of values that will be comma-spliced into the defined attribute value.
 * @param propertyNames list of properties that will be invoked on the values.
 * @return this
 * @throws IllegalArgumentException if the list of values or properties is empty.
 * @throws UnableToCreateStatementException If a property can't be found on an value or we can't find a Argument for it.
 */
public final This bindBeanList(String key, List<?> values, List<String> propertyNames) throws UnableToCreateStatementException {
    if (values.isEmpty()) {
        throw new IllegalArgumentException(getClass().getSimpleName() + ".bindBeanList was called with no values.");
    }
    if (propertyNames.isEmpty()) {
        throw new IllegalArgumentException(getClass().getSimpleName() + ".bindBeanList was called with no properties.");
    }
    StringBuilder names = new StringBuilder();
    StatementContext ctx = getContext();
    for (int valueIndex = 0; valueIndex < values.size(); valueIndex++) {
        if (valueIndex > 0) {
            names.append(',');
        }
        Object bean = values.get(valueIndex);
        BeanPropertyArguments beanProperties = new BeanPropertyArguments(null, bean);
        names.append("(");
        for (int propertyIndex = 0; propertyIndex < propertyNames.size(); propertyIndex++) {
            if (propertyIndex > 0) {
                names.append(",");
            }
            String propertyName = propertyNames.get(propertyIndex);
            String name = "__" + key + "_" + valueIndex + "_" + propertyName;
            names.append(':').append(name);
            Argument argument = beanProperties.find(propertyName, ctx).orElseThrow(() -> new UnableToCreateStatementException("Unable to get " + propertyName + " argument for " + bean, ctx));
            bind(name, argument);
        }
        names.append(")");
    }
    return define(key, names.toString());
}
Also used : InputStreamArgument(org.jdbi.v3.core.argument.InputStreamArgument) NullArgument(org.jdbi.v3.core.argument.NullArgument) ObjectArgument(org.jdbi.v3.core.argument.ObjectArgument) Argument(org.jdbi.v3.core.argument.Argument) CharacterStreamArgument(org.jdbi.v3.core.argument.CharacterStreamArgument) BeanPropertyArguments(org.jdbi.v3.core.argument.BeanPropertyArguments)

Example 3 with UnableToCreateStatementException

use of org.jdbi.v3.core.statement.UnableToCreateStatementException in project jdbi by jdbi.

the class ObjectFieldArguments method getValue.

@Override
Optional<TypedValue> getValue(String name, StatementContext ctx) {
    Field field = fields.get(name);
    if (field == null) {
        return Optional.empty();
    }
    try {
        Type type = field.getGenericType();
        Object value = field.get(object);
        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, object), e, ctx);
    }
}
Also used : Field(java.lang.reflect.Field) Type(java.lang.reflect.Type) UnableToCreateStatementException(org.jdbi.v3.core.statement.UnableToCreateStatementException)

Aggregations

Field (java.lang.reflect.Field)1 Type (java.lang.reflect.Type)1 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)1 Token (org.antlr.runtime.Token)1 Argument (org.jdbi.v3.core.argument.Argument)1 BeanPropertyArguments (org.jdbi.v3.core.argument.BeanPropertyArguments)1 CharacterStreamArgument (org.jdbi.v3.core.argument.CharacterStreamArgument)1 InputStreamArgument (org.jdbi.v3.core.argument.InputStreamArgument)1 NullArgument (org.jdbi.v3.core.argument.NullArgument)1 ObjectArgument (org.jdbi.v3.core.argument.ObjectArgument)1 DefineStatementLexer (org.jdbi.v3.core.internal.lexer.DefineStatementLexer)1 UnableToCreateStatementException (org.jdbi.v3.core.statement.UnableToCreateStatementException)1