Search in sources :

Example 11 with StatementContext

use of org.jdbi.v3.core.statement.StatementContext 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)

Example 12 with StatementContext

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

the class JoinRowMapper method specialize.

@Override
public RowMapper<JoinRow> specialize(ResultSet r, StatementContext ctx) throws SQLException {
    RowMapper<?>[] mappers = new RowMapper[types.length];
    for (int i = 0; i < types.length; i++) {
        Type type = types[i];
        mappers[i] = ctx.findRowMapperFor(type).orElseThrow(() -> new IllegalArgumentException("No row mapper registered for " + type)).specialize(r, ctx);
    }
    return (rs, context) -> {
        final Map<Type, Object> entries = new HashMap<>(types.length);
        for (int i = 0; i < types.length; i++) {
            Type type = types[i];
            RowMapper<?> mapper = mappers[i];
            entries.put(type, mapper.map(r, ctx));
        }
        return new JoinRow(entries);
    };
}
Also used : SQLException(java.sql.SQLException) Type(java.lang.reflect.Type) ResultSet(java.sql.ResultSet) Map(java.util.Map) HashMap(java.util.HashMap) StatementContext(org.jdbi.v3.core.statement.StatementContext) Type(java.lang.reflect.Type) Map(java.util.Map) HashMap(java.util.HashMap)

Example 13 with StatementContext

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

the class ConstructorMapper method specialize0.

private RowMapper<T> specialize0(ResultSet rs, StatementContext ctx, List<String> columnNames, List<ColumnNameMatcher> columnNameMatchers, List<String> unmatchedColumns) throws SQLException {
    final int count = constructor.getParameterCount();
    final Parameter[] parameters = constructor.getParameters();
    final RowMapper<?>[] mappers = new RowMapper<?>[count];
    for (int i = 0; i < count; i++) {
        final Parameter parameter = parameters[i];
        Nested anno = parameter.getAnnotation(Nested.class);
        if (anno == null) {
            final String paramName = prefix + paramName(parameters, i, constructorProperties);
            final int columnIndex = findColumnIndex(paramName, columnNames, columnNameMatchers, () -> debugName(parameter)).orElseThrow(() -> new IllegalArgumentException(String.format("Constructor '%s' parameter '%s' has no column in the result set. " + "Verify that the Java compiler is configured to emit parameter names, " + "that your result set has the columns expected, or annotate the " + "parameter names explicitly with @ColumnName", constructor, paramName)));
            final Type type = parameter.getParameterizedType();
            mappers[i] = ctx.findColumnMapperFor(type).map(mapper -> new SingleColumnMapper(mapper, columnIndex + 1)).orElseThrow(() -> new IllegalArgumentException(String.format("Could not find column mapper for type '%s' of parameter '%s' for constructor '%s'", type, paramName, constructor)));
            unmatchedColumns.remove(columnNames.get(columnIndex));
        } else {
            String nestedPrefix = prefix + anno.value();
            mappers[i] = nestedMappers.computeIfAbsent(parameter, p -> new ConstructorMapper<>(findConstructorFor(p.getType()), nestedPrefix)).specialize0(rs, ctx, columnNames, columnNameMatchers, unmatchedColumns);
        }
    }
    return (r, c) -> {
        final Object[] params = new Object[count];
        for (int i = 0; i < count; i++) {
            params[i] = mappers[i].map(r, c);
        }
        return construct(params);
    };
}
Also used : ReflectionMapperUtil.getColumnNames(org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.getColumnNames) RowMapperFactory(org.jdbi.v3.core.mapper.RowMapperFactory) ConstructorProperties(java.beans.ConstructorProperties) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Nested(org.jdbi.v3.core.mapper.Nested) SingleColumnMapper(org.jdbi.v3.core.mapper.SingleColumnMapper) Constructor(java.lang.reflect.Constructor) JdbiConstructors.findConstructorFor(org.jdbi.v3.core.mapper.reflect.JdbiConstructors.findConstructorFor) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) StatementContext(org.jdbi.v3.core.statement.StatementContext) SQLException(java.sql.SQLException) List(java.util.List) Type(java.lang.reflect.Type) Parameter(java.lang.reflect.Parameter) ResultSet(java.sql.ResultSet) Map(java.util.Map) ReflectionMapperUtil.findColumnIndex(org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.findColumnIndex) RowMapper(org.jdbi.v3.core.mapper.RowMapper) Type(java.lang.reflect.Type) SingleColumnMapper(org.jdbi.v3.core.mapper.SingleColumnMapper) Nested(org.jdbi.v3.core.mapper.Nested) Parameter(java.lang.reflect.Parameter) RowMapper(org.jdbi.v3.core.mapper.RowMapper)

Example 14 with StatementContext

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

the class FieldMapper method specialize0.

