Search in sources :

Example 1 with Value

use of org.adbcj.Value in project adbcj by mheath.

the class TransactionTest method testRollback.

public void testRollback() throws Exception {
    Connection connection = connectionManager.connect().get();
    try {
        // Clear out updates table
        Result result = connection.executeUpdate("DELETE FROM updates").get();
        assertNotNull(result);
        // Make sure updates is empty
        ResultSet rs = connection.executeQuery("SELECT id FROM updates").get();
        assertNotNull(rs);
        assertEquals(rs.size(), 0);
        connection.beginTransaction();
        // Insert a row
        result = connection.executeUpdate("INSERT INTO updates (id) VALUES (1)").get();
        assertNotNull(result);
        assertEquals(result.getAffectedRows(), Long.valueOf(1));
        // Make sure we can select the row
        rs = connection.executeQuery("SELECT id FROM updates").get();
        assertNotNull(rs);
        assertEquals(rs.size(), 1);
        Value value = rs.get(0).get(0);
        assertEquals(value.getInt(), 1);
        // Rollback transaction
        connection.rollback().get();
        // select query should now be empty
        rs = connection.executeQuery("SELECT id FROM updates").get();
        assertNotNull(rs);
        assertEquals(rs.size(), 0);
    } finally {
        connection.close(true);
    }
}
Also used : Connection(org.adbcj.Connection) ResultSet(org.adbcj.ResultSet) Value(org.adbcj.Value) Result(org.adbcj.Result)

Example 2 with Value

use of org.adbcj.Value in project adbcj by mheath.

the class ProtocolHandler method doDataRow.

private void doDataRow(AbstractConnection connection, DataRowMessage dataRowMessage) {
    AbstractDbSession.Request<Object> request = connection.getActiveRequest();
    if (request == null) {
        throw new IllegalStateException("Received a data row without an active request");
    }
    logger.debug("Received data row for request {}", request);
    request.getEventHandler().startRow(request.getAccumulator());
    for (Value value : dataRowMessage.getValues()) {
        request.getEventHandler().value(value, request.getAccumulator());
    }
    request.getEventHandler().endRow(request.getAccumulator());
}
Also used : AbstractDbSession(org.adbcj.support.AbstractDbSession) Value(org.adbcj.Value)

Example 3 with Value

use of org.adbcj.Value in project adbcj by mheath.

the class UpdateTest method testSimpleUpdates.

public void testSimpleUpdates() throws InterruptedException {
    Connection connection = connectionManager.connect().get();
    assertNotNull(connection);
    // Clear out updates table
    Result result = connection.executeUpdate("DELETE FROM updates").get();
    assertNotNull(result);
    // Insert a row
    result = connection.executeUpdate("INSERT INTO updates (id) VALUES (1)").get();
    assertNotNull(result);
    assertEquals(result.getAffectedRows(), Long.valueOf(1));
    // Select the row
    ResultSet rs = connection.executeQuery("SELECT id FROM updates").get();
    assertNotNull(rs);
    assertEquals(rs.size(), 1);
    Value value = rs.get(0).get(0);
    assertEquals(value.getInt(), 1);
    assertEquals(value.getField().getColumnLabel(), "id");
    // Update nothing
    result = connection.executeUpdate("UPDATE updates SET id=1 WHERE id=2").get();
    assertNotNull(result);
    assertEquals(result.getAffectedRows(), Long.valueOf(0));
    // Update inserted row
    result = connection.executeUpdate("UPDATE updates SET id=2").get();
    assertNotNull(result);
    assertEquals(result.getAffectedRows(), Long.valueOf(1));
    // Delete inserted row
    result = connection.executeUpdate("DELETE FROM updates WHERE id=2").get();
    assertNotNull(result);
    assertEquals(result.getAffectedRows(), Long.valueOf(1));
}
Also used : Connection(org.adbcj.Connection) ResultSet(org.adbcj.ResultSet) Value(org.adbcj.Value) Result(org.adbcj.Result)

Example 4 with Value

use of org.adbcj.Value in project adbcj by mheath.

the class MySqlClientDecoder method doDecode.

