📄 jdbcconnection.java
字号:
/*
* jdbcConnection.java
*/
package org.hsql;
import java.sql.*;
import java.util.*;
import java.io.*;
import java.net.*;
/**
* <P>A connection (session) with a specific
* database. Within the context of a Connection, SQL statements are
* executed and results are returned.
*
* <P>A Connection's database is able to provide information
* describing its tables, its supported SQL grammar, its stored
* procedures, the capabilities of this connection, and so on. This
* information is obtained with the <code>getMetaData</code> method.
*
* <P><B>Note:</B> By default the Connection automatically commits
* changes after executing each statement. If auto commit has been
* disabled, an explicit commit must be done or database changes will
* not be saved.
* <P><font color="#009900">
* To connect to a Hypersonic SQL database, the following code may be used:
* <pre>
* Class.forName("org.hsql.jdbcDriver");
* Connection c=DriverManager.getConnection(url,user,password);
* </pre>
* For Hypersonic SQL, the url must start with jdbc:HypersonicSQL.
* The url for an In-Memory database is <code>jdbc:HypersonicSQL:.</code>
* The url for a standalone database is jdbc:HypersonicSQL:name
* where name is the filename of the database, including path. For example:
* <code>jdbc:HypersonicSQL:test</code> will connect to a database called
* 'test', with the files test.properties, test.data and test.script.
* For more information about the internal file structure of Hypersonic SQL,
* please read the documentation, page 'Files'.
* </font><P>
* @see jdbcStatement
* @see jdbcResultSet
* @see jdbcDatabaseMetaData
*/
public class jdbcConnection implements Connection {
private boolean bClosed;
private boolean bAutoCommit;
private String sDatabaseName;
private final static int HTTP=0,STANDALONE=1,INTERNAL=2,HSQL=3;
private int iType;
/**
* Creates a <code>Statement</code> object for sending
* SQL statements to the database.
* SQL statements without parameters are normally
* executed using Statement objects.
*
* @return a new Statement object
*/
public Statement createStatement() {
if(Trace.TRACE) Trace.trace();
if(bClosed) {
return null;
}
return new jdbcStatement(this);
}
/**
* Creates a <code>PreparedStatement</code> object for sending
* parameterized SQL statements to the database.
*
* A SQL statement with or without IN parameters can be stored in
* a PreparedStatement object. This object can then be used to execute
* this statement multiple times.
* <P><font color="#009900">
* In Hypersonic SQL, the statement is not sent to the database until
* the <code>PreparedStatement</code> is executed. Other drivers may send
* the statment to the database when this function is called. This has
* no direct effect on users; however, it does affect which method throws
* certain SQLExceptions.
* </font><P>
* @param sql a SQL statement that may contain one or more '?' IN
* parameter placeholders
* @return a new PreparedStatement object containing the
* pre-compiled statement
*/
public PreparedStatement prepareStatement(String sql) {
if(Trace.TRACE) Trace.trace(sql);
if(bClosed) {
return null;
}
return new jdbcPreparedStatement(this,sql);
}
/**
* Creates a <code>CallableStatement</code> object for calling
* database stored procedures.
* The CallableStatement provides
* methods for setting up its IN and OUT parameters, and
* methods for executing the call to a stored procedure.
*
* <P><B>Note:</B> This method is optimized for handling stored
* procedure call statements.
* <P><font color="#009900">
* Hypersonic SQL does not send the call
* statement to the database when the method <code>prepareCall</code>
* is done; it waits until the CallableStatement is executed. This has
* no direct effect on users; however, it does affect which method
* throws certain SQLExceptions.
* </font><P>
* @param sql a SQL statement that may contain one or more '?'
* parameter placeholders. Typically this statement is a JDBC
* function call escape string.
* @return a new CallableStatement object containing the
* pre-compiled SQL statement
*/
public CallableStatement prepareCall(String sql) {
if(Trace.TRACE) Trace.trace(sql);
if(bClosed) {
return null;
}
return new jdbcPreparedStatement(this,sql);
}
/**
* Converts the given SQL statement into the system's native SQL grammar.
* <P><font color="#009900">
* Hypersonic SQL does convert the JDBC sql grammar into its system's
* native SQL grammar prior to sending it; this method returns the
* native form of the statement that the driver would have sent.
* </font><P>
* @param sql a SQL statement that may contain one or more '?'
* parameter placeholders
* @return the native form of this statement
*/
public String nativeSQL(String sql) {
if(sql.indexOf('{')==-1) {
return sql;
}
char s[]=sql.toCharArray();
boolean changed=false;
int state=0;
int len=s.length;
for(int i=0;i<len;i++) {
char c=s[i];
switch(state) {
case 0: // normal
if(c=='\'') {
state=1;
} else if(c=='"') {
state=2;
} else if(c=='{') {
s[i]=' ';
changed=true;
String sub=sql.substring(i+1).toUpperCase();
if(sub.startsWith("?=")) {
i+=2;
} else if(sub.startsWith("CALL")) {
i+=4;
} else if(sub.startsWith("ESCAPE")) {
i+=6;
}
state=3;
}
break;
case 1: // inside ' '
case 5: // inside { } and ' '
if(c=='\'') {
state-=1;
}
break;
case 2: // inside " "
case 6: // inside { } and " "
if(c=='"') {
state-=2;
}
break;
case 3: // inside { } before whitespace
if(c==' ') {
state=4;
} else {
s[i]=' ';
changed=true;
}
break;
case 4: // inside { } after whitespace
if(c=='\'') {
state=5;
} else if(c=='"') {
state=6;
} else if(c=='}') {
s[i]=' ';
changed=true;
state=0;
}
}
}
if(changed) {
sql=new String(s);
if(Trace.TRACE) Trace.trace(s+" > "+sql);
}
return sql;
}
/**
* Sets this connection's auto-commit mode.
* If a connection is in auto-commit mode, then all its SQL
* statements will be executed and committed as individual
* transactions. Otherwise, its SQL statements are grouped into
* transactions that are terminated by a call to either
* the method <code>commit</code> or the method <code>rollback</code>.
* By default, new connections are in auto-commit
* mode.
*
* The commit occurs when the statement completes or the next
* execute occurs, whichever comes first. In the case of
* statements returning a ResultSet, the statement completes when
* the last row of the ResultSet has been retrieved or the
* ResultSet has been closed. In advanced cases, a single
* statement may return multiple results as well as output
* parameter values. In these cases the commit occurs when all results and
* output parameter values have been retrieved.
*
* @param autoCommit true enables auto-commit; false disables
* auto-commit.
* @exception SQLException if a database access error occurs
*/
public void setAutoCommit(boolean autoCommit) throws SQLException {
bAutoCommit=autoCommit;
execute("SET AUTOCOMMIT "+(bAutoCommit?"TRUE":"FALSE"));
}
/**
* Gets the current auto-commit state.
*
* @return the current state of auto-commit mode
* @see #setAutoCommit
*/
public boolean getAutoCommit() {
if(Trace.TRACE) Trace.trace();
return bAutoCommit;
}
/**
* Makes all changes made since the previous
* commit/rollback permanent and releases any database locks
* currently held by the Connection. This method should be
* used only when auto-commit mode has been disabled.
*
* @exception SQLException if a database access error occurs
* @see #setAutoCommit
*/
public void commit() throws SQLException {
execute("COMMIT");
}
/**
* Drops all changes made since the previous
* commit/rollback and releases any database locks currently held
* by this Connection. This method should be used only when auto-
* commit has been disabled.
*
* @exception SQLException if a database access error occurs
* @see #setAutoCommit
*/
public void rollback() throws SQLException {
execute("ROLLBACK");
}
/**
* Releases a Connection's database and JDBC resources
* immediately instead of waiting for
* them to be automatically released.
*
* <P><B>Note:</B> A Connection is automatically closed when it is
* garbage collected.
*
* @exception SQLException if a database access error occurs
*/
public void close() throws SQLException {
if(Trace.TRACE) Trace.trace();
if(bClosed) {
return;
}
if(iType==STANDALONE) {
closeStandalone();
}
bClosed=true;
}
/**
* Tests to see if a Connection is closed.
*
* @return true if the connection is closed; false if it's still open
*/
public boolean isClosed() {
if(Trace.TRACE) Trace.trace();
return bClosed;
}
/**
* Gets the metadata regarding this connection's database.
* A Connection's database is able to provide information
* describing its tables, its supported SQL grammar, its stored
* procedures, the capabilities of this connection, and so on. This
* information is made available through a DatabaseMetaData
* object.
*
* @return a DatabaseMetaData object for this Connection
*/
public DatabaseMetaData getMetaData() {
if(Trace.TRACE) Trace.trace();
return new jdbcDatabaseMetaData(this);
}
/**
* Puts this connection in read-only mode as a hint to enable
* database optimizations.
*
* <P><B>Note:</B> This method should not be called while in the
* middle of a transaction.
* <P><font color="#009900">
* Hypersonic SQL will in this case commit
* the transaction automatically.
* <P>
* In Hypersonic SQL, additionally the whole database can be put in
* read-only mode by manually adding the line 'readonly=true' to the
* .properties file. All connections are then automatically readonly.
* The database files will then be opened in readonly mode, and it is
* thus possible to create a CD with this database.
* </font><P>
* @param readOnly true enables read-only mode; false disables
* read-only mode.
* @exception SQLException if a database access error occurs
*/
public void setReadOnly(boolean readonly) throws SQLException {
execute("SET READONLY "+(readonly?"TRUE":"FALSE"));
}
/**
* Tests to see if the connection is in read-only mode.
*
* @return true if connection is read-only and false otherwise
* @exception SQLException if a database access error occurs
*/
public boolean isReadOnly() throws SQLException {
String s="SELECT * FROM SYSTEM_CONNECTIONINFO WHERE KEY='READONLY'";
ResultSet r=execute(s);
r.next();
return r.getString(2).equals("TRUE");
}
/**
* Sets a catalog name in order to select
* a subspace of this Connection's database in which to work.
* <P><font color="#009900">
* Hypersonic SQL does not support catalogs and ignores
* this request.
* </font><P>
*/
public void setCatalog(String catalog) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -