001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.db.driver;
020
021import java.lang.reflect.InvocationTargetException;
022import java.sql.Array;
023import java.sql.Blob;
024import java.sql.CallableStatement;
025import java.sql.Clob;
026import java.sql.Connection;
027import java.sql.DatabaseMetaData;
028import java.sql.NClob;
029import java.sql.PreparedStatement;
030import java.sql.SQLClientInfoException;
031import java.sql.SQLException;
032import java.sql.SQLWarning;
033import java.sql.SQLXML;
034import java.sql.Savepoint;
035import java.sql.Statement;
036import java.sql.Struct;
037import java.util.Map;
038import java.util.Properties;
039import java.util.concurrent.Executor;
040
041import lucee.commons.lang.ExceptionUtil;
042import lucee.runtime.exp.PageRuntimeException;
043import lucee.runtime.op.Caster;
044
045public class ConnectionProxy implements Connection {
046        
047        private Connection conn;
048        private Factory factory;
049
050        public ConnectionProxy(Factory factory,Connection conn){
051                this.conn=conn;
052                this.factory=factory;
053        }
054        
055        public Connection getConnection() {
056                return conn;
057        }
058
059        @Override
060        public Statement createStatement() throws SQLException {
061                return factory.createStatementProxy(this,conn.createStatement());
062        }
063
064        @Override
065        public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
066                return factory.createStatementProxy(this,conn.createStatement(resultSetType, resultSetConcurrency));
067        }
068
069        @Override
070        public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
071                return factory.createStatementProxy(this,conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability));
072        }
073
074        @Override
075        public CallableStatement prepareCall(String sql) throws SQLException {
076                return factory.createCallableStatementProxy(this,conn.prepareCall(sql),sql);
077        }
078
079        @Override
080        public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
081                return factory.createCallableStatementProxy(this,conn.prepareCall(sql, resultSetType, resultSetConcurrency),sql);
082        }
083
084        @Override
085        public CallableStatement prepareCall(String sql, int resultSetType,int resultSetConcurrency, int resultSetHoldability) throws SQLException {
086                return factory.createCallableStatementProxy(this,conn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability),sql);
087        }
088
089        @Override
090        public PreparedStatement prepareStatement(String sql) throws SQLException {
091                return factory.createPreparedStatementProxy(this, conn.prepareStatement(sql),sql);
092        }
093
094        @Override
095        public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
096                return factory.createPreparedStatementProxy(this, conn.prepareStatement(sql, autoGeneratedKeys),sql);
097        }
098
099        @Override
100        public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
101                return factory.createPreparedStatementProxy(this, conn.prepareStatement(sql, columnIndexes),sql);
102        }
103
104        @Override
105        public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
106                return factory.createPreparedStatementProxy(this, conn.prepareStatement(sql, columnNames),sql);
107        }
108
109        @Override
110        public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
111                return factory.createPreparedStatementProxy(this, conn.prepareStatement(sql, resultSetType, resultSetConcurrency),sql);
112        }
113
114        @Override
115        public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
116                return factory.createPreparedStatementProxy(this, conn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability),sql);
117        }
118        
119        
120        
121        @Override
122        public boolean isWrapperFor(Class<?> iface) throws SQLException {
123                return conn.isWrapperFor(iface);
124        }
125
126        @Override
127        public <T> T unwrap(Class<T> iface) throws SQLException {
128                return conn.unwrap(iface);
129        }
130
131        @Override
132        public void clearWarnings() throws SQLException {
133                conn.clearWarnings();
134        }
135
136        @Override
137        public void close() throws SQLException {
138                conn.close();
139        }
140
141        @Override
142        public void commit() throws SQLException {
143                conn.commit();
144        }
145
146        @Override
147        public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
148                return conn.createArrayOf(typeName, elements);
149        }
150
151        @Override
152        public Blob createBlob() throws SQLException {
153                return conn.createBlob();
154        }
155
156        @Override
157        public Clob createClob() throws SQLException {
158                return conn.createClob();
159        }
160
161        @Override
162        public NClob createNClob() throws SQLException {
163                return conn.createNClob();
164        }
165
166        @Override
167        public SQLXML createSQLXML() throws SQLException {
168                return conn.createSQLXML();
169        }
170
171        @Override
172        public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
173                return conn.createStruct(typeName, attributes);
174        }
175
176        @Override
177        public boolean getAutoCommit() throws SQLException {
178                return conn.getAutoCommit();
179        }
180
181        @Override
182        public String getCatalog() throws SQLException {
183                return conn.getCatalog();
184        }
185
186        @Override
187        public Properties getClientInfo() throws SQLException {
188                return conn.getClientInfo();
189        }
190
191        @Override
192        public String getClientInfo(String name) throws SQLException {
193                return conn.getClientInfo(name);
194        }
195
196        @Override
197        public int getHoldability() throws SQLException {
198                return conn.getHoldability();
199        }
200
201        @Override
202        public DatabaseMetaData getMetaData() throws SQLException {
203                return conn.getMetaData();
204        }
205
206        @Override
207        public int getTransactionIsolation() throws SQLException {
208                return conn.getTransactionIsolation();
209        }
210
211        @Override
212        public Map<String, Class<?>> getTypeMap() throws SQLException {
213                return conn.getTypeMap();
214        }
215
216        @Override
217        public SQLWarning getWarnings() throws SQLException {
218                return conn.getWarnings();
219        }
220
221        @Override
222        public boolean isClosed() throws SQLException {
223                return conn.isClosed();
224        }
225
226        @Override
227        public boolean isReadOnly() throws SQLException {
228                return conn.isReadOnly();
229        }
230
231        @Override
232        public boolean isValid(int timeout) throws SQLException {
233                return conn.isValid(timeout);
234        }
235
236        @Override
237        public String nativeSQL(String sql) throws SQLException {
238                return conn.nativeSQL(sql);
239        }
240
241        @Override
242        public void releaseSavepoint(Savepoint savepoint) throws SQLException {
243                conn.releaseSavepoint(savepoint);
244        }
245
246        @Override
247        public void rollback() throws SQLException {
248                conn.rollback();
249        }
250
251        @Override
252        public void rollback(Savepoint savepoint) throws SQLException {
253                conn.rollback(savepoint);
254        }
255
256        @Override
257        public void setAutoCommit(boolean autoCommit) throws SQLException {
258                conn.setAutoCommit(autoCommit);
259        }
260
261        @Override
262        public void setCatalog(String catalog) throws SQLException {
263                conn.setCatalog(catalog);
264        }
265
266        @Override
267        public void setClientInfo(Properties properties) throws SQLClientInfoException {
268                conn.setClientInfo(properties);
269        }
270
271        @Override
272        public void setClientInfo(String name, String value) throws SQLClientInfoException {
273                conn.setClientInfo(name, value);
274        }
275
276        @Override
277        public void setHoldability(int holdability) throws SQLException {
278                conn.setHoldability(holdability);
279        }
280
281        @Override
282        public void setReadOnly(boolean readOnly) throws SQLException {
283                conn.setReadOnly(readOnly);
284        }
285
286        @Override
287        public Savepoint setSavepoint() throws SQLException {
288                return conn.setSavepoint();
289        }
290
291        @Override
292        public Savepoint setSavepoint(String name) throws SQLException {
293                return conn.setSavepoint(name);
294        }
295
296        @Override
297        public void setTransactionIsolation(int level) throws SQLException {
298                conn.setTransactionIsolation(level);
299        }
300
301        @Override
302        public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
303                conn.setTypeMap(map);
304        }
305
306
307
308
309        public void setSchema(String schema) throws SQLException {
310                // used reflection to make sure this work with Java 5 and 6
311                try {
312                        conn.getClass().getMethod("setSchema", new Class[]{String.class}).invoke(conn, new Object[]{schema});
313                }
314                catch (Throwable t) {
315                ExceptionUtil.rethrowIfNecessary(t);
316                        if(t instanceof InvocationTargetException && ((InvocationTargetException)t).getTargetException() instanceof SQLException)
317                                throw (SQLException)((InvocationTargetException)t).getTargetException();
318                        throw new PageRuntimeException(Caster.toPageException(t));
319                }
320        }
321
322        public String getSchema() throws SQLException {
323                // used reflection to make sure this work with Java 5 and 6
324                try {
325                        return Caster.toString(conn.getClass().getMethod("getSchema", new Class[]{}).invoke(conn, new Object[]{}));
326                }
327                catch (Throwable t) {
328                ExceptionUtil.rethrowIfNecessary(t);
329                        if(t instanceof InvocationTargetException && ((InvocationTargetException)t).getTargetException() instanceof SQLException)
330                                throw (SQLException)((InvocationTargetException)t).getTargetException();
331                        throw new PageRuntimeException(Caster.toPageException(t));
332                }
333        }
334
335        public void abort(Executor executor) throws SQLException {
336                // used reflection to make sure this work with Java 5 and 6
337                try {
338                        conn.getClass().getMethod("abort", new Class[]{Executor.class}).invoke(conn, new Object[]{executor});
339                }
340                catch (Throwable t) {
341                ExceptionUtil.rethrowIfNecessary(t);
342                        if(t instanceof InvocationTargetException && ((InvocationTargetException)t).getTargetException() instanceof SQLException)
343                                throw (SQLException)((InvocationTargetException)t).getTargetException();
344                        throw new PageRuntimeException(Caster.toPageException(t));
345                }
346        }
347
348        public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
349                // used reflection to make sure this work with Java 5 and 6
350                try {
351                        conn.getClass().getMethod("setNetworkTimeout", new Class[]{Executor.class,int.class}).invoke(conn, new Object[]{executor,milliseconds});
352                }
353                catch (Throwable t) {
354                ExceptionUtil.rethrowIfNecessary(t);
355                        if(t instanceof InvocationTargetException && ((InvocationTargetException)t).getTargetException() instanceof SQLException)
356                                throw (SQLException)((InvocationTargetException)t).getTargetException();
357                        throw new PageRuntimeException(Caster.toPageException(t));
358                }
359        }
360
361        public int getNetworkTimeout() throws SQLException {
362                // used reflection to make sure this work with Java 5 and 6
363                try {
364                        return Caster.toIntValue(conn.getClass().getMethod("getNetworkTimeout", new Class[]{}).invoke(conn, new Object[]{}));
365                }
366                catch (Throwable t) {
367                ExceptionUtil.rethrowIfNecessary(t);
368                        if(t instanceof InvocationTargetException && ((InvocationTargetException)t).getTargetException() instanceof SQLException)
369                                throw (SQLException)((InvocationTargetException)t).getTargetException();
370                        throw new PageRuntimeException(Caster.toPageException(t));
371                }
372        }
373}