Search in sources :

Example 1 with Arguments

use of org.jdbi.v3.core.argument.Arguments in project jdbi by jdbi.

the class Handle method execute.

/**
 * Execute a SQL statement, and return the number of rows affected by the statement.
 *
 * @param sql the SQL statement to execute, using positional parameters (if any)
 * @param args positional arguments
 *
 * @return the number of rows affected
 */
public int execute(String sql, Object... args) {
    Update stmt = createUpdate(sql);
    int position = 0;
    for (Object arg : args) {
        stmt.bind(position++, arg);
    }
    return stmt.execute();
}
Also used : Update(org.jdbi.v3.core.statement.Update)

Example 2 with Arguments

use of org.jdbi.v3.core.argument.Arguments in project jdbi by jdbi.

the class DefineListFactory method createForParameter.

@Override
public SqlStatementParameterCustomizer createForParameter(Annotation annotation, Class<?> sqlObjectType, Method method, Parameter param, int index, Type type) {
    final DefineList d = (DefineList) annotation;
    final String name = ParameterUtil.findParameterName(d.value(), param).orElseThrow(() -> new UnsupportedOperationException("A @DefineList parameter was not given a name, " + "and parameter name data is not present in the class file, for: " + param.getDeclaringExecutable() + "::" + param));
    return (stmt, arg) -> {
        List<?> argsList;
        if (arg instanceof List) {
            argsList = (List<?>) arg;
        } else if (arg instanceof Object[]) {
            argsList = Arrays.asList((Object[]) arg);
        } else if (arg == null) {
            throw new IllegalArgumentException("A null object was passed as a @DefineList parameter. " + "@DefineList is only supported on List and array arguments");
        } else {
            throw new IllegalArgumentException("A " + arg.getClass() + " object was passed as a @DefineList " + "parameter. @DefineList is only supported on List and array arguments");
        }
        if (argsList.isEmpty()) {
            throw new IllegalArgumentException("An empty list was passed as a @DefineList parameter. Can't define " + "an empty attribute.");
        }
        if (argsList.contains(null)) {
            throw new IllegalArgumentException("A @DefineList parameter was passed a list with null values in it.");
        }
        stmt.defineList(name, argsList);
    };
}
Also used : Arrays(java.util.Arrays) List(java.util.List) ParameterUtil(org.jdbi.v3.sqlobject.internal.ParameterUtil) Type(java.lang.reflect.Type) Parameter(java.lang.reflect.Parameter) SqlStatementCustomizerFactory(org.jdbi.v3.sqlobject.customizer.SqlStatementCustomizerFactory) Annotation(java.lang.annotation.Annotation) SqlStatementParameterCustomizer(org.jdbi.v3.sqlobject.customizer.SqlStatementParameterCustomizer) DefineList(org.jdbi.v3.sqlobject.customizer.DefineList) Method(java.lang.reflect.Method) DefineList(org.jdbi.v3.sqlobject.customizer.DefineList) List(java.util.List) DefineList(org.jdbi.v3.sqlobject.customizer.DefineList)

Example 3 with Arguments

use of org.jdbi.v3.core.argument.Arguments in project jdbi by jdbi.

the class Handle method select.

/**
 * Convenience method which creates a query with the given positional arguments
 * @param sql SQL or named statement
 * @param args arguments to bind positionally
 * @return query object
 */
public Query select(String sql, Object... args) {
    Query query = this.createQuery(sql);
    int position = 0;
    for (Object arg : args) {
        query.bind(position++, arg);
    }
    return query;
}
Also used : Query(org.jdbi.v3.core.statement.Query)

Example 4 with Arguments

use of org.jdbi.v3.core.argument.Arguments in project jdbi by jdbi.

the class RegisterObjectArgumentFactoryImpl method configureForType.

@Override
public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) {
    RegisterObjectArgumentFactory registerObjectArgumentFactory = (RegisterObjectArgumentFactory) annotation;
    Arguments arguments = registry.get(Arguments.class);
    Class<?> clazz = registerObjectArgumentFactory.value();
    int sqlType = registerObjectArgumentFactory.sqlType();
    if (sqlType == Integer.MIN_VALUE) {
        arguments.register(ObjectArgumentFactory.create(clazz));
    } else {
        arguments.register(ObjectArgumentFactory.create(clazz, sqlType));
    }
}
Also used : Arguments(org.jdbi.v3.core.argument.Arguments) RegisterObjectArgumentFactory(org.jdbi.v3.sqlobject.config.RegisterObjectArgumentFactory)

Example 5 with Arguments

use of org.jdbi.v3.core.argument.Arguments in project jdbi by jdbi.

the class RegisterArgumentFactoryImpl method configureForType.

@Override
public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) {
    RegisterArgumentFactory raf = (RegisterArgumentFactory) annotation;
    Arguments arguments = registry.get(Arguments.class);
    try {
        arguments.register(raf.value().newInstance());
    } catch (Exception e) {
        throw new IllegalStateException("unable to instantiate specified argument factory", e);
    }
}
Also used : RegisterArgumentFactory(org.jdbi.v3.sqlobject.config.RegisterArgumentFactory) Arguments(org.jdbi.v3.core.argument.Arguments)

Aggregations

Arguments (org.jdbi.v3.core.argument.Arguments)2 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 Type (java.lang.reflect.Type)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Query (org.jdbi.v3.core.statement.Query)1 Update (org.jdbi.v3.core.statement.Update)1 RegisterArgumentFactory (org.jdbi.v3.sqlobject.config.RegisterArgumentFactory)1 RegisterObjectArgumentFactory (org.jdbi.v3.sqlobject.config.RegisterObjectArgumentFactory)1 DefineList (org.jdbi.v3.sqlobject.customizer.DefineList)1 SqlStatementCustomizerFactory (org.jdbi.v3.sqlobject.customizer.SqlStatementCustomizerFactory)1 SqlStatementParameterCustomizer (org.jdbi.v3.sqlobject.customizer.SqlStatementParameterCustomizer)1 ParameterUtil (org.jdbi.v3.sqlobject.internal.ParameterUtil)1