Search in sources :

Example 1 with ResultWithGeneratedKeys

use of org.h2.result.ResultWithGeneratedKeys in project h2database by h2database.

the class Command method executeUpdate.

@Override
public ResultWithGeneratedKeys executeUpdate(Object generatedKeysRequest) {
    long start = 0;
    Database database = session.getDatabase();
    Object sync = database.isMultiThreaded() ? (Object) session : (Object) database;
    session.waitIfExclusiveModeEnabled();
    boolean callStop = true;
    boolean writing = !isReadOnly();
    if (writing) {
        while (!database.beforeWriting()) {
        // wait
        }
    }
    synchronized (sync) {
        Session.Savepoint rollback = session.setSavepoint();
        session.setCurrentCommand(this, generatedKeysRequest);
        try {
            while (true) {
                database.checkPowerOff();
                try {
                    int updateCount = update();
                    if (!Boolean.FALSE.equals(generatedKeysRequest)) {
                        return new ResultWithGeneratedKeys.WithKeys(updateCount, session.getGeneratedKeys().getKeys(session));
                    }
                    return ResultWithGeneratedKeys.of(updateCount);
                } catch (DbException e) {
                    start = filterConcurrentUpdate(e, start);
                } catch (OutOfMemoryError e) {
                    callStop = false;
                    database.shutdownImmediately();
                    throw DbException.convert(e);
                } catch (Throwable e) {
                    throw DbException.convert(e);
                }
            }
        } catch (DbException e) {
            e = e.addSQL(sql);
            SQLException s = e.getSQLException();
            database.exceptionThrown(s, sql);
            if (s.getErrorCode() == ErrorCode.OUT_OF_MEMORY) {
                callStop = false;
                database.shutdownImmediately();
                throw e;
            }
            database.checkPowerOff();
            if (s.getErrorCode() == ErrorCode.DEADLOCK_1) {
                session.rollback();
            } else {
                session.rollbackTo(rollback, false);
            }
            throw e;
        } finally {
            try {
                if (callStop) {
                    stop();
                }
            } finally {
                if (writing) {
                    database.afterWriting();
                }
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Database(org.h2.engine.Database) Session(org.h2.engine.Session) DbException(org.h2.message.DbException)

Example 2 with ResultWithGeneratedKeys

use of org.h2.result.ResultWithGeneratedKeys in project h2database by h2database.

the class JdbcPreparedStatement method executeUpdateInternal.

private int executeUpdateInternal() throws SQLException {
    closeOldResultSet();
    synchronized (session) {
        try {
            setExecutingStatement(command);
            ResultWithGeneratedKeys result = command.executeUpdate(generatedKeysRequest);
            updateCount = result.getUpdateCount();
            ResultInterface gk = result.getGeneratedKeys();
            if (gk != null) {
                int id = getNextId(TraceObject.RESULT_SET);
                generatedKeys = new JdbcResultSet(conn, this, command, gk, id, false, true, false);
            }
        } finally {
            setExecutingStatement(null);
        }
    }
    return updateCount;
}
Also used : ResultInterface(org.h2.result.ResultInterface) ResultWithGeneratedKeys(org.h2.result.ResultWithGeneratedKeys)

Example 3 with ResultWithGeneratedKeys

use of org.h2.result.ResultWithGeneratedKeys in project h2database by h2database.

the class JdbcStatement method executeUpdateInternal.

private int executeUpdateInternal(String sql, Object generatedKeysRequest) throws SQLException {
    checkClosedForWrite();
    try {
        closeOldResultSet();
        sql = JdbcConnection.translateSQL(sql, escapeProcessing);
        CommandInterface command = conn.prepareCommand(sql, fetchSize);
        synchronized (session) {
            setExecutingStatement(command);
            try {
                ResultWithGeneratedKeys result = command.executeUpdate(conn.scopeGeneratedKeys() ? false : generatedKeysRequest);
                updateCount = result.getUpdateCount();
                ResultInterface gk = result.getGeneratedKeys();
                if (gk != null) {
                    int id = getNextId(TraceObject.RESULT_SET);
                    generatedKeys = new JdbcResultSet(conn, this, command, gk, id, false, true, false);
                }
            } finally {
                setExecutingStatement(null);
            }
        }
        command.close();
        return updateCount;
    } finally {
        afterWriting();
    }
}
Also used : ResultInterface(org.h2.result.ResultInterface) CommandInterface(org.h2.command.CommandInterface) ResultWithGeneratedKeys(org.h2.result.ResultWithGeneratedKeys)

Example 4 with ResultWithGeneratedKeys

use of org.h2.result.ResultWithGeneratedKeys 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 5 with ResultWithGeneratedKeys

use of org.h2.result.ResultWithGeneratedKeys in project h2database by h2database.

the class JdbcPreparedStatement method execute.

/**
 * Executes an arbitrary statement. If another result set exists for this
 * statement, this will be closed (even if this statement fails). If auto
 * commit is on, and the statement is not a select, this statement will be
 * committed.
 *
 * @return true if a result set is available, false if not
 * @throws SQLException if this object is closed or invalid
 */
@Override
public boolean execute() throws SQLException {
    try {
        int id = getNextId(TraceObject.RESULT_SET);
        if (isDebugEnabled()) {
            debugCodeCall("execute");
        }
        checkClosedForWrite();
        try {
            boolean returnsResultSet;
            synchronized (conn.getSession()) {
                closeOldResultSet();
                boolean lazy = false;
                try {
                    setExecutingStatement(command);
                    if (command.isQuery()) {
                        returnsResultSet = true;
                        boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY;
                        boolean updatable = resultSetConcurrency == ResultSet.CONCUR_UPDATABLE;
                        ResultInterface result = command.executeQuery(maxRows, scrollable);
                        lazy = result.isLazy();
                        resultSet = new JdbcResultSet(conn, this, command, result, id, closedByResultSet, scrollable, updatable, cachedColumnLabelMap);
                    } else {
                        returnsResultSet = false;
                        ResultWithGeneratedKeys result = command.executeUpdate(generatedKeysRequest);
                        updateCount = result.getUpdateCount();
                        ResultInterface gk = result.getGeneratedKeys();
                        if (gk != null) {
                            generatedKeys = new JdbcResultSet(conn, this, command, gk, id, false, true, false);
                        }
                    }
                } finally {
                    if (!lazy) {
                        setExecutingStatement(null);
                    }
                }
            }
            return returnsResultSet;
        } finally {
            afterWriting();
        }
    } catch (Throwable e) {
        throw logAndConvert(e);
    }
}
Also used : ResultInterface(org.h2.result.ResultInterface) ResultWithGeneratedKeys(org.h2.result.ResultWithGeneratedKeys)

Aggregations

ResultInterface (org.h2.result.ResultInterface)4 ResultWithGeneratedKeys (org.h2.result.ResultWithGeneratedKeys)4 CommandInterface (org.h2.command.CommandInterface)2 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 Database (org.h2.engine.Database)1 Session (org.h2.engine.Session)1 DbException (org.h2.message.DbException)1 ResultRemote (org.h2.result.ResultRemote)1 Transfer (org.h2.value.Transfer)1