All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.sql.jdbc.JDBCConnection Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.sql.jdbc;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.sql.*;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.hfg.security.LoginCredentials;
import com.hfg.sql.SQLUtil;
import com.hfg.util.StackTraceUtil;

//------------------------------------------------------------------------------
/**
 Represents an RDBMS database connection.
 
@author J. Alex Taylor, hairyfatguy.com
*/ //------------------------------------------------------------------------------ // com.hfg XML/HTML Coding Library // // 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com // [email protected] //------------------------------------------------------------------------------ public abstract class JDBCConnection implements Connection { private final Connection mInnerConn; private JDBCServer mServer; private String mDatabaseName; private LoginCredentials mCredentials; private JDBCConnectionSettings mSettings = new JDBCConnectionSettings(); private final static Logger LOGGER = Logger.getLogger(JDBCConnection.class.getName()); static { LOGGER.setLevel(Level.INFO); } //########################################################################### // CONSTRUCTORS //########################################################################### //--------------------------------------------------------------------------- protected JDBCConnection(Connection inConnection) { mInnerConn = inConnection; } //--------------------------------------------------------------------------- protected JDBCConnection(JDBCServer inServer, String inDatabaseName, LoginCredentials inCredentials) { this(inServer, inDatabaseName, inCredentials, null); } //--------------------------------------------------------------------------- protected JDBCConnection(JDBCServer inServer, String inDatabaseName, LoginCredentials inCredentials, JDBCConnectionSettings inSettings) { mServer = inServer; mDatabaseName = inDatabaseName; mCredentials = inCredentials; mSettings = inSettings; mInnerConn = establishConnection(); } //########################################################################### // PUBLIC METHODS //########################################################################### //--------------------------------------------------------------------------- public static Logger getLogger() { return LOGGER; } //--------------------------------------------------------------------------- public boolean tableExists(String inTablename) throws SQLException { boolean tableExists; DatabaseMetaData metaData = getMetaData(); ResultSet rs = null; try { rs = metaData.getTables(null, null, inTablename.toLowerCase(), null); tableExists = (rs.next() && rs.getString("TABLE_NAME").equalsIgnoreCase(inTablename)); } finally { if (rs != null) rs.close(); } return tableExists; } //--------------------------------------------------------------------------- public boolean dropTable(String inTablename) throws SQLException { return SQLUtil.execute(this, "DROP TABLE " + inTablename); } //--------------------------------------------------------------------------- public abstract String getCurrentUser() throws SQLException; //--------------------------------------------------------------------------- public boolean execute(String inSQL) throws SQLException { return SQLUtil.execute(this, inSQL); } //--------------------------------------------------------------------------- public int[] batchExecute(String inSQL) throws IOException, SQLException { return batchExecute(new StringReader(inSQL)); } //--------------------------------------------------------------------------- public int[] batchExecute(Reader inSQL) { int[] results; try { Statement stmt = null; try { stmt = createStatement(); List sqlCmds = SQLUtil.splitBatchSQL(inSQL); for (String sql : sqlCmds) { stmt.addBatch(sql); } results = stmt.executeBatch(); } finally { SQLUtil.close(stmt); } } catch (SQLException e) { // SQLExceptions are broken out separately so that we can expose the next exception if present. throw new JDBCException(e); } catch (IOException e) { throw new JDBCException(e); } return results; } //--------------------------------------------------------------------------- public String name() { String connName = toString(); int index = connName.lastIndexOf("."); if (index >= 0) { connName = connName.substring(index+ 1); } return connName; } //--------------------------------------------------------------------------- @Override public Statement createStatement() throws SQLException { return mInnerConn.createStatement(); } //--------------------------------------------------------------------------- @Override public PreparedStatement prepareStatement(String inSQL) throws SQLException { return mInnerConn.prepareStatement(inSQL); } //--------------------------------------------------------------------------- @Override public CallableStatement prepareCall(String inSQL) throws SQLException { return mInnerConn.prepareCall(inSQL); } //--------------------------------------------------------------------------- @Override public String nativeSQL(String inSQL) throws SQLException { return mInnerConn.nativeSQL(inSQL); } //--------------------------------------------------------------------------- @Override public void setAutoCommit(boolean inAutoCommit) throws SQLException { // Don't spend time extracting the stack trace if it won't be logged anyway if (LOGGER.getLevel() != null && LOGGER.getLevel().intValue() <= Level.FINE.intValue() && ! StackTraceUtil.getCallingMethodName().equals("setAutoCommit")) // Avoid double logging if the connection is wrapped { LOGGER.fine(name() + " autocommit set from " + mInnerConn.getAutoCommit() + " to " + inAutoCommit + " by " + StackTraceUtil.getCallingStackTraceElement(null).toString()); } mInnerConn.setAutoCommit(inAutoCommit); } //--------------------------------------------------------------------------- @Override public boolean getAutoCommit() throws SQLException { return mInnerConn.getAutoCommit(); } //--------------------------------------------------------------------------- @Override public void commit() throws SQLException { mInnerConn.commit(); } //--------------------------------------------------------------------------- @Override public void rollback() throws SQLException { mInnerConn.rollback(); } //--------------------------------------------------------------------------- @Override public void close() throws SQLException { mInnerConn.close(); } //--------------------------------------------------------------------------- @Override public boolean isClosed() throws SQLException { return mInnerConn.isClosed(); } //--------------------------------------------------------------------------- @Override public DatabaseMetaData getMetaData() throws SQLException { return mInnerConn.getMetaData(); } //--------------------------------------------------------------------------- @Override public void setReadOnly(boolean inValue) throws SQLException { mInnerConn.setReadOnly(inValue); } //--------------------------------------------------------------------------- @Override public boolean isReadOnly() throws SQLException { return mInnerConn.isReadOnly(); } //--------------------------------------------------------------------------- @Override public void setCatalog(String inValue) throws SQLException { mInnerConn.setCatalog(inValue); } //--------------------------------------------------------------------------- @Override public String getCatalog() throws SQLException { return mInnerConn.getCatalog(); } //--------------------------------------------------------------------------- @Override public void setTransactionIsolation(int inLevel) throws SQLException { mInnerConn.setTransactionIsolation(inLevel); } //--------------------------------------------------------------------------- @Override public int getTransactionIsolation() throws SQLException { return mInnerConn.getTransactionIsolation(); } //--------------------------------------------------------------------------- @Override public SQLWarning getWarnings() throws SQLException { return mInnerConn.getWarnings(); } //--------------------------------------------------------------------------- @Override public void clearWarnings() throws SQLException { mInnerConn.clearWarnings(); } //--------------------------------------------------------------------------- @Override public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { return mInnerConn.createStatement(resultSetType, resultSetConcurrency); } //--------------------------------------------------------------------------- @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { return mInnerConn.prepareStatement(sql, resultSetType, resultSetConcurrency); } //--------------------------------------------------------------------------- @Override public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { return mInnerConn.prepareCall(sql, resultSetType, resultSetConcurrency); } //--------------------------------------------------------------------------- @Override public Map> getTypeMap() throws SQLException { return mInnerConn.getTypeMap(); } //--------------------------------------------------------------------------- @Override public void setTypeMap(Map> map) throws SQLException { mInnerConn.setTypeMap(map); } //--------------------------------------------------------------------------- @Override public void setHoldability(int inValue) throws SQLException { mInnerConn.setHoldability(inValue); } //--------------------------------------------------------------------------- @Override public int getHoldability() throws SQLException { return mInnerConn.getHoldability(); } //--------------------------------------------------------------------------- @Override public Savepoint setSavepoint() throws SQLException { return mInnerConn.setSavepoint(); } //--------------------------------------------------------------------------- @Override public Savepoint setSavepoint(String name) throws SQLException { return mInnerConn.setSavepoint(name); } //--------------------------------------------------------------------------- @Override public void rollback(Savepoint savepoint) throws SQLException { mInnerConn.rollback(savepoint); } //--------------------------------------------------------------------------- @Override public void releaseSavepoint(Savepoint savepoint) throws SQLException { mInnerConn.releaseSavepoint(savepoint); } //--------------------------------------------------------------------------- @Override public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return mInnerConn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability); } //--------------------------------------------------------------------------- @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return mInnerConn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability); } //--------------------------------------------------------------------------- @Override public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return mInnerConn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability); } //--------------------------------------------------------------------------- @Override public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { return mInnerConn.prepareStatement(sql, autoGeneratedKeys); } //--------------------------------------------------------------------------- @Override public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { return mInnerConn.prepareStatement(sql, columnIndexes); } //--------------------------------------------------------------------------- @Override public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { return mInnerConn.prepareStatement(sql, columnNames); } //--------------------------------------------------------------------------- @Override public Clob createClob() throws SQLException { return mInnerConn.createClob(); } //--------------------------------------------------------------------------- @Override public Blob createBlob() throws SQLException { return mInnerConn.createBlob(); } //--------------------------------------------------------------------------- @Override public NClob createNClob() throws SQLException { return mInnerConn.createNClob(); } //--------------------------------------------------------------------------- @Override public SQLXML createSQLXML() throws SQLException { return mInnerConn.createSQLXML(); } //--------------------------------------------------------------------------- @Override public boolean isValid(int timeout) throws SQLException { return mInnerConn.isValid(timeout); } //--------------------------------------------------------------------------- @Override public void setClientInfo(String name, String value) throws SQLClientInfoException { mInnerConn.setClientInfo(name, value); } //--------------------------------------------------------------------------- @Override public void setClientInfo(Properties properties) throws SQLClientInfoException { mInnerConn.setClientInfo(properties); } //--------------------------------------------------------------------------- @Override public String getClientInfo(String name) throws SQLException { return mInnerConn.getClientInfo(name); } //--------------------------------------------------------------------------- @Override public Properties getClientInfo() throws SQLException { return mInnerConn.getClientInfo(); } //--------------------------------------------------------------------------- @Override public Array createArrayOf(String typeName, Object[] elements) throws SQLException { return mInnerConn.createArrayOf(typeName, elements); } //--------------------------------------------------------------------------- @Override public Struct createStruct(String typeName, Object[] attributes) throws SQLException { return mInnerConn.createStruct(typeName, attributes); } //--------------------------------------------------------------------------- @Override public void setSchema(String schema) throws SQLException { mInnerConn.setSchema(schema); } //--------------------------------------------------------------------------- @Override public String getSchema() throws SQLException { return mInnerConn.getSchema(); } //--------------------------------------------------------------------------- @Override public void abort(Executor executor) throws SQLException { mInnerConn.abort(executor); } //--------------------------------------------------------------------------- @Override public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException { mInnerConn.setNetworkTimeout(executor, milliseconds); } //--------------------------------------------------------------------------- @Override public int getNetworkTimeout() throws SQLException { return mInnerConn.getNetworkTimeout(); } //--------------------------------------------------------------------------- @Override public T unwrap(Class iface) throws SQLException { return mInnerConn.unwrap(iface); } //--------------------------------------------------------------------------- @Override public boolean isWrapperFor(Class iface) throws SQLException { return mInnerConn.isWrapperFor(iface); } //########################################################################### // PROTECTED METHODS //########################################################################### //--------------------------------------------------------------------------- protected Connection getInnerConnection() { return mInnerConn; } //########################################################################### // PRIVATE METHODS //########################################################################### //--------------------------------------------------------------------------- private Connection establishConnection() { return mServer.getConnection(mDatabaseName, mCredentials, mSettings); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy