001/*******************************************************************************
002The MIT License (MIT)
003
004Copyright (c) 2024 KILLCODING.COM
005
006Permission is hereby granted, free of charge, to any person obtaining a copy
007of this software and associated documentation files (the "Software"), to deal
008in the Software without restriction, including without limitation the rights
009to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010copies of the Software, and to permit persons to whom the Software is
011furnished to do so, subject to the following conditions:
012
013The above copyright notice and this permission notice shall be included in
014all copies or substantial portions of the Software.
015
016THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
022THE SOFTWARE.
023*****************************************************************************/
024package com.killcoding.datasource;
025
026import java.sql.Array;
027import java.sql.Blob;
028import java.sql.CallableStatement;
029import java.sql.Clob;
030import java.sql.Connection;
031import java.sql.DatabaseMetaData;
032import java.sql.NClob;
033import java.sql.PreparedStatement;
034import java.sql.SQLClientInfoException;
035import java.sql.SQLException;
036import java.sql.SQLWarning;
037import java.sql.SQLXML;
038import java.sql.Savepoint;
039import java.sql.ShardingKey;
040import java.sql.Statement;
041import java.sql.Struct;
042import java.util.Map;
043import java.util.Properties;
044import java.util.concurrent.Executor;
045
046/**
047 * Implement and enhance the JDBC connection class
048 * */
049public final class DriverConnection implements Connection {
050    
051    /**
052     * Original connection 
053     * */
054    private Connection originConnection;
055    
056    /**
057     * DriverDataSource Object
058     * */
059    private DriverDataSource driverDataSource;
060
061    /**
062     * New a DriverConnection object
063     * */
064        public DriverConnection() {
065                super();
066        }
067        
068        /**
069         * New a DriverConnection object
070         * @param driverDataSource 
071         * @param originConnection
072         * */
073        public DriverConnection(DriverDataSource driverDataSource,Connection originConnection){
074            super();
075            this.driverDataSource = driverDataSource;
076            this.originConnection = originConnection;
077        }
078        
079        /**
080         * Get origin connection hash code
081         * @return int - Connection hash code
082         * */
083        public int getOriginConnectionHashCode(){
084            return this.originConnection.hashCode();
085        }
086        
087        /**
088         * Get current DriverDataSource
089         * @return DriverDataSource
090         * */
091        public DriverDataSource getDriverDataSource(){
092            return driverDataSource;
093        }
094        
095        @Override
096        public Object unwrap(java.lang.Class arg0) throws SQLException {
097            return originConnection.unwrap(arg0);
098        }
099
100    @Override   
101        public boolean isWrapperFor(java.lang.Class arg0) throws SQLException{
102            return originConnection.isWrapperFor(arg0);
103        }
104
105    @Override
106        public SQLXML createSQLXML() throws SQLException {
107                return originConnection.createSQLXML();
108        }
109
110    @Override
111        public NClob createNClob() throws SQLException {
112                return originConnection.createNClob();
113        }
114
115    @Override
116        public Blob createBlob() throws SQLException {
117                return originConnection.createBlob();
118        }
119
120    @Override
121        public Clob createClob() throws SQLException {
122                return originConnection.createClob();
123        }
124    
125    @Override
126        public PreparedStatement prepareStatement(String arg0, String[] arg1) throws SQLException {
127                return originConnection.prepareStatement(arg0,arg1);
128        }
129
130    @Override
131        public Array createArrayOf(String arg0, Object[] arg1) throws SQLException {
132                return originConnection.createArrayOf(arg0,arg1);
133        }
134
135    @Override
136        public Properties getClientInfo() throws SQLException {
137                return originConnection.getClientInfo();
138        }
139
140    @Override
141        public String getClientInfo(String arg0) throws SQLException {
142                return originConnection.getClientInfo(arg0);
143        }
144
145    @Override
146        public void setClientInfo(Properties arg0) throws SQLClientInfoException {
147            originConnection.setClientInfo(arg0);
148        }
149
150    @Override
151        public void setClientInfo(String arg0, String arg1) throws SQLClientInfoException {
152            originConnection.setClientInfo(arg0,arg1);
153        }
154
155    @Override
156        public boolean isValid(int arg0) throws SQLException {
157                return originConnection.isValid(arg0);
158        }
159
160    @Override
161        public void abort(Executor arg0) throws SQLException {
162            if(originConnection != null)
163                driverDataSource.abort(originConnection.hashCode());
164        }
165
166    @Override
167        public String getSchema() throws SQLException {
168                return originConnection.getSchema();
169        }
170        
171    @Override
172        public void setSchema(String arg0) throws SQLException {
173            originConnection.setSchema(arg0);
174        }
175
176    @Override
177        public Struct createStruct(String arg0, Object[] arg1) throws SQLException {
178                return originConnection.createStruct(arg0,arg1);
179        }
180
181    @Override
182        public boolean isReadOnly() throws SQLException {
183                return originConnection.isReadOnly();
184        }
185
186    @Override
187        public void setShardingKey(ShardingKey arg0, ShardingKey arg1) throws SQLException {
188            originConnection.setShardingKey(arg0,arg1);
189        }
190
191    @Override
192        public void setReadOnly(boolean arg0) throws SQLException {
193            originConnection.setReadOnly(arg0);
194        }
195
196    @Override
197        public boolean setShardingKeyIfValid(ShardingKey arg0, int arg1) throws SQLException {
198                return originConnection.setShardingKeyIfValid(arg0,arg1);
199        }
200
201    @Override
202        public DatabaseMetaData getMetaData() throws SQLException {
203                return originConnection.getMetaData();
204        }
205
206    @Override
207        public boolean setShardingKeyIfValid(ShardingKey arg0, ShardingKey arg1, int arg2) throws SQLException {
208                return originConnection.setShardingKeyIfValid( arg0, arg1, arg2);
209        }
210
211    @Override
212        public void endRequest() throws SQLException {
213            originConnection.endRequest();
214        }
215
216    @Override
217        public void beginRequest() throws SQLException {
218            originConnection.beginRequest();
219        }
220
221    @Override
222        public int getNetworkTimeout() throws SQLException {
223                return originConnection.getNetworkTimeout();
224        }
225
226    @Override
227        public void setNetworkTimeout(Executor arg0, int arg1) throws SQLException {
228            originConnection.setNetworkTimeout(arg0,arg1);
229        }
230
231    @Override
232        public String nativeSQL(String arg0) throws SQLException {
233                return originConnection.nativeSQL(arg0);
234        }
235
236    @Override
237        public CallableStatement prepareCall(String arg0) throws SQLException {
238                return originConnection.prepareCall(arg0);
239        }
240
241    @Override
242        public boolean getAutoCommit() throws SQLException {
243                return originConnection.getAutoCommit();
244        }
245
246    @Override
247        public void setAutoCommit(boolean arg0) throws SQLException {
248            originConnection.setAutoCommit(arg0);
249        }
250
251    @Override
252        public PreparedStatement prepareStatement(String arg0) throws SQLException {
253                return originConnection.prepareStatement(arg0);
254        }   
255
256    @Override
257        public Statement createStatement() throws SQLException {
258                return originConnection.createStatement();
259        }
260
261    @Override
262        public Statement createStatement(int arg0, int arg1) throws SQLException {
263                return originConnection.createStatement(arg0,arg1);
264        }
265
266    @Override
267        public void clearWarnings() throws SQLException {
268            originConnection.clearWarnings();
269        }
270    
271    @Override
272        public SQLWarning getWarnings() throws SQLException {
273                return originConnection.getWarnings();
274        }
275
276    @Override
277        public void rollback() throws SQLException {
278            originConnection.rollback();
279        }
280        
281    @Override
282        public int getTransactionIsolation() throws SQLException {
283                return originConnection.getTransactionIsolation();
284        }
285
286    @Override
287        public void commit() throws SQLException {
288            originConnection.commit();
289        }
290
291    @Override
292        public void setTransactionIsolation(int arg0) throws SQLException {
293            originConnection.setTransactionIsolation(arg0);
294        }
295
296    @Override
297        public boolean isClosed() throws SQLException {
298                return originConnection.isClosed();
299        }
300
301    @Override
302        public String getCatalog() throws SQLException {
303                return originConnection.getCatalog();
304        }
305
306    @Override
307        public void close() throws SQLException {
308            driverDataSource.closeConnection(originConnection);
309        }
310
311    @Override
312        public void setCatalog(String arg0) throws SQLException {
313            originConnection.setCatalog(arg0);
314        }
315    
316    @Override
317        public void setShardingKey(ShardingKey arg0) throws SQLException {
318            originConnection.setShardingKey(arg0);
319        }
320
321    @Override
322        public void setTypeMap(Map arg0) throws SQLException {
323            originConnection.setTypeMap(arg0);
324        }
325
326    @Override
327        public Map getTypeMap() throws SQLException {
328                return originConnection.getTypeMap();
329        }
330
331    @Override
332        public CallableStatement prepareCall(String arg0, int arg1, int arg2) throws SQLException {
333                return originConnection.prepareCall(arg0, arg1, arg2);
334        }
335
336    @Override
337        public PreparedStatement prepareStatement(String arg0, int arg1, int arg2) throws SQLException {
338                return originConnection.prepareStatement(arg0,arg1,arg2);
339        }
340        
341    @Override
342        public void releaseSavepoint(Savepoint arg0) throws SQLException {
343            originConnection.releaseSavepoint(arg0);
344        }
345
346    @Override
347        public void rollback(Savepoint arg0) throws SQLException {
348            originConnection.rollback(arg0);
349        }
350
351    @Override
352        public Savepoint setSavepoint(String arg0) throws SQLException {
353                return originConnection.setSavepoint(arg0);
354        }
355
356    @Override
357        public Savepoint setSavepoint() throws SQLException {
358                return originConnection.setSavepoint();
359        }
360
361    @Override
362        public int getHoldability() throws SQLException {
363                return originConnection.getHoldability();
364        }
365
366    @Override
367        public void setHoldability(int arg0) throws SQLException {
368            originConnection.setHoldability(arg0);
369        }
370
371    @Override
372        public PreparedStatement prepareStatement(String arg0, int[] arg1) throws SQLException {
373                return originConnection.prepareStatement(arg0,arg1);
374        }
375
376    @Override
377        public PreparedStatement prepareStatement(String arg0, int arg1) throws SQLException {
378                return originConnection.prepareStatement(arg0,arg1);
379        }
380
381        public CallableStatement prepareCall(String arg0, int arg1, int arg2, int arg3) throws SQLException {
382                return originConnection.prepareCall(arg0,arg1,arg2,arg3);
383        }
384
385    @Override
386        public PreparedStatement prepareStatement(String arg0, int arg1, int arg2, int arg3) throws SQLException {
387                return originConnection.prepareStatement(arg0,arg1,arg2,arg3);
388        }   
389        
390        @Override
391        public Statement createStatement(int arg0, int arg1, int arg2) throws SQLException {
392                return originConnection.createStatement(arg0, arg1, arg2);
393        }
394
395}