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();
}
}
}
}
}
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;
}
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();
}
}
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);
}
}
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);
}
}
Aggregations