case SessionRemote.SESSION_PREPARE_READ_PARAMS:
case SessionRemote.SESSION_PREPARE: {
int id = transfer.readInt();
String sql = transfer.readString();
int old = session.getModificationId();
Command command = session.prepareCommand(sql);
boolean readonly = command.isReadOnly();
cache.addObject(id, command);
boolean isQuery = command.isQuery();
ArrayList<? extends ParameterInterface> params = command.getParameters();
transfer.writeInt(getState(old)).writeBoolean(isQuery).writeBoolean(readonly).writeInt(params.size());
if (operation == SessionRemote.SESSION_PREPARE_READ_PARAMS) {
for (ParameterInterface p : params) {
ParameterRemote.writeMetaData(transfer, p);
}
}
transfer.flush();
break;
}
case SessionRemote.SESSION_CLOSE: {
stop = true;
closeSession();
transfer.writeInt(SessionRemote.STATUS_OK).flush();
close();
break;
}
case SessionRemote.COMMAND_COMMIT: {
if (commit == null) {
commit = session.prepareLocal("COMMIT");
}
int old = session.getModificationId();
commit.executeUpdate();
transfer.writeInt(getState(old)).flush();
break;
}
case SessionRemote.COMMAND_GET_META_DATA: {
int id = transfer.readInt();
int objectId = transfer.readInt();
Command command = (Command) cache.getObject(id, false);
ResultInterface result = command.getMetaData();
cache.addObject(objectId, result);
int columnCount = result.getVisibleColumnCount();
transfer.writeInt(SessionRemote.STATUS_OK).writeInt(columnCount).writeInt(0);
for (int i = 0; i < columnCount; i++) {
ResultColumn.writeColumn(transfer, result, i);
}
transfer.flush();
break;
}
case SessionRemote.COMMAND_EXECUTE_DISTRIBUTED_QUERY: {
session.setAutoCommit(false);
session.setRoot(false);
}
case SessionRemote.COMMAND_EXECUTE_QUERY: {
int id = transfer.readInt();
int objectId = transfer.readInt();
int maxRows = transfer.readInt();
int fetchSize = transfer.readInt();
Command command = (Command) cache.getObject(id, false);
command.getPrepared().setFetchSize(fetchSize);
setParameters(command);
int old = session.getModificationId();
ResultInterface result;
synchronized (session) {
result = command.executeQuery(maxRows, false);
}
cache.addObject(objectId, result);
int columnCount = result.getVisibleColumnCount();
int state = getState(old);
transfer.writeInt(state);
if (operation == SessionRemote.COMMAND_EXECUTE_DISTRIBUTED_QUERY)
transfer.writeString(session.getTransaction().getLocalTransactionNames());
transfer.writeInt(columnCount);
int rowCount = result.getRowCount();
transfer.writeInt(rowCount);
for (int i = 0; i < columnCount; i++) {
ResultColumn.writeColumn(transfer, result, i);
}
int fetch = fetchSize;
if (rowCount != -1)
fetch = Math.min(rowCount, fetchSize);
sendRow(result, fetch);
transfer.flush();
break;
}
case SessionRemote.COMMAND_EXECUTE_DISTRIBUTED_UPDATE: {
session.setAutoCommit(false);
session.setRoot(false);
}
case SessionRemote.COMMAND_EXECUTE_UPDATE: {
int id = transfer.readInt();
Command command = (Command) cache.getObject(id, false);
setParameters(command);
int old = session.getModificationId();
int updateCount;
synchronized (session) {
updateCount = command.executeUpdate();
}
int status;
if (session.isClosed()) {
status = SessionRemote.STATUS_CLOSED;
} else {
status = getState(old);
}
transfer.writeInt(status);
if (operation == SessionRemote.COMMAND_EXECUTE_DISTRIBUTED_UPDATE)
transfer.writeString(session.getTransaction().getLocalTransactionNames());
transfer.writeInt(updateCount).writeBoolean(session.getAutoCommit());
transfer.flush();
break;
}
case SessionRemote.COMMAND_EXECUTE_DISTRIBUTED_COMMIT: {
int old = session.getModificationId();
synchronized (session) {
session.commit(false, transfer.readString());
}
int status;
if (session.isClosed()) {
status = SessionRemote.STATUS_CLOSED;
} else {
status = getState(old);
}
transfer.writeInt(status);
transfer.flush();
break;
}
case SessionRemote.COMMAND_EXECUTE_DISTRIBUTED_ROLLBACK: {
int old = session.getModificationId();
synchronized (session) {
session.rollback();
}
int status;
if (session.isClosed()) {
status = SessionRemote.STATUS_CLOSED;
} else {
status = getState(old);
}
transfer.writeInt(status);
transfer.flush();
break;
}
case SessionRemote.COMMAND_EXECUTE_DISTRIBUTED_SAVEPOINT_ADD:
case SessionRemote.COMMAND_EXECUTE_DISTRIBUTED_SAVEPOINT_ROLLBACK: {
int old = session.getModificationId();
String name = transfer.readString();
synchronized (session) {
if (operation == SessionRemote.COMMAND_EXECUTE_DISTRIBUTED_SAVEPOINT_ADD)
session.addSavepoint(name);
else
session.rollbackToSavepoint(name);
}
int status;
if (session.isClosed()) {
status = SessionRemote.STATUS_CLOSED;
} else {
status = getState(old);
}
transfer.writeInt(status);
transfer.flush();
break;
}
case SessionRemote.COMMAND_EXECUTE_BATCH_UPDATE_STATEMENT: {
int size = transfer.readInt();
ArrayList<String> batchCommands = New.arrayList(size);
for (int i = 0; i < size; i++)
batchCommands.add(transfer.readString());
BackendBatchCommand command = new BackendBatchCommand(session, batchCommands);
executeBatch(size, command);
break;
}
case SessionRemote.COMMAND_EXECUTE_BATCH_UPDATE_PREPAREDSTATEMENT: {
int id = transfer.readInt();
int size = transfer.readInt();
Command preparedCommand = (Command) cache.getObject(id, false);
ArrayList<Value[]> batchParameters = New.arrayList(size);
int paramsSize = preparedCommand.getParameters().size();
Value[] values;
for (int i = 0; i < size; i++) {
values = new Value[paramsSize];
for (int j = 0; j < paramsSize; j++) {
values[j] = transfer.readValue();
}
batchParameters.add(values);
}
BackendBatchCommand command = new BackendBatchCommand(session, preparedCommand, batchParameters);
executeBatch(size, command);
break;
}
case SessionRemote.COMMAND_CLOSE: {
int id = transfer.readInt();
Command command = (Command) cache.getObject(id, true);
if (command != null) {
command.close();
cache.freeObject(id);
}
break;
}
case SessionRemote.RESULT_FETCH_ROWS: {