private RowMapper<T> specialize0(ResultSet rs, StatementContext ctx, List<String> columnNames, List<ColumnNameMatcher> columnNameMatchers, List<String> unmatchedColumns) throws SQLException {
    final List<RowMapper<?>> mappers = new ArrayList<>();
    final List<Field> fields = new ArrayList<>();
    for (Class<?> aType = type; aType != null; aType = aType.getSuperclass()) {
        for (Field field : aType.getDeclaredFields()) {
            Nested anno = field.getAnnotation(Nested.class);
            if (anno == null) {
                String paramName = prefix + paramName(field);
                findColumnIndex(paramName, columnNames, columnNameMatchers, () -> debugName(field)).ifPresent(index -> {
                    Type type = field.getGenericType();
                    ColumnMapper<?> mapper = ctx.findColumnMapperFor(type).orElse((r, n, c) -> rs.getObject(n));
                    mappers.add(new SingleColumnMapper(mapper, index + 1));
                    fields.add(field);
                    unmatchedColumns.remove(columnNames.get(index));
                });
            } else {
                String nestedPrefix = prefix + anno.value().toLowerCase();
                RowMapper<?> mapper = nestedMappers.computeIfAbsent(field, f -> new FieldMapper<>(field.getType(), nestedPrefix)).specialize0(rs, ctx, columnNames, columnNameMatchers, unmatchedColumns);
                mappers.add(mapper);
                fields.add(field);
            }
        }
    }
    if (mappers.isEmpty() && columnNames.size() > 0) {
        throw new IllegalArgumentException(String.format("Mapping fields for type %s " + "didn't find any matching columns in result set", type));
    }
    return (r, c) -> {
        T obj = construct();
        for (int i = 0; i < mappers.size(); i++) {
            RowMapper<?> mapper = mappers.get(i);
            Field field = fields.get(i);
            Object value = mapper.map(rs, ctx);
            writeField(obj, field, value);
        }
        return obj;
    };
}
Also used : ReflectionMapperUtil.getColumnNames(org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.getColumnNames) RowMapperFactory(org.jdbi.v3.core.mapper.RowMapperFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Nested(org.jdbi.v3.core.mapper.Nested) Field(java.lang.reflect.Field) SingleColumnMapper(org.jdbi.v3.core.mapper.SingleColumnMapper) ArrayList(java.util.ArrayList) StatementContext(org.jdbi.v3.core.statement.StatementContext) SQLException(java.sql.SQLException) List(java.util.List) Type(java.lang.reflect.Type) ResultSet(java.sql.ResultSet) Map(java.util.Map) ReflectionMapperUtil.findColumnIndex(org.jdbi.v3.core.mapper.reflect.ReflectionMapperUtil.findColumnIndex) ColumnMapper(org.jdbi.v3.core.mapper.ColumnMapper) Optional(java.util.Optional) RowMapper(org.jdbi.v3.core.mapper.RowMapper) SingleColumnMapper(org.jdbi.v3.core.mapper.SingleColumnMapper) ArrayList(java.util.ArrayList) Nested(org.jdbi.v3.core.mapper.Nested) Field(java.lang.reflect.Field) Type(java.lang.reflect.Type) RowMapper(org.jdbi.v3.core.mapper.RowMapper)

Example 15 with StatementContext

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

the class StatementContext method close.

@Override
public void close() {
    SQLException exception = null;
    try {
        List<Cleanable> cleanables = new ArrayList<>(this.cleanables);
        this.cleanables.clear();
        Collections.reverse(cleanables);
        for (Cleanable cleanable : cleanables) {
            try {
                cleanable.close();
            } catch (SQLException e) {
                if (exception == null) {
                    exception = e;
                } else {
                    exception.addSuppressed(e);
                }
            }
        }
    } finally {
        if (exception != null) {
            throw new CloseException("Exception thrown while cleaning StatementContext", exception);
        }
    }
}
Also used : SQLException(java.sql.SQLException) CloseException(org.jdbi.v3.core.CloseException) ArrayList(java.util.ArrayList)

Aggregations

StatementContext (org.jdbi.v3.core.statement.StatementContext)9 Type (java.lang.reflect.Type)7 SQLException (java.sql.SQLException)7 ResultSet (java.sql.ResultSet)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 List (java.util.List)4 RowMapper (org.jdbi.v3.core.mapper.RowMapper)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ColumnMapper (org.jdbi.v3.core.mapper.ColumnMapper)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 Constructor (java.lang.reflect.Constructor)2 Field (java.lang.reflect.Field)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Timestamp (java.sql.Timestamp)2 PMessage (net.morimekta.providence.PMessage)2 PMessageDescriptor (net.morimekta.providence.descriptor.PMessageDescriptor)2 NullArgument (org.jdbi.v3.core.argument.NullArgument)2