source: Ballon/out/artifacts/geisa_artifact/WEB-INF/lib/mysql-connector-java-5.1.21/src/com/mysql/jdbc/jdbc2/optional/JDBC4PreparedStatementWrapper.java

Last change on this file was 766, checked in by npipsl, 11 years ago
File size: 17.4 KB
Line 
1/*
2 Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4
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>.
10
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
13  of the License.
14
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.
18
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
22
23
24
25 */
26package com.mysql.jdbc.jdbc2.optional;
27
28import java.io.InputStream;
29import java.io.Reader;
30import java.lang.reflect.InvocationHandler;
31import java.lang.reflect.InvocationTargetException;
32import java.lang.reflect.Method;
33import java.lang.reflect.Proxy;
34import java.sql.Array;
35import java.sql.Blob;
36import java.sql.Clob;
37import java.sql.Connection;
38import java.sql.NClob;
39import java.sql.PreparedStatement;
40import java.sql.RowId;
41import java.sql.SQLClientInfoException;
42import java.sql.SQLException;
43import java.sql.SQLXML;
44import java.sql.Statement;
45import java.util.HashMap;
46import java.util.List;
47import java.util.Map;
48import java.util.Properties;
49
50import javax.sql.StatementEvent;
51
52import com.mysql.jdbc.ConnectionImpl;
53import com.mysql.jdbc.SQLError;
54import com.mysql.jdbc.jdbc2.optional.ConnectionWrapper;
55import com.mysql.jdbc.jdbc2.optional.MysqlPooledConnection;
56
57/**
58 */
59public class JDBC4PreparedStatementWrapper extends PreparedStatementWrapper {
60
61        public JDBC4PreparedStatementWrapper(ConnectionWrapper c, MysqlPooledConnection conn,
62                        PreparedStatement toWrap) {
63                super(c, conn, toWrap);
64        }
65       
66        public synchronized void close() throws SQLException {
67                if (this.pooledConnection == null) {
68                        // no-op
69                        return;
70                }
71               
72                MysqlPooledConnection con = this.pooledConnection; // we need this
73                                                                                                                   // later...
74
75                try {
76                        super.close();
77                } finally {
78                        try {
79                                StatementEvent e = new StatementEvent(con, this);
80                                // todo: pull this all up into base classes when we support *only* JDK6 or newer
81                                if (con instanceof JDBC4MysqlPooledConnection) {
82                                        ((JDBC4MysqlPooledConnection) con).fireStatementEvent(e);
83                                } else if (con instanceof JDBC4MysqlXAConnection) {
84                                        ((JDBC4MysqlXAConnection) con).fireStatementEvent(e);
85                                } else if (con instanceof JDBC4SuspendableXAConnection) {
86                                        ((JDBC4SuspendableXAConnection) con).fireStatementEvent(e);
87                                }
88                        } finally {
89                                this.unwrappedInterfaces = null;
90                        }
91                }
92        }
93       
94        public boolean isClosed() throws SQLException {
95                try {
96                        if (this.wrappedStmt != null) {
97                                return this.wrappedStmt.isClosed();
98                        } else {
99                                throw SQLError.createSQLException("Statement already closed",
100                                                SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
101                        }
102                } catch (SQLException sqlEx) {
103                        checkAndFireConnectionError(sqlEx);
104                }
105               
106                return false; // never get here - compiler can't tell
107        }
108       
109        public void setPoolable(boolean poolable) throws SQLException {
110                try {
111                        if (this.wrappedStmt != null) {
112                                this.wrappedStmt.setPoolable(poolable);
113                        } else {
114                                throw SQLError.createSQLException("Statement already closed",
115                                                SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
116                        }
117                } catch (SQLException sqlEx) {
118                        checkAndFireConnectionError(sqlEx);
119                }
120        }
121       
122        public boolean isPoolable() throws SQLException {
123                try {
124                        if (this.wrappedStmt != null) {
125                                return this.wrappedStmt.isPoolable();
126                        } else {
127                                throw SQLError.createSQLException("Statement already closed",
128                                                SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
129                        }
130                } catch (SQLException sqlEx) {
131                        checkAndFireConnectionError(sqlEx);
132                }
133               
134                return false; // never get here - compiler can't tell
135        }
136   
137        public void setRowId(int parameterIndex, RowId x) throws SQLException {
138                try {
139                        if (this.wrappedStmt != null) {
140                                ((PreparedStatement) this.wrappedStmt).setRowId(parameterIndex,
141                                                x);
142                        } else {
143                                throw SQLError.createSQLException(
144                                                "No operations allowed after statement closed",
145                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
146                        }
147                } catch (SQLException sqlEx) {
148                        checkAndFireConnectionError(sqlEx);
149                }
150        }
151       
152        public void setNClob(int parameterIndex, NClob value) throws SQLException {
153                try {
154                        if (this.wrappedStmt != null) {
155                                ((PreparedStatement) this.wrappedStmt).setNClob(parameterIndex,
156                                                value);
157                        } else {
158                                throw SQLError.createSQLException(
159                                                "No operations allowed after statement closed",
160                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
161                        }
162                } catch (SQLException sqlEx) {
163                        checkAndFireConnectionError(sqlEx);
164                }
165        }
166
167        public void setSQLXML(int parameterIndex, SQLXML xmlObject)
168                        throws SQLException {
169                try {
170                        if (this.wrappedStmt != null) {
171                                ((PreparedStatement) this.wrappedStmt).setSQLXML(parameterIndex,
172                                                xmlObject);
173                        } else {
174                                throw SQLError.createSQLException(
175                                                "No operations allowed after statement closed",
176                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
177                        }
178                } catch (SQLException sqlEx) {
179                        checkAndFireConnectionError(sqlEx);
180                }
181        }
182       
183       
184        public void setNString(int parameterIndex,
185            String value)
186            throws SQLException {
187                try {
188                        if (this.wrappedStmt != null) {
189                                ((PreparedStatement) this.wrappedStmt).setNString(parameterIndex,
190                                                value);
191                        } else {
192                                throw SQLError.createSQLException(
193                                                "No operations allowed after statement closed",
194                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
195                        }
196                } catch (SQLException sqlEx) {
197                        checkAndFireConnectionError(sqlEx);
198                }
199        }
200           
201    public void setNCharacterStream(int parameterIndex,
202                    Reader value,
203                    long length)
204                    throws SQLException {
205        try {
206                        if (this.wrappedStmt != null) {
207                                ((PreparedStatement) this.wrappedStmt).setNCharacterStream(parameterIndex,
208                                                value, length);
209                        } else {
210                                throw SQLError.createSQLException(
211                                                "No operations allowed after statement closed",
212                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
213                        }
214                } catch (SQLException sqlEx) {
215                        checkAndFireConnectionError(sqlEx);
216                }
217    }
218
219    public void setClob(int parameterIndex,
220            Reader reader,
221            long length)
222            throws SQLException {
223        try {
224                        if (this.wrappedStmt != null) {
225                                ((PreparedStatement) this.wrappedStmt).setClob(parameterIndex,
226                                                reader, length);
227                        } else {
228                                throw SQLError.createSQLException(
229                                                "No operations allowed after statement closed",
230                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
231                        }
232                } catch (SQLException sqlEx) {
233                        checkAndFireConnectionError(sqlEx);
234                }       
235    }
236   
237    public void setBlob(int parameterIndex,
238            InputStream inputStream,
239            long length)
240            throws SQLException {
241        try {
242                        if (this.wrappedStmt != null) {
243                                ((PreparedStatement) this.wrappedStmt).setBlob(parameterIndex,
244                                                inputStream, length);
245                        } else {
246                                throw SQLError.createSQLException(
247                                                "No operations allowed after statement closed",
248                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
249                        }
250                } catch (SQLException sqlEx) {
251                        checkAndFireConnectionError(sqlEx);
252                }
253    }
254   
255    public void setNClob(int parameterIndex,
256            Reader reader,
257            long length)
258            throws SQLException {
259        try {
260                        if (this.wrappedStmt != null) {
261                                ((PreparedStatement) this.wrappedStmt).setNClob(parameterIndex,
262                                                reader, length);
263                        } else {
264                                throw SQLError.createSQLException(
265                                                "No operations allowed after statement closed",
266                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
267                        }
268                } catch (SQLException sqlEx) {
269                        checkAndFireConnectionError(sqlEx);
270                }
271    }
272   
273    public void setAsciiStream(int parameterIndex,
274            InputStream x,
275            long length)
276            throws SQLException {
277        try {
278                        if (this.wrappedStmt != null) {
279                                ((PreparedStatement) this.wrappedStmt).setAsciiStream(parameterIndex,
280                                                x, length);
281                        } else {
282                                throw SQLError.createSQLException(
283                                                "No operations allowed after statement closed",
284                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
285                        }
286                } catch (SQLException sqlEx) {
287                        checkAndFireConnectionError(sqlEx);
288                }
289    }
290   
291    public void setBinaryStream(int parameterIndex,
292            InputStream x,
293            long length)
294            throws SQLException {
295        try {
296                        if (this.wrappedStmt != null) {
297                                ((PreparedStatement) this.wrappedStmt).setBinaryStream(parameterIndex,
298                                                x, length);
299                        } else {
300                                throw SQLError.createSQLException(
301                                                "No operations allowed after statement closed",
302                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
303                        }
304                } catch (SQLException sqlEx) {
305                        checkAndFireConnectionError(sqlEx);
306                }
307    }
308   
309    public void setCharacterStream(int parameterIndex,
310            Reader reader,
311            long length)
312            throws SQLException {
313        try {
314                        if (this.wrappedStmt != null) {
315                                ((PreparedStatement) this.wrappedStmt).setCharacterStream(parameterIndex,
316                                                reader, length);
317                        } else {
318                                throw SQLError.createSQLException(
319                                                "No operations allowed after statement closed",
320                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
321                        }
322                } catch (SQLException sqlEx) {
323                        checkAndFireConnectionError(sqlEx);
324                }
325    }
326   
327    public void setAsciiStream(int parameterIndex,
328            InputStream x)
329            throws SQLException {
330        try {
331                        if (this.wrappedStmt != null) {
332                                ((PreparedStatement) this.wrappedStmt).setAsciiStream(parameterIndex,
333                                                x);
334                        } else {
335                                throw SQLError.createSQLException(
336                                                "No operations allowed after statement closed",
337                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
338                        }
339                } catch (SQLException sqlEx) {
340                        checkAndFireConnectionError(sqlEx);
341                }
342    }
343   
344    public void setBinaryStream(int parameterIndex,
345            InputStream x)
346            throws SQLException {
347        try {
348                        if (this.wrappedStmt != null) {
349                                ((PreparedStatement) this.wrappedStmt).setBinaryStream(parameterIndex,
350                                                x);
351                        } else {
352                                throw SQLError.createSQLException(
353                                                "No operations allowed after statement closed",
354                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
355                        }
356                } catch (SQLException sqlEx) {
357                        checkAndFireConnectionError(sqlEx);
358                }
359    }
360   
361    public void setCharacterStream(int parameterIndex,
362            Reader reader)
363            throws SQLException {
364        try {
365                        if (this.wrappedStmt != null) {
366                                ((PreparedStatement) this.wrappedStmt).setCharacterStream(parameterIndex,
367                                                reader);
368                        } else {
369                                throw SQLError.createSQLException(
370                                                "No operations allowed after statement closed",
371                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
372                        }
373                } catch (SQLException sqlEx) {
374                        checkAndFireConnectionError(sqlEx);
375                }
376       
377    }
378   
379    public void setNCharacterStream(int parameterIndex,
380            Reader value)
381            throws SQLException {
382        try {
383                        if (this.wrappedStmt != null) {
384                                ((PreparedStatement) this.wrappedStmt).setNCharacterStream(parameterIndex,
385                                                value);
386                        } else {
387                                throw SQLError.createSQLException(
388                                                "No operations allowed after statement closed",
389                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
390                        }
391                } catch (SQLException sqlEx) {
392                        checkAndFireConnectionError(sqlEx);
393                }
394       
395    }
396   
397    public void setClob(int parameterIndex,
398            Reader reader)
399            throws SQLException {
400        try {
401                        if (this.wrappedStmt != null) {
402                                ((PreparedStatement) this.wrappedStmt).setClob(parameterIndex,
403                                                reader);
404                        } else {
405                                throw SQLError.createSQLException(
406                                                "No operations allowed after statement closed",
407                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
408                        }
409                } catch (SQLException sqlEx) {
410                        checkAndFireConnectionError(sqlEx);
411                }
412       
413    }
414   
415    public void setBlob(int parameterIndex,
416            InputStream inputStream)
417            throws SQLException {
418        try {
419                        if (this.wrappedStmt != null) {
420                                ((PreparedStatement) this.wrappedStmt).setBlob(parameterIndex,
421                                                inputStream);
422                        } else {
423                                throw SQLError.createSQLException(
424                                                "No operations allowed after statement closed",
425                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
426                        }
427                } catch (SQLException sqlEx) {
428                        checkAndFireConnectionError(sqlEx);
429                }
430    }
431   
432    public void setNClob(int parameterIndex,
433            Reader reader)
434            throws SQLException {
435        try {
436                        if (this.wrappedStmt != null) {
437                                ((PreparedStatement) this.wrappedStmt).setNClob(parameterIndex,
438                                                reader);
439                        } else {
440                                throw SQLError.createSQLException(
441                                                "No operations allowed after statement closed",
442                                                SQLError.SQL_STATE_GENERAL_ERROR, this.exceptionInterceptor);
443                        }
444                } catch (SQLException sqlEx) {
445                        checkAndFireConnectionError(sqlEx);
446                }
447    }
448       
449        /**
450         * Returns true if this either implements the interface argument or is
451         * directly or indirectly a wrapper for an object that does. Returns false
452         * otherwise. If this implements the interface then return true, else if
453         * this is a wrapper then return the result of recursively calling
454         * <code>isWrapperFor</code> on the wrapped object. If this does not
455         * implement the interface and is not a wrapper, return false. This method
456         * should be implemented as a low-cost operation compared to
457         * <code>unwrap</code> so that callers can use this method to avoid
458         * expensive <code>unwrap</code> calls that may fail. If this method
459         * returns true then calling <code>unwrap</code> with the same argument
460         * should succeed.
461         *
462         * @param interfaces
463         *            a Class defining an interface.
464         * @return true if this implements the interface or directly or indirectly
465         *         wraps an object that does.
466         * @throws java.sql.SQLException
467         *             if an error occurs while determining whether this is a
468         *             wrapper for an object with the given interface.
469         * @since 1.6
470         */
471        public boolean isWrapperFor(Class<?> iface) throws SQLException {
472
473                boolean isInstance = iface.isInstance(this);
474
475                if (isInstance) {
476                        return true;
477                }
478
479                String interfaceClassName = iface.getName();
480               
481                return (interfaceClassName.equals("com.mysql.jdbc.Statement")
482                                || interfaceClassName.equals("java.sql.Statement")
483                                || interfaceClassName.equals("java.sql.PreparedStatement")
484                                || interfaceClassName.equals("java.sql.Wrapper"));
485        }
486
487        /**
488         * Returns an object that implements the given interface to allow access to
489         * non-standard methods, or standard methods not exposed by the proxy. The
490         * result may be either the object found to implement the interface or a
491         * proxy for that object. If the receiver implements the interface then that
492         * is the object. If the receiver is a wrapper and the wrapped object
493         * implements the interface then that is the object. Otherwise the object is
494         * the result of calling <code>unwrap</code> recursively on the wrapped
495         * object. If the receiver is not a wrapper and does not implement the
496         * interface, then an <code>SQLException</code> is thrown.
497         *
498         * @param iface
499         *            A Class defining an interface that the result must implement.
500         * @return an object that implements the interface. May be a proxy for the
501         *         actual implementing object.
502         * @throws java.sql.SQLException
503         *             If no object found that implements the interface
504         * @since 1.6
505         */
506        public synchronized <T> T unwrap(java.lang.Class<T> iface)
507                        throws java.sql.SQLException {
508                try {
509                        if ("java.sql.Statement".equals(iface.getName()) 
510                                        || "java.sql.PreparedStatement".equals(iface.getName()) 
511                                        || "java.sql.Wrapper.class".equals(iface.getName())) {
512                                return iface.cast(this);
513                        }
514                       
515                        if (unwrappedInterfaces == null) {
516                                unwrappedInterfaces = new HashMap();
517                        }
518                       
519                        Object cachedUnwrapped = unwrappedInterfaces.get(iface);
520                       
521                        if (cachedUnwrapped == null) {
522                                if (cachedUnwrapped == null) {
523                                        cachedUnwrapped = Proxy.newProxyInstance(
524                                                        this.wrappedStmt.getClass().getClassLoader(), 
525                                                        new Class[] { iface },
526                                                        new ConnectionErrorFiringInvocationHandler(this.wrappedStmt));
527                                        unwrappedInterfaces.put(iface, cachedUnwrapped);
528                                }
529                                unwrappedInterfaces.put(iface, cachedUnwrapped);
530                        }
531                       
532                        return iface.cast(cachedUnwrapped);
533                } catch (ClassCastException cce) {
534                        throw SQLError.createSQLException("Unable to unwrap to "
535                                        + iface.toString(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
536                }
537        }
538}
Note: See TracBrowser for help on using the repository browser.