Search in sources :

Example 1 with ResultRemote

use of org.h2.result.ResultRemote 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 2 with ResultRemote

use of org.h2.result.ResultRemote 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 3 with ResultRemote

use of org.h2.result.ResultRemote 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)

Aggregations

IOException (java.io.IOException)3 ResultRemote (org.h2.result.ResultRemote)3 Transfer (org.h2.value.Transfer)3