| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, and the |
| 3 |
* EPL 1.0 (https://h2database.com/html/license.html). Initial Developer: H2 |
| 4 |
* Group |
| 5 |
*/ |
| 6 |
package org.h2.jdbc; |
| 7 |
|
| 8 |
import java.io.InputStream; |
| 9 |
import java.io.Reader; |
| 10 |
import java.sql.Array; |
| 11 |
import java.sql.Blob; |
| 12 |
import java.sql.CallableStatement; |
| 13 |
import java.sql.Clob; |
| 14 |
import java.sql.Connection; |
| 15 |
import java.sql.DatabaseMetaData; |
| 16 |
import java.sql.NClob; |
| 17 |
import java.sql.PreparedStatement; |
| 18 |
import java.sql.ResultSet; |
| 19 |
import java.sql.SQLClientInfoException; |
| 20 |
import java.sql.SQLException; |
| 21 |
import java.sql.SQLWarning; |
| 22 |
import java.sql.SQLXML; |
| 23 |
import java.sql.Savepoint; |
| 24 |
import java.sql.Statement; |
| 25 |
import java.sql.Struct; |
| 26 |
import java.util.ArrayList; |
| 27 |
import java.util.Collections; |
| 28 |
import java.util.HashMap; |
| 29 |
import java.util.Map; |
| 30 |
import java.util.Objects; |
| 31 |
import java.util.Properties; |
| 32 |
import java.util.concurrent.Executor; |
| 33 |
import java.util.regex.Pattern; |
| 34 |
|
| 35 |
import org.h2.api.ErrorCode; |
| 36 |
import org.h2.api.JavaObjectSerializer; |
| 37 |
import org.h2.command.CommandInterface; |
| 38 |
import org.h2.engine.CastDataProvider; |
| 39 |
import org.h2.engine.ConnectionInfo; |
| 40 |
import org.h2.engine.Constants; |
| 41 |
import org.h2.engine.IsolationLevel; |
| 42 |
import org.h2.engine.Mode; |
| 43 |
import org.h2.engine.Session; |
| 44 |
import org.h2.engine.Session.StaticSettings; |
| 45 |
import org.h2.engine.SessionRemote; |
| 46 |
import org.h2.engine.SysProperties; |
| 47 |
import org.h2.message.DbException; |
| 48 |
import org.h2.message.TraceObject; |
| 49 |
import org.h2.result.ResultInterface; |
| 50 |
import org.h2.util.CloseWatcher; |
| 51 |
import org.h2.util.TimeZoneProvider; |
| 52 |
import org.h2.value.CompareMode; |
| 53 |
import org.h2.value.Value; |
| 54 |
import org.h2.value.ValueInteger; |
| 55 |
import org.h2.value.ValueNull; |
| 56 |
import org.h2.value.ValueTimestampTimeZone; |
| 57 |
import org.h2.value.ValueToObjectConverter; |
| 58 |
import org.h2.value.ValueVarbinary; |
| 59 |
import org.h2.value.ValueVarchar; |
| 60 |
|
| 61 |
/** |
| 62 |
* Represents a connection (session) to a database. |
| 63 |
* <p> |
| 64 |
* Thread safety: the connection is thread-safe, because access is synchronized. |
| 65 |
* Different statements from the same connection may try to execute their |
| 66 |
* commands in parallel, but they will be executed sequentially. If real |
| 67 |
* concurrent execution of these commands is needed, different connections |
| 68 |
* should be used. |
| 69 |
* </p> |
| 70 |
*/ |
| 71 |
public class JdbcConnection extends TraceObject implements Connection, JdbcConnectionBackwardsCompat, |
| 72 |
CastDataProvider { |
| 73 |
|
| 74 |
private static final String NUM_SERVERS = "numServers"; |
| 75 |
private static final String PREFIX_SERVER = "server"; |
| 76 |
|
| 77 |
private static boolean keepOpenStackTrace; |
| 78 |
|
| 79 |
private final String url; |
| 80 |
private final String user; |
| 81 |
|
| 82 |
// ResultSet.HOLD_CURSORS_OVER_COMMIT |
| 83 |
private int holdability = 1; |
| 84 |
|
| 85 |
private Session session; |
| 86 |
private CommandInterface commit, rollback; |
| 87 |
private CommandInterface getReadOnly, getGeneratedKeys; |
| 88 |
private CommandInterface setQueryTimeout, getQueryTimeout; |
| 89 |
|
| 90 |
private int savepointId; |
| 91 |
private String catalog; |
| 92 |
private Statement executingStatement; |
| 93 |
private final CloseWatcher watcher; |
| 94 |
private int queryTimeoutCache = -1; |
| 95 |
|
| 96 |
private Map<String, String> clientInfo; |
| 97 |
|
| 98 |
/** |
| 99 |
* INTERNAL |
| 100 |
* the session closable object does not leak as Eclipse warns - due to the |
| 101 |
* CloseWatcher. |
| 102 |
* @param url of this connection |
| 103 |
* @param info of this connection |
| 104 |
* @param user of this connection |
| 105 |
* @param password for the user |
| 106 |
* @param forbidCreation whether database creation is forbidden |
| 107 |
* @throws SQLException on failure |
| 108 |
*/ |
| 109 |
@SuppressWarnings("resource") |
| 110 |
public JdbcConnection(String url, Properties info, String user, Object password, boolean forbidCreation) |
| 111 |
throws SQLException { |
| 112 |
try { |
| 113 |
ConnectionInfo ci = new ConnectionInfo(url, info, user, password); |
| 114 |
if (forbidCreation) { |
| 115 |
ci.setProperty("FORBID_CREATION", "TRUE"); |
| 116 |
} |
| 117 |
String baseDir = SysProperties.getBaseDir(); |
| 118 |
if (baseDir != null) { |
| 119 |
ci.setBaseDir(baseDir); |
| 120 |
} |
| 121 |
// this will return an embedded or server connection |
| 122 |
session = new SessionRemote(ci).connectEmbeddedOrServer(false); |
| 123 |
setTrace(session.getTrace(), TraceObject.CONNECTION, getNextId(TraceObject.CONNECTION)); |
| 124 |
this.user = ci.getUserName(); |
| 125 |
if (isInfoEnabled()) { |
| 126 |
trace.infoCode("Connection " + getTraceObjectName() |
| 127 |
+ " = DriverManager.getConnection(" |
| 128 |
+ quote(ci.getOriginalURL()) + ", " + quote(this.user) |
| 129 |
+ ", \"\");"); |
| 130 |
} |
| 131 |
this.url = ci.getURL(); |
| 132 |
closeOld(); |
| 133 |
watcher = CloseWatcher.register(this, session, keepOpenStackTrace); |
| 134 |
} catch (Exception e) { |
| 135 |
throw logAndConvert(e); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* INTERNAL |
| 141 |
* @param clone connection to clone |
| 142 |
*/ |
| 143 |
public JdbcConnection(JdbcConnection clone) { |
| 144 |
this.session = clone.session; |
| 145 |
setTrace(session.getTrace(), TraceObject.CONNECTION, getNextId(TraceObject.CONNECTION)); |
| 146 |
this.user = clone.user; |
| 147 |
this.url = clone.url; |
| 148 |
this.catalog = clone.catalog; |
| 149 |
this.commit = clone.commit; |
| 150 |
this.getGeneratedKeys = clone.getGeneratedKeys; |
| 151 |
this.getQueryTimeout = clone.getQueryTimeout; |
| 152 |
this.getReadOnly = clone.getReadOnly; |
| 153 |
this.rollback = clone.rollback; |
| 154 |
this.watcher = null; |
| 155 |
if (clone.clientInfo != null) { |
| 156 |
this.clientInfo = new HashMap<>(clone.clientInfo); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* INTERNAL |
| 162 |
* @param session of this connection |
| 163 |
* @param user of this connection |
| 164 |
* @param url of this connection |
| 165 |
*/ |
| 166 |
public JdbcConnection(Session session, String user, String url) { |
| 167 |
this.session = session; |
| 168 |
setTrace(session.getTrace(), TraceObject.CONNECTION, getNextId(TraceObject.CONNECTION)); |
| 169 |
this.user = user; |
| 170 |
this.url = url; |
| 171 |
this.watcher = null; |
| 172 |
} |
| 173 |
|
| 174 |
private void closeOld() { |
| 175 |
while (true) { |
| 176 |
CloseWatcher w = CloseWatcher.pollUnclosed(); |
| 177 |
if (w == null) { |
| 178 |
break; |
| 179 |
} |
| 180 |
try { |
| 181 |
w.getCloseable().close(); |
| 182 |
} catch (Exception e) { |
| 183 |
trace.error(e, "closing session"); |
| 184 |
} |
| 185 |
// there was an unclosed object - |
| 186 |
// keep the stack trace from now on |
| 187 |
keepOpenStackTrace = true; |
| 188 |
String s = w.getOpenStackTrace(); |
| 189 |
Exception ex = DbException |
| 190 |
.get(ErrorCode.TRACE_CONNECTION_NOT_CLOSED); |
| 191 |
trace.error(ex, s); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Creates a new statement. |
| 197 |
* |
| 198 |
* @return the new statement |
| 199 |
* @throws SQLException if the connection is closed |
| 200 |
*/ |
| 201 |
@Override |
| 202 |
public Statement createStatement() throws SQLException { |
| 203 |
try { |
| 204 |
int id = getNextId(TraceObject.STATEMENT); |
| 205 |
debugCodeAssign("Statement", TraceObject.STATEMENT, id, "createStatement()"); |
| 206 |
checkClosed(); |
| 207 |
return new JdbcStatement(this, id, ResultSet.TYPE_FORWARD_ONLY, Constants.DEFAULT_RESULT_SET_CONCURRENCY); |
| 208 |
} catch (Exception e) { |
| 209 |
throw logAndConvert(e); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Creates a statement with the specified result set type and concurrency. |
| 215 |
* |
| 216 |
* @param resultSetType the result set type (ResultSet.TYPE_*) |
| 217 |
* @param resultSetConcurrency the concurrency (ResultSet.CONCUR_*) |
| 218 |
* @return the statement |
| 219 |
* @throws SQLException if the connection is closed or the result set type |
| 220 |
* or concurrency are not supported |
| 221 |
*/ |
| 222 |
@Override |
| 223 |
public Statement createStatement(int resultSetType, |
| 224 |
int resultSetConcurrency) throws SQLException { |
| 225 |
try { |
| 226 |
int id = getNextId(TraceObject.STATEMENT); |
| 227 |
if (isDebugEnabled()) { |
| 228 |
debugCodeAssign("Statement", TraceObject.STATEMENT, id, |
| 229 |
"createStatement(" + resultSetType + ", " + resultSetConcurrency + ')'); |
| 230 |
} |
| 231 |
checkTypeConcurrency(resultSetType, resultSetConcurrency); |
| 232 |
checkClosed(); |
| 233 |
return new JdbcStatement(this, id, resultSetType, resultSetConcurrency); |
| 234 |
} catch (Exception e) { |
| 235 |
throw logAndConvert(e); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Creates a statement with the specified result set type, concurrency, and |
| 241 |
* holdability. |
| 242 |
* |
| 243 |
* @param resultSetType the result set type (ResultSet.TYPE_*) |
| 244 |
* @param resultSetConcurrency the concurrency (ResultSet.CONCUR_*) |
| 245 |
* @param resultSetHoldability the holdability (ResultSet.HOLD* / CLOSE*) |
| 246 |
* @return the statement |
| 247 |
* @throws SQLException if the connection is closed or the result set type, |
| 248 |
* concurrency, or holdability are not supported |
| 249 |
*/ |
| 250 |
@Override |
| 251 |
public Statement createStatement(int resultSetType, |
| 252 |
int resultSetConcurrency, int resultSetHoldability) |
| 253 |
throws SQLException { |
| 254 |
try { |
| 255 |
int id = getNextId(TraceObject.STATEMENT); |
| 256 |
if (isDebugEnabled()) { |
| 257 |
debugCodeAssign("Statement", TraceObject.STATEMENT, id, |
| 258 |
"createStatement(" + resultSetType + ", " |
| 259 |
+ resultSetConcurrency + ", " |
| 260 |
+ resultSetHoldability + ')'); |
| 261 |
} |
| 262 |
checkTypeConcurrency(resultSetType, resultSetConcurrency); |
| 263 |
checkHoldability(resultSetHoldability); |
| 264 |
checkClosed(); |
| 265 |
return new JdbcStatement(this, id, resultSetType, resultSetConcurrency); |
| 266 |
} catch (Exception e) { |
| 267 |
throw logAndConvert(e); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Creates a new prepared statement. |
| 273 |
* |
| 274 |
* @param sql the SQL statement |
| 275 |
* @return the prepared statement |
| 276 |
* @throws SQLException if the connection is closed |
| 277 |
*/ |
| 278 |
@Override |
| 279 |
public PreparedStatement prepareStatement(String sql) throws SQLException { |
| 280 |
try { |
| 281 |
int id = getNextId(TraceObject.PREPARED_STATEMENT); |
| 282 |
if (isDebugEnabled()) { |
| 283 |
debugCodeAssign("PreparedStatement", TraceObject.PREPARED_STATEMENT, id, |
| 284 |
"prepareStatement(" + quote(sql) + ')'); |
| 285 |
} |
| 286 |
checkClosed(); |
| 287 |
sql = translateSQL(sql); |
| 288 |
return new JdbcPreparedStatement(this, sql, id, ResultSet.TYPE_FORWARD_ONLY, |
| 289 |
Constants.DEFAULT_RESULT_SET_CONCURRENCY, null); |
| 290 |
} catch (Exception e) { |
| 291 |
throw logAndConvert(e); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Gets the database meta data for this database. |
| 297 |
* |
| 298 |
* @return the database meta data |
| 299 |
* @throws SQLException if the connection is closed |
| 300 |
*/ |
| 301 |
@Override |
| 302 |
public DatabaseMetaData getMetaData() throws SQLException { |
| 303 |
try { |
| 304 |
int id = getNextId(TraceObject.DATABASE_META_DATA); |
| 305 |
debugCodeAssign("DatabaseMetaData", TraceObject.DATABASE_META_DATA, id, "getMetaData()"); |
| 306 |
checkClosed(); |
| 307 |
return new JdbcDatabaseMetaData(this, trace, id); |
| 308 |
} catch (Exception e) { |
| 309 |
throw logAndConvert(e); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* INTERNAL |
| 315 |
* @return session |
| 316 |
*/ |
| 317 |
public Session getSession() { |
| 318 |
return session; |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Closes this connection. All open statements, prepared statements and |
| 323 |
* result sets that where created by this connection become invalid after |
| 324 |
* calling this method. If there is an uncommitted transaction, it will be |
| 325 |
* rolled back. |
| 326 |
*/ |
| 327 |
@Override |
| 328 |
public synchronized void close() throws SQLException { |
| 329 |
try { |
| 330 |
debugCodeCall("close"); |
| 331 |
if (session == null) { |
| 332 |
return; |
| 333 |
} |
| 334 |
CloseWatcher.unregister(watcher); |
| 335 |
session.cancel(); |
| 336 |
synchronized (session) { |
| 337 |
if (executingStatement != null) { |
| 338 |
try { |
| 339 |
executingStatement.cancel(); |
| 340 |
} catch (NullPointerException | SQLException e) { |
| 341 |
// ignore |
| 342 |
} |
| 343 |
} |
| 344 |
try { |
| 345 |
if (!session.isClosed()) { |
| 346 |
try { |
| 347 |
if (session.hasPendingTransaction()) { |
| 348 |
try { |
| 349 |
rollbackInternal(); |
| 350 |
} catch (DbException e) { |
| 351 |
// ignore if the connection is broken or database shut down |
| 352 |
if (e.getErrorCode() != ErrorCode.CONNECTION_BROKEN_1 && |
| 353 |
e.getErrorCode() != ErrorCode.DATABASE_IS_CLOSED) { |
| 354 |
throw e; |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
closePreparedCommands(); |
| 359 |
} finally { |
| 360 |
session.close(); |
| 361 |
} |
| 362 |
} |
| 363 |
} finally { |
| 364 |
session = null; |
| 365 |
} |
| 366 |
} |
| 367 |
} catch (Throwable e) { |
| 368 |
throw logAndConvert(e); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
private void closePreparedCommands() { |
| 373 |
commit = closeAndSetNull(commit); |
| 374 |
rollback = closeAndSetNull(rollback); |
| 375 |
getReadOnly = closeAndSetNull(getReadOnly); |
| 376 |
getGeneratedKeys = closeAndSetNull(getGeneratedKeys); |
| 377 |
getQueryTimeout = closeAndSetNull(getQueryTimeout); |
| 378 |
setQueryTimeout = closeAndSetNull(setQueryTimeout); |
| 379 |
} |
| 380 |
|
| 381 |
private static CommandInterface closeAndSetNull(CommandInterface command) { |
| 382 |
if (command != null) { |
| 383 |
command.close(); |
| 384 |
} |
| 385 |
return null; |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Switches auto commit on or off. Enabling it commits an uncommitted |
| 390 |
* transaction, if there is one. |
| 391 |
* |
| 392 |
* @param autoCommit true for auto commit on, false for off |
| 393 |
* @throws SQLException if the connection is closed |
| 394 |
*/ |
| 395 |
@Override |
| 396 |
public synchronized void setAutoCommit(boolean autoCommit) |
| 397 |
throws SQLException { |
| 398 |
try { |
| 399 |
if (isDebugEnabled()) { |
| 400 |
debugCode("setAutoCommit(" + autoCommit + ')'); |
| 401 |
} |
| 402 |
checkClosed(); |
| 403 |
synchronized (session) { |
| 404 |
if (autoCommit && !session.getAutoCommit()) { |
| 405 |
commit(); |
| 406 |
} |
| 407 |
session.setAutoCommit(autoCommit); |
| 408 |
} |
| 409 |
} catch (Exception e) { |
| 410 |
throw logAndConvert(e); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
/** |
| 415 |
* Gets the current setting for auto commit. |
| 416 |
* |
| 417 |
* @return true for on, false for off |
| 418 |
* @throws SQLException if the connection is closed |
| 419 |
*/ |
| 420 |
@Override |
| 421 |
public synchronized boolean getAutoCommit() throws SQLException { |
| 422 |
try { |
| 423 |
checkClosed(); |
| 424 |
debugCodeCall("getAutoCommit"); |
| 425 |
return session.getAutoCommit(); |
| 426 |
} catch (Exception e) { |
| 427 |
throw logAndConvert(e); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
* Commits the current transaction. This call has only an effect if auto |
| 433 |
* commit is switched off. |
| 434 |
* |
| 435 |
* @throws SQLException if the connection is closed |
| 436 |
*/ |
| 437 |
@Override |
| 438 |
public synchronized void commit() throws SQLException { |
| 439 |
try { |
| 440 |
debugCodeCall("commit"); |
| 441 |
checkClosed(); |
| 442 |
if (SysProperties.FORCE_AUTOCOMMIT_OFF_ON_COMMIT |
| 443 |
&& getAutoCommit()) { |
| 444 |
throw DbException.get(ErrorCode.METHOD_DISABLED_ON_AUTOCOMMIT_TRUE, "commit()"); |
| 445 |
} |
| 446 |
commit = prepareCommand("COMMIT", commit); |
| 447 |
commit.executeUpdate(null); |
| 448 |
} catch (Exception e) { |
| 449 |
throw logAndConvert(e); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* Rolls back the current transaction. This call has only an effect if auto |
| 455 |
* commit is switched off. |
| 456 |
* |
| 457 |
* @throws SQLException if the connection is closed |
| 458 |
*/ |
| 459 |
@Override |
| 460 |
public synchronized void rollback() throws SQLException { |
| 461 |
try { |
| 462 |
debugCodeCall("rollback"); |
| 463 |
checkClosed(); |
| 464 |
if (SysProperties.FORCE_AUTOCOMMIT_OFF_ON_COMMIT |
| 465 |
&& getAutoCommit()) { |
| 466 |
throw DbException.get(ErrorCode.METHOD_DISABLED_ON_AUTOCOMMIT_TRUE, "rollback()"); |
| 467 |
} |
| 468 |
rollbackInternal(); |
| 469 |
} catch (Exception e) { |
| 470 |
throw logAndConvert(e); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
/** |
| 475 |
* Returns true if this connection has been closed. |
| 476 |
* |
| 477 |
* @return true if close was called |
| 478 |
*/ |
| 479 |
@Override |
| 480 |
public boolean isClosed() throws SQLException { |
| 481 |
try { |
| 482 |
debugCodeCall("isClosed"); |
| 483 |
return session == null || session.isClosed(); |
| 484 |
} catch (Exception e) { |
| 485 |
throw logAndConvert(e); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
/** |
| 490 |
* Translates a SQL statement into the database grammar. |
| 491 |
* |
| 492 |
* @param sql the SQL statement with or without JDBC escape sequences |
| 493 |
* @return the translated statement |
| 494 |
* @throws SQLException if the connection is closed |
| 495 |
*/ |
| 496 |
@Override |
| 497 |
public String nativeSQL(String sql) throws SQLException { |
| 498 |
try { |
| 499 |
debugCodeCall("nativeSQL", sql); |
| 500 |
checkClosed(); |
| 501 |
return translateSQL(sql); |
| 502 |
} catch (Exception e) { |
| 503 |
throw logAndConvert(e); |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
/** |
| 508 |
* According to the JDBC specs, this setting is only a hint to the database |
| 509 |
* to enable optimizations - it does not cause writes to be prohibited. |
| 510 |
* |
| 511 |
* @param readOnly ignored |
| 512 |
* @throws SQLException if the connection is closed |
| 513 |
*/ |
| 514 |
@Override |
| 515 |
public void setReadOnly(boolean readOnly) throws SQLException { |
| 516 |
try { |
| 517 |
if (isDebugEnabled()) { |
| 518 |
debugCode("setReadOnly(" + readOnly + ')'); |
| 519 |
} |
| 520 |
checkClosed(); |
| 521 |
} catch (Exception e) { |
| 522 |
throw logAndConvert(e); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
/** |
| 527 |
* Returns true if the database is read-only. |
| 528 |
* |
| 529 |
* @return if the database is read-only |
| 530 |
* @throws SQLException if the connection is closed |
| 531 |
*/ |
| 532 |
@Override |
| 533 |
public boolean isReadOnly() throws SQLException { |
| 534 |
try { |
| 535 |
debugCodeCall("isReadOnly"); |
| 536 |
checkClosed(); |
| 537 |
getReadOnly = prepareCommand("CALL READONLY()", getReadOnly); |
| 538 |
ResultInterface result = getReadOnly.executeQuery(0, false); |
| 539 |
result.next(); |
| 540 |
return result.currentRow()[0].getBoolean(); |
| 541 |
} catch (Exception e) { |
| 542 |
throw logAndConvert(e); |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
/** |
| 547 |
* Set the default catalog name. This call is ignored. |
| 548 |
* |
| 549 |
* @param catalog ignored |
| 550 |
* @throws SQLException if the connection is closed |
| 551 |
*/ |
| 552 |
@Override |
| 553 |
public void setCatalog(String catalog) throws SQLException { |
| 554 |
try { |
| 555 |
debugCodeCall("setCatalog", catalog); |
| 556 |
checkClosed(); |
| 557 |
} catch (Exception e) { |
| 558 |
throw logAndConvert(e); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
/** |
| 563 |
* Gets the current catalog name. |
| 564 |
* |
| 565 |
* @return the catalog name |
| 566 |
* @throws SQLException if the connection is closed |
| 567 |
*/ |
| 568 |
@Override |
| 569 |
public String getCatalog() throws SQLException { |
| 570 |
try { |
| 571 |
debugCodeCall("getCatalog"); |
| 572 |
checkClosed(); |
| 573 |
if (catalog == null) { |
| 574 |
CommandInterface cat = prepareCommand("CALL DATABASE()", |
| 575 |
Integer.MAX_VALUE); |
| 576 |
ResultInterface result = cat.executeQuery(0, false); |
| 577 |
result.next(); |
| 578 |
catalog = result.currentRow()[0].getString(); |
| 579 |
cat.close(); |
| 580 |
} |
| 581 |
return catalog; |
| 582 |
} catch (Exception e) { |
| 583 |
throw logAndConvert(e); |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
/** |
| 588 |
* Gets the first warning reported by calls on this object. |
| 589 |
* |
| 590 |
* @return null |
| 591 |
*/ |
| 592 |
@Override |
| 593 |
public SQLWarning getWarnings() throws SQLException { |
| 594 |
try { |
| 595 |
debugCodeCall("getWarnings"); |
| 596 |
checkClosed(); |
| 597 |
return null; |
| 598 |
} catch (Exception e) { |
| 599 |
throw logAndConvert(e); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
/** |
| 604 |
* Clears all warnings. |
| 605 |
*/ |
| 606 |
@Override |
| 607 |
public void clearWarnings() throws SQLException { |
| 608 |
try { |
| 609 |
debugCodeCall("clearWarnings"); |
| 610 |
checkClosed(); |
| 611 |
} catch (Exception e) { |
| 612 |
throw logAndConvert(e); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
/** |
| 617 |
* Creates a prepared statement with the specified result set type and |
| 618 |
* concurrency. |
| 619 |
* |
| 620 |
* @param sql the SQL statement |
| 621 |
* @param resultSetType the result set type (ResultSet.TYPE_*) |
| 622 |
* @param resultSetConcurrency the concurrency (ResultSet.CONCUR_*) |
| 623 |
* @return the prepared statement |
| 624 |
* @throws SQLException if the connection is closed or the result set type |
| 625 |
* or concurrency are not supported |
| 626 |
*/ |
| 627 |
@Override |
| 628 |
public PreparedStatement prepareStatement(String sql, int resultSetType, |
| 629 |
int resultSetConcurrency) throws SQLException { |
| 630 |
try { |
| 631 |
int id = getNextId(TraceObject.PREPARED_STATEMENT); |
| 632 |
if (isDebugEnabled()) { |
| 633 |
debugCodeAssign("PreparedStatement", TraceObject.PREPARED_STATEMENT, id, |
| 634 |
"prepareStatement(" + quote(sql) + ", " + resultSetType + ", " + resultSetConcurrency + ')'); |
| 635 |
} |
| 636 |
checkTypeConcurrency(resultSetType, resultSetConcurrency); |
| 637 |
checkClosed(); |
| 638 |
sql = translateSQL(sql); |
| 639 |
return new JdbcPreparedStatement(this, sql, id, resultSetType, resultSetConcurrency, null); |
| 640 |
} catch (Exception e) { |
| 641 |
throw logAndConvert(e); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
/** |
| 646 |
* Changes the current transaction isolation level. Calling this method will |
| 647 |
* commit an open transaction, even if the new level is the same as the old |
| 648 |
* one. |
| 649 |
* |
| 650 |
* @param level the new transaction isolation level: |
| 651 |
* Connection.TRANSACTION_READ_UNCOMMITTED, |
| 652 |
* Connection.TRANSACTION_READ_COMMITTED, |
| 653 |
* Connection.TRANSACTION_REPEATABLE_READ, |
| 654 |
* 6 (SNAPSHOT), or |
| 655 |
* Connection.TRANSACTION_SERIALIZABLE |
| 656 |
* @throws SQLException if the connection is closed or the isolation level |
| 657 |
* is not valid |
| 658 |
*/ |
| 659 |
@Override |
| 660 |
public void setTransactionIsolation(int level) throws SQLException { |
| 661 |
try { |
| 662 |
debugCodeCall("setTransactionIsolation", level); |
| 663 |
checkClosed(); |
| 664 |
if (!getAutoCommit()) { |
| 665 |
commit(); |
| 666 |
} |
| 667 |
session.setIsolationLevel(IsolationLevel.fromJdbc(level)); |
| 668 |
} catch (Exception e) { |
| 669 |
throw logAndConvert(e); |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
/** |
| 674 |
* INTERNAL |
| 675 |
*/ |
| 676 |
void setQueryTimeout(int seconds) throws SQLException { |
| 677 |
try { |
| 678 |
debugCodeCall("setQueryTimeout", seconds); |
| 679 |
checkClosed(); |
| 680 |
setQueryTimeout = prepareCommand("SET QUERY_TIMEOUT ?", |
| 681 |
setQueryTimeout); |
| 682 |
setQueryTimeout.getParameters().get(0) |
| 683 |
.setValue(ValueInteger.get(seconds * 1000), false); |
| 684 |
setQueryTimeout.executeUpdate(null); |
| 685 |
queryTimeoutCache = seconds; |
| 686 |
} catch (Exception e) { |
| 687 |
throw logAndConvert(e); |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
/** |
| 692 |
* INTERNAL |
| 693 |
*/ |
| 694 |
int getQueryTimeout() throws SQLException { |
| 695 |
try { |
| 696 |
if (queryTimeoutCache == -1) { |
| 697 |
checkClosed(); |
| 698 |
getQueryTimeout = prepareCommand(!session.isOldInformationSchema() |
| 699 |
? "SELECT SETTING_VALUE FROM INFORMATION_SCHEMA.SETTINGS WHERE SETTING_NAME=?" |
| 700 |
: "SELECT `VALUE` FROM INFORMATION_SCHEMA.SETTINGS WHERE NAME=?", getQueryTimeout); |
| 701 |
getQueryTimeout.getParameters().get(0) |
| 702 |
.setValue(ValueVarchar.get("QUERY_TIMEOUT"), false); |
| 703 |
ResultInterface result = getQueryTimeout.executeQuery(0, false); |
| 704 |
result.next(); |
| 705 |
int queryTimeout = result.currentRow()[0].getInt(); |
| 706 |
result.close(); |
| 707 |
if (queryTimeout != 0) { |
| 708 |
// round to the next second, otherwise 999 millis would |
| 709 |
// return 0 seconds |
| 710 |
queryTimeout = (queryTimeout + 999) / 1000; |
| 711 |
} |
| 712 |
queryTimeoutCache = queryTimeout; |
| 713 |
} |
| 714 |
return queryTimeoutCache; |
| 715 |
} catch (Exception e) { |
| 716 |
throw logAndConvert(e); |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
/** |
| 721 |
* Returns the current transaction isolation level. |
| 722 |
* |
| 723 |
* @return the isolation level |
| 724 |
* @throws SQLException if the connection is closed |
| 725 |
*/ |
| 726 |
@Override |
| 727 |
public int getTransactionIsolation() throws SQLException { |
| 728 |
try { |
| 729 |
debugCodeCall("getTransactionIsolation"); |
| 730 |
checkClosed(); |
| 731 |
return session.getIsolationLevel().getJdbc(); |
| 732 |
} catch (Exception e) { |
| 733 |
throw logAndConvert(e); |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
/** |
| 738 |
* Changes the current result set holdability. |
| 739 |
* |
| 740 |
* @param holdability ResultSet.HOLD_CURSORS_OVER_COMMIT or |
| 741 |
* ResultSet.CLOSE_CURSORS_AT_COMMIT; |
| 742 |
* @throws SQLException if the connection is closed or the holdability is |
| 743 |
* not supported |
| 744 |
*/ |
| 745 |
@Override |
| 746 |
public void setHoldability(int holdability) throws SQLException { |
| 747 |
try { |
| 748 |
debugCodeCall("setHoldability", holdability); |
| 749 |
checkClosed(); |
| 750 |
checkHoldability(holdability); |
| 751 |
this.holdability = holdability; |
| 752 |
} catch (Exception e) { |
| 753 |
throw logAndConvert(e); |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
/** |
| 758 |
* Returns the current result set holdability. |
| 759 |
* |
| 760 |
* @return the holdability |
| 761 |
* @throws SQLException if the connection is closed |
| 762 |
*/ |
| 763 |
@Override |
| 764 |
public int getHoldability() throws SQLException { |
| 765 |
try { |
| 766 |
debugCodeCall("getHoldability"); |
| 767 |
checkClosed(); |
| 768 |
return holdability; |
| 769 |
} catch (Exception e) { |
| 770 |
throw logAndConvert(e); |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
/** |
| 775 |
* Gets the type map. |
| 776 |
* |
| 777 |
* @return null |
| 778 |
* @throws SQLException if the connection is closed |
| 779 |
*/ |
| 780 |
@Override |
| 781 |
public Map<String, Class<?>> getTypeMap() throws SQLException { |
| 782 |
try { |
| 783 |
debugCodeCall("getTypeMap"); |
| 784 |
checkClosed(); |
| 785 |
return null; |
| 786 |
} catch (Exception e) { |
| 787 |
throw logAndConvert(e); |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
/** |
| 792 |
* [Partially supported] Sets the type map. This is only supported if the |
| 793 |
* map is empty or null. |
| 794 |
*/ |
| 795 |
@Override |
| 796 |
public void setTypeMap(Map<String, Class<?>> map) throws SQLException { |
| 797 |
try { |
| 798 |
if (isDebugEnabled()) { |
| 799 |
debugCode("setTypeMap(" + quoteMap(map) + ')'); |
| 800 |
} |
| 801 |
checkMap(map); |
| 802 |
} catch (Exception e) { |
| 803 |
throw logAndConvert(e); |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
/** |
| 808 |
* Creates a new callable statement. |
| 809 |
* |
| 810 |
* @param sql the SQL statement |
| 811 |
* @return the callable statement |
| 812 |
* @throws SQLException if the connection is closed or the statement is not |
| 813 |
* valid |
| 814 |
*/ |
| 815 |
@Override |
| 816 |
public CallableStatement prepareCall(String sql) throws SQLException { |
| 817 |
try { |
| 818 |
int id = getNextId(TraceObject.CALLABLE_STATEMENT); |
| 819 |
if (isDebugEnabled()) { |
| 820 |
debugCodeAssign("CallableStatement", TraceObject.CALLABLE_STATEMENT, id, |
| 821 |
"prepareCall(" + quote(sql) + ')'); |
| 822 |
} |
| 823 |
checkClosed(); |
| 824 |
sql = translateSQL(sql); |
| 825 |
return new JdbcCallableStatement(this, sql, id, |
| 826 |
ResultSet.TYPE_FORWARD_ONLY, |
| 827 |
Constants.DEFAULT_RESULT_SET_CONCURRENCY); |
| 828 |
} catch (Exception e) { |
| 829 |
throw logAndConvert(e); |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
/** |
| 834 |
* Creates a callable statement with the specified result set type and |
| 835 |
* concurrency. |
| 836 |
* |
| 837 |
* @param sql the SQL statement |
| 838 |
* @param resultSetType the result set type (ResultSet.TYPE_*) |
| 839 |
* @param resultSetConcurrency the concurrency (ResultSet.CONCUR_*) |
| 840 |
* @return the callable statement |
| 841 |
* @throws SQLException if the connection is closed or the result set type |
| 842 |
* or concurrency are not supported |
| 843 |
*/ |
| 844 |
@Override |
| 845 |
public CallableStatement prepareCall(String sql, int resultSetType, |
| 846 |
int resultSetConcurrency) throws SQLException { |
| 847 |
try { |
| 848 |
int id = getNextId(TraceObject.CALLABLE_STATEMENT); |
| 849 |
if (isDebugEnabled()) { |
| 850 |
debugCodeAssign("CallableStatement", TraceObject.CALLABLE_STATEMENT, id, |
| 851 |
"prepareCall(" + quote(sql) + ", " + resultSetType + ", " + resultSetConcurrency + ')'); |
| 852 |
} |
| 853 |
checkTypeConcurrency(resultSetType, resultSetConcurrency); |
| 854 |
checkClosed(); |
| 855 |
sql = translateSQL(sql); |
| 856 |
return new JdbcCallableStatement(this, sql, id, resultSetType, |
| 857 |
resultSetConcurrency); |
| 858 |
} catch (Exception e) { |
| 859 |
throw logAndConvert(e); |
| 860 |
} |
| 861 |
} |
| 862 |
|
| 863 |
/** |
| 864 |
* Creates a callable statement with the specified result set type, |
| 865 |
* concurrency, and holdability. |
| 866 |
* |
| 867 |
* @param sql the SQL statement |
| 868 |
* @param resultSetType the result set type (ResultSet.TYPE_*) |
| 869 |
* @param resultSetConcurrency the concurrency (ResultSet.CONCUR_*) |
| 870 |
* @param resultSetHoldability the holdability (ResultSet.HOLD* / CLOSE*) |
| 871 |
* @return the callable statement |
| 872 |
* @throws SQLException if the connection is closed or the result set type, |
| 873 |
* concurrency, or holdability are not supported |
| 874 |
*/ |
| 875 |
@Override |
| 876 |
public CallableStatement prepareCall(String sql, int resultSetType, |
| 877 |
int resultSetConcurrency, int resultSetHoldability) |
| 878 |
throws SQLException { |
| 879 |
try { |
| 880 |
int id = getNextId(TraceObject.CALLABLE_STATEMENT); |
| 881 |
if (isDebugEnabled()) { |
| 882 |
debugCodeAssign("CallableStatement", TraceObject.CALLABLE_STATEMENT, id, |
| 883 |
"prepareCall(" + quote(sql) + ", " + resultSetType + ", " + resultSetConcurrency + ", " |
| 884 |
+ resultSetHoldability + ')'); |
| 885 |
} |
| 886 |
checkTypeConcurrency(resultSetType, resultSetConcurrency); |
| 887 |
checkHoldability(resultSetHoldability); |
| 888 |
checkClosed(); |
| 889 |
sql = translateSQL(sql); |
| 890 |
return new JdbcCallableStatement(this, sql, id, resultSetType, |
| 891 |
resultSetConcurrency); |
| 892 |
} catch (Exception e) { |
| 893 |
throw logAndConvert(e); |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
/** |
| 898 |
* Creates a new unnamed savepoint. |
| 899 |
* |
| 900 |
* @return the new savepoint |
| 901 |
*/ |
| 902 |
@Override |
| 903 |
public Savepoint setSavepoint() throws SQLException { |
| 904 |
try { |
| 905 |
int id = getNextId(TraceObject.SAVEPOINT); |
| 906 |
debugCodeAssign("Savepoint", TraceObject.SAVEPOINT, id, "setSavepoint()"); |
| 907 |
checkClosed(); |
| 908 |
CommandInterface set = prepareCommand( |
| 909 |
"SAVEPOINT " + JdbcSavepoint.getName(null, savepointId), |
| 910 |
Integer.MAX_VALUE); |
| 911 |
set.executeUpdate(null); |
| 912 |
JdbcSavepoint savepoint = new JdbcSavepoint(this, savepointId, null, |
| 913 |
trace, id); |
| 914 |
savepointId++; |
| 915 |
return savepoint; |
| 916 |
} catch (Exception e) { |
| 917 |
throw logAndConvert(e); |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
/** |
| 922 |
* Creates a new named savepoint. |
| 923 |
* |
| 924 |
* @param name the savepoint name |
| 925 |
* @return the new savepoint |
| 926 |
*/ |
| 927 |
@Override |
| 928 |
public Savepoint setSavepoint(String name) throws SQLException { |
| 929 |
try { |
| 930 |
int id = getNextId(TraceObject.SAVEPOINT); |
| 931 |
if (isDebugEnabled()) { |
| 932 |
debugCodeAssign("Savepoint", TraceObject.SAVEPOINT, id, "setSavepoint(" + quote(name) + ')'); |
| 933 |
} |
| 934 |
checkClosed(); |
| 935 |
CommandInterface set = prepareCommand( |
| 936 |
"SAVEPOINT " + JdbcSavepoint.getName(name, 0), |
| 937 |
Integer.MAX_VALUE); |
| 938 |
set.executeUpdate(null); |
| 939 |
return new JdbcSavepoint(this, 0, name, trace, id); |
| 940 |
} catch (Exception e) { |
| 941 |
throw logAndConvert(e); |
| 942 |
} |
| 943 |
} |
| 944 |
|
| 945 |
/** |
| 946 |
* Rolls back to a savepoint. |
| 947 |
* |
| 948 |
* @param savepoint the savepoint |
| 949 |
*/ |
| 950 |
@Override |
| 951 |
public void rollback(Savepoint savepoint) throws SQLException { |
| 952 |
try { |
| 953 |
JdbcSavepoint sp = convertSavepoint(savepoint); |
| 954 |
if (isDebugEnabled()) { |
| 955 |
debugCode("rollback(" + sp.getTraceObjectName() + ')'); |
| 956 |
} |
| 957 |
checkClosed(); |
| 958 |
sp.rollback(); |
| 959 |
} catch (Exception e) { |
| 960 |
throw logAndConvert(e); |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
/** |
| 965 |
* Releases a savepoint. |
| 966 |
* |
| 967 |
* @param savepoint the savepoint to release |
| 968 |
*/ |
| 969 |
@Override |
| 970 |
public void releaseSavepoint(Savepoint savepoint) throws SQLException { |
| 971 |
try { |
| 972 |
debugCode("releaseSavepoint(savepoint)"); |
| 973 |
checkClosed(); |
| 974 |
convertSavepoint(savepoint).release(); |
| 975 |
} catch (Exception e) { |
| 976 |
throw logAndConvert(e); |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
private static JdbcSavepoint convertSavepoint(Savepoint savepoint) { |
| 981 |
if (!(savepoint instanceof JdbcSavepoint)) { |
| 982 |
throw DbException.get(ErrorCode.SAVEPOINT_IS_INVALID_1, |
| 983 |
String.valueOf(savepoint)); |
| 984 |
} |
| 985 |
return (JdbcSavepoint) savepoint; |
| 986 |
} |
| 987 |
|
| 988 |
/** |
| 989 |
* Creates a prepared statement with the specified result set type, |
| 990 |
* concurrency, and holdability. |
| 991 |
* |
| 992 |
* @param sql the SQL statement |
| 993 |
* @param resultSetType the result set type (ResultSet.TYPE_*) |
| 994 |
* @param resultSetConcurrency the concurrency (ResultSet.CONCUR_*) |
| 995 |
* @param resultSetHoldability the holdability (ResultSet.HOLD* / CLOSE*) |
| 996 |
* @return the prepared statement |
| 997 |
* @throws SQLException if the connection is closed or the result set type, |
| 998 |
* concurrency, or holdability are not supported |
| 999 |
*/ |
| 1000 |
@Override |
| 1001 |
public PreparedStatement prepareStatement(String sql, int resultSetType, |
| 1002 |
int resultSetConcurrency, int resultSetHoldability) |
| 1003 |
throws SQLException { |
| 1004 |
try { |
| 1005 |
int id = getNextId(TraceObject.PREPARED_STATEMENT); |
| 1006 |
if (isDebugEnabled()) { |
| 1007 |
debugCodeAssign("PreparedStatement", TraceObject.PREPARED_STATEMENT, id, |
| 1008 |
"prepareStatement(" + quote(sql) + ", " + resultSetType + ", " + resultSetConcurrency + ", " |
| 1009 |
+ resultSetHoldability + ')'); |
| 1010 |
} |
| 1011 |
checkTypeConcurrency(resultSetType, resultSetConcurrency); |
| 1012 |
checkHoldability(resultSetHoldability); |
| 1013 |
checkClosed(); |
| 1014 |
sql = translateSQL(sql); |
| 1015 |
return new JdbcPreparedStatement(this, sql, id, resultSetType, resultSetConcurrency, null); |
| 1016 |
} catch (Exception e) { |
| 1017 |
throw logAndConvert(e); |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
/** |
| 1022 |
* Creates a new prepared statement. |
| 1023 |
* |
| 1024 |
* @param sql the SQL statement |
| 1025 |
* @param autoGeneratedKeys |
| 1026 |
* {@link Statement#RETURN_GENERATED_KEYS} if generated keys should |
| 1027 |
* be available for retrieval, {@link Statement#NO_GENERATED_KEYS} if |
| 1028 |
* generated keys should not be available |
| 1029 |
* @return the prepared statement |
| 1030 |
* @throws SQLException if the connection is closed |
| 1031 |
*/ |
| 1032 |
@Override |
| 1033 |
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) |
| 1034 |
throws SQLException { |
| 1035 |
try { |
| 1036 |
int id = getNextId(TraceObject.PREPARED_STATEMENT); |
| 1037 |
if (isDebugEnabled()) { |
| 1038 |
debugCodeAssign("PreparedStatement", TraceObject.PREPARED_STATEMENT, id, |
| 1039 |
"prepareStatement(" + quote(sql) + ", " + autoGeneratedKeys + ')'); |
| 1040 |
} |
| 1041 |
checkClosed(); |
| 1042 |
sql = translateSQL(sql); |
| 1043 |
return new JdbcPreparedStatement(this, sql, id, ResultSet.TYPE_FORWARD_ONLY, |
| 1044 |
Constants.DEFAULT_RESULT_SET_CONCURRENCY, autoGeneratedKeys == Statement.RETURN_GENERATED_KEYS); |
| 1045 |
} catch (Exception e) { |
| 1046 |
throw logAndConvert(e); |
| 1047 |
} |
| 1048 |
} |
| 1049 |
|
| 1050 |
/** |
| 1051 |
* Creates a new prepared statement. |
| 1052 |
* |
| 1053 |
* @param sql the SQL statement |
| 1054 |
* @param columnIndexes |
| 1055 |
* an array of column indexes indicating the columns with generated |
| 1056 |
* keys that should be returned from the inserted row |
| 1057 |
* @return the prepared statement |
| 1058 |
* @throws SQLException if the connection is closed |
| 1059 |
*/ |
| 1060 |
@Override |
| 1061 |
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) |
| 1062 |
throws SQLException { |
| 1063 |
try { |
| 1064 |
int id = getNextId(TraceObject.PREPARED_STATEMENT); |
| 1065 |
if (isDebugEnabled()) { |
| 1066 |
debugCodeAssign("PreparedStatement", TraceObject.PREPARED_STATEMENT, id, |
| 1067 |
"prepareStatement(" + quote(sql) + ", " + quoteIntArray(columnIndexes) + ')'); |
| 1068 |
} |
| 1069 |
checkClosed(); |
| 1070 |
sql = translateSQL(sql); |
| 1071 |
return new JdbcPreparedStatement(this, sql, id, ResultSet.TYPE_FORWARD_ONLY, |
| 1072 |
Constants.DEFAULT_RESULT_SET_CONCURRENCY, columnIndexes); |
| 1073 |
} catch (Exception e) { |
| 1074 |
throw logAndConvert(e); |
| 1075 |
} |
| 1076 |
} |
| 1077 |
|
| 1078 |
/** |
| 1079 |
* Creates a new prepared statement. |
| 1080 |
* |
| 1081 |
* @param sql the SQL statement |
| 1082 |
* @param columnNames |
| 1083 |
* an array of column names indicating the columns with generated |
| 1084 |
* keys that should be returned from the inserted row |
| 1085 |
* @return the prepared statement |
| 1086 |
* @throws SQLException if the connection is closed |
| 1087 |
*/ |
| 1088 |
@Override |
| 1089 |
public PreparedStatement prepareStatement(String sql, String[] columnNames) |
| 1090 |
throws SQLException { |
| 1091 |
try { |
| 1092 |
int id = getNextId(TraceObject.PREPARED_STATEMENT); |
| 1093 |
if (isDebugEnabled()) { |
| 1094 |
debugCodeAssign("PreparedStatement", TraceObject.PREPARED_STATEMENT, id, |
| 1095 |
"prepareStatement(" + quote(sql) + ", " + quoteArray(columnNames) + ')'); |
| 1096 |
} |
| 1097 |
checkClosed(); |
| 1098 |
sql = translateSQL(sql); |
| 1099 |
return new JdbcPreparedStatement(this, sql, id, ResultSet.TYPE_FORWARD_ONLY, |
| 1100 |
Constants.DEFAULT_RESULT_SET_CONCURRENCY, columnNames); |
| 1101 |
} catch (Exception e) { |
| 1102 |
throw logAndConvert(e); |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|
| 1106 |
// ============================================================= |
| 1107 |
|
| 1108 |
/** |
| 1109 |
* Prepare an command. This will parse the SQL statement. |
| 1110 |
* |
| 1111 |
* @param sql the SQL statement |
| 1112 |
* @param fetchSize the fetch size (used in remote connections) |
| 1113 |
* @return the command |
| 1114 |
*/ |
| 1115 |
CommandInterface prepareCommand(String sql, int fetchSize) { |
| 1116 |
return session.prepareCommand(sql, fetchSize); |
| 1117 |
} |
| 1118 |
|
| 1119 |
private CommandInterface prepareCommand(String sql, CommandInterface old) { |
| 1120 |
return old == null ? session.prepareCommand(sql, Integer.MAX_VALUE) |
| 1121 |
: old; |
| 1122 |
} |
| 1123 |
|
| 1124 |
private static int translateGetEnd(String sql, int i, char c) { |
| 1125 |
int len = sql.length(); |
| 1126 |
switch (c) { |
| 1127 |
case '$': { |
| 1128 |
if (i < len - 1 && sql.charAt(i + 1) == '$' |
| 1129 |
&& (i == 0 || sql.charAt(i - 1) <= ' ')) { |
| 1130 |
int j = sql.indexOf("$$", i + 2); |
| 1131 |
if (j < 0) { |
| 1132 |
throw DbException.getSyntaxError(sql, i); |
| 1133 |
} |
| 1134 |
return j + 1; |
| 1135 |
} |
| 1136 |
return i; |
| 1137 |
} |
| 1138 |
case '\'': { |
| 1139 |
int j = sql.indexOf('\'', i + 1); |
| 1140 |
if (j < 0) { |
| 1141 |
throw DbException.getSyntaxError(sql, i); |
| 1142 |
} |
| 1143 |
return j; |
| 1144 |
} |
| 1145 |
case '"': { |
| 1146 |
int j = sql.indexOf('"', i + 1); |
| 1147 |
if (j < 0) { |
| 1148 |
throw DbException.getSyntaxError(sql, i); |
| 1149 |
} |
| 1150 |
return j; |
| 1151 |
} |
| 1152 |
case '/': { |
| 1153 |
checkRunOver(i + 1, len, sql); |
| 1154 |
if (sql.charAt(i + 1) == '*') { |
| 1155 |
// block comment |
| 1156 |
int j = sql.indexOf("*/", i + 2); |
| 1157 |
if (j < 0) { |
| 1158 |
throw DbException.getSyntaxError(sql, i); |
| 1159 |
} |
| 1160 |
i = j + 1; |
| 1161 |
} else if (sql.charAt(i + 1) == '/') { |
| 1162 |
// single line comment |
| 1163 |
i += 2; |
| 1164 |
while (i < len && (c = sql.charAt(i)) != '\r' && c != '\n') { |
| 1165 |
i++; |
| 1166 |
} |
| 1167 |
} |
| 1168 |
return i; |
| 1169 |
} |
| 1170 |
case '-': { |
| 1171 |
checkRunOver(i + 1, len, sql); |
| 1172 |
if (sql.charAt(i + 1) == '-') { |
| 1173 |
// single line comment |
| 1174 |
i += 2; |
| 1175 |
while (i < len && (c = sql.charAt(i)) != '\r' && c != '\n') { |
| 1176 |
i++; |
| 1177 |
} |
| 1178 |
} |
| 1179 |
return i; |
| 1180 |
} |
| 1181 |
default: |
| 1182 |
throw DbException.getInternalError("c=" + c); |
| 1183 |
} |
| 1184 |
} |
| 1185 |
|
| 1186 |
/** |
| 1187 |
* Convert JDBC escape sequences in the SQL statement. This method throws an |
| 1188 |
* exception if the SQL statement is null. |
| 1189 |
* |
| 1190 |
* @param sql the SQL statement with or without JDBC escape sequences |
| 1191 |
* @return the SQL statement without JDBC escape sequences |
| 1192 |
*/ |
| 1193 |
private static String translateSQL(String sql) { |
| 1194 |
return translateSQL(sql, true); |
| 1195 |
} |
| 1196 |
|
| 1197 |
/** |
| 1198 |
* Convert JDBC escape sequences in the SQL statement if required. This |
| 1199 |
* method throws an exception if the SQL statement is null. |
| 1200 |
* |
| 1201 |
* @param sql the SQL statement with or without JDBC escape sequences |
| 1202 |
* @param escapeProcessing whether escape sequences should be replaced |
| 1203 |
* @return the SQL statement without JDBC escape sequences |
| 1204 |
*/ |
| 1205 |
static String translateSQL(String sql, boolean escapeProcessing) { |
| 1206 |
if (sql == null) { |
| 1207 |
throw DbException.getInvalidValueException("SQL", null); |
| 1208 |
} |
| 1209 |
if (!escapeProcessing || sql.indexOf('{') < 0) { |
| 1210 |
return sql; |
| 1211 |
} |
| 1212 |
return translateSQLImpl(sql); |
| 1213 |
} |
| 1214 |
|
| 1215 |
private static String translateSQLImpl(String sql) { |
| 1216 |
int len = sql.length(); |
| 1217 |
char[] chars = null; |
| 1218 |
int level = 0; |
| 1219 |
for (int i = 0; i < len; i++) { |
| 1220 |
char c = sql.charAt(i); |
| 1221 |
switch (c) { |
| 1222 |
case '\'': |
| 1223 |
case '"': |
| 1224 |
case '/': |
| 1225 |
case '-': |
| 1226 |
i = translateGetEnd(sql, i, c); |
| 1227 |
break; |
| 1228 |
case '{': |
| 1229 |
level++; |
| 1230 |
if (chars == null) { |
| 1231 |
chars = sql.toCharArray(); |
| 1232 |
} |
| 1233 |
chars[i] = ' '; |
| 1234 |
while (Character.isSpaceChar(chars[i])) { |
| 1235 |
i++; |
| 1236 |
checkRunOver(i, len, sql); |
| 1237 |
} |
| 1238 |
int start = i; |
| 1239 |
if (chars[i] >= '0' && chars[i] <= '9') { |
| 1240 |
chars[i - 1] = '{'; |
| 1241 |
while (true) { |
| 1242 |
checkRunOver(i, len, sql); |
| 1243 |
c = chars[i]; |
| 1244 |
if (c == '}') { |
| 1245 |
break; |
| 1246 |
} |
| 1247 |
switch (c) { |
| 1248 |
case '\'': |
| 1249 |
case '"': |
| 1250 |
case '/': |
| 1251 |
case '-': |
| 1252 |
i = translateGetEnd(sql, i, c); |
| 1253 |
break; |
| 1254 |
default: |
| 1255 |
} |
| 1256 |
i++; |
| 1257 |
} |
| 1258 |
level--; |
| 1259 |
break; |
| 1260 |
} else if (chars[i] == '?') { |
| 1261 |
i++; |
| 1262 |
checkRunOver(i, len, sql); |
| 1263 |
while (Character.isSpaceChar(chars[i])) { |
| 1264 |
i++; |
| 1265 |
checkRunOver(i, len, sql); |
| 1266 |
} |
| 1267 |
if (sql.charAt(i) != '=') { |
| 1268 |
throw DbException.getSyntaxError(sql, i, "="); |
| 1269 |
} |
| 1270 |
i++; |
| 1271 |
checkRunOver(i, len, sql); |
| 1272 |
while (Character.isSpaceChar(chars[i])) { |
| 1273 |
i++; |
| 1274 |
checkRunOver(i, len, sql); |
| 1275 |
} |
| 1276 |
} |
| 1277 |
while (!Character.isSpaceChar(chars[i])) { |
| 1278 |
i++; |
| 1279 |
checkRunOver(i, len, sql); |
| 1280 |
} |
| 1281 |
int remove = 0; |
| 1282 |
if (found(sql, start, "fn")) { |
| 1283 |
remove = 2; |
| 1284 |
} else if (found(sql, start, "escape")) { |
| 1285 |
break; |
| 1286 |
} else if (found(sql, start, "call")) { |
| 1287 |
break; |
| 1288 |
} else if (found(sql, start, "oj")) { |
| 1289 |
remove = 2; |
| 1290 |
} else if (found(sql, start, "ts")) { |
| 1291 |
break; |
| 1292 |
} else if (found(sql, start, "t")) { |
| 1293 |
break; |
| 1294 |
} else if (found(sql, start, "d")) { |
| 1295 |
break; |
| 1296 |
} else if (found(sql, start, "params")) { |
| 1297 |
remove = "params".length(); |
| 1298 |
} |
| 1299 |
for (i = start; remove > 0; i++, remove--) { |
| 1300 |
chars[i] = ' '; |
| 1301 |
} |
| 1302 |
break; |
| 1303 |
case '}': |
| 1304 |
if (--level < 0) { |
| 1305 |
throw DbException.getSyntaxError(sql, i); |
| 1306 |
} |
| 1307 |
chars[i] = ' '; |
| 1308 |
break; |
| 1309 |
case '$': |
| 1310 |
i = translateGetEnd(sql, i, c); |
| 1311 |
break; |
| 1312 |
default: |
| 1313 |
} |
| 1314 |
} |
| 1315 |
if (level != 0) { |
| 1316 |
throw DbException.getSyntaxError(sql, sql.length() - 1); |
| 1317 |
} |
| 1318 |
if (chars != null) { |
| 1319 |
sql = new String(chars); |
| 1320 |
} |
| 1321 |
return sql; |
| 1322 |
} |
| 1323 |
|
| 1324 |
private static void checkRunOver(int i, int len, String sql) { |
| 1325 |
if (i >= len) { |
| 1326 |
throw DbException.getSyntaxError(sql, i); |
| 1327 |
} |
| 1328 |
} |
| 1329 |
|
| 1330 |
private static boolean found(String sql, int start, String other) { |
| 1331 |
return sql.regionMatches(true, start, other, 0, other.length()); |
| 1332 |
} |
| 1333 |
|
| 1334 |
private static void checkTypeConcurrency(int resultSetType, |
| 1335 |
int resultSetConcurrency) { |
| 1336 |
switch (resultSetType) { |
| 1337 |
case ResultSet.TYPE_FORWARD_ONLY: |
| 1338 |
case ResultSet.TYPE_SCROLL_INSENSITIVE: |
| 1339 |
case ResultSet.TYPE_SCROLL_SENSITIVE: |
| 1340 |
break; |
| 1341 |
default: |
| 1342 |
throw DbException.getInvalidValueException("resultSetType", |
| 1343 |
resultSetType); |
| 1344 |
} |
| 1345 |
switch (resultSetConcurrency) { |
| 1346 |
case ResultSet.CONCUR_READ_ONLY: |
| 1347 |
case ResultSet.CONCUR_UPDATABLE: |
| 1348 |
break; |
| 1349 |
default: |
| 1350 |
throw DbException.getInvalidValueException("resultSetConcurrency", |
| 1351 |
resultSetConcurrency); |
| 1352 |
} |
| 1353 |
} |
| 1354 |
|
| 1355 |
private static void checkHoldability(int resultSetHoldability) { |
| 1356 |
// TODO compatibility / correctness: DBPool uses |
| 1357 |
// ResultSet.HOLD_CURSORS_OVER_COMMIT |
| 1358 |
if (resultSetHoldability != ResultSet.HOLD_CURSORS_OVER_COMMIT |
| 1359 |
&& resultSetHoldability != ResultSet.CLOSE_CURSORS_AT_COMMIT) { |
| 1360 |
throw DbException.getInvalidValueException("resultSetHoldability", |
| 1361 |
resultSetHoldability); |
| 1362 |
} |
| 1363 |
} |
| 1364 |
|
| 1365 |
/** |
| 1366 |
* INTERNAL. Check if this connection is closed. |
| 1367 |
* |
| 1368 |
* @throws DbException if the connection or session is closed |
| 1369 |
*/ |
| 1370 |
protected void checkClosed() { |
| 1371 |
if (session == null) { |
| 1372 |
throw DbException.get(ErrorCode.OBJECT_CLOSED); |
| 1373 |
} |
| 1374 |
if (session.isClosed()) { |
| 1375 |
throw DbException.get(ErrorCode.DATABASE_CALLED_AT_SHUTDOWN); |
| 1376 |
} |
| 1377 |
} |
| 1378 |
|
| 1379 |
String getURL() { |
| 1380 |
checkClosed(); |
| 1381 |
return url; |
| 1382 |
} |
| 1383 |
|
| 1384 |
String getUser() { |
| 1385 |
checkClosed(); |
| 1386 |
return user; |
| 1387 |
} |
| 1388 |
|
| 1389 |
private void rollbackInternal() { |
| 1390 |
rollback = prepareCommand("ROLLBACK", rollback); |
| 1391 |
rollback.executeUpdate(null); |
| 1392 |
} |
| 1393 |
|
| 1394 |
/** |
| 1395 |
* INTERNAL |
| 1396 |
*/ |
| 1397 |
void setExecutingStatement(Statement stat) { |
| 1398 |
executingStatement = stat; |
| 1399 |
} |
| 1400 |
|
| 1401 |
/** |
| 1402 |
* Create a new empty Clob object. |
| 1403 |
* |
| 1404 |
* @return the object |
| 1405 |
*/ |
| 1406 |
@Override |
| 1407 |
public Clob createClob() throws SQLException { |
| 1408 |
try { |
| 1409 |
int id = getNextId(TraceObject.CLOB); |
| 1410 |
debugCodeAssign("Clob", TraceObject.CLOB, id, "createClob()"); |
| 1411 |
checkClosed(); |
| 1412 |
return new JdbcClob(this, ValueVarchar.EMPTY, JdbcLob.State.NEW, id); |
| 1413 |
} catch (Exception e) { |
| 1414 |
throw logAndConvert(e); |
| 1415 |
} |
| 1416 |
} |
| 1417 |
|
| 1418 |
/** |
| 1419 |
* Create a new empty Blob object. |
| 1420 |
* |
| 1421 |
* @return the object |
| 1422 |
*/ |
| 1423 |
@Override |
| 1424 |
public Blob createBlob() throws SQLException { |
| 1425 |
try { |
| 1426 |
int id = getNextId(TraceObject.BLOB); |
| 1427 |
debugCodeAssign("Blob", TraceObject.BLOB, id, "createClob()"); |
| 1428 |
checkClosed(); |
| 1429 |
return new JdbcBlob(this, ValueVarbinary.EMPTY, JdbcLob.State.NEW, id); |
| 1430 |
} catch (Exception e) { |
| 1431 |
throw logAndConvert(e); |
| 1432 |
} |
| 1433 |
} |
| 1434 |
|
| 1435 |
/** |
| 1436 |
* Create a new empty NClob object. |
| 1437 |
* |
| 1438 |
* @return the object |
| 1439 |
*/ |
| 1440 |
@Override |
| 1441 |
public NClob createNClob() throws SQLException { |
| 1442 |
try { |
| 1443 |
int id = getNextId(TraceObject.CLOB); |
| 1444 |
debugCodeAssign("NClob", TraceObject.CLOB, id, "createNClob()"); |
| 1445 |
checkClosed(); |
| 1446 |
return new JdbcClob(this, ValueVarchar.EMPTY, JdbcLob.State.NEW, id); |
| 1447 |
} catch (Exception e) { |
| 1448 |
throw logAndConvert(e); |
| 1449 |
} |
| 1450 |
} |
| 1451 |
|
| 1452 |
/** |
| 1453 |
* Create a new SQLXML object with no data. |
| 1454 |
* |
| 1455 |
* @return the object |
| 1456 |
*/ |
| 1457 |
@Override |
| 1458 |
public SQLXML createSQLXML() throws SQLException { |
| 1459 |
try { |
| 1460 |
int id = getNextId(TraceObject.SQLXML); |
| 1461 |
debugCodeAssign("SQLXML", TraceObject.SQLXML, id, "createSQLXML()"); |
| 1462 |
checkClosed(); |
| 1463 |
return new JdbcSQLXML(this, ValueVarchar.EMPTY, JdbcLob.State.NEW, id); |
| 1464 |
} catch (Exception e) { |
| 1465 |
throw logAndConvert(e); |
| 1466 |
} |
| 1467 |
} |
| 1468 |
|
| 1469 |
/** |
| 1470 |
* Create a new Array object. |
| 1471 |
* |
| 1472 |
* @param typeName the type name |
| 1473 |
* @param elements the values |
| 1474 |
* @return the array |
| 1475 |
*/ |
| 1476 |
@Override |
| 1477 |
public Array createArrayOf(String typeName, Object[] elements) |
| 1478 |
throws SQLException { |
| 1479 |
try { |
| 1480 |
int id = getNextId(TraceObject.ARRAY); |
| 1481 |
debugCodeAssign("Array", TraceObject.ARRAY, id, "createArrayOf()"); |
| 1482 |
checkClosed(); |
| 1483 |
Value value = ValueToObjectConverter.objectToValue(session, elements, Value.ARRAY); |
| 1484 |
return new JdbcArray(this, value, id); |
| 1485 |
} catch (Exception e) { |
| 1486 |
throw logAndConvert(e); |
| 1487 |
} |
| 1488 |
} |
| 1489 |
|
| 1490 |
/** |
| 1491 |
* [Not supported] Create a new empty Struct object. |
| 1492 |
*/ |
| 1493 |
@Override |
| 1494 |
public Struct createStruct(String typeName, Object[] attributes) |
| 1495 |
throws SQLException { |
| 1496 |
throw unsupported("Struct"); |
| 1497 |
} |
| 1498 |
|
| 1499 |
/** |
| 1500 |
* Returns true if this connection is still valid. |
| 1501 |
* |
| 1502 |
* @param timeout the number of seconds to wait for the database to respond |
| 1503 |
* (ignored) |
| 1504 |
* @return true if the connection is valid. |
| 1505 |
*/ |
| 1506 |
@Override |
| 1507 |
public synchronized boolean isValid(int timeout) { |
| 1508 |
try { |
| 1509 |
debugCodeCall("isValid", timeout); |
| 1510 |
if (session == null || session.isClosed()) { |
| 1511 |
return false; |
| 1512 |
} |
| 1513 |
// force a network round trip (if networked) |
| 1514 |
getTransactionIsolation(); |
| 1515 |
return true; |
| 1516 |
} catch (Exception e) { |
| 1517 |
// this method doesn't throw an exception, but it logs it |
| 1518 |
logAndConvert(e); |
| 1519 |
return false; |
| 1520 |
} |
| 1521 |
} |
| 1522 |
|
| 1523 |
/** |
| 1524 |
* Set a client property. This method always throws a SQLClientInfoException |
| 1525 |
* in standard mode. In compatibility mode the following properties are |
| 1526 |
* supported: |
| 1527 |
* <ul> |
| 1528 |
* <li>DB2: The properties: ApplicationName, ClientAccountingInformation, |
| 1529 |
* ClientUser and ClientCorrelationToken are supported.</li> |
| 1530 |
* <li>MySQL: All property names are supported.</li> |
| 1531 |
* <li>Oracle: All properties in the form <namespace>.<key name> |
| 1532 |
* are supported.</li> |
| 1533 |
* <li>PostgreSQL: The ApplicationName property is supported.</li> |
| 1534 |
* </ul> |
| 1535 |
* For unsupported properties a SQLClientInfoException is thrown. |
| 1536 |
* |
| 1537 |
* @param name the name of the property |
| 1538 |
* @param value the value |
| 1539 |
*/ |
| 1540 |
@Override |
| 1541 |
public void setClientInfo(String name, String value) |
| 1542 |
throws SQLClientInfoException { |
| 1543 |
try { |
| 1544 |
if (isDebugEnabled()) { |
| 1545 |
debugCode("setClientInfo(" + quote(name) + ", " + quote(value) + ')'); |
| 1546 |
} |
| 1547 |
checkClosed(); |
| 1548 |
|
| 1549 |
// no change to property: Ignore call. This early exit fixes a |
| 1550 |
// problem with websphere liberty resetting the client info of a |
| 1551 |
// pooled connection to its initial values. |
| 1552 |
if (Objects.equals(value, getClientInfo(name))) { |
| 1553 |
return; |
| 1554 |
} |
| 1555 |
|
| 1556 |
if (isInternalProperty(name)) { |
| 1557 |
throw new SQLClientInfoException( |
| 1558 |
"Property name '" + name + " is used internally by H2.", |
| 1559 |
Collections.emptyMap()); |
| 1560 |
} |
| 1561 |
|
| 1562 |
Pattern clientInfoNameRegEx = getMode().supportedClientInfoPropertiesRegEx; |
| 1563 |
|
| 1564 |
if (clientInfoNameRegEx != null |
| 1565 |
&& clientInfoNameRegEx.matcher(name).matches()) { |
| 1566 |
if (clientInfo == null) { |
| 1567 |
clientInfo = new HashMap<>(); |
| 1568 |
} |
| 1569 |
clientInfo.put(name, value); |
| 1570 |
} else { |
| 1571 |
throw new SQLClientInfoException( |
| 1572 |
"Client info name '" + name + "' not supported.", |
| 1573 |
Collections.emptyMap()); |
| 1574 |
} |
| 1575 |
} catch (Exception e) { |
| 1576 |
throw convertToClientInfoException(logAndConvert(e)); |
| 1577 |
} |
| 1578 |
} |
| 1579 |
|
| 1580 |
private static boolean isInternalProperty(String name) { |
| 1581 |
return NUM_SERVERS.equals(name) || name.startsWith(PREFIX_SERVER); |
| 1582 |
} |
| 1583 |
|
| 1584 |
private static SQLClientInfoException convertToClientInfoException( |
| 1585 |
SQLException x) { |
| 1586 |
if (x instanceof SQLClientInfoException) { |
| 1587 |
return (SQLClientInfoException) x; |
| 1588 |
} |
| 1589 |
return new SQLClientInfoException(x.getMessage(), x.getSQLState(), |
| 1590 |
x.getErrorCode(), null, null); |
| 1591 |
} |
| 1592 |
|
| 1593 |
/** |
| 1594 |
* Set the client properties. This replaces all existing properties. This |
| 1595 |
* method always throws a SQLClientInfoException in standard mode. In |
| 1596 |
* compatibility mode some properties may be supported (see |
| 1597 |
* setProperty(String, String) for details). |
| 1598 |
* |
| 1599 |
* @param properties the properties (ignored) |
| 1600 |
*/ |
| 1601 |
@Override |
| 1602 |
public void setClientInfo(Properties properties) |
| 1603 |
throws SQLClientInfoException { |
| 1604 |
try { |
| 1605 |
if (isDebugEnabled()) { |
| 1606 |
debugCode("setClientInfo(properties)"); |
| 1607 |
} |
| 1608 |
checkClosed(); |
| 1609 |
if (clientInfo == null) { |
| 1610 |
clientInfo = new HashMap<>(); |
| 1611 |
} else { |
| 1612 |
clientInfo.clear(); |
| 1613 |
} |
| 1614 |
for (Map.Entry<Object, Object> entry : properties.entrySet()) { |
| 1615 |
setClientInfo((String) entry.getKey(), |
| 1616 |
(String) entry.getValue()); |
| 1617 |
} |
| 1618 |
} catch (Exception e) { |
| 1619 |
throw convertToClientInfoException(logAndConvert(e)); |
| 1620 |
} |
| 1621 |
} |
| 1622 |
|
| 1623 |
/** |
| 1624 |
* Get the client properties. |
| 1625 |
* |
| 1626 |
* @return the property list |
| 1627 |
*/ |
| 1628 |
@Override |
| 1629 |
public Properties getClientInfo() throws SQLException { |
| 1630 |
try { |
| 1631 |
debugCodeCall("getClientInfo"); |
| 1632 |
checkClosed(); |
| 1633 |
ArrayList<String> serverList = session.getClusterServers(); |
| 1634 |
Properties p = new Properties(); |
| 1635 |
|
| 1636 |
if (clientInfo != null) { |
| 1637 |
for (Map.Entry<String, String> entry : clientInfo.entrySet()) { |
| 1638 |
p.setProperty(entry.getKey(), entry.getValue()); |
| 1639 |
} |
| 1640 |
} |
| 1641 |
|
| 1642 |
p.setProperty(NUM_SERVERS, Integer.toString(serverList.size())); |
| 1643 |
for (int i = 0; i < serverList.size(); i++) { |
| 1644 |
p.setProperty(PREFIX_SERVER + i, serverList.get(i)); |
| 1645 |
} |
| 1646 |
|
| 1647 |
return p; |
| 1648 |
} catch (Exception e) { |
| 1649 |
throw logAndConvert(e); |
| 1650 |
} |
| 1651 |
} |
| 1652 |
|
| 1653 |
/** |
| 1654 |
* Get a client property. |
| 1655 |
* |
| 1656 |
* @param name the client info name |
| 1657 |
* @return the property value or null if the property is not found or not |
| 1658 |
* supported. |
| 1659 |
*/ |
| 1660 |
@Override |
| 1661 |
public String getClientInfo(String name) throws SQLException { |
| 1662 |
try { |
| 1663 |
if (isDebugEnabled()) { |
| 1664 |
debugCodeCall("getClientInfo", name); |
| 1665 |
} |
| 1666 |
checkClosed(); |
| 1667 |
if (name == null) { |
| 1668 |
throw DbException.getInvalidValueException("name", null); |
| 1669 |
} |
| 1670 |
return getClientInfo().getProperty(name); |
| 1671 |
} catch (Exception e) { |
| 1672 |
throw logAndConvert(e); |
| 1673 |
} |
| 1674 |
} |
| 1675 |
|
| 1676 |
/** |
| 1677 |
* Return an object of this class if possible. |
| 1678 |
* |
| 1679 |
* @param iface the class |
| 1680 |
* @return this |
| 1681 |
*/ |
| 1682 |
@Override |
| 1683 |
@SuppressWarnings("unchecked") |
| 1684 |
public <T> T unwrap(Class<T> iface) throws SQLException { |
| 1685 |
try { |
| 1686 |
if (isWrapperFor(iface)) { |
| 1687 |
return (T) this; |
| 1688 |
} |
| 1689 |
throw DbException.getInvalidValueException("iface", iface); |
| 1690 |
} catch (Exception e) { |
| 1691 |
throw logAndConvert(e); |
| 1692 |
} |
| 1693 |
} |
| 1694 |
|
| 1695 |
/** |
| 1696 |
* Checks if unwrap can return an object of this class. |
| 1697 |
* |
| 1698 |
* @param iface the class |
| 1699 |
* @return whether or not the interface is assignable from this class |
| 1700 |
*/ |
| 1701 |
@Override |
| 1702 |
public boolean isWrapperFor(Class<?> iface) throws SQLException { |
| 1703 |
return iface != null && iface.isAssignableFrom(getClass()); |
| 1704 |
} |
| 1705 |
|
| 1706 |
/** |
| 1707 |
* Create a Clob value from this reader. |
| 1708 |
* |
| 1709 |
* @param x the reader |
| 1710 |
* @param length the length (if smaller or equal than 0, all data until the |
| 1711 |
* end of file is read) |
| 1712 |
* @return the value |
| 1713 |
*/ |
| 1714 |
Value createClob(Reader x, long length) { |
| 1715 |
if (x == null) { |
| 1716 |
return ValueNull.INSTANCE; |
| 1717 |
} |
| 1718 |
if (length <= 0) { |
| 1719 |
length = -1; |
| 1720 |
} |
| 1721 |
return session.addTemporaryLob(session.getDataHandler().getLobStorage().createClob(x, length)); |
| 1722 |
} |
| 1723 |
|
| 1724 |
/** |
| 1725 |
* Create a Blob value from this input stream. |
| 1726 |
* |
| 1727 |
* @param x the input stream |
| 1728 |
* @param length the length (if smaller or equal than 0, all data until the |
| 1729 |
* end of file is read) |
| 1730 |
* @return the value |
| 1731 |
*/ |
| 1732 |
Value createBlob(InputStream x, long length) { |
| 1733 |
if (x == null) { |
| 1734 |
return ValueNull.INSTANCE; |
| 1735 |
} |
| 1736 |
if (length <= 0) { |
| 1737 |
length = -1; |
| 1738 |
} |
| 1739 |
return session.addTemporaryLob(session.getDataHandler().getLobStorage().createBlob(x, length)); |
| 1740 |
} |
| 1741 |
|
| 1742 |
/** |
| 1743 |
* Sets the given schema name to access. Current implementation is case |
| 1744 |
* sensitive, i.e. requires schema name to be passed in correct case. |
| 1745 |
* |
| 1746 |
* @param schema the schema name |
| 1747 |
*/ |
| 1748 |
@Override |
| 1749 |
public void setSchema(String schema) throws SQLException { |
| 1750 |
try { |
| 1751 |
if (isDebugEnabled()) { |
| 1752 |
debugCodeCall("setSchema", schema); |
| 1753 |
} |
| 1754 |
checkClosed(); |
| 1755 |
session.setCurrentSchemaName(schema); |
| 1756 |
} catch (Exception e) { |
| 1757 |
throw logAndConvert(e); |
| 1758 |
} |
| 1759 |
} |
| 1760 |
|
| 1761 |
/** |
| 1762 |
* Retrieves this current schema name for this connection. |
| 1763 |
* |
| 1764 |
* @return current schema name |
| 1765 |
*/ |
| 1766 |
@Override |
| 1767 |
public String getSchema() throws SQLException { |
| 1768 |
try { |
| 1769 |
debugCodeCall("getSchema"); |
| 1770 |
checkClosed(); |
| 1771 |
return session.getCurrentSchemaName(); |
| 1772 |
} catch (Exception e) { |
| 1773 |
throw logAndConvert(e); |
| 1774 |
} |
| 1775 |
} |
| 1776 |
|
| 1777 |
/** |
| 1778 |
* [Not supported] |
| 1779 |
* |
| 1780 |
* @param executor the executor used by this method |
| 1781 |
*/ |
| 1782 |
@Override |
| 1783 |
public void abort(Executor executor) { |
| 1784 |
// not supported |
| 1785 |
} |
| 1786 |
|
| 1787 |
/** |
| 1788 |
* [Not supported] |
| 1789 |
* |
| 1790 |
* @param executor the executor used by this method |
| 1791 |
* @param milliseconds the TCP connection timeout |
| 1792 |
*/ |
| 1793 |
@Override |
| 1794 |
public void setNetworkTimeout(Executor executor, int milliseconds) { |
| 1795 |
// not supported |
| 1796 |
} |
| 1797 |
|
| 1798 |
/** |
| 1799 |
* [Not supported] |
| 1800 |
*/ |
| 1801 |
@Override |
| 1802 |
public int getNetworkTimeout() { |
| 1803 |
return 0; |
| 1804 |
} |
| 1805 |
|
| 1806 |
/** |
| 1807 |
* Check that the given type map is either null or empty. |
| 1808 |
* |
| 1809 |
* @param map the type map |
| 1810 |
* @throws DbException if the map is not empty |
| 1811 |
*/ |
| 1812 |
static void checkMap(Map<String, Class<?>> map) { |
| 1813 |
if (map != null && map.size() > 0) { |
| 1814 |
throw DbException.getUnsupportedException("map.size > 0"); |
| 1815 |
} |
| 1816 |
} |
| 1817 |
|
| 1818 |
/** |
| 1819 |
* INTERNAL |
| 1820 |
*/ |
| 1821 |
@Override |
| 1822 |
public String toString() { |
| 1823 |
return getTraceObjectName() + ": url=" + url + " user=" + user; |
| 1824 |
} |
| 1825 |
|
| 1826 |
CompareMode getCompareMode() { |
| 1827 |
return session.getDataHandler().getCompareMode(); |
| 1828 |
} |
| 1829 |
|
| 1830 |
@Override |
| 1831 |
public Mode getMode() { |
| 1832 |
return session.getMode(); |
| 1833 |
} |
| 1834 |
|
| 1835 |
/** |
| 1836 |
* INTERNAL |
| 1837 |
* @return StaticSettings |
| 1838 |
*/ |
| 1839 |
public StaticSettings getStaticSettings() { |
| 1840 |
checkClosed(); |
| 1841 |
return session.getStaticSettings(); |
| 1842 |
} |
| 1843 |
|
| 1844 |
@Override |
| 1845 |
public ValueTimestampTimeZone currentTimestamp() { |
| 1846 |
Session session = this.session; |
| 1847 |
if (session == null) { |
| 1848 |
throw DbException.get(ErrorCode.OBJECT_CLOSED); |
| 1849 |
} |
| 1850 |
return session.currentTimestamp(); |
| 1851 |
} |
| 1852 |
|
| 1853 |
@Override |
| 1854 |
public TimeZoneProvider currentTimeZone() { |
| 1855 |
Session session = this.session; |
| 1856 |
if (session == null) { |
| 1857 |
throw DbException.get(ErrorCode.OBJECT_CLOSED); |
| 1858 |
} |
| 1859 |
return session.currentTimeZone(); |
| 1860 |
} |
| 1861 |
|
| 1862 |
@Override |
| 1863 |
public JavaObjectSerializer getJavaObjectSerializer() { |
| 1864 |
Session session = this.session; |
| 1865 |
if (session == null) { |
| 1866 |
throw DbException.get(ErrorCode.OBJECT_CLOSED); |
| 1867 |
} |
| 1868 |
return session.getJavaObjectSerializer(); |
| 1869 |
} |
| 1870 |
|
| 1871 |
@Override |
| 1872 |
public boolean zeroBasedEnums() { |
| 1873 |
Session session = this.session; |
| 1874 |
if (session == null) { |
| 1875 |
throw DbException.get(ErrorCode.OBJECT_CLOSED); |
| 1876 |
} |
| 1877 |
return session.zeroBasedEnums(); |
| 1878 |
} |
| 1879 |
|
| 1880 |
} |