2
Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
5
The MySQL Connector/J is licensed under the terms of the GPLv2
6
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
7
There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
8
this software, see the FLOSS License Exception
9
<http://www.mysql.com/about/legal/licensing/foss-exception.html>.
11
This program is free software; you can redistribute it and/or modify it under the terms
12
of the GNU General Public License as published by the Free Software Foundation; version 2
15
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
16
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
See the GNU General Public License for more details.
19
You should have received a copy of the GNU General Public License along with this
20
program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
21
Floor, Boston, MA 02110-1301 USA
26
package com.mysql.jdbc.jdbc2.optional;
28
import java.lang.reflect.Constructor;
29
import java.sql.Connection;
30
import java.sql.ResultSet;
31
import java.sql.SQLException;
32
import java.sql.SQLWarning;
33
import java.sql.Statement;
35
import com.mysql.jdbc.SQLError;
36
import com.mysql.jdbc.Util;
39
* Wraps statements so that errors can be reported correctly to
40
* ConnectionEventListeners.
42
* @author Mark Matthews
44
* @version $Id: StatementWrapper.java,v 1.1.2.1 2005/05/13 18:58:38 mmatthews
47
public class StatementWrapper extends WrapperBase implements Statement {
48
private static final Constructor<?> JDBC_4_STATEMENT_WRAPPER_CTOR;
53
JDBC_4_STATEMENT_WRAPPER_CTOR = Class.forName(
54
"com.mysql.jdbc.jdbc2.optional.JDBC4StatementWrapper").getConstructor(
55
new Class[] { ConnectionWrapper.class,
56
MysqlPooledConnection.class,
58
} catch (SecurityException e) {
59
throw new RuntimeException(e);
60
} catch (NoSuchMethodException e) {
61
throw new RuntimeException(e);
62
} catch (ClassNotFoundException e) {
63
throw new RuntimeException(e);
66
JDBC_4_STATEMENT_WRAPPER_CTOR = null;
70
protected static StatementWrapper getInstance(ConnectionWrapper c,
71
MysqlPooledConnection conn,
72
Statement toWrap) throws SQLException {
73
if (!Util.isJdbc4()) {
74
return new StatementWrapper(c,
78
return (StatementWrapper) Util.handleNewInstance(
79
JDBC_4_STATEMENT_WRAPPER_CTOR,
81
conn, toWrap }, conn.getExceptionInterceptor());
84
protected Statement wrappedStmt;
86
protected ConnectionWrapper wrappedConn;
88
public StatementWrapper(ConnectionWrapper c, MysqlPooledConnection conn,
91
this.wrappedStmt = toWrap;
98
* @see java.sql.Statement#getConnection()
100
public Connection getConnection() throws SQLException {
102
if (this.wrappedStmt != null) {
103
return this.wrappedConn;
106
throw SQLError.createSQLException("Statement already closed",
107
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
108
} catch (SQLException sqlEx) {
109
checkAndFireConnectionError(sqlEx);
112
return null; // we actually never get here, but the compiler can't
121
* @see java.sql.Statement#setCursorName(java.lang.String)
123
public void setCursorName(String name) throws SQLException {
125
if (this.wrappedStmt != null) {
126
this.wrappedStmt.setCursorName(name);
128
throw SQLError.createSQLException("Statement already closed",
129
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
131
} catch (SQLException sqlEx) {
132
checkAndFireConnectionError(sqlEx);
139
* @see java.sql.Statement#setEscapeProcessing(boolean)
141
public void setEscapeProcessing(boolean enable) throws SQLException {
143
if (this.wrappedStmt != null) {
144
this.wrappedStmt.setEscapeProcessing(enable);
146
throw SQLError.createSQLException("Statement already closed",
147
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
149
} catch (SQLException sqlEx) {
150
checkAndFireConnectionError(sqlEx);
157
* @see java.sql.Statement#setFetchDirection(int)
159
public void setFetchDirection(int direction) throws SQLException {
161
if (this.wrappedStmt != null) {
162
this.wrappedStmt.setFetchDirection(direction);
164
throw SQLError.createSQLException("Statement already closed",
165
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
167
} catch (SQLException sqlEx) {
168
checkAndFireConnectionError(sqlEx);
175
* @see java.sql.Statement#getFetchDirection()
177
public int getFetchDirection() throws SQLException {
179
if (this.wrappedStmt != null) {
180
return this.wrappedStmt.getFetchDirection();
183
throw SQLError.createSQLException("Statement already closed",
184
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
185
} catch (SQLException sqlEx) {
186
checkAndFireConnectionError(sqlEx);
189
return ResultSet.FETCH_FORWARD; // we actually never get here, but the
190
// compiler can't figure
198
* @see java.sql.Statement#setFetchSize(int)
200
public void setFetchSize(int rows) throws SQLException {
202
if (this.wrappedStmt != null) {
203
this.wrappedStmt.setFetchSize(rows);
205
throw SQLError.createSQLException("Statement already closed",
206
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
208
} catch (SQLException sqlEx) {
209
checkAndFireConnectionError(sqlEx);
216
* @see java.sql.Statement#getFetchSize()
218
public int getFetchSize() throws SQLException {
220
if (this.wrappedStmt != null) {
221
return this.wrappedStmt.getFetchSize();
224
throw SQLError.createSQLException("Statement already closed",
225
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
226
} catch (SQLException sqlEx) {
227
checkAndFireConnectionError(sqlEx);
230
return 0; // we actually never get here, but the compiler can't figure
238
* @see java.sql.Statement#getGeneratedKeys()
240
public ResultSet getGeneratedKeys() throws SQLException {
242
if (this.wrappedStmt != null) {
243
return this.wrappedStmt.getGeneratedKeys();
246
throw SQLError.createSQLException("Statement already closed",
247
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
248
} catch (SQLException sqlEx) {
249
checkAndFireConnectionError(sqlEx);
252
return null; // we actually never get here, but the compiler can't
261
* @see java.sql.Statement#setMaxFieldSize(int)
263
public void setMaxFieldSize(int max) throws SQLException {
265
if (this.wrappedStmt != null) {
266
this.wrappedStmt.setMaxFieldSize(max);
268
throw SQLError.createSQLException("Statement already closed",
269
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
271
} catch (SQLException sqlEx) {
272
checkAndFireConnectionError(sqlEx);
279
* @see java.sql.Statement#getMaxFieldSize()
281
public int getMaxFieldSize() throws SQLException {
283
if (this.wrappedStmt != null) {
284
return this.wrappedStmt.getMaxFieldSize();
287
throw SQLError.createSQLException("Statement already closed",
288
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
289
} catch (SQLException sqlEx) {
290
checkAndFireConnectionError(sqlEx);
293
return 0; // we actually never get here, but the compiler can't figure
301
* @see java.sql.Statement#setMaxRows(int)
303
public void setMaxRows(int max) throws SQLException {
305
if (this.wrappedStmt != null) {
306
this.wrappedStmt.setMaxRows(max);
308
throw SQLError.createSQLException("Statement already closed",
309
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
311
} catch (SQLException sqlEx) {
312
checkAndFireConnectionError(sqlEx);
319
* @see java.sql.Statement#getMaxRows()
321
public int getMaxRows() throws SQLException {
323
if (this.wrappedStmt != null) {
324
return this.wrappedStmt.getMaxRows();
327
throw SQLError.createSQLException("Statement already closed",
328
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
329
} catch (SQLException sqlEx) {
330
checkAndFireConnectionError(sqlEx);
333
return 0; // we actually never get here, but the compiler can't figure
341
* @see java.sql.Statement#getMoreResults()
343
public boolean getMoreResults() throws SQLException {
345
if (this.wrappedStmt != null) {
346
return this.wrappedStmt.getMoreResults();
349
throw SQLError.createSQLException("Statement already closed",
350
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
351
} catch (SQLException sqlEx) {
352
checkAndFireConnectionError(sqlEx);
361
* @see java.sql.Statement#getMoreResults(int)
363
public boolean getMoreResults(int current) throws SQLException {
365
if (this.wrappedStmt != null) {
366
return this.wrappedStmt.getMoreResults(current);
369
throw SQLError.createSQLException("Statement already closed",
370
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
371
} catch (SQLException sqlEx) {
372
checkAndFireConnectionError(sqlEx);
381
* @see java.sql.Statement#setQueryTimeout(int)
383
public void setQueryTimeout(int seconds) throws SQLException {
385
if (this.wrappedStmt != null) {
386
this.wrappedStmt.setQueryTimeout(seconds);
388
throw SQLError.createSQLException("Statement already closed",
389
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
391
} catch (SQLException sqlEx) {
392
checkAndFireConnectionError(sqlEx);
399
* @see java.sql.Statement#getQueryTimeout()
401
public int getQueryTimeout() throws SQLException {
403
if (this.wrappedStmt != null) {
404
return this.wrappedStmt.getQueryTimeout();
407
throw SQLError.createSQLException("Statement already closed",
408
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
409
} catch (SQLException sqlEx) {
410
checkAndFireConnectionError(sqlEx);
419
* @see java.sql.Statement#getResultSet()
421
public ResultSet getResultSet() throws SQLException {
423
if (this.wrappedStmt != null) {
424
ResultSet rs = this.wrappedStmt.getResultSet();
427
((com.mysql.jdbc.ResultSetInternalMethods) rs).setWrapperStatement(this);
432
throw SQLError.createSQLException("Statement already closed",
433
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
434
} catch (SQLException sqlEx) {
435
checkAndFireConnectionError(sqlEx);
444
* @see java.sql.Statement#getResultSetConcurrency()
446
public int getResultSetConcurrency() throws SQLException {
448
if (this.wrappedStmt != null) {
449
return this.wrappedStmt.getResultSetConcurrency();
452
throw SQLError.createSQLException("Statement already closed",
453
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
454
} catch (SQLException sqlEx) {
455
checkAndFireConnectionError(sqlEx);
464
* @see java.sql.Statement#getResultSetHoldability()
466
public int getResultSetHoldability() throws SQLException {
468
if (this.wrappedStmt != null) {
469
return this.wrappedStmt.getResultSetHoldability();
472
throw SQLError.createSQLException("Statement already closed",
473
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
474
} catch (SQLException sqlEx) {
475
checkAndFireConnectionError(sqlEx);
478
return Statement.CLOSE_CURRENT_RESULT;
484
* @see java.sql.Statement#getResultSetType()
486
public int getResultSetType() throws SQLException {
488
if (this.wrappedStmt != null) {
489
return this.wrappedStmt.getResultSetType();
492
throw SQLError.createSQLException("Statement already closed",
493
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
494
} catch (SQLException sqlEx) {
495
checkAndFireConnectionError(sqlEx);
498
return ResultSet.TYPE_FORWARD_ONLY;
504
* @see java.sql.Statement#getUpdateCount()
506
public int getUpdateCount() throws SQLException {
508
if (this.wrappedStmt != null) {
509
return this.wrappedStmt.getUpdateCount();
512
throw SQLError.createSQLException("Statement already closed",
513
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
514
} catch (SQLException sqlEx) {
515
checkAndFireConnectionError(sqlEx);
524
* @see java.sql.Statement#getWarnings()
526
public SQLWarning getWarnings() throws SQLException {
528
if (this.wrappedStmt != null) {
529
return this.wrappedStmt.getWarnings();
532
throw SQLError.createSQLException("Statement already closed",
533
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
534
} catch (SQLException sqlEx) {
535
checkAndFireConnectionError(sqlEx);
544
* @see java.sql.Statement#addBatch(java.lang.String)
546
public void addBatch(String sql) throws SQLException {
548
if (this.wrappedStmt != null) {
549
this.wrappedStmt.addBatch(sql);
551
} catch (SQLException sqlEx) {
552
checkAndFireConnectionError(sqlEx);
559
* @see java.sql.Statement#cancel()
561
public void cancel() throws SQLException {
563
if (this.wrappedStmt != null) {
564
this.wrappedStmt.cancel();
566
} catch (SQLException sqlEx) {
567
checkAndFireConnectionError(sqlEx);
574
* @see java.sql.Statement#clearBatch()
576
public void clearBatch() throws SQLException {
578
if (this.wrappedStmt != null) {
579
this.wrappedStmt.clearBatch();
581
} catch (SQLException sqlEx) {
582
checkAndFireConnectionError(sqlEx);
589
* @see java.sql.Statement#clearWarnings()
591
public void clearWarnings() throws SQLException {
593
if (this.wrappedStmt != null) {
594
this.wrappedStmt.clearWarnings();
596
} catch (SQLException sqlEx) {
597
checkAndFireConnectionError(sqlEx);
604
* @see java.sql.Statement#close()
606
public void close() throws SQLException {
608
if (this.wrappedStmt != null) {
609
this.wrappedStmt.close();
611
} catch (SQLException sqlEx) {
612
checkAndFireConnectionError(sqlEx);
614
this.wrappedStmt = null;
615
this.pooledConnection = null;
622
* @see java.sql.Statement#execute(java.lang.String, int)
624
public boolean execute(String sql, int autoGeneratedKeys)
625
throws SQLException {
627
if (this.wrappedStmt != null) {
628
return this.wrappedStmt.execute(sql, autoGeneratedKeys);
631
throw SQLError.createSQLException("Statement already closed",
632
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
633
} catch (SQLException sqlEx) {
634
checkAndFireConnectionError(sqlEx);
637
return false; // we actually never get here, but the compiler can't
646
* @see java.sql.Statement#execute(java.lang.String, int[])
648
public boolean execute(String sql, int[] columnIndexes) throws SQLException {
650
if (this.wrappedStmt != null) {
651
return this.wrappedStmt.execute(sql, columnIndexes);
654
throw SQLError.createSQLException("Statement already closed",
655
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
656
} catch (SQLException sqlEx) {
657
checkAndFireConnectionError(sqlEx);
660
return false; // we actually never get here, but the compiler can't
669
* @see java.sql.Statement#execute(java.lang.String, java.lang.String[])
671
public boolean execute(String sql, String[] columnNames)
672
throws SQLException {
674
if (this.wrappedStmt != null) {
675
return this.wrappedStmt.execute(sql, columnNames);
678
throw SQLError.createSQLException("Statement already closed",
679
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
680
} catch (SQLException sqlEx) {
681
checkAndFireConnectionError(sqlEx);
684
return false; // we actually never get here, but the compiler can't
693
* @see java.sql.Statement#execute(java.lang.String)
695
public boolean execute(String sql) throws SQLException {
697
if (this.wrappedStmt != null) {
698
return this.wrappedStmt.execute(sql);
701
throw SQLError.createSQLException("Statement already closed",
702
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
703
} catch (SQLException sqlEx) {
704
checkAndFireConnectionError(sqlEx);
707
return false; // we actually never get here, but the compiler can't
716
* @see java.sql.Statement#executeBatch()
718
public int[] executeBatch() throws SQLException {
720
if (this.wrappedStmt != null) {
721
return this.wrappedStmt.executeBatch();
724
throw SQLError.createSQLException("Statement already closed",
725
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
726
} catch (SQLException sqlEx) {
727
checkAndFireConnectionError(sqlEx);
730
return null; // we actually never get here, but the compiler can't
739
* @see java.sql.Statement#executeQuery(java.lang.String)
741
public ResultSet executeQuery(String sql) throws SQLException {
743
if (this.wrappedStmt != null) {
745
ResultSet rs = this.wrappedStmt.executeQuery(sql);
746
((com.mysql.jdbc.ResultSetInternalMethods) rs).setWrapperStatement(this);
751
throw SQLError.createSQLException("Statement already closed",
752
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
753
} catch (SQLException sqlEx) {
754
checkAndFireConnectionError(sqlEx);
757
return null; // we actually never get here, but the compiler can't
766
* @see java.sql.Statement#executeUpdate(java.lang.String, int)
768
public int executeUpdate(String sql, int autoGeneratedKeys)
769
throws SQLException {
771
if (this.wrappedStmt != null) {
772
return this.wrappedStmt.executeUpdate(sql, autoGeneratedKeys);
775
throw SQLError.createSQLException("Statement already closed",
776
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
777
} catch (SQLException sqlEx) {
778
checkAndFireConnectionError(sqlEx);
781
return -1; // we actually never get here, but the compiler can't figure
789
* @see java.sql.Statement#executeUpdate(java.lang.String, int[])
791
public int executeUpdate(String sql, int[] columnIndexes)
792
throws SQLException {
794
if (this.wrappedStmt != null) {
795
return this.wrappedStmt.executeUpdate(sql, columnIndexes);
798
throw SQLError.createSQLException("Statement already closed",
799
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
800
} catch (SQLException sqlEx) {
801
checkAndFireConnectionError(sqlEx);
804
return -1; // we actually never get here, but the compiler can't figure
812
* @see java.sql.Statement#executeUpdate(java.lang.String,
813
* java.lang.String[])
815
public int executeUpdate(String sql, String[] columnNames)
816
throws SQLException {
818
if (this.wrappedStmt != null) {
819
return this.wrappedStmt.executeUpdate(sql, columnNames);
822
throw SQLError.createSQLException("Statement already closed",
823
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
824
} catch (SQLException sqlEx) {
825
checkAndFireConnectionError(sqlEx);
828
return -1; // we actually never get here, but the compiler can't figure
836
* @see java.sql.Statement#executeUpdate(java.lang.String)
838
public int executeUpdate(String sql) throws SQLException {
840
if (this.wrappedStmt != null) {
841
return this.wrappedStmt.executeUpdate(sql);
844
throw SQLError.createSQLException("Statement already closed",
845
SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
846
} catch (SQLException sqlEx) {
847
checkAndFireConnectionError(sqlEx);
850
return -1; // we actually never get here, but the compiler can't figure
855
public void enableStreamingResults() throws SQLException {
857
if (this.wrappedStmt != null) {
858
((com.mysql.jdbc.Statement) this.wrappedStmt)
859
.enableStreamingResults();
861
throw SQLError.createSQLException(
862
"No operations allowed after statement closed",
863
SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
865
} catch (SQLException sqlEx) {
866
checkAndFireConnectionError(sqlEx);