protected ServerPacket doDecode(InputStream input, boolean block) throws IOException {
    // If we can't block, make sure there's enough data available to read
    if (!block) {
        if (input.available() < 3) {
            return null;
        }
    }
    // Read the packet length
    final int length = IoUtils.readUnsignedMediumInt(input);
    // If we can't block, make sure the stream has enough data
    if (!block) {
        // Make sure we have enough data for the packet length and the packet number
        if (input.available() < length + 1) {
            return null;
        }
    }
    final int packetNumber = IoUtils.safeRead(input);
    BoundedInputStream in = new BoundedInputStream(input, length);
    boolean threwException = false;
    try {
        logger.trace("Decoding in state {}", state);
        switch(state) {
            case CONNECTING:
                ServerGreeting serverGreeting = decodeServerGreeting(in, length, packetNumber);
                state = State.RESPONSE;
                return serverGreeting;
            case RESPONSE:
                int fieldCount = in.read();
                if (fieldCount == RESPONSE_OK) {
                    // Create Ok response
                    return decodeOkResponse(in, length, packetNumber);
                }
                if (fieldCount == RESPONSE_ERROR) {
                    // Create error response
                    ErrorResponse response = decodeErrorResponse(in, length, packetNumber);
                    return response;
                }
                if (fieldCount == RESPONSE_EOF) {
                    throw new IllegalStateException("Did not expect an EOF response from the server");
                }
                // Must be receiving result set header
                // Get the number of fields. The largest this can be is a 24-bit
                // integer so cast to int is ok
                expectedFieldPackets = (int) IoUtils.readBinaryLengthEncoding(in, fieldCount);
                fields = new MysqlField[expectedFieldPackets];
                logger.trace("Field count {}", expectedFieldPackets);
                Long extra = null;
                if (in.getRemaining() > 0) {
                    extra = IoUtils.readBinaryLengthEncoding(in);
                }
                state = State.FIELD;
                return new ResultSetResponse(length, packetNumber, expectedFieldPackets, extra);
            case FIELD:
                ResultSetFieldResponse resultSetFieldResponse = decodeFieldResponse(in, length, packetNumber);
                expectedFieldPackets--;
                logger.trace("fieldPacketCount: {}", expectedFieldPackets);
                if (expectedFieldPackets == 0) {
                    state = State.FIELD_EOF;
                }
                return resultSetFieldResponse;
            case FIELD_EOF:
                fieldCount = in.read();
                if (fieldCount != RESPONSE_EOF) {
                    throw new IllegalStateException("Expected an EOF response from the server");
                }
                EofResponse fieldEof = decodeEofResponse(in, length, packetNumber, EofResponse.Type.FIELD);
                state = State.ROW;
                fieldIndex = 0;
                return fieldEof;
            case ROW:
                // This is only for checking for EOF
                fieldCount = in.read();
                if (fieldCount == RESPONSE_EOF) {
                    EofResponse rowEof = decodeEofResponse(in, length, packetNumber, EofResponse.Type.ROW);
                    state = State.RESPONSE;
                    return rowEof;
                }
                Value[] values = new Value[fields.length];
                for (int i = 0; i < fields.length; ) {
                    MysqlField field = fields[i++];
                    Object value = null;
                    if (fieldCount != IoUtils.NULL_VALUE) {
                        // We will have to move this as some datatypes will not be sent across the wire as strings
                        String strVal = IoUtils.readLengthCodedString(in, fieldCount, charset);
                        // TODO add decoding for all column types
                        switch(field.getColumnType()) {
                            case TINYINT:
                                value = Byte.valueOf(strVal);
                                break;
                            case INTEGER:
                            case BIGINT:
                                value = Long.valueOf(strVal);
                                break;
                            case VARCHAR:
                                value = strVal;
                                break;
                            default:
                                throw new IllegalStateException("Don't know how to handle column type of " + field.getColumnType());
                        }
                    }
                    values[field.getIndex()] = new DefaultValue(field, value);
                    if (i < fields.length) {
                        fieldCount = in.read();
                    }
                }
                return new ResultSetRowResponse(length, packetNumber, values);
            default:
                throw new IllegalStateException("Unkown decoder state " + state);
        }
    } catch (IOException e) {
        threwException = true;
        throw e;
    } catch (RuntimeException e) {
        threwException = true;
        throw e;
    } finally {
        if (!threwException && in.getRemaining() > 0) {
            throw new IllegalStateException("Buffer underrun occured; remaining bytes: " + in.getRemaining());
        }
    }
}
Also used : IOException(java.io.IOException) DefaultValue(org.adbcj.support.DefaultValue) Value(org.adbcj.Value) DefaultValue(org.adbcj.support.DefaultValue)

Example 5 with Value

use of org.adbcj.Value 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)

Aggregations

Value (org.adbcj.Value)8 ResultSet (org.adbcj.ResultSet)3 Connection (org.adbcj.Connection)2 Result (org.adbcj.Result)2 DefaultValue (org.adbcj.support.DefaultValue)2 IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1 AbstractMap (java.util.AbstractMap)1 HashSet (java.util.HashSet)1 Field (org.adbcj.Field)1 PgField (org.adbcj.postgresql.codec.PgField)1 AbstractDbSession (org.adbcj.support.AbstractDbSession)1