📄 ssconnection.java
字号:
/* =============================================================
* SmallSQL : a free Java DBMS library for the Java(tm) platform
* =============================================================
*
* (C) Copyright 2004-2007, by Volker Berlin.
*
* Project Info: http://www.smallsql.de/
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ---------------
* SSConnection.java
* ---------------
* Author: Volker Berlin
*
*/
package smallsql.database;
import java.io.RandomAccessFile;
import java.sql.*;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Properties;
public class SSConnection implements Connection {
private Database database;
private boolean autoCommit = true;
int isolationLevel = TRANSACTION_READ_COMMITTED; // see also getDefaultTransactionIsolation
private List commitPages = new ArrayList();
/** The time on which a transaction is starting. */
private long transactionTime;
private final SSDatabaseMetaData metadata;
private int holdability;
final Logger log;
SSConnection( Properties props ) throws SQLException{
log = new Logger();
String name = props.getProperty("dbpath");
boolean create = "true".equals(props.getProperty("create"));
database = Database.getDatabase(name, this, create);
metadata = new SSDatabaseMetaData(this);
}
/**
* Create a copy of the Connection with it own transaction room.
* @param con the original Connection
*/
SSConnection( SSConnection con ){
database = con.database;
metadata = con.metadata;
log = con.log;
}
/**
* @param returnNull If null is a valid return value for the case of not connected to a database.
* @throws SQLException If not connected and returnNull is false.
*/
Database getDatabase(boolean returnNull) throws SQLException{
testClosedConnection();
if(!returnNull && database == null) throw Utils.createSQLException("You are not connected with a Database.");
return database;
}
public Statement createStatement() throws SQLException {
return new SSStatement(this);
}
public PreparedStatement prepareStatement(String sql) throws SQLException {
return new SSPreparedStatement( this, sql);
}
public CallableStatement prepareCall(String sql) throws SQLException {
return new SSCallableStatement( this, sql);
}
public String nativeSQL(String sql){
return sql;
}
public void setAutoCommit(boolean autoCommit) throws SQLException {
if(log.isLogging()) log.println("AutoCommit:"+autoCommit);
if(this.autoCommit != autoCommit){
commit();
this.autoCommit = autoCommit;
}
}
public boolean getAutoCommit(){
return autoCommit;
}
/**
* Add a page for later commit or rollback.
*/
void add(StorePage storePage) throws SQLException{
testClosedConnection();
commitPages.add(storePage);
}
public void commit() throws SQLException {
try{
log.println("Commit");
testClosedConnection();
synchronized(commitPages){
int count = commitPages.size();
for(int i=0; i<count; i++){
StorePage page = (StorePage)commitPages.get(i);
page.commit();
}
for(int i=0; i<count; i++){
StorePage page = (StorePage)commitPages.get(i);
page.freeLock();
}
commitPages.clear();
transactionTime = System.currentTimeMillis();
}
}catch(Throwable e){
rollback();
throw Utils.createSQLException(e);
}
}
/**
* Discard all changes of a file because it was deleted.
*/
void rollbackFile(RandomAccessFile raFile) throws SQLException{
testClosedConnection();
// remove the all commits that point to this table
for(int i=commitPages.size()-1; i>=0; i--){
StorePage page = (StorePage)commitPages.get(i);
if(page.raFile == raFile){
page.rollback();
page.freeLock();
}
}
}
void rollback(int savepoint) throws SQLException{
testClosedConnection();
for(int i = commitPages.size()-1; i>=savepoint; i--){
StorePage page = (StorePage)commitPages.remove(i);
page.rollback();
page.freeLock();
}
}
public void rollback() throws SQLException {
log.println("Rollback");
testClosedConnection();
synchronized(commitPages){
int count = commitPages.size();
for(int i=0; i<count; i++){
StorePage page = (StorePage)commitPages.get(i);
page.rollback();
page.freeLock();
}
commitPages.clear();
transactionTime = System.currentTimeMillis();
}
}
public void close() throws SQLException {
rollback();
database = null;
commitPages = null;
Database.closeConnection(this);
}
final void testClosedConnection() throws SQLException{
if(isClosed()) throw Utils.createSQLException("Connection is already closed.");
}
public boolean isClosed(){
return (commitPages == null);
}
public DatabaseMetaData getMetaData(){
return metadata;
}
public void setReadOnly(boolean readOnly){
//TODO Connection ReadOnly implementing
}
public boolean isReadOnly(){
return false;
}
public void setCatalog(String catalog) throws SQLException {
testClosedConnection();
database = Database.getDatabase(catalog, this, false);
}
public String getCatalog(){
if(database == null)
return "";
return database.getName();
}
public void setTransactionIsolation(int level) throws SQLException {
if(!metadata.supportsTransactionIsolationLevel(level))
throw Utils.createSQLException("Unknown Transaction Isolation Level:"+level);
isolationLevel = level;
}
public int getTransactionIsolation(){
return isolationLevel;
}
public SQLWarning getWarnings(){
return null;
}
public void clearWarnings(){
//TODO support for Warnings
}
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
return new SSStatement( this, resultSetType, resultSetConcurrency);
}
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return new SSPreparedStatement( this, sql, resultSetType, resultSetConcurrency);
}
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return new SSCallableStatement( this, sql, resultSetType, resultSetConcurrency);
}
public Map getTypeMap(){
return null;
}
public void setTypeMap(Map map){
//TODO support for TypeMap
}
public void setHoldability(int holdability){
this.holdability = holdability;
}
public int getHoldability(){
return holdability;
}
int getSavepoint() throws SQLException{
testClosedConnection();
return commitPages.size();
}
public Savepoint setSavepoint() throws SQLException {
return new SSSavepoint(getSavepoint(), null, transactionTime);
}
public Savepoint setSavepoint(String name) throws SQLException {
return new SSSavepoint(getSavepoint(), name, transactionTime);
}
public void rollback(Savepoint savepoint) throws SQLException {
if(savepoint instanceof SSSavepoint){
if(((SSSavepoint)savepoint).transactionTime != transactionTime){
throw Utils.createSQLException("Savepoint is not valid for this transaction.");
}
rollback( savepoint.getSavepointId() );
return;
}
throw Utils.createSQLException("Savepoint is not valid for this driver."+savepoint);
}
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
if(savepoint instanceof SSSavepoint){
((SSSavepoint)savepoint).transactionTime = 0;
return;
}
throw Utils.createSQLException("Savepoint is not valid for this driver."+savepoint);
}
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
//TODO resultSetHoldability
return new SSStatement( this, resultSetType, resultSetConcurrency);
}
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
//TODO resultSetHoldability
return new SSPreparedStatement( this, sql);
}
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
//TODO resultSetHoldability
return new SSCallableStatement( this, sql, resultSetType, resultSetConcurrency);
}
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
SSPreparedStatement pr = new SSPreparedStatement( this, sql);
pr.setNeedGeneratedKeys(autoGeneratedKeys);
return pr;
}
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
SSPreparedStatement pr = new SSPreparedStatement( this, sql);
pr.setNeedGeneratedKeys(columnIndexes);
return pr;
}
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
SSPreparedStatement pr = new SSPreparedStatement( this, sql);
pr.setNeedGeneratedKeys(columnNames);
return pr;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -