Search in sources :

Example 16 with Transfer

use of org.h2.value.Transfer in project h2database by h2database.

the class CommandRemote method executeUpdate.

@Override
public ResultWithGeneratedKeys executeUpdate(Object generatedKeysRequest) {
    checkParameters();
    boolean supportsGeneratedKeys = session.isSupportsGeneratedKeys();
    boolean readGeneratedKeys = supportsGeneratedKeys && !Boolean.FALSE.equals(generatedKeysRequest);
    int objectId = readGeneratedKeys ? session.getNextId() : 0;
    synchronized (session) {
        int updateCount = 0;
        ResultRemote generatedKeys = null;
        boolean autoCommit = false;
        for (int i = 0, count = 0; i < transferList.size(); i++) {
            prepareIfRequired();
            Transfer transfer = transferList.get(i);
            try {
                session.traceOperation("COMMAND_EXECUTE_UPDATE", id);
                transfer.writeInt(SessionRemote.COMMAND_EXECUTE_UPDATE).writeInt(id);
                sendParameters(transfer);
                if (supportsGeneratedKeys) {
                    int mode = GeneratedKeysMode.valueOf(generatedKeysRequest);
                    transfer.writeInt(mode);
                    switch(mode) {
                        case GeneratedKeysMode.COLUMN_NUMBERS:
                            {
                                int[] keys = (int[]) generatedKeysRequest;
                                transfer.writeInt(keys.length);
                                for (int key : keys) {
                                    transfer.writeInt(key);
                                }
                                break;
                            }
                        case GeneratedKeysMode.COLUMN_NAMES:
                            {
                                String[] keys = (String[]) generatedKeysRequest;
                                transfer.writeInt(keys.length);
                                for (String key : keys) {
                                    transfer.writeString(key);
                                }
                                break;
                            }
                    }
                }
                session.done(transfer);
                updateCount = transfer.readInt();
                autoCommit = transfer.readBoolean();
                if (readGeneratedKeys) {
                    int columnCount = transfer.readInt();
                    if (generatedKeys != null) {
                        generatedKeys.close();
                        generatedKeys = null;
                    }
                    generatedKeys = new ResultRemote(session, transfer, objectId, columnCount, Integer.MAX_VALUE);
                }
            } catch (IOException e) {
                session.removeServer(e, i--, ++count);
            }
        }
        session.setAutoCommitFromServer(autoCommit);
        session.autoCommitIfCluster();
        session.readSessionState();
        if (generatedKeys != null) {
            return new ResultWithGeneratedKeys.WithKeys(updateCount, generatedKeys);
        }
        return ResultWithGeneratedKeys.of(updateCount);
    }
}
Also used : ResultRemote(org.h2.result.ResultRemote) Transfer(org.h2.value.Transfer) IOException(java.io.IOException)

Example 17 with Transfer

use of org.h2.value.Transfer in project h2database by h2database.

the class CommandRemote method executeQuery.

@Override
public ResultInterface executeQuery(int maxRows, boolean scrollable) {
    checkParameters();
    synchronized (session) {
        int objectId = session.getNextId();
        ResultRemote result = null;
        for (int i = 0, count = 0; i < transferList.size(); i++) {
            prepareIfRequired();
            Transfer transfer = transferList.get(i);
            try {
                session.traceOperation("COMMAND_EXECUTE_QUERY", id);
                transfer.writeInt(SessionRemote.COMMAND_EXECUTE_QUERY).writeInt(id).writeInt(objectId).writeInt(maxRows);
                int fetch;
                if (session.isClustered() || scrollable) {
                    fetch = Integer.MAX_VALUE;
                } else {
                    fetch = fetchSize;
                }
                transfer.writeInt(fetch);
                sendParameters(transfer);
                session.done(transfer);
                int columnCount = transfer.readInt();
                if (result != null) {
                    result.close();
                    result = null;
                }
                result = new ResultRemote(session, transfer, objectId, columnCount, fetch);
                if (readonly) {
                    break;
                }
            } catch (IOException e) {
                session.removeServer(e, i--, ++count);
            }
        }
        session.autoCommitIfCluster();
        session.readSessionState();
        return result;
    }
}
Also used : ResultRemote(org.h2.result.ResultRemote) Transfer(org.h2.value.Transfer) IOException(java.io.IOException)

