Search in sources :

Example 6 with StatementContext

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

the class SqlQueryHandler method configureReturner.

@Override
void configureReturner(Query q, SqlObjectStatementConfiguration cfg) {
    UseRowMapper useRowMapper = getMethod().getAnnotation(UseRowMapper.class);
    UseRowReducer useRowReducer = getMethod().getAnnotation(UseRowReducer.class);
    if (useRowReducer != null && useRowMapper != null) {
        throw new IllegalStateException("Cannot declare @UseRowMapper and @UseRowReducer on the same method.");
    }
    cfg.setReturner(() -> {
        StatementContext ctx = q.getContext();
        Type elementType = magic.elementType(ctx);
        if (useRowReducer != null) {
            return magic.reducedResult(q.reduceRows(rowReducerFor(useRowReducer)), ctx);
        }
        ResultIterable<?> iterable = useRowMapper == null ? q.mapTo(elementType) : q.map(rowMapperFor(useRowMapper));
        return magic.mappedResult(iterable, ctx);
    });
}
Also used : UseRowMapper(org.jdbi.v3.sqlobject.statement.UseRowMapper) Type(java.lang.reflect.Type) UseRowReducer(org.jdbi.v3.sqlobject.statement.UseRowReducer) StatementContext(org.jdbi.v3.core.statement.StatementContext)

Example 7 with StatementContext

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

the class JpaMapper method specialize.

@Override
public RowMapper<C> specialize(ResultSet rs, StatementContext ctx) throws SQLException {
    Constructor<C> constructor;
    try {
        constructor = clazz.getDeclaredConstructor();
    } catch (ReflectiveOperationException e) {
        throw new EntityMemberAccessException("Unable to get constructor for " + clazz, e);
    }
    constructor.setAccessible(true);
    List<MemberSetter<C>> setters = new ArrayList<>();
    for (int colIndex = rs.getMetaData().getColumnCount(); colIndex >= 1; colIndex--) {
        String columnLabel = rs.getMetaData().getColumnLabel(colIndex);
        JpaMember member = jpaClass.lookupMember(columnLabel);
        if (member != null) {
            Type memberType = member.getType();
            ColumnMapper<?> columnMapper = ctx.findColumnMapperFor(memberType).orElseThrow(() -> new NoSuchMapperException("No column mapper for " + memberType));
            final int columnIndex = colIndex;
            setters.add(obj -> member.write(obj, columnMapper.map(rs, columnIndex, ctx)));
        }
    }
    return (r, c) -> {
        C obj;
        try {
            obj = constructor.newInstance();
        } catch (ReflectiveOperationException e) {
            throw new EntityMemberAccessException("Unable to invoke " + constructor, e);
        }
        for (MemberSetter<C> setter : setters) {
            setter.mapAndSetMember(obj);
        }
        return obj;
    };
}
Also used : SQLException(java.sql.SQLException) List(java.util.List) Type(java.lang.reflect.Type) ResultSet(java.sql.ResultSet) ColumnMapper(org.jdbi.v3.core.mapper.ColumnMapper) JpaMember(org.jdbi.v3.jpa.internal.JpaMember) NoSuchMapperException(org.jdbi.v3.core.mapper.NoSuchMapperException) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) RowMapper(org.jdbi.v3.core.mapper.RowMapper) StatementContext(org.jdbi.v3.core.statement.StatementContext) JpaClass(org.jdbi.v3.jpa.internal.JpaClass) ArrayList(java.util.ArrayList) NoSuchMapperException(org.jdbi.v3.core.mapper.NoSuchMapperException) Type(java.lang.reflect.Type) JpaMember(org.jdbi.v3.jpa.internal.JpaMember)

Example 8 with StatementContext

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

the class StatementContextTest method testMapperForDelegatesToRegistry.

@Test
public void testMapperForDelegatesToRegistry() {
    ColumnMapper<Foo> mapper = new FooMapper();
    ConfigRegistry config = new ConfigRegistry();
    config.get(ColumnMappers.class).register(mapper);
    final StatementContext context = StatementContextAccess.createContext(config);
    assertThat(context.findColumnMapperFor(Foo.class)).contains(mapper);
}
Also used : ConfigRegistry(org.jdbi.v3.core.config.ConfigRegistry) ColumnMappers(org.jdbi.v3.core.mapper.ColumnMappers) Test(org.junit.Test)

