/** * Executes an arbitrary statement. If another result set exists for this statement, this will be * closed (even if this statement fails). * * <p>If the statement is a create or drop and does not throw an exception, the current * transaction (if any) is committed after executing the statement. If auto commit is on, and the * statement is not a select, this statement will be committed. * * @param sql the SQL statement to execute * @return true if a result set is available, false if not */ public boolean execute(String sql) throws SQLException { try { int id = getNextId(TraceObject.RESULT_SET); if (isDebugEnabled()) { debugCodeCall("execute", sql); } checkClosedForWrite(); closeOldResultSet(); sql = conn.translateSQL(sql, escapeProcessing); CommandInterface command = conn.prepareCommand(sql, fetchSize); boolean returnsResultSet; synchronized (session) { setExecutingStatement(command); try { if (command.isQuery()) { returnsResultSet = true; boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY; boolean updatable = resultSetConcurrency == ResultSet.CONCUR_UPDATABLE; ResultInterface result = command.executeQuery(maxRows, scrollable); resultSet = new JdbcResultSet(conn, this, result, id, closedByResultSet, scrollable, updatable); } else { returnsResultSet = false; updateCount = command.executeUpdate(); } } finally { setExecutingStatement(null); } } command.close(); return returnsResultSet; } catch (Exception e) { throw logAndConvert(e); } }
/** * Executes a query (select statement) and returns the result set. If another result set exists * for this statement, this will be closed (even if this statement fails). * * @param sql the SQL statement to execute * @return the result set */ public ResultSet executeQuery(String sql) throws SQLException { try { int id = getNextId(TraceObject.RESULT_SET); if (isDebugEnabled()) { debugCodeAssign( "ResultSet", TraceObject.RESULT_SET, id, "executeQuery(" + quote(sql) + ")"); } checkClosed(); closeOldResultSet(); sql = conn.translateSQL(sql, escapeProcessing); synchronized (session) { CommandInterface command = conn.prepareCommand(sql, fetchSize); ResultInterface result; boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY; boolean updatable = resultSetConcurrency == ResultSet.CONCUR_UPDATABLE; setExecutingStatement(command); try { result = command.executeQuery(maxRows, scrollable); } finally { setExecutingStatement(null); } command.close(); resultSet = new JdbcResultSet(conn, this, result, id, closedByResultSet, scrollable, updatable); } return resultSet; } catch (Exception e) { throw logAndConvert(e); } }
/** * Returns the current transaction isolation level. * * @return the isolation level. * @throws SQLException if the connection is closed */ public int getTransactionIsolation() throws SQLException { try { debugCodeCall("getTransactionIsolation"); checkClosed(); getLockMode = prepareCommand("CALL LOCK_MODE()", getLockMode); ResultInterface result = getLockMode.executeQuery(0, false); result.next(); int lockMode = result.currentRow()[0].getInt(); result.close(); int transactionIsolationLevel; switch (lockMode) { case Constants.LOCK_MODE_OFF: transactionIsolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED; break; case Constants.LOCK_MODE_READ_COMMITTED: transactionIsolationLevel = Connection.TRANSACTION_READ_COMMITTED; break; case Constants.LOCK_MODE_TABLE: case Constants.LOCK_MODE_TABLE_GC: transactionIsolationLevel = Connection.TRANSACTION_SERIALIZABLE; break; default: throw DbException.throwInternalError("lockMode:" + lockMode); } return transactionIsolationLevel; } catch (Exception e) { throw logAndConvert(e); } }
private boolean getInternalAutoCommit() { getAutoCommit = prepareCommand("CALL AUTOCOMMIT()", getAutoCommit); ResultInterface result = getAutoCommit.executeQuery(0, false); result.next(); boolean autoCommit = result.currentRow()[0].getBoolean().booleanValue(); result.close(); return autoCommit; }
/** * Returns true if the database is read-only. * * @return if the database is read-only * @throws SQLException if the connection is closed */ public boolean isReadOnly() throws SQLException { try { debugCodeCall("isReadOnly"); checkClosed(); getReadOnly = prepareCommand("CALL READONLY()", getReadOnly); ResultInterface result = getReadOnly.executeQuery(0, false); result.next(); boolean readOnly = result.currentRow()[0].getBoolean().booleanValue(); return readOnly; } catch (Exception e) { throw logAndConvert(e); } }
/** * Gets the current catalog name. * * @return the catalog name * @throws SQLException if the connection is closed */ public String getCatalog() throws SQLException { try { debugCodeCall("getCatalog"); checkClosed(); if (catalog == null) { CommandInterface cat = prepareCommand("CALL DATABASE()", Integer.MAX_VALUE); ResultInterface result = cat.executeQuery(0, false); result.next(); catalog = result.currentRow()[0].getString(); cat.close(); } return catalog; } catch (Exception e) { throw logAndConvert(e); } }
/** * Read the serializer name from the persistent database settings. * * @return the serializer */ private String readSerializationSettings() { String javaObjectSerializerFQN = null; CommandInterface ci = prepareCommand( "SELECT VALUE FROM INFORMATION_SCHEMA.SETTINGS " + " WHERE NAME='JAVA_OBJECT_SERIALIZER'", Integer.MAX_VALUE); try { ResultInterface result = ci.executeQuery(0, false); if (result.next()) { Value[] row = result.currentRow(); javaObjectSerializerFQN = row[0].getString(); } } finally { ci.close(); } return javaObjectSerializerFQN; }
/** INTERNAL */ public int getQueryTimeout() throws SQLException { try { debugCodeCall("getQueryTimeout"); checkClosed(); getQueryTimeout = prepareCommand( "SELECT VALUE FROM INFORMATION_SCHEMA.SETTINGS WHERE NAME=?", getQueryTimeout); getQueryTimeout.getParameters().get(0).setValue(ValueString.get("QUERY_TIMEOUT"), false); ResultInterface result = getQueryTimeout.executeQuery(0, false); result.next(); int queryTimeout = result.currentRow()[0].getInt(); result.close(); if (queryTimeout == 0) { return 0; } // round to the next second, otherwise 999 millis would return 0 seconds return (queryTimeout + 999) / 1000; } catch (Exception e) { throw logAndConvert(e); } }
/** INTERNAL */ ResultSet getGeneratedKeys(JdbcStatement stat, int id) { getGeneratedKeys = prepareCommand("CALL SCOPE_IDENTITY()", getGeneratedKeys); ResultInterface result = getGeneratedKeys.executeQuery(0, false); ResultSet rs = new JdbcResultSet(this, stat, result, id, false, true, false); return rs; }