Example 18 with Transfer

use of org.h2.value.Transfer in project h2database by h2database.

the class CommandRemote method sendParameters.

private void sendParameters(Transfer transfer) throws IOException {
    int len = parameters.size();
    transfer.writeInt(len);
    for (ParameterInterface p : parameters) {
        Value pVal = p.getParamValue();
        if (pVal == null && cmdType == EXPLAIN) {
            pVal = ValueNull.INSTANCE;
        }
        transfer.writeValue(pVal);
    }
}
Also used : ParameterInterface(org.h2.expression.ParameterInterface) Value(org.h2.value.Value)

Example 19 with Transfer

use of org.h2.value.Transfer in project h2database by h2database.

the class CommandRemote method getMetaData.

@Override
public ResultInterface getMetaData() {
    synchronized (session) {
        if (!isQuery) {
            return null;
        }
        int objectId = session.getNextId();
        ResultRemote result = null;
        for (int i = 0, count = 0; i < transferList.size(); i++) {
            prepareIfRequired();
            Transfer transfer = transferList.get(i);
            try {
                session.traceOperation("COMMAND_GET_META_DATA", id);
                transfer.writeInt(SessionRemote.COMMAND_GET_META_DATA).writeInt(id).writeInt(objectId);
                session.done(transfer);
                int columnCount = transfer.readInt();
                result = new ResultRemote(session, transfer, objectId, columnCount, Integer.MAX_VALUE);
                break;
            } catch (IOException e) {
                session.removeServer(e, i--, ++count);
            }
        }
        session.autoCommitIfCluster();
        return result;
    }
}
Also used : ResultRemote(org.h2.result.ResultRemote) Transfer(org.h2.value.Transfer) IOException(java.io.IOException)

Example 20 with Transfer

use of org.h2.value.Transfer in project h2database by h2database.

the class ResultRemote method fetchRows.

private void fetchRows(boolean sendFetch) {
    synchronized (session) {
        session.checkClosed();
        try {
            rowOffset += result.size();
            result.clear();
            int fetch = Math.min(fetchSize, rowCount - rowOffset);
            if (sendFetch) {
                session.traceOperation("RESULT_FETCH_ROWS", id);
                transfer.writeInt(SessionRemote.RESULT_FETCH_ROWS).writeInt(id).writeInt(fetch);
                session.done(transfer);
            }
            for (int r = 0; r < fetch; r++) {
                boolean row = transfer.readBoolean();
                if (!row) {
                    break;
                }
                int len = columns.length;
                Value[] values = new Value[len];
                for (int i = 0; i < len; i++) {
                    Value v = transfer.readValue();
                    values[i] = v;
                }
                result.add(values);
            }
            if (rowOffset + result.size() >= rowCount) {
                sendClose();
            }
        } catch (IOException e) {
            throw DbException.convertIOException(e, null);
        }
    }
}
Also used : Value(org.h2.value.Value) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)15 Transfer (org.h2.value.Transfer)14 DbException (org.h2.message.DbException)5 Socket (java.net.Socket)3 ResultRemote (org.h2.result.ResultRemote)3 Value (org.h2.value.Value)3 Properties (java.util.Properties)2 ParameterInterface (org.h2.expression.ParameterInterface)2 JdbcSQLException (org.h2.jdbc.JdbcSQLException)2 ServerSocket (java.net.ServerSocket)1 Iterator (java.util.Iterator)1 Constraint (org.h2.constraint.Constraint)1 ConnectionInfo (org.h2.engine.ConnectionInfo)1 SysProperties (org.h2.engine.SysProperties)1 ParameterRemote (org.h2.expression.ParameterRemote)1 TraceSystem (org.h2.message.TraceSystem)1 SortedProperties (org.h2.util.SortedProperties)1