Example 9 with StatementContext

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

the class MessageRowMapper method map.

@Override
public M map(ResultSet rs, StatementContext ctx) throws SQLException {
    PMessageBuilder<M, F> builder = descriptor.builder();
    for (int i = 1; i <= rs.getMetaData().getColumnCount(); ++i) {
        if (!tableName.isEmpty() && !tableName.equalsIgnoreCase(rs.getMetaData().getTableName(i))) {
            continue;
        }
        String name = rs.getMetaData().getColumnLabel(i).toUpperCase(Locale.US);
        F field = fieldNameMapping.get(name);
        if (field != null) {
            int columnType = rs.getMetaData().getColumnType(i);
            switch(field.getType()) {
                case BOOL:
                    {
                        if (columnType == Types.BOOLEAN || columnType == Types.BIT) {
                            boolean b = rs.getBoolean(i);
                            if (!rs.wasNull()) {
                                builder.set(field, b);
                            }
                        } else {
                            int b = rs.getInt(i);
                            if (!rs.wasNull()) {
                                builder.set(field, b != 0);
                            }
                        }
                        break;
                    }
                case BYTE:
                    {
                        byte b = rs.getByte(i);
                        if (!rs.wasNull()) {
                            builder.set(field, b);
                        }
                        break;
                    }
                case I16:
                    {
                        short b = rs.getShort(i);
                        if (!rs.wasNull()) {
                            builder.set(field, b);
                        }
                        break;
                    }
                case I32:
                    {
                        if (columnType == Types.TIMESTAMP) {
                            Timestamp ts = rs.getTimestamp(i);
                            if (ts != null) {
                                builder.set(field, (int) (ts.getTime() / 1000L));
                            }
                        } else {
                            int b = rs.getInt(i);
                            if (!rs.wasNull()) {
                                builder.set(field, b);
                            }
                        }
                        break;
                    }
                case I64:
                    {
                        if (columnType == Types.TIMESTAMP) {
                            Timestamp ts = rs.getTimestamp(i);
                            if (ts != null) {
                                builder.set(field, ts.getTime());
                            }
                        } else {
                            long b = rs.getLong(i);
                            if (!rs.wasNull()) {
                                builder.set(field, b);
                            }
                        }
                        break;
                    }
                case DOUBLE:
                    {
                        double b = rs.getDouble(i);
                        if (!rs.wasNull()) {
                            builder.set(field, b);
                        }
                        break;
                    }
                case STRING:
                    {
                        builder.set(field, rs.getString(i));
                        break;
                    }
                case BINARY:
                    {
                        switch(columnType) {
                            case Types.BINARY:
                            case Types.VARBINARY:
                                byte[] ts = rs.getBytes(i);
                                if (ts != null) {
                                    builder.set(field, Binary.copy(ts));
                                }
                                break;
                            case Types.BLOB:
                                Blob blob = rs.getBlob(i);
                                if (blob != null) {
                                    try {
                                        builder.set(field, Binary.read(blob.getBinaryStream(), (int) blob.length()));
                                    } catch (IOException e) {
                                        throw new UncheckedIOException(e.getMessage(), e);
                                    }
                                }
                                break;
                            case Types.CHAR:
                            case Types.VARCHAR:
                            case Types.NCHAR:
                            case Types.NVARCHAR:
                                {
                                    String tmp = rs.getString(i);
                                    if (tmp != null) {
                                        builder.set(field, Binary.fromBase64(tmp));
                                    }
                                    break;
                                }
                            case Types.NULL:
                                break;
                            default:
                                throw new ResultSetException("Unknown column type " + rs.getMetaData().getColumnTypeName(i) + " for " + descriptor.getType().toString() + " field " + name + " in " + descriptor.getQualifiedName(), null, ctx);
                        }
                        break;
                    }
                case ENUM:
                    {
                        int val = rs.getInt(i);
                        if (!rs.wasNull()) {
                            PEnumDescriptor ed = (PEnumDescriptor) field.getDescriptor();
                            builder.set(field, ed.findById(val));
                        }
                        break;
                    }
                case MESSAGE:
                    {
                        try {
                            PMessageDescriptor<?, ?> md = (PMessageDescriptor) field.getDescriptor();
                            switch(columnType) {
                                case Types.BINARY:
                                case Types.VARBINARY:
                                    byte[] data = rs.getBytes(i);
                                    if (data != null) {
                                        ByteArrayInputStream in = new ByteArrayInputStream(data);
                                        builder.set(field, BINARY.deserialize(in, md));
                                    }
                                    break;
                                case Types.BLOB:
                                    {
                                        Blob blob = rs.getBlob(i);
                                        if (blob != null) {
                                            builder.set(field, BINARY.deserialize(blob.getBinaryStream(), md));
                                        }
                                        break;
                                    }
                                case Types.CHAR:
                                case Types.VARCHAR:
                                case Types.NCHAR:
                                case Types.NVARCHAR:
                                    {
                                        String tmp = rs.getString(i);
                                        if (tmp != null) {
                                            StringReader reader = new StringReader(tmp);
                                            builder.set(field, JSON.deserialize(reader, md));
                                        }
                                        break;
                                    }
                                case Types.CLOB:
                                    {
                                        Clob clob = rs.getClob(i);
                                        if (clob != null) {
                                            builder.set(field, JSON.deserialize(clob.getCharacterStream(), md));
                                        }
                                        break;
                                    }
                                case Types.NULL:
                                    break;
                                default:
                                    throw new ResultSetException("Unknown column type " + rs.getMetaData().getColumnTypeName(i) + " for " + descriptor.getType().toString() + " field " + name + " in " + descriptor.getQualifiedName(), null, ctx);
                            }
                        } catch (IOException e) {
                            throw new UncheckedIOException(e.getMessage(), e);
                        }
                        break;
                    }
                case LIST:
                case SET:
                case MAP:
                    {
                    // ... woot?
                    }
                case VOID:
                default:
                    {
                        throw new ResultSetException("Unhandled column of type " + rs.getMetaData().getColumnTypeName(i) + " for " + descriptor.getType().toString() + " field " + name + " in " + descriptor.getQualifiedName(), null, ctx);
                    }
            }
        }
    }
    return builder.build();
}
Also used : Blob(java.sql.Blob) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) PEnumDescriptor(net.morimekta.providence.descriptor.PEnumDescriptor) Timestamp(java.sql.Timestamp) ResultSetException(org.jdbi.v3.core.result.ResultSetException) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) PMessageDescriptor(net.morimekta.providence.descriptor.PMessageDescriptor) Clob(java.sql.Clob)

