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 | */ |
---|
26 | package com.mysql.jdbc.jdbc2.optional; |
---|
27 | |
---|
28 | import java.lang.reflect.InvocationHandler; |
---|
29 | import java.lang.reflect.InvocationTargetException; |
---|
30 | import java.lang.reflect.Method; |
---|
31 | import java.lang.reflect.Proxy; |
---|
32 | import java.sql.Array; |
---|
33 | import java.sql.Blob; |
---|
34 | import java.sql.Clob; |
---|
35 | import java.sql.Connection; |
---|
36 | import java.sql.NClob; |
---|
37 | import java.sql.SQLClientInfoException; |
---|
38 | import java.sql.SQLException; |
---|
39 | import java.sql.SQLXML; |
---|
40 | import java.sql.Savepoint; |
---|
41 | import java.sql.Statement; |
---|
42 | import java.sql.Struct; |
---|
43 | import java.util.ArrayList; |
---|
44 | import java.util.HashMap; |
---|
45 | import java.util.List; |
---|
46 | import java.util.Map; |
---|
47 | import java.util.Properties; |
---|
48 | |
---|
49 | import com.mysql.jdbc.ConnectionImpl; |
---|
50 | import com.mysql.jdbc.SQLError; |
---|
51 | import com.mysql.jdbc.jdbc2.optional.ConnectionWrapper; |
---|
52 | import com.mysql.jdbc.jdbc2.optional.MysqlPooledConnection; |
---|
53 | |
---|
54 | /** |
---|
55 | */ |
---|
56 | public class JDBC4StatementWrapper extends StatementWrapper { |
---|
57 | |
---|
58 | public JDBC4StatementWrapper(ConnectionWrapper c, |
---|
59 | MysqlPooledConnection conn, |
---|
60 | Statement toWrap) { |
---|
61 | super(c, conn, toWrap); |
---|
62 | } |
---|
63 | |
---|
64 | public void close() throws SQLException { |
---|
65 | try { |
---|
66 | super.close(); |
---|
67 | } finally { |
---|
68 | this.unwrappedInterfaces = null; |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | public boolean isClosed() throws SQLException { |
---|
73 | try { |
---|
74 | if (this.wrappedStmt != null) { |
---|
75 | return this.wrappedStmt.isClosed(); |
---|
76 | } else { |
---|
77 | throw SQLError.createSQLException("Statement already closed", |
---|
78 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor); |
---|
79 | } |
---|
80 | } catch (SQLException sqlEx) { |
---|
81 | checkAndFireConnectionError(sqlEx); |
---|
82 | } |
---|
83 | |
---|
84 | return false; // We never get here, compiler can't tell |
---|
85 | } |
---|
86 | |
---|
87 | public void setPoolable(boolean poolable) throws SQLException { |
---|
88 | try { |
---|
89 | if (this.wrappedStmt != null) { |
---|
90 | this.wrappedStmt.setPoolable(poolable); |
---|
91 | } else { |
---|
92 | throw SQLError.createSQLException("Statement already closed", |
---|
93 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor); |
---|
94 | } |
---|
95 | } catch (SQLException sqlEx) { |
---|
96 | checkAndFireConnectionError(sqlEx); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | public boolean isPoolable() throws SQLException { |
---|
101 | try { |
---|
102 | if (this.wrappedStmt != null) { |
---|
103 | return this.wrappedStmt.isPoolable(); |
---|
104 | } else { |
---|
105 | throw SQLError.createSQLException("Statement already closed", |
---|
106 | SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor); |
---|
107 | } |
---|
108 | } catch (SQLException sqlEx) { |
---|
109 | checkAndFireConnectionError(sqlEx); |
---|
110 | } |
---|
111 | |
---|
112 | return false; // We never get here, compiler can't tell |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * Returns true if this either implements the interface argument or is |
---|
117 | * directly or indirectly a wrapper for an object that does. Returns false |
---|
118 | * otherwise. If this implements the interface then return true, else if |
---|
119 | * this is a wrapper then return the result of recursively calling |
---|
120 | * <code>isWrapperFor</code> on the wrapped object. If this does not |
---|
121 | * implement the interface and is not a wrapper, return false. This method |
---|
122 | * should be implemented as a low-cost operation compared to |
---|
123 | * <code>unwrap</code> so that callers can use this method to avoid |
---|
124 | * expensive <code>unwrap</code> calls that may fail. If this method |
---|
125 | * returns true then calling <code>unwrap</code> with the same argument |
---|
126 | * should succeed. |
---|
127 | * |
---|
128 | * @param interfaces |
---|
129 | * a Class defining an interface. |
---|
130 | * @return true if this implements the interface or directly or indirectly |
---|
131 | * wraps an object that does. |
---|
132 | * @throws java.sql.SQLException |
---|
133 | * if an error occurs while determining whether this is a |
---|
134 | * wrapper for an object with the given interface. |
---|
135 | * @since 1.6 |
---|
136 | */ |
---|
137 | public boolean isWrapperFor(Class<?> iface) throws SQLException { |
---|
138 | |
---|
139 | boolean isInstance = iface.isInstance(this); |
---|
140 | |
---|
141 | if (isInstance) { |
---|
142 | return true; |
---|
143 | } |
---|
144 | |
---|
145 | String interfaceClassName = iface.getName(); |
---|
146 | |
---|
147 | return (interfaceClassName.equals("com.mysql.jdbc.Statement") |
---|
148 | || interfaceClassName.equals("java.sql.Statement") |
---|
149 | || interfaceClassName.equals("java.sql.Wrapper")); |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * Returns an object that implements the given interface to allow access to |
---|
154 | * non-standard methods, or standard methods not exposed by the proxy. The |
---|
155 | * result may be either the object found to implement the interface or a |
---|
156 | * proxy for that object. If the receiver implements the interface then that |
---|
157 | * is the object. If the receiver is a wrapper and the wrapped object |
---|
158 | * implements the interface then that is the object. Otherwise the object is |
---|
159 | * the result of calling <code>unwrap</code> recursively on the wrapped |
---|
160 | * object. If the receiver is not a wrapper and does not implement the |
---|
161 | * interface, then an <code>SQLException</code> is thrown. |
---|
162 | * |
---|
163 | * @param iface |
---|
164 | * A Class defining an interface that the result must implement. |
---|
165 | * @return an object that implements the interface. May be a proxy for the |
---|
166 | * actual implementing object. |
---|
167 | * @throws java.sql.SQLException |
---|
168 | * If no object found that implements the interface |
---|
169 | * @since 1.6 |
---|
170 | */ |
---|
171 | public synchronized <T> T unwrap(java.lang.Class<T> iface) |
---|
172 | throws java.sql.SQLException { |
---|
173 | try { |
---|
174 | if ("java.sql.Statement".equals(iface.getName()) |
---|
175 | || "java.sql.Wrapper.class".equals(iface.getName())) { |
---|
176 | return iface.cast(this); |
---|
177 | } |
---|
178 | |
---|
179 | if (unwrappedInterfaces == null) { |
---|
180 | unwrappedInterfaces = new HashMap(); |
---|
181 | } |
---|
182 | |
---|
183 | Object cachedUnwrapped = unwrappedInterfaces.get(iface); |
---|
184 | |
---|
185 | if (cachedUnwrapped == null) { |
---|
186 | cachedUnwrapped = Proxy.newProxyInstance( |
---|
187 | this.wrappedStmt.getClass().getClassLoader(), |
---|
188 | new Class[] { iface }, |
---|
189 | new ConnectionErrorFiringInvocationHandler(this.wrappedStmt)); |
---|
190 | unwrappedInterfaces.put(iface, cachedUnwrapped); |
---|
191 | } |
---|
192 | |
---|
193 | return iface.cast(cachedUnwrapped); |
---|
194 | } catch (ClassCastException cce) { |
---|
195 | throw SQLError.createSQLException("Unable to unwrap to " |
---|
196 | + iface.toString(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor); |
---|
197 | } |
---|
198 | } |
---|
199 | } |
---|