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