Example 10 with StatementContext

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

the class TimedAnnotationNameStrategy method getStatementName.

@Override
public String getStatementName(StatementContext statementContext) {
    final ExtensionMethod extensionMethod = statementContext.getExtensionMethod();
    if (extensionMethod == null) {
        return null;
    }
    final Class<?> clazz = extensionMethod.getType();
    final Timed classTimed = clazz.getAnnotation(Timed.class);
    final Method method = extensionMethod.getMethod();
    final Timed methodTimed = method.getAnnotation(Timed.class);
    // If the method is timed, figure out the name
    if (methodTimed != null) {
        String methodName = methodTimed.name().isEmpty() ? method.getName() : methodTimed.name();
        if (methodTimed.absolute()) {
            return methodName;
        } else {
            // We need to check if the class has a custom timer name
            return classTimed == null || classTimed.name().isEmpty() ? MetricRegistry.name(clazz, methodName) : MetricRegistry.name(classTimed.name(), methodName);
        }
    } else if (classTimed != null) {
        // Maybe the class is timed?
        return classTimed.name().isEmpty() ? MetricRegistry.name(clazz, method.getName()) : MetricRegistry.name(classTimed.name(), method.getName());
    } else {
        // No timers neither on the method or the class
        return null;
    }
}
Also used : Timed(com.codahale.metrics.annotation.Timed) ExtensionMethod(org.jdbi.v3.core.extension.ExtensionMethod) Method(java.lang.reflect.Method) ExtensionMethod(org.jdbi.v3.core.extension.ExtensionMethod)

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