Search in sources :

Example 1 with ResultSetException

use of org.jdbi.v3.core.result.ResultSetException 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 2 with ResultSetException

use of org.jdbi.v3.core.result.ResultSetException in project providence by morimekta.

the class MessageFieldArgument method apply.

@Override
@SuppressWarnings("unchecked")
public void apply(int position, PreparedStatement statement, StatementContext ctx) throws SQLException {
    if (message.has(field)) {
        switch(field.getType()) {
            case BOOL:
                {
                    boolean value = message.get(field);
                    if (type == Types.BOOLEAN || type == Types.BIT) {
                        statement.setBoolean(position, value);
                    } else {
                        statement.setInt(position, value ? 1 : 0);
                    }
                    break;
                }
            case BYTE:
                {
                    statement.setByte(position, message.get(field));
                    break;
                }
            case I16:
                {
                    statement.setShort(position, message.get(field));
                    break;
                }
            case I32:
                {
                    if (type == Types.TIMESTAMP) {
                        Timestamp timestamp = new Timestamp(1000L * (int) message.get(field));
                        statement.setTimestamp(position, timestamp);
                    } else {
                        statement.setInt(position, message.get(field));
                    }
                    break;
                }
            case I64:
                {
                    if (type == Types.TIMESTAMP) {
                        Timestamp timestamp = new Timestamp(message.get(field));
                        statement.setTimestamp(position, timestamp);
                    } else {
                        statement.setLong(position, message.get(field));
                    }
                    break;
                }
            case DOUBLE:
                {
                    statement.setDouble(position, message.get(field));
                    break;
                }
            case STRING:
                {
                    statement.setString(position, message.get(field));
                    break;
                }
            case BINARY:
                {
                    Binary binary = message.get(field);
                    switch(type) {
                        case Types.BINARY:
                        case Types.VARBINARY:
                            {
                                statement.setBytes(position, binary.get());
                                break;
                            }
                        case Types.BLOB:
                            {
                                statement.setBlob(position, binary.getInputStream());
                                break;
                            }
                        case Types.CHAR:
                        case Types.VARCHAR:
                        case Types.NCHAR:
                        case Types.NVARCHAR:
                            {
                                statement.setString(position, binary.toBase64());
                                break;
                            }
                        default:
                            throw new ResultSetException("Unknown binary field type: " + type + " for " + field, null, ctx);
                    }
                    break;
                }
            case ENUM:
                {
                    PEnumValue value = message.get(field);
                    statement.setInt(position, value.asInteger());
                    break;
                }
            case MESSAGE:
                {
                    PMessage value = message.get(field);
                    switch(type) {
                        case Types.BINARY:
                        case Types.VARBINARY:
                            {
                                ByteArrayOutputStream out = new ByteArrayOutputStream();
                                try {
                                    BINARY.serialize(out, value);
                                    statement.setBytes(position, out.toByteArray());
                                } catch (IOException e) {
                                    throw new ResultSetException(e.getMessage(), e, ctx);
                                }
                                break;
                            }
                        case Types.BLOB:
                            {
                                ByteArrayOutputStream out = new ByteArrayOutputStream();
                                try {
                                    BINARY.serialize(out, value);
                                    statement.setBlob(position, new ByteArrayInputStream(out.toByteArray()));
                                } catch (IOException e) {
                                    throw new ResultSetException(e.getMessage(), e, ctx);
                                }
                                break;
                            }
                        case Types.CHAR:
                        case Types.VARCHAR:
                        case Types.NCHAR:
                        case Types.NVARCHAR:
                            {
                                StringWriter writer = new StringWriter();
                                try {
                                    JSON.serialize(new PrintWriter(writer), value);
                                    statement.setString(position, writer.getBuffer().toString());
                                } catch (IOException e) {
                                    throw new ResultSetException(e.getMessage(), e, ctx);
                                }
                                break;
                            }
                        case Types.CLOB:
                            {
                                StringWriter writer = new StringWriter();
                                try {
                                    JSON.serialize(new PrintWriter(writer), value);
                                    statement.setClob(position, new StringReader(writer.getBuffer().toString()));
                                } catch (IOException e) {
                                    throw new ResultSetException(e.getMessage(), e, ctx);
                                }
                                break;
                            }
                        default:
                            throw new ResultSetException("Unknown message field type: " + type + " for " + field, null, ctx);
                    }
                    break;
                }
            default:
                throw new ResultSetException("Unhandled field type in SQL: " + field, null, ctx);
        }
    } else {
        statement.setNull(position, type);
    }
}
Also used : ResultSetException(org.jdbi.v3.core.result.ResultSetException) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) PMessage(net.morimekta.providence.PMessage) StringReader(java.io.StringReader) PEnumValue(net.morimekta.providence.PEnumValue) Binary(net.morimekta.util.Binary) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Timestamp(java.sql.Timestamp) PrintWriter(java.io.PrintWriter)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 Timestamp (java.sql.Timestamp)2 ResultSetException (org.jdbi.v3.core.result.ResultSetException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 UncheckedIOException (java.io.UncheckedIOException)1 Blob (java.sql.Blob)1 Clob (java.sql.Clob)1 PEnumValue (net.morimekta.providence.PEnumValue)1 PMessage (net.morimekta.providence.PMessage)1 PEnumDescriptor (net.morimekta.providence.descriptor.PEnumDescriptor)1 PMessageDescriptor (net.morimekta.providence.descriptor.PMessageDescriptor)1 Binary (net.morimekta.util.Binary)1