📄 jdbcstatement.java
字号:
/*
* Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.jdbc;
import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import org.h2.command.CommandInterface;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
import org.h2.engine.SessionInterface;
import org.h2.message.Message;
import org.h2.message.TraceObject;
import org.h2.result.ResultInterface;
import org.h2.util.ObjectArray;
/**
* Represents a statement.
*/
public class JdbcStatement extends TraceObject implements Statement {
protected JdbcConnection conn;
protected SessionInterface session;
protected JdbcResultSet resultSet;
protected int maxRows;
protected boolean escapeProcessing = true;
protected int fetchSize = SysProperties.SERVER_RESULT_SET_FETCH_SIZE;
protected int updateCount;
private CommandInterface executingCommand;
private ObjectArray batchCommands;
protected int resultSetType;
protected boolean closedByResultSet;
/**
* 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).
*
* @return the result set
*/
public ResultSet executeQuery(String sql) throws SQLException {
try {
int id = getNextId(TraceObject.RESULT_SET);
if (debug()) {
debugCodeAssign("ResultSet", TraceObject.RESULT_SET, id, "executeQuery(" + quote(sql) + ")");
}
checkClosed();
closeOld();
if (escapeProcessing) {
sql = conn.translateSQL(sql);
}
synchronized (session) {
CommandInterface command = conn.prepareCommand(sql, fetchSize);
ResultInterface result;
boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY;
setExecutingStatement(command);
try {
result = command.executeQuery(maxRows, scrollable);
} finally {
setExecutingStatement(null);
}
command.close();
resultSet = new JdbcResultSet(session, conn, this, result, id, closedByResultSet, scrollable);
}
return resultSet;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Executes a statement (insert, update, delete, create, drop)
* and returns the update count.
* If another result set exists for this statement, this will be closed
* (even if this statement fails).
*
* 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, this statement will be committed.
*
* @param sql the SQL statement
* @return the update count (number of row affected by an insert,
* update or delete, or 0 if no rows or the statement was a
* create, drop, commit or rollback)
* @throws SQLException if a database error occurred or a
* select statement was executed
*/
public int executeUpdate(String sql) throws SQLException {
try {
debugCodeCall("executeUpdate", sql);
checkClosed();
closeOld();
if (escapeProcessing) {
sql = conn.translateSQL(sql);
}
CommandInterface command = conn.prepareCommand(sql, fetchSize);
synchronized (session) {
setExecutingStatement(command);
try {
updateCount = command.executeUpdate();
} finally {
setExecutingStatement(null);
}
}
command.close();
return updateCount;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Executes an arbitrary statement. If another result set exists for this
* statement, this will be closed (even if this statement fails).
*
* 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.
*
* @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 (debug()) {
debugCodeCall("execute", sql);
}
checkClosed();
closeOld();
if (escapeProcessing) {
sql = conn.translateSQL(sql);
}
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;
ResultInterface result = command.executeQuery(maxRows, scrollable);
resultSet = new JdbcResultSet(session, conn, this, result, id, closedByResultSet, scrollable);
} else {
returnsResultSet = false;
updateCount = command.executeUpdate();
}
} finally {
setExecutingStatement(null);
}
}
command.close();
return returnsResultSet;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Returns the last result set produces by this statement.
*
* @return the result set
*/
public ResultSet getResultSet() throws SQLException {
try {
checkClosed();
if (resultSet != null) {
int id = resultSet.getTraceId();
debugCodeAssign("ResultSet", TraceObject.RESULT_SET, id, "getResultSet()");
} else {
debugCodeCall("getResultSet");
}
return resultSet;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Returns the last update count of this statement.
*
* @return the update count (number of row affected by an insert,
* update or delete, or 0 if no rows or the statement was a
* create, drop, commit or rollback; -1 if the statement was a select).
* @throws SQLException if this object is closed or invalid
*/
public int getUpdateCount() throws SQLException {
try {
debugCodeCall("getUpdateCount");
checkClosed();
return updateCount;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Closes this statement.
* All result sets that where created by this statement
* become invalid after calling this method.
*/
public void close() throws SQLException {
try {
debugCodeCall("close");
closeOld();
if (conn != null) {
conn = null;
}
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Returns the connection that created this object.
*
* @return the connection
*/
public Connection getConnection() throws SQLException {
try {
debugCodeCall("getConnection");
checkClosed();
return conn;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Gets the first warning reported by calls on this object.
* This driver does not support warnings, and will always return null.
*
* @return null
*/
public SQLWarning getWarnings() throws SQLException {
try {
debugCodeCall("getWarnings");
checkClosed();
return null;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Clears all warnings. As this driver does not support warnings,
* this call is ignored.
*/
public void clearWarnings() throws SQLException {
try {
debugCodeCall("clearWarnings");
checkClosed();
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Moves to the next result set - however there is always only one result
* set. This call also closes the current result set (if there is one).
* Returns true if there is a next result set (that means - it always
* returns false).
*
* @return false
* @throws SQLException if this object is closed.
*/
public boolean getMoreResults() throws SQLException {
try {
debugCodeCall("getMoreResults");
checkClosed();
closeOld();
return false;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Sets the name of the cursor. This call is ignored.
*
* @param name ignored
* @throws SQLException if this object is closed
*/
public void setCursorName(String name) throws SQLException {
try {
debugCodeCall("setCursorName", name);
checkClosed();
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Sets the fetch direction.
* This call is ignored by this driver.
*
* @param direction ignored
* @throws SQLException if this object is closed
*/
public void setFetchDirection(int direction) throws SQLException {
try {
debugCodeCall("setFetchDirection", direction);
checkClosed();
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Gets the fetch direction.
*
* @return FETCH_FORWARD
* @throws SQLException if this object is closed
*/
public int getFetchDirection() throws SQLException {
try {
debugCodeCall("getFetchDirection");
checkClosed();
return ResultSet.FETCH_FORWARD;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Gets the maximum number of rows for a ResultSet.
*
* @return the number of rows where 0 means no limit
* @throws SQLException if this object is closed
*/
public int getMaxRows() throws SQLException {
try {
debugCodeCall("getMaxRows");
checkClosed();
return maxRows;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Gets the maximum number of rows for a ResultSet.
*
* @param maxRows the number of rows where 0 means no limit
* @throws SQLException if this object is closed
*/
public void setMaxRows(int maxRows) throws SQLException {
try {
debugCodeCall("setMaxRows", maxRows);
checkClosed();
if (maxRows < 0) {
throw Message.getInvalidValueException(""+maxRows, "maxRows");
}
this.maxRows = maxRows;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Sets the number of rows suggested to read in one step.
* This value cannot be higher than the maximum rows (setMaxRows)
* set by the statement or prepared statement, otherwise an exception
* is throws. Setting the value to 0 will set the default value.
* The default value can be changed using the system property
* h2.serverResultSetFetchSize.
*
* @param rows the number of rows
* @throws SQLException if this object is closed
*/
public void setFetchSize(int rows) throws SQLException {
try {
debugCodeCall("setFetchSize", rows);
checkClosed();
if (rows < 0 || (rows > 0 && maxRows > 0 && rows > maxRows)) {
throw Message.getInvalidValueException("" + rows, "rows");
}
if (rows == 0) {
rows = SysProperties.SERVER_RESULT_SET_FETCH_SIZE;
}
fetchSize = rows;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Gets the number of rows suggested to read in one step.
*
* @return the current fetch size
* @throws SQLException if this object is closed
*/
public int getFetchSize() throws SQLException {
try {
debugCodeCall("getFetchSize");
checkClosed();
return fetchSize;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Gets the result set concurrency created by this object.
*
* @return ResultSet.CONCUR_UPDATABLE
* @throws SQLException if this object is closed
*/
public int getResultSetConcurrency() throws SQLException {
try {
debugCodeCall("getResultSetConcurrency");
checkClosed();
return ResultSet.CONCUR_UPDATABLE;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Gets the result set type.
*
* @return the type
* @throws SQLException if this object is closed
*/
public int getResultSetType() throws SQLException {
try {
debugCodeCall("getResultSetType");
checkClosed();
return resultSetType;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Gets the maximum number of bytes for a result set column.
*
* @return always 0 for no limit
* @throws SQLException if this object is closed
*/
public int getMaxFieldSize() throws SQLException {
try {
debugCodeCall("getMaxFieldSize");
checkClosed();
return 0;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -