📄 jdbcconnection.java
字号:
}
/**
* Creates a callable statement with the specified result set type,
* concurrency, and holdability.
*
* @return the callable statement
* @throws SQLException
* if the connection is closed or the result set type,
* concurrency, or holdability are not supported
*/
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency, int resultSetHoldability) throws SQLException {
try {
int id = getNextId(TraceObject.CALLABLE_STATEMENT);
if (debug()) {
debugCodeAssign("CallableStatement", TraceObject.CALLABLE_STATEMENT, id,
"prepareCall(" + quote(sql) + ", " + resultSetType + ", " + resultSetConcurrency + ", "
+ resultSetHoldability + ")");
}
checkClosed();
checkTypeAndConcurrency(resultSetType, resultSetConcurrency);
checkHoldability(resultSetHoldability);
sql = translateSQL(sql);
return new JdbcCallableStatement(session, this, sql, resultSetType, id);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Creates a new unnamed savepoint.
*
* @return the new savepoint
*/
//#ifdef JDK14
public Savepoint setSavepoint() throws SQLException {
try {
int id = getNextId(TraceObject.SAVEPOINT);
if (debug()) {
debugCodeAssign("Savepoint", TraceObject.SAVEPOINT, id, "setSavepoint()");
}
checkClosed();
CommandInterface set = prepareCommand("SAVEPOINT " + JdbcSavepoint.getName(null, savepointId), Integer.MAX_VALUE);
set.executeUpdate();
JdbcSavepoint savepoint = new JdbcSavepoint(this, savepointId, null, trace, id);
savepointId++;
return savepoint;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
//#endif
/**
* Creates a new named savepoint.
*
* @return the new savepoint
*/
//#ifdef JDK14
public Savepoint setSavepoint(String name) throws SQLException {
try {
int id = getNextId(TraceObject.SAVEPOINT);
if (debug()) {
debugCodeAssign("Savepoint", TraceObject.SAVEPOINT, id, "setSavepoint(" + quote(name) + ")");
}
checkClosed();
CommandInterface set = prepareCommand("SAVEPOINT " + JdbcSavepoint.getName(name, 0), Integer.MAX_VALUE);
set.executeUpdate();
JdbcSavepoint savepoint = new JdbcSavepoint(this, 0, name, trace, id);
return savepoint;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
//#endif
/**
* Rolls back to a savepoint.
*/
//#ifdef JDK14
public void rollback(Savepoint savepoint) throws SQLException {
try {
JdbcSavepoint sp = convertSavepoint(savepoint);
debugCode("rollback(" + sp.getTraceObjectName() + ");");
checkClosed();
sp.rollback();
} catch (Throwable e) {
throw logAndConvert(e);
}
}
//#endif
/**
* Releases a savepoint.
*/
//#ifdef JDK14
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
try {
debugCode("releaseSavepoint(savepoint);");
checkClosed();
convertSavepoint(savepoint).release();
} catch (Throwable e) {
throw logAndConvert(e);
}
}
private JdbcSavepoint convertSavepoint(Savepoint savepoint) throws SQLException {
if (!(savepoint instanceof JdbcSavepoint)) {
throw Message.getSQLException(ErrorCode.SAVEPOINT_IS_INVALID_1, "" + savepoint);
}
return (JdbcSavepoint) savepoint;
}
//#endif
/**
* Creates a prepared statement with the specified result set type,
* concurrency, and holdability.
*
* @return the prepared statement
* @throws SQLException if the connection is closed or the result set type,
* concurrency, or holdability are not supported
*/
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
try {
int id = getNextId(TraceObject.PREPARED_STATEMENT);
if (debug()) {
debugCodeAssign("PreparedStatement", TraceObject.PREPARED_STATEMENT, id,
"prepareStatement(" + quote(sql) + ", " + resultSetType + ", " + resultSetConcurrency + ", "
+ resultSetHoldability + ")");
}
checkClosed();
checkTypeAndConcurrency(resultSetType, resultSetConcurrency);
checkHoldability(resultSetHoldability);
sql = translateSQL(sql);
return new JdbcPreparedStatement(session, this, sql, resultSetType, id, false);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Creates a new prepared statement. This method just calls
* prepareStatement(String sql).
*
* @return the prepared statement
* @throws SQLException
* if the connection is closed
*/
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
try {
if (debug()) {
debugCode("prepareStatement(" + quote(sql) + ", " + autoGeneratedKeys + ");");
}
return prepareStatement(sql);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Creates a new prepared statement. This method just calls
* prepareStatement(String sql).
*
* @return the prepared statement
* @throws SQLException
* if the connection is closed
*/
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
try {
if (debug()) {
debugCode("prepareStatement(" + quote(sql) + ", " + quoteIntArray(columnIndexes) + ");");
}
return prepareStatement(sql);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Creates a new prepared statement. This method just calls
* prepareStatement(String sql).
*
* @return the prepared statement
* @throws SQLException
* if the connection is closed
*/
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
try {
if (debug()) {
debugCode("prepareStatement(" + quote(sql) + ", " + quoteArray(columnNames) + ");");
}
return prepareStatement(sql);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
// =============================================================
/**
* INTERNAL
*/
public JdbcConnection(String url, Properties info) throws SQLException {
this(new ConnectionInfo(url, info), true);
}
/**
* INTERNAL
*/
public JdbcConnection(ConnectionInfo ci, boolean useBaseDir) throws SQLException {
try {
checkJavaVersion();
if (ci.isRemote()) {
session = new SessionRemote().createSession(ci);
} else {
SessionInterface si = (SessionInterface) ClassUtils.loadSystemClass("org.h2.engine.Session").newInstance();
if (useBaseDir) {
String baseDir = SysProperties.getBaseDir();
if (baseDir != null) {
ci.setBaseDir(baseDir);
}
}
session = si.createSession(ci);
}
trace = session.getTrace();
int id = getNextId(TraceObject.CONNECTION);
setTrace(trace, TraceObject.CONNECTION, id);
this.user = ci.getUserName();
if (info()) {
trace.infoCode("Connection " + getTraceObjectName()
+ " = DriverManager.getConnection(" + quote(ci.getOriginalURL())
+ ", " + quote(user) + ", \"\")");
}
this.url = ci.getURL();
openStackTrace = new Exception("Stack Trace");
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* INTERNAL
*/
public JdbcConnection(SessionInterface session, String user, String url) throws SQLException {
isInternal = true;
this.session = session;
trace = session.getTrace();
int id = getNextId(TraceObject.CONNECTION);
setTrace(trace, TraceObject.CONNECTION, id);
this.user = user;
this.url = url;
}
private void checkJavaVersion() throws SQLException {
try {
//#ifdef JDK14
// check for existence of this class (avoiding Class . forName)
Class clazz = java.sql.Savepoint.class;
clazz.getClass();
//#endif
} catch (Throwable e) {
throw Message.getSQLException(ErrorCode.UNSUPPORTED_JAVA_VERSION);
}
}
CommandInterface prepareCommand(String sql, int fetchSize) throws SQLException {
return session.prepareCommand(sql, fetchSize);
}
CommandInterface prepareCommand(String sql, CommandInterface old) throws SQLException {
return old == null ? session.prepareCommand(sql, Integer.MAX_VALUE) : old;
}
private int translateGetEnd(String sql, int i, char c) throws SQLException {
int len = sql.length();
switch(c) {
case '\'': {
int j = sql.indexOf('\'', i + 1);
if (j < 0) {
throw Message.getSyntaxError(sql, i);
}
return j;
}
case '"': {
int j = sql.indexOf('"', i + 1);
if (j < 0) {
throw Message.getSyntaxError(sql, i);
}
return j;
}
case '/': {
checkRunOver(i+1, len, sql);
if (sql.charAt(i + 1) == '*') {
// block comment
int j = sql.indexOf("*/", i + 2);
if (j < 0) {
throw Message.getSyntaxError(sql, i);
}
i = j + 1;
} else if (sql.charAt(i + 1) == '/') {
// single line comment
i += 2;
while (i < len && (c = sql.charAt(i)) != '\r' && c != '\n') {
i++;
}
}
return i;
}
case '-': {
checkRunOver(i+1, len, sql);
if (sql.charAt(i + 1) == '-') {
// single line comment
i += 2;
while (i < len && (c = sql.charAt(i)) != '\r' && c != '\n') {
i++;
}
}
return i;
}
default:
throw Message.getInternalError("c=" + c);
}
}
String translateSQL(String sql) throws SQLException {
if (sql == null || sql.indexOf('{') < 0) {
return sql;
}
int len = sql.length();
char[] chars = null;
int level = 0;
for (int i = 0; i < len; i++) {
char c = sql.charAt(i);
switch (c) {
case '\'':
case '"':
case '/':
case '-':
i = translateGetEnd(sql, i, c);
break;
case '{':
level++;
if (chars == null) {
chars = sql.toCharArray();
}
chars[i] = ' ';
while (Character.isSpaceChar(chars[i])) {
i++;
checkRunOver(i, len, sql);
}
int start = i;
if (chars[i] >= '0' && chars[i] <= '9') {
chars[i - 1] = '{';
while (true) {
checkRunOver(i, len, sql);
c = chars[i];
if (c == '}') {
break;
}
switch (c) {
case '\'':
case '"':
case '/':
case '-':
i = translateGetEnd(sql, i, c);
break;
default:
}
i++;
}
level--;
break;
} else if (chars[i] == '?') {
// TODO nativeSQL: '? = ...' : register out parameter
chars[i++] = ' ';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -