Search in sources :

Example 1 with PgField

use of org.adbcj.postgresql.codec.PgField in project adbcj by mheath.

the class BackendMessageDecoderTest method rowDescription.

@Test
public void rowDescription() throws Exception {
    RowDescriptionMessage message = decode(ROW_DESCRIPTION);
    Assert.assertEquals(message.getType(), BackendMessageType.ROW_DESCRIPTION);
    Assert.assertEquals(message.getFields().length, 1);
    PgField field = message.getFields()[0];
    Assert.assertEquals(field.getColumnLabel(), "id");
    Assert.assertEquals(field.getColumnType(), Type.INTEGER);
}
Also used : PgField(org.adbcj.postgresql.codec.PgField) Test(org.testng.annotations.Test)

Example 2 with PgField

use of org.adbcj.postgresql.codec.PgField in project adbcj by mheath.

the class BackendMessageDecoder method decodeDataRow.

private DataRowMessage decodeDataRow(DecoderInputStream input) throws IOException {
    Charset charset = connectionState.getBackendCharset();
    PgField[] fields = connectionState.getCurrentResultSetFields();
    if (fields == null) {
        throw new IllegalStateException("Received a data row without any field definitions in the request payload");
    }
    int fieldCount = input.readUnsignedShort();
    Value[] values = new Value[fieldCount];
    for (int i = 0; i < fieldCount; i++) {
        int valueLength = input.readInt();
        PgField field = fields[i];
        Value value;
        if (valueLength < 0) {
            value = new DefaultValue(field, null);
        } else {
            String strVal;
            switch(field.getColumnType()) {
                case INTEGER:
                    switch(field.getFormatCode()) {
                        case BINARY:
                            value = new DefaultValue(field, input.readInt());
                            break;
                        case TEXT:
                            strVal = input.readString(valueLength, charset);
                            value = new DefaultValue(field, Integer.valueOf(strVal));
                            break;
                        default:
                            throw new IllegalStateException("Unable to decode format of " + field.getFormatCode());
                    }
                    break;
                case BIGINT:
                    switch(field.getFormatCode()) {
                        case BINARY:
                            value = new DefaultValue(field, (long) input.readInt() << 32 | input.readInt());
                            break;
                        case TEXT:
                            strVal = input.readString(valueLength, charset);
                            value = new DefaultValue(field, Long.valueOf(strVal));
                            break;
                        default:
                            throw new IllegalStateException("Unable to decode format of " + field.getFormatCode());
                    }
                    break;
                case VARCHAR:
                    strVal = input.readString(valueLength, charset);
                    value = new DefaultValue(field, strVal);
                    break;
                default:
                    // Advance buffer
                    input.skip(valueLength);
                    // TODO Handle remaining ADBCJ types
                    throw new IllegalStateException("Unable to decode column of type " + field.getColumnType());
            }
        }
        values[i] = value;
    }
    return new DataRowMessage(values);
}
Also used : DefaultValue(org.adbcj.support.DefaultValue) PgField(org.adbcj.postgresql.codec.PgField) DefaultValue(org.adbcj.support.DefaultValue) Value(org.adbcj.Value) Charset(java.nio.charset.Charset)

Example 3 with PgField

use of org.adbcj.postgresql.codec.PgField in project adbcj by mheath.

the class BackendMessageDecoder method decodeRowDescription.

private AbstractBackendMessage decodeRowDescription(DecoderInputStream input) throws IOException {
    Charset charset = connectionState.getBackendCharset();
    int fieldCount = input.readUnsignedShort();
    PgField[] fields = new PgField[fieldCount];
    for (int i = 0; i < fieldCount; i++) {
        String name = input.readString(charset);
        int tableOid = input.readInt();
        int columnAttributeNumber = input.readUnsignedShort();
        int typeOid = input.readInt();
        short typeSize = input.readShort();
        int typeModifier = input.readInt();
        FormatCode code = FormatCode.values()[input.readShort()];
        Type type;
        switch(typeOid) {
            case PgFieldType.BOOLEAN:
                type = Type.BOOLEAN;
                break;
            case PgFieldType.BIGINT:
                type = Type.BIGINT;
                break;
            case PgFieldType.CHAR:
                type = Type.CHAR;
                break;
            case PgFieldType.DATE:
                type = Type.DATE;
                break;
            case PgFieldType.DOUBLE:
                type = Type.DOUBLE;
                break;
            case PgFieldType.INTEGER:
                type = Type.INTEGER;
                break;
            case PgFieldType.REAL:
                type = Type.REAL;
                break;
            case PgFieldType.SMALLINT:
                type = Type.SMALLINT;
                break;
            case PgFieldType.VARCHAR:
                type = Type.VARCHAR;
                break;
            default:
                // TODO Convert more typeOids to ADBCJ types
                throw new IllegalStateException("Unable to handle field type with oid " + typeOid);
        }
        fields[i] = new PgField(i, connectionState.getDatabaseName(), type, name, tableOid, columnAttributeNumber, code, typeSize, typeModifier);
        logger.debug("Setting fields for current result set: {}", fields);
        connectionState.setCurrentResultSetFields(fields);
    }
    return new RowDescriptionMessage(fields);
}
Also used : PgField(org.adbcj.postgresql.codec.PgField) PgFieldType(org.adbcj.postgresql.codec.PgFieldType) Type(org.adbcj.Type) FormatCode(org.adbcj.postgresql.codec.FormatCode) Charset(java.nio.charset.Charset)

Aggregations

PgField (org.adbcj.postgresql.codec.PgField)3 Charset (java.nio.charset.Charset)2 Type (org.adbcj.Type)1 Value (org.adbcj.Value)1 FormatCode (org.adbcj.postgresql.codec.FormatCode)1 PgFieldType (org.adbcj.postgresql.codec.PgFieldType)1 DefaultValue (org.adbcj.support.DefaultValue)1 Test (org.testng.annotations.Test)1