| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.server; |
| 7 |
|
| 8 |
import java.io.ByteArrayInputStream; |
| 9 |
import java.io.FilterInputStream; |
| 10 |
import java.io.IOException; |
| 11 |
import java.io.InputStream; |
| 12 |
import java.io.PrintWriter; |
| 13 |
import java.io.StringWriter; |
| 14 |
import java.net.Socket; |
| 15 |
import java.sql.SQLException; |
| 16 |
import java.util.ArrayList; |
| 17 |
import java.util.Objects; |
| 18 |
|
| 19 |
import org.h2.api.ErrorCode; |
| 20 |
import org.h2.command.Command; |
| 21 |
import org.h2.engine.ConnectionInfo; |
| 22 |
import org.h2.engine.Constants; |
| 23 |
import org.h2.engine.Engine; |
| 24 |
import org.h2.engine.GeneratedKeysMode; |
| 25 |
import org.h2.engine.Session; |
| 26 |
import org.h2.engine.SessionLocal; |
| 27 |
import org.h2.engine.SessionRemote; |
| 28 |
import org.h2.engine.SysProperties; |
| 29 |
import org.h2.expression.Parameter; |
| 30 |
import org.h2.expression.ParameterInterface; |
| 31 |
import org.h2.expression.ParameterRemote; |
| 32 |
import org.h2.jdbc.JdbcException; |
| 33 |
import org.h2.jdbc.meta.DatabaseMetaServer; |
| 34 |
import org.h2.message.DbException; |
| 35 |
import org.h2.result.ResultColumn; |
| 36 |
import org.h2.result.ResultInterface; |
| 37 |
import org.h2.result.ResultWithGeneratedKeys; |
| 38 |
import org.h2.store.LobStorageInterface; |
| 39 |
import org.h2.util.IOUtils; |
| 40 |
import org.h2.util.NetUtils; |
| 41 |
import org.h2.util.NetworkConnectionInfo; |
| 42 |
import org.h2.util.SmallLRUCache; |
| 43 |
import org.h2.util.SmallMap; |
| 44 |
import org.h2.util.TimeZoneProvider; |
| 45 |
import org.h2.value.Transfer; |
| 46 |
import org.h2.value.Value; |
| 47 |
import org.h2.value.ValueLob; |
| 48 |
|
| 49 |
/** |
| 50 |
* One server thread is opened per client connection. |
| 51 |
*/ |
| 52 |
public class TcpServerThread implements Runnable { |
| 53 |
|
| 54 |
protected final Transfer transfer; |
| 55 |
private final TcpServer server; |
| 56 |
private SessionLocal session; |
| 57 |
private boolean stop; |
| 58 |
private Thread thread; |
| 59 |
private Command commit; |
| 60 |
private final SmallMap cache = |
| 61 |
new SmallMap(SysProperties.SERVER_CACHED_OBJECTS); |
| 62 |
private final SmallLRUCache<Long, CachedInputStream> lobs = |
| 63 |
SmallLRUCache.newInstance(Math.max( |
| 64 |
SysProperties.SERVER_CACHED_OBJECTS, |
| 65 |
SysProperties.SERVER_RESULT_SET_FETCH_SIZE * 5)); |
| 66 |
private final int threadId; |
| 67 |
private int clientVersion; |
| 68 |
private String sessionId; |
| 69 |
private long lastRemoteSettingsId; |
| 70 |
|
| 71 |
TcpServerThread(Socket socket, TcpServer server, int id) { |
| 72 |
this.server = server; |
| 73 |
this.threadId = id; |
| 74 |
transfer = new Transfer(null, socket); |
| 75 |
} |
| 76 |
|
| 77 |
private void trace(String s) { |
| 78 |
server.trace(this + " " + s); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public void run() { |
| 83 |
try { |
| 84 |
transfer.init(); |
| 85 |
trace("Connect"); |
| 86 |
// TODO server: should support a list of allowed databases |
| 87 |
// and a list of allowed clients |
| 88 |
try { |
| 89 |
Socket socket = transfer.getSocket(); |
| 90 |
if (socket == null) { |
| 91 |
// the transfer is already closed, prevent NPE in TcpServer#allow(Socket) |
| 92 |
return; |
| 93 |
} |
| 94 |
if (!server.allow(transfer.getSocket())) { |
| 95 |
throw DbException.get(ErrorCode.REMOTE_CONNECTION_NOT_ALLOWED); |
| 96 |
} |
| 97 |
int minClientVersion = transfer.readInt(); |
| 98 |
if (minClientVersion < 6) { |
| 99 |
throw DbException.get(ErrorCode.DRIVER_VERSION_ERROR_2, |
| 100 |
Integer.toString(minClientVersion), "" + Constants.TCP_PROTOCOL_VERSION_MIN_SUPPORTED); |
| 101 |
} |
| 102 |
int maxClientVersion = transfer.readInt(); |
| 103 |
if (maxClientVersion < Constants.TCP_PROTOCOL_VERSION_MIN_SUPPORTED) { |
| 104 |
throw DbException.get(ErrorCode.DRIVER_VERSION_ERROR_2, |
| 105 |
Integer.toString(maxClientVersion), "" + Constants.TCP_PROTOCOL_VERSION_MIN_SUPPORTED); |
| 106 |
} else if (minClientVersion > Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED) { |
| 107 |
throw DbException.get(ErrorCode.DRIVER_VERSION_ERROR_2, |
| 108 |
Integer.toString(minClientVersion), "" + Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED); |
| 109 |
} |
| 110 |
if (maxClientVersion >= Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED) { |
| 111 |
clientVersion = Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED; |
| 112 |
} else { |
| 113 |
clientVersion = maxClientVersion; |
| 114 |
} |
| 115 |
transfer.setVersion(clientVersion); |
| 116 |
String db = transfer.readString(); |
| 117 |
String originalURL = transfer.readString(); |
| 118 |
if (db == null && originalURL == null) { |
| 119 |
String targetSessionId = transfer.readString(); |
| 120 |
int command = transfer.readInt(); |
| 121 |
stop = true; |
| 122 |
if (command == SessionRemote.SESSION_CANCEL_STATEMENT) { |
| 123 |
// cancel a running statement |
| 124 |
int statementId = transfer.readInt(); |
| 125 |
server.cancelStatement(targetSessionId, statementId); |
| 126 |
} else if (command == SessionRemote.SESSION_CHECK_KEY) { |
| 127 |
// check if this is the correct server |
| 128 |
db = server.checkKeyAndGetDatabaseName(targetSessionId); |
| 129 |
if (!targetSessionId.equals(db)) { |
| 130 |
transfer.writeInt(SessionRemote.STATUS_OK); |
| 131 |
} else { |
| 132 |
transfer.writeInt(SessionRemote.STATUS_ERROR); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
String baseDir = server.getBaseDir(); |
| 137 |
if (baseDir == null) { |
| 138 |
baseDir = SysProperties.getBaseDir(); |
| 139 |
} |
| 140 |
db = server.checkKeyAndGetDatabaseName(db); |
| 141 |
ConnectionInfo ci = new ConnectionInfo(db); |
| 142 |
ci.setOriginalURL(originalURL); |
| 143 |
ci.setUserName(transfer.readString()); |
| 144 |
ci.setUserPasswordHash(transfer.readBytes()); |
| 145 |
ci.setFilePasswordHash(transfer.readBytes()); |
| 146 |
int len = transfer.readInt(); |
| 147 |
for (int i = 0; i < len; i++) { |
| 148 |
ci.setProperty(transfer.readString(), transfer.readString()); |
| 149 |
} |
| 150 |
// override client's requested properties with server settings |
| 151 |
if (baseDir != null) { |
| 152 |
ci.setBaseDir(baseDir); |
| 153 |
} |
| 154 |
if (server.getIfExists()) { |
| 155 |
ci.setProperty("FORBID_CREATION", "TRUE"); |
| 156 |
} |
| 157 |
transfer.writeInt(SessionRemote.STATUS_OK); |
| 158 |
transfer.writeInt(clientVersion); |
| 159 |
transfer.flush(); |
| 160 |
if (ci.getFilePasswordHash() != null) { |
| 161 |
ci.setFileEncryptionKey(transfer.readBytes()); |
| 162 |
} |
| 163 |
ci.setNetworkConnectionInfo(new NetworkConnectionInfo( |
| 164 |
NetUtils.ipToShortForm(new StringBuilder(server.getSSL() ? "ssl://" : "tcp://"), |
| 165 |
socket.getLocalAddress().getAddress(), true) // |
| 166 |
.append(':').append(socket.getLocalPort()).toString(), // |
| 167 |
socket.getInetAddress().getAddress(), socket.getPort(), |
| 168 |
new StringBuilder().append('P').append(clientVersion).toString())); |
| 169 |
if (clientVersion < Constants.TCP_PROTOCOL_VERSION_20) { |
| 170 |
// For DatabaseMetaData |
| 171 |
ci.setProperty("OLD_INFORMATION_SCHEMA", "TRUE"); |
| 172 |
// For H2 Console |
| 173 |
ci.setProperty("NON_KEYWORDS", "VALUE"); |
| 174 |
} |
| 175 |
session = Engine.createSession(ci); |
| 176 |
transfer.setSession(session); |
| 177 |
server.addConnection(threadId, originalURL, ci.getUserName()); |
| 178 |
trace("Connected"); |
| 179 |
lastRemoteSettingsId = session.getDatabase().getRemoteSettingsId(); |
| 180 |
} catch (OutOfMemoryError e) { |
| 181 |
// catch this separately otherwise such errors will never hit the console |
| 182 |
server.traceError(e); |
| 183 |
sendError(e, true); |
| 184 |
stop = true; |
| 185 |
} catch (Throwable e) { |
| 186 |
sendError(e,true); |
| 187 |
stop = true; |
| 188 |
} |
| 189 |
while (!stop) { |
| 190 |
try { |
| 191 |
process(); |
| 192 |
} catch (Throwable e) { |
| 193 |
sendError(e, true); |
| 194 |
} |
| 195 |
} |
| 196 |
trace("Disconnect"); |
| 197 |
} catch (Throwable e) { |
| 198 |
server.traceError(e); |
| 199 |
} finally { |
| 200 |
close(); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
private void closeSession() { |
| 205 |
if (session != null) { |
| 206 |
RuntimeException closeError = null; |
| 207 |
try { |
| 208 |
session.close(); |
| 209 |
server.removeConnection(threadId); |
| 210 |
} catch (RuntimeException e) { |
| 211 |
closeError = e; |
| 212 |
server.traceError(e); |
| 213 |
} catch (Exception e) { |
| 214 |
server.traceError(e); |
| 215 |
} finally { |
| 216 |
session = null; |
| 217 |
} |
| 218 |
if (closeError != null) { |
| 219 |
throw closeError; |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Close a connection. |
| 226 |
*/ |
| 227 |
void close() { |
| 228 |
try { |
| 229 |
stop = true; |
| 230 |
closeSession(); |
| 231 |
} catch (Exception e) { |
| 232 |
server.traceError(e); |
| 233 |
} finally { |
| 234 |
transfer.close(); |
| 235 |
trace("Close"); |
| 236 |
server.remove(this); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
private void sendError(Throwable t, boolean withStatus) { |
| 241 |
try { |
| 242 |
SQLException e = DbException.convert(t).getSQLException(); |
| 243 |
StringWriter writer = new StringWriter(); |
| 244 |
e.printStackTrace(new PrintWriter(writer)); |
| 245 |
String trace = writer.toString(); |
| 246 |
String message; |
| 247 |
String sql; |
| 248 |
if (e instanceof JdbcException) { |
| 249 |
JdbcException j = (JdbcException) e; |
| 250 |
message = j.getOriginalMessage(); |
| 251 |
sql = j.getSQL(); |
| 252 |
} else { |
| 253 |
message = e.getMessage(); |
| 254 |
sql = null; |
| 255 |
} |
| 256 |
if (withStatus) { |
| 257 |
transfer.writeInt(SessionRemote.STATUS_ERROR); |
| 258 |
} |
| 259 |
transfer. |
| 260 |
writeString(e.getSQLState()).writeString(message). |
| 261 |
writeString(sql).writeInt(e.getErrorCode()).writeString(trace).flush(); |
| 262 |
} catch (Exception e2) { |
| 263 |
if (!transfer.isClosed()) { |
| 264 |
server.traceError(e2); |
| 265 |
} |
| 266 |
// if writing the error does not work, close the connection |
| 267 |
stop = true; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
private void setParameters(Command command) throws IOException { |
| 272 |
int len = transfer.readInt(); |
| 273 |
ArrayList<? extends ParameterInterface> params = command.getParameters(); |
| 274 |
for (int i = 0; i < len; i++) { |
| 275 |
Parameter p = (Parameter) params.get(i); |
| 276 |
p.setValue(transfer.readValue(null)); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
private void process() throws IOException { |
| 281 |
int operation = transfer.readInt(); |
| 282 |
switch (operation) { |
| 283 |
case SessionRemote.SESSION_PREPARE: |
| 284 |
case SessionRemote.SESSION_PREPARE_READ_PARAMS2: { |
| 285 |
int id = transfer.readInt(); |
| 286 |
String sql = transfer.readString(); |
| 287 |
int old = session.getModificationId(); |
| 288 |
Command command = session.prepareLocal(sql); |
| 289 |
boolean readonly = command.isReadOnly(); |
| 290 |
cache.addObject(id, command); |
| 291 |
boolean isQuery = command.isQuery(); |
| 292 |
|
| 293 |
transfer.writeInt(getState(old)).writeBoolean(isQuery). |
| 294 |
writeBoolean(readonly); |
| 295 |
|
| 296 |
if (operation != SessionRemote.SESSION_PREPARE) { |
| 297 |
transfer.writeInt(command.getCommandType()); |
| 298 |
} |
| 299 |
|
| 300 |
ArrayList<? extends ParameterInterface> params = command.getParameters(); |
| 301 |
|
| 302 |
transfer.writeInt(params.size()); |
| 303 |
|
| 304 |
if (operation != SessionRemote.SESSION_PREPARE) { |
| 305 |
for (ParameterInterface p : params) { |
| 306 |
ParameterRemote.writeMetaData(transfer, p); |
| 307 |
} |
| 308 |
} |
| 309 |
transfer.flush(); |
| 310 |
break; |
| 311 |
} |
| 312 |
case SessionRemote.SESSION_CLOSE: { |
| 313 |
stop = true; |
| 314 |
closeSession(); |
| 315 |
transfer.writeInt(SessionRemote.STATUS_OK).flush(); |
| 316 |
close(); |
| 317 |
break; |
| 318 |
} |
| 319 |
case SessionRemote.COMMAND_COMMIT: { |
| 320 |
if (commit == null) { |
| 321 |
commit = session.prepareLocal("COMMIT"); |
| 322 |
} |
| 323 |
int old = session.getModificationId(); |
| 324 |
commit.executeUpdate(null); |
| 325 |
transfer.writeInt(getState(old)).flush(); |
| 326 |
break; |
| 327 |
} |
| 328 |
case SessionRemote.COMMAND_GET_META_DATA: { |
| 329 |
int id = transfer.readInt(); |
| 330 |
int objectId = transfer.readInt(); |
| 331 |
Command command = (Command) cache.getObject(id, false); |
| 332 |
ResultInterface result = command.getMetaData(); |
| 333 |
cache.addObject(objectId, result); |
| 334 |
int columnCount = result.getVisibleColumnCount(); |
| 335 |
transfer.writeInt(SessionRemote.STATUS_OK). |
| 336 |
writeInt(columnCount).writeRowCount(0L); |
| 337 |
for (int i = 0; i < columnCount; i++) { |
| 338 |
ResultColumn.writeColumn(transfer, result, i); |
| 339 |
} |
| 340 |
transfer.flush(); |
| 341 |
break; |
| 342 |
} |
| 343 |
case SessionRemote.COMMAND_EXECUTE_QUERY: { |
| 344 |
int id = transfer.readInt(); |
| 345 |
int objectId = transfer.readInt(); |
| 346 |
long maxRows = transfer.readRowCount(); |
| 347 |
int fetchSize = transfer.readInt(); |
| 348 |
Command command = (Command) cache.getObject(id, false); |
| 349 |
setParameters(command); |
| 350 |
int old = session.getModificationId(); |
| 351 |
ResultInterface result; |
| 352 |
synchronized (session) { |
| 353 |
result = command.executeQuery(maxRows, false); |
| 354 |
} |
| 355 |
cache.addObject(objectId, result); |
| 356 |
int columnCount = result.getVisibleColumnCount(); |
| 357 |
int state = getState(old); |
| 358 |
transfer.writeInt(state).writeInt(columnCount); |
| 359 |
long rowCount = result.isLazy() ? -1L : result.getRowCount(); |
| 360 |
transfer.writeRowCount(rowCount); |
| 361 |
for (int i = 0; i < columnCount; i++) { |
| 362 |
ResultColumn.writeColumn(transfer, result, i); |
| 363 |
} |
| 364 |
sendRows(result, rowCount >= 0L ? Math.min(rowCount, fetchSize) : fetchSize); |
| 365 |
transfer.flush(); |
| 366 |
break; |
| 367 |
} |
| 368 |
case SessionRemote.COMMAND_EXECUTE_UPDATE: { |
| 369 |
int id = transfer.readInt(); |
| 370 |
Command command = (Command) cache.getObject(id, false); |
| 371 |
setParameters(command); |
| 372 |
boolean writeGeneratedKeys = true; |
| 373 |
Object generatedKeysRequest; |
| 374 |
int mode = transfer.readInt(); |
| 375 |
switch (mode) { |
| 376 |
case GeneratedKeysMode.NONE: |
| 377 |
generatedKeysRequest = false; |
| 378 |
writeGeneratedKeys = false; |
| 379 |
break; |
| 380 |
case GeneratedKeysMode.AUTO: |
| 381 |
generatedKeysRequest = true; |
| 382 |
break; |
| 383 |
case GeneratedKeysMode.COLUMN_NUMBERS: { |
| 384 |
int len = transfer.readInt(); |
| 385 |
int[] keys = new int[len]; |
| 386 |
for (int i = 0; i < len; i++) { |
| 387 |
keys[i] = transfer.readInt(); |
| 388 |
} |
| 389 |
generatedKeysRequest = keys; |
| 390 |
break; |
| 391 |
} |
| 392 |
case GeneratedKeysMode.COLUMN_NAMES: { |
| 393 |
int len = transfer.readInt(); |
| 394 |
String[] keys = new String[len]; |
| 395 |
for (int i = 0; i < len; i++) { |
| 396 |
keys[i] = transfer.readString(); |
| 397 |
} |
| 398 |
generatedKeysRequest = keys; |
| 399 |
break; |
| 400 |
} |
| 401 |
default: |
| 402 |
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, |
| 403 |
"Unsupported generated keys' mode " + mode); |
| 404 |
} |
| 405 |
int old = session.getModificationId(); |
| 406 |
ResultWithGeneratedKeys result; |
| 407 |
synchronized (session) { |
| 408 |
result = command.executeUpdate(generatedKeysRequest); |
| 409 |
} |
| 410 |
int status; |
| 411 |
if (session.isClosed()) { |
| 412 |
status = SessionRemote.STATUS_CLOSED; |
| 413 |
stop = true; |
| 414 |
} else { |
| 415 |
status = getState(old); |
| 416 |
} |
| 417 |
transfer.writeInt(status); |
| 418 |
transfer.writeRowCount(result.getUpdateCount()); |
| 419 |
transfer.writeBoolean(session.getAutoCommit()); |
| 420 |
if (writeGeneratedKeys) { |
| 421 |
ResultInterface generatedKeys = result.getGeneratedKeys(); |
| 422 |
int columnCount = generatedKeys.getVisibleColumnCount(); |
| 423 |
transfer.writeInt(columnCount); |
| 424 |
long rowCount = generatedKeys.getRowCount(); |
| 425 |
transfer.writeRowCount(rowCount); |
| 426 |
for (int i = 0; i < columnCount; i++) { |
| 427 |
ResultColumn.writeColumn(transfer, generatedKeys, i); |
| 428 |
} |
| 429 |
sendRows(generatedKeys, rowCount); |
| 430 |
generatedKeys.close(); |
| 431 |
} |
| 432 |
transfer.flush(); |
| 433 |
break; |
| 434 |
} |
| 435 |
case SessionRemote.COMMAND_CLOSE: { |
| 436 |
int id = transfer.readInt(); |
| 437 |
Command command = (Command) cache.getObject(id, true); |
| 438 |
if (command != null) { |
| 439 |
command.close(); |
| 440 |
cache.freeObject(id); |
| 441 |
} |
| 442 |
break; |
| 443 |
} |
| 444 |
case SessionRemote.RESULT_FETCH_ROWS: { |
| 445 |
int id = transfer.readInt(); |
| 446 |
int count = transfer.readInt(); |
| 447 |
ResultInterface result = (ResultInterface) cache.getObject(id, false); |
| 448 |
transfer.writeInt(SessionRemote.STATUS_OK); |
| 449 |
sendRows(result, count); |
| 450 |
transfer.flush(); |
| 451 |
break; |
| 452 |
} |
| 453 |
case SessionRemote.RESULT_RESET: { |
| 454 |
int id = transfer.readInt(); |
| 455 |
ResultInterface result = (ResultInterface) cache.getObject(id, false); |
| 456 |
result.reset(); |
| 457 |
break; |
| 458 |
} |
| 459 |
case SessionRemote.RESULT_CLOSE: { |
| 460 |
int id = transfer.readInt(); |
| 461 |
ResultInterface result = (ResultInterface) cache.getObject(id, true); |
| 462 |
if (result != null) { |
| 463 |
result.close(); |
| 464 |
cache.freeObject(id); |
| 465 |
} |
| 466 |
break; |
| 467 |
} |
| 468 |
case SessionRemote.CHANGE_ID: { |
| 469 |
int oldId = transfer.readInt(); |
| 470 |
int newId = transfer.readInt(); |
| 471 |
Object obj = cache.getObject(oldId, false); |
| 472 |
cache.freeObject(oldId); |
| 473 |
cache.addObject(newId, obj); |
| 474 |
break; |
| 475 |
} |
| 476 |
case SessionRemote.SESSION_SET_ID: { |
| 477 |
sessionId = transfer.readString(); |
| 478 |
if (clientVersion >= Constants.TCP_PROTOCOL_VERSION_20) { |
| 479 |
session.setTimeZone(TimeZoneProvider.ofId(transfer.readString())); |
| 480 |
} |
| 481 |
transfer.writeInt(SessionRemote.STATUS_OK) |
| 482 |
.writeBoolean(session.getAutoCommit()) |
| 483 |
.flush(); |
| 484 |
break; |
| 485 |
} |
| 486 |
case SessionRemote.SESSION_SET_AUTOCOMMIT: { |
| 487 |
boolean autoCommit = transfer.readBoolean(); |
| 488 |
session.setAutoCommit(autoCommit); |
| 489 |
transfer.writeInt(SessionRemote.STATUS_OK).flush(); |
| 490 |
break; |
| 491 |
} |
| 492 |
case SessionRemote.SESSION_HAS_PENDING_TRANSACTION: { |
| 493 |
transfer.writeInt(SessionRemote.STATUS_OK). |
| 494 |
writeInt(session.hasPendingTransaction() ? 1 : 0).flush(); |
| 495 |
break; |
| 496 |
} |
| 497 |
case SessionRemote.LOB_READ: { |
| 498 |
long lobId = transfer.readLong(); |
| 499 |
byte[] hmac = transfer.readBytes(); |
| 500 |
long offset = transfer.readLong(); |
| 501 |
int length = transfer.readInt(); |
| 502 |
transfer.verifyLobMac(hmac, lobId); |
| 503 |
CachedInputStream in = lobs.get(lobId); |
| 504 |
if (in == null || in.getPos() != offset) { |
| 505 |
LobStorageInterface lobStorage = session.getDataHandler().getLobStorage(); |
| 506 |
// only the lob id is used |
| 507 |
InputStream lobIn = lobStorage.getInputStream(lobId, -1); |
| 508 |
in = new CachedInputStream(lobIn); |
| 509 |
lobs.put(lobId, in); |
| 510 |
lobIn.skip(offset); |
| 511 |
} |
| 512 |
// limit the buffer size |
| 513 |
length = Math.min(16 * Constants.IO_BUFFER_SIZE, length); |
| 514 |
byte[] buff = new byte[length]; |
| 515 |
length = IOUtils.readFully(in, buff, length); |
| 516 |
transfer.writeInt(SessionRemote.STATUS_OK); |
| 517 |
transfer.writeInt(length); |
| 518 |
transfer.writeBytes(buff, 0, length); |
| 519 |
transfer.flush(); |
| 520 |
break; |
| 521 |
} |
| 522 |
case SessionRemote.GET_JDBC_META: { |
| 523 |
int code = transfer.readInt(); |
| 524 |
int length = transfer.readInt(); |
| 525 |
Value[] args = new Value[length]; |
| 526 |
for (int i = 0; i < length; i++) { |
| 527 |
args[i] = transfer.readValue(null); |
| 528 |
} |
| 529 |
int old = session.getModificationId(); |
| 530 |
ResultInterface result; |
| 531 |
synchronized (session) { |
| 532 |
result = DatabaseMetaServer.process(session, code, args); |
| 533 |
} |
| 534 |
int columnCount = result.getVisibleColumnCount(); |
| 535 |
int state = getState(old); |
| 536 |
transfer.writeInt(state).writeInt(columnCount); |
| 537 |
long rowCount = result.getRowCount(); |
| 538 |
transfer.writeRowCount(rowCount); |
| 539 |
for (int i = 0; i < columnCount; i++) { |
| 540 |
ResultColumn.writeColumn(transfer, result, i); |
| 541 |
} |
| 542 |
sendRows(result, rowCount); |
| 543 |
transfer.flush(); |
| 544 |
break; |
| 545 |
} |
| 546 |
default: |
| 547 |
trace("Unknown operation: " + operation); |
| 548 |
close(); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
private int getState(int oldModificationId) { |
| 553 |
if (session == null) { |
| 554 |
return SessionRemote.STATUS_CLOSED; |
| 555 |
} |
| 556 |
if (session.getModificationId() == oldModificationId) { |
| 557 |
long remoteSettingsId = session.getDatabase().getRemoteSettingsId(); |
| 558 |
if (lastRemoteSettingsId == remoteSettingsId) { |
| 559 |
return SessionRemote.STATUS_OK; |
| 560 |
} |
| 561 |
lastRemoteSettingsId = remoteSettingsId; |
| 562 |
} |
| 563 |
return SessionRemote.STATUS_OK_STATE_CHANGED; |
| 564 |
} |
| 565 |
|
| 566 |
private void sendRows(ResultInterface result, long count) throws IOException { |
| 567 |
int columnCount = result.getVisibleColumnCount(); |
| 568 |
boolean lazy = result.isLazy(); |
| 569 |
Session oldSession = lazy ? session.setThreadLocalSession() : null; |
| 570 |
try { |
| 571 |
while (count-- > 0L) { |
| 572 |
boolean hasNext; |
| 573 |
try { |
| 574 |
hasNext = result.next(); |
| 575 |
} catch (Exception e) { |
| 576 |
transfer.writeByte((byte) -1); |
| 577 |
sendError(e, false); |
| 578 |
break; |
| 579 |
} |
| 580 |
if (hasNext) { |
| 581 |
transfer.writeByte((byte) 1); |
| 582 |
Value[] values = result.currentRow(); |
| 583 |
for (int i = 0; i < columnCount; i++) { |
| 584 |
Value v = values[i]; |
| 585 |
if (lazy && v instanceof ValueLob) { |
| 586 |
ValueLob v2 = ((ValueLob) v).copyToResult(); |
| 587 |
if (v2 != v) { |
| 588 |
v = session.addTemporaryLob(v2); |
| 589 |
} |
| 590 |
} |
| 591 |
transfer.writeValue(v); |
| 592 |
} |
| 593 |
} else { |
| 594 |
transfer.writeByte((byte) 0); |
| 595 |
break; |
| 596 |
} |
| 597 |
} |
| 598 |
} finally { |
| 599 |
if (lazy) { |
| 600 |
session.resetThreadLocalSession(oldSession); |
| 601 |
} |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
void setThread(Thread thread) { |
| 606 |
this.thread = thread; |
| 607 |
} |
| 608 |
|
| 609 |
Thread getThread() { |
| 610 |
return thread; |
| 611 |
} |
| 612 |
|
| 613 |
/** |
| 614 |
* Cancel a running statement. |
| 615 |
* |
| 616 |
* @param targetSessionId the session id |
| 617 |
* @param statementId the statement to cancel |
| 618 |
*/ |
| 619 |
void cancelStatement(String targetSessionId, int statementId) { |
| 620 |
if (Objects.equals(targetSessionId, this.sessionId)) { |
| 621 |
Command cmd = (Command) cache.getObject(statementId, false); |
| 622 |
cmd.cancel(); |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
/** |
| 627 |
* An input stream with a position. |
| 628 |
*/ |
| 629 |
static class CachedInputStream extends FilterInputStream { |
| 630 |
|
| 631 |
private static final ByteArrayInputStream DUMMY = |
| 632 |
new ByteArrayInputStream(new byte[0]); |
| 633 |
private long pos; |
| 634 |
|
| 635 |
CachedInputStream(InputStream in) { |
| 636 |
super(in == null ? DUMMY : in); |
| 637 |
if (in == null) { |
| 638 |
pos = -1; |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
@Override |
| 643 |
public int read(byte[] buff, int off, int len) throws IOException { |
| 644 |
len = super.read(buff, off, len); |
| 645 |
if (len > 0) { |
| 646 |
pos += len; |
| 647 |
} |
| 648 |
return len; |
| 649 |
} |
| 650 |
|
| 651 |
@Override |
| 652 |
public int read() throws IOException { |
| 653 |
int x = in.read(); |
| 654 |
if (x >= 0) { |
| 655 |
pos++; |
| 656 |
} |
| 657 |
return x; |
| 658 |
} |
| 659 |
|
| 660 |
@Override |
| 661 |
public long skip(long n) throws IOException { |
| 662 |
n = super.skip(n); |
| 663 |
if (n > 0) { |
| 664 |
pos += n; |
| 665 |
} |
| 666 |
return n; |
| 667 |
} |
| 668 |
|
| 669 |
public long getPos() { |
| 670 |
return pos; |
| 671 |
} |
| 672 |
|
| 673 |
} |
| 674 |
|
| 675 |
} |