1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.jdbc.BrokeredPreparedStatement |
4 | |
5 | Copyright 2003, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.jdbc; |
22 | |
23 | import java.io.InputStream; |
24 | import java.io.Reader; |
25 | import java.util.Calendar; |
26 | |
27 | import java.sql.*; |
28 | import java.net.URL; |
29 | |
30 | /** |
31 | JDBC 2 brokered PreparedStatement. Forwards calls off to a real prepared statement |
32 | obtained through the BrokeredStatementControl getRealPreparedStatement method. |
33 | */ |
34 | public class BrokeredPreparedStatement extends BrokeredStatement |
35 | implements PreparedStatement |
36 | { |
37 | |
38 | /** |
39 | SQL used to create me. |
40 | */ |
41 | final String sql; |
42 | |
43 | public BrokeredPreparedStatement(BrokeredStatementControl control, int jdbcLevel, String sql) throws SQLException |
44 | { |
45 | super(control, jdbcLevel); |
46 | this.sql = sql; |
47 | } |
48 | |
49 | /** |
50 | * A prepared SQL query is executed and its ResultSet is returned. |
51 | * |
52 | * @return a ResultSet that contains the data produced by the |
53 | * query; never null |
54 | * @exception SQLException thrown on failure. |
55 | */ |
56 | public final ResultSet executeQuery() throws SQLException |
57 | { |
58 | return wrapResultSet(getPreparedStatement().executeQuery()); |
59 | } |
60 | |
61 | /** |
62 | * Execute a SQL INSERT, UPDATE or DELETE statement. In addition, |
63 | * SQL statements that return nothing such as SQL DDL statements |
64 | * can be executed. |
65 | * |
66 | * @return either the row count for INSERT, UPDATE or DELETE; or 0 |
67 | * for SQL statements that return nothing |
68 | * @exception SQLException thrown on failure. |
69 | */ |
70 | public final int executeUpdate() throws SQLException |
71 | { |
72 | return getPreparedStatement().executeUpdate(); |
73 | } |
74 | |
75 | /** |
76 | * Set a parameter to SQL NULL. |
77 | * |
78 | * <P><B>Note:</B> You must specify the parameter's SQL type. |
79 | * |
80 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
81 | * @param sqlType SQL type code defined by java.sql.Types |
82 | * @exception SQLException thrown on failure. |
83 | */ |
84 | public final void setNull(int parameterIndex, int sqlType) throws SQLException |
85 | { |
86 | getPreparedStatement().setNull( parameterIndex, sqlType); |
87 | } |
88 | |
89 | /** |
90 | * Set a parameter to SQL NULL. |
91 | * |
92 | * <P><B>Note:</B> You must specify the parameter's SQL type. |
93 | * |
94 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
95 | * @param sqlType SQL type code defined by java.sql.Types |
96 | * @exception SQLException thrown on failure. |
97 | */ |
98 | public final void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException |
99 | { |
100 | getPreparedStatement().setNull( parameterIndex, sqlType, typeName); |
101 | } |
102 | |
103 | /** |
104 | * Set a parameter to a Java boolean value. According to the JDBC API spec, |
105 | * the driver converts this to a SQL BIT value when it sends it to the |
106 | * database. But we don't have to do this, since the database engine |
107 | * supports a boolean type. |
108 | * |
109 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
110 | * @param x the parameter value |
111 | * @exception SQLException thrown on failure. |
112 | */ |
113 | public final void setBoolean(int parameterIndex, boolean x) throws SQLException |
114 | { |
115 | getPreparedStatement().setBoolean( parameterIndex, x); |
116 | } |
117 | |
118 | /** |
119 | * Set a parameter to a Java byte value. The driver converts this |
120 | * to a SQL TINYINT value when it sends it to the database. |
121 | * |
122 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
123 | * @param x the parameter value |
124 | * @exception SQLException thrown on failure. |
125 | */ |
126 | public final void setByte(int parameterIndex, byte x) throws SQLException |
127 | { |
128 | getPreparedStatement().setByte( parameterIndex, x); |
129 | } |
130 | |
131 | /** |
132 | * Set a parameter to a Java short value. The driver converts this |
133 | * to a SQL SMALLINT value when it sends it to the database. |
134 | * |
135 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
136 | * @param x the parameter value |
137 | * @exception SQLException thrown on failure. |
138 | */ |
139 | public final void setShort(int parameterIndex, short x) throws SQLException |
140 | { |
141 | getPreparedStatement().setShort( parameterIndex, x); |
142 | } |
143 | |
144 | /** |
145 | * Set a parameter to a Java int value. The driver converts this |
146 | * to a SQL INTEGER value when it sends it to the database. |
147 | * |
148 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
149 | * @param x the parameter value |
150 | * @exception SQLException thrown on failure. |
151 | */ |
152 | public final void setInt(int parameterIndex, int x) throws SQLException |
153 | { |
154 | getPreparedStatement().setInt( parameterIndex, x); |
155 | } |
156 | |
157 | /** |
158 | * Set a parameter to a Java long value. The driver converts this |
159 | * to a SQL BIGINT value when it sends it to the database. |
160 | * |
161 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
162 | * @param x the parameter value |
163 | * @exception SQLException thrown on failure. |
164 | */ |
165 | public final void setLong(int parameterIndex, long x) throws SQLException |
166 | { |
167 | getPreparedStatement().setLong( parameterIndex, x); |
168 | } |
169 | |
170 | /** |
171 | * Set a parameter to a Java float value. The driver converts this |
172 | * to a SQL FLOAT value when it sends it to the database. |
173 | * |
174 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
175 | * @param x the parameter value |
176 | * @exception SQLException thrown on failure. |
177 | */ |
178 | public final void setFloat(int parameterIndex, float x) throws SQLException |
179 | { |
180 | getPreparedStatement().setFloat( parameterIndex, x); |
181 | } |
182 | |
183 | /** |
184 | * Set a parameter to a Java double value. The driver converts this |
185 | * to a SQL DOUBLE value when it sends it to the database. |
186 | * |
187 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
188 | * @param x the parameter value |
189 | * @exception SQLException thrown on failure. |
190 | */ |
191 | public final void setDouble(int parameterIndex, double x) throws SQLException |
192 | { |
193 | getPreparedStatement().setDouble( parameterIndex, x); |
194 | } |
195 | |
196 | |
197 | /** |
198 | * Set a parameter to a java.math.BigDecimal value. |
199 | * The driver converts this to a SQL NUMERIC value when |
200 | * it sends it to the database. |
201 | * |
202 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
203 | * @param x the parameter value |
204 | * @exception SQLException thrown on failure. |
205 | */ |
206 | public final void setBigDecimal(int parameterIndex, java.math.BigDecimal x) throws SQLException |
207 | { |
208 | getPreparedStatement().setBigDecimal( parameterIndex, x); |
209 | } |
210 | |
211 | /** |
212 | * Set a parameter to a Java String value. The driver converts this |
213 | * to a SQL VARCHAR or LONGVARCHAR value (depending on the arguments |
214 | * size relative to the driver's limits on VARCHARs) when it sends |
215 | * it to the database. |
216 | * |
217 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
218 | * @param x the parameter value |
219 | * @exception SQLException thrown on failure. |
220 | */ |
221 | public final void setString(int parameterIndex, String x) throws SQLException |
222 | { |
223 | getPreparedStatement().setString( parameterIndex, x); |
224 | } |
225 | |
226 | /** |
227 | * Set a parameter to a Java array of bytes. The driver converts |
228 | * this to a SQL VARBINARY or LONGVARBINARY (depending on the |
229 | * argument's size relative to the driver's limits on VARBINARYs) |
230 | * when it sends it to the database. |
231 | * |
232 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
233 | * @param x the parameter value |
234 | * @exception SQLException thrown on failure. |
235 | */ |
236 | public final void setBytes(int parameterIndex, byte[] x) throws SQLException |
237 | { |
238 | getPreparedStatement().setBytes( parameterIndex, x); |
239 | } |
240 | |
241 | /** |
242 | * Set a parameter to a java.sql.Date value. The driver converts this |
243 | * to a SQL DATE value when it sends it to the database. |
244 | * |
245 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
246 | * @param x the parameter value |
247 | * @exception SQLException thrown on failure. |
248 | */ |
249 | public final void setDate(int parameterIndex, Date x) throws SQLException |
250 | { |
251 | getPreparedStatement().setDate( parameterIndex, x); |
252 | } |
253 | |
254 | /** |
255 | * Set a parameter to a java.sql.Time value. The driver converts this |
256 | * to a SQL TIME value when it sends it to the database. |
257 | * |
258 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
259 | * @param x the parameter value |
260 | * @exception SQLException thrown on failure. |
261 | */ |
262 | public final void setTime(int parameterIndex, Time x) throws SQLException |
263 | { |
264 | getPreparedStatement().setTime( parameterIndex, x); |
265 | } |
266 | |
267 | /** |
268 | * Set a parameter to a java.sql.Timestamp value. The driver |
269 | * converts this to a SQL TIMESTAMP value when it sends it to the |
270 | * database. |
271 | * |
272 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
273 | * @param x the parameter value |
274 | * @exception SQLException thrown on failure. |
275 | */ |
276 | public final void setTimestamp(int parameterIndex, Timestamp x) throws SQLException |
277 | { |
278 | getPreparedStatement().setTimestamp( parameterIndex, x); |
279 | } |
280 | |
281 | /** |
282 | * We do this inefficiently and read it all in here. The target type |
283 | * is assumed to be a String. |
284 | * |
285 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
286 | * @param x the java input stream which contains the ASCII parameter value |
287 | * @param length the number of bytes in the stream |
288 | * @exception SQLException thrown on failure. |
289 | */ |
290 | public final void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException |
291 | { |
292 | getPreparedStatement().setAsciiStream( parameterIndex, x, length); |
293 | } |
294 | |
295 | /** |
296 | * We do this inefficiently and read it all in here. The target type |
297 | * is assumed to be a String. The unicode source is assumed to be |
298 | * in char[]. RESOLVE: might it be in UTF, instead? that'd be faster! |
299 | * |
300 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
301 | * @param x the java input stream which contains the |
302 | * UNICODE parameter value |
303 | * @param length the number of bytes in the stream |
304 | * @exception SQLException thrown on failure. |
305 | */ |
306 | public final void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException |
307 | { |
308 | getPreparedStatement().setUnicodeStream( parameterIndex, x, length); |
309 | } |
310 | |
311 | /** |
312 | * @param parameterIndex the first parameter is 1, the second is 2, ... |
313 | * @param x the java input stream which contains the binary parameter value |
314 | * @param length the number of bytes in the stream |
315 | * @exception SQLException thrown on failure. |
316 | */ |
317 | public final void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException |
318 | { |
319 | getPreparedStatement().setBinaryStream( parameterIndex, x, length); |
320 | } |
321 | |
322 | /** |
323 | * JDBC 2.0 |
324 | * |
325 | * Add a set of parameters to the batch. |
326 | * |
327 | * @exception SQLException if a database-access error occurs. |
328 | */ |
329 | public final void addBatch() throws SQLException |
330 | { |
331 | getPreparedStatement().addBatch( ); |
332 | } |
333 | |
334 | /** |
335 | * <P>In general, parameter values remain in force for repeated use of a |
336 | * Statement. Setting a parameter value automatically clears its |
337 | * previous value. However, in some cases it is useful to immediately |
338 | * release the resources used by the current parameter values; this can |
339 | * be done by calling clearParameters. |
340 | * @exception SQLException thrown on failure. |
341 | */ |
342 | public final void clearParameters() throws SQLException |
343 | { |
344 | getPreparedStatement().clearParameters( ); |
345 | } |
346 | |
347 | /** |
348 | * JDBC 2.0 |
349 | * |
350 | * The number, types and properties of a ResultSet's columns |
351 | * are provided by the getMetaData method. |
352 | * |
353 | * @return the description of a ResultSet's columns |
354 | * @exception SQLException Feature not implemented for now. |
355 | */ |
356 | public final java.sql.ResultSetMetaData getMetaData() throws SQLException |
357 | { |
358 | return getPreparedStatement().getMetaData(); |
359 | } |
360 | |
361 | /** |
362 | * The interface says that the type of the Object parameter must |
363 | * be compatible with the type of the targetSqlType. We check that, |
364 | * and if it flies, we expect the underlying engine to do the |
365 | * required conversion once we pass in the value using its type. |
366 | * So, an Integer converting to a CHAR is done via setInteger() |
367 | * support on the underlying CHAR type. |
368 | * |
369 | * <p>If x is null, it won't tell us its type, so we pass it on to setNull |
370 | * |
371 | * @param parameterIndex The first parameter is 1, the second is 2, ... |
372 | * @param x The object containing the input parameter value |
373 | * @param targetSqlType The SQL type (as defined in java.sql.Types) to be |
374 | * sent to the database. The scale argument may further qualify this type. |
375 | * @param scale For java.sql.Types.DECIMAL or java.sql.Types.NUMERIC types |
376 | * this is the number of digits after the decimal. For all other |
377 | * types this value will be ignored, |
378 | * @exception SQLException thrown on failure. |
379 | */ |
380 | public final void setObject(int parameterIndex, Object x, int targetSqlType, int scale) |
381 | throws SQLException |
382 | { |
383 | getPreparedStatement().setObject( parameterIndex, x, targetSqlType, scale); |
384 | } |
385 | |
386 | /** |
387 | * This method is like setObject above, but assumes a scale of zero. |
388 | * @exception SQLException thrown on failure. |
389 | */ |
390 | public final void setObject(int parameterIndex, Object x, int targetSqlType) |
391 | throws SQLException |
392 | { |
393 | getPreparedStatement().setObject( parameterIndex, x, targetSqlType); |
394 | } |
395 | |
396 | /** |
397 | * <p>Set the value of a parameter using an object; use the |
398 | * java.lang equivalent objects for integral values. |
399 | * |
400 | * <p>The JDBC specification specifies a standard mapping from |
401 | * Java Object types to SQL types. The given argument java object |
402 | * will be converted to the corresponding SQL type before being |
403 | * sent to the database. |
404 | * |
405 | * <p>Note that this method may be used to pass datatabase |
406 | * specific abstract data types, by using a Driver specific Java |
407 | * type. |
408 | * |
409 | * @param parameterIndex The first parameter is 1, the second is 2, ... |
410 | * @param x The object containing the input parameter value |
411 | * @exception SQLException thrown on failure. |
412 | */ |
413 | public final void setObject(int parameterIndex, Object x) |
414 | throws SQLException |
415 | { |
416 | getPreparedStatement().setObject( parameterIndex, x); |
417 | } |
418 | |
419 | /** |
420 | * @see java.sql.Statement#execute |
421 | * @exception SQLException thrown on failure. |
422 | */ |
423 | public final boolean execute() throws SQLException |
424 | { |
425 | return getPreparedStatement().execute(); |
426 | } |
427 | |
428 | public final void setCharacterStream(int parameterIndex, |
429 | Reader reader, |
430 | int length) |
431 | throws SQLException |
432 | { |
433 | getPreparedStatement().setCharacterStream( parameterIndex, reader, length); |
434 | } |
435 | |
436 | public final void setRef(int i, |
437 | Ref x) |
438 | throws SQLException |
439 | { |
440 | getPreparedStatement().setRef( i, x); |
441 | } |
442 | |
443 | public final void setBlob(int i, |
444 | Blob x) |
445 | throws SQLException |
446 | { |
447 | getPreparedStatement().setBlob( i, x); |
448 | } |
449 | |
450 | public final void setClob(int i, |
451 | Clob x) |
452 | throws SQLException |
453 | { |
454 | getPreparedStatement().setClob( i, x); |
455 | } |
456 | |
457 | public final void setArray(int i, |
458 | Array x) |
459 | throws SQLException |
460 | { |
461 | getPreparedStatement().setArray( i, x); |
462 | } |
463 | |
464 | public final void setDate(int i, |
465 | Date x, |
466 | Calendar cal) |
467 | throws SQLException |
468 | { |
469 | getPreparedStatement().setDate( i, x, cal); |
470 | } |
471 | |
472 | public final void setTime(int i, |
473 | Time x, |
474 | Calendar cal) |
475 | throws SQLException |
476 | { |
477 | getPreparedStatement().setTime( i, x, cal); |
478 | } |
479 | |
480 | public final void setTimestamp(int i, |
481 | Timestamp x, |
482 | Calendar cal) |
483 | throws SQLException |
484 | { |
485 | getPreparedStatement().setTimestamp( i, x, cal); |
486 | } |
487 | |
488 | /* |
489 | ** Control methods. |
490 | */ |
491 | |
492 | /** |
493 | * Access the underlying PreparedStatement. This method |
494 | * is package protected to restrict access to the underlying |
495 | * object to the brokered objects. Allowing the application to |
496 | * access the underlying object thtough a public method would |
497 | * |
498 | */ |
499 | PreparedStatement getPreparedStatement() throws SQLException { |
500 | return control.getRealPreparedStatement(); |
501 | } |
502 | |
503 | /** |
504 | Override the BrokeredStatement's getStatement() to always return a PreparedStatement. |
505 | */ |
506 | public final Statement getStatement() throws SQLException { |
507 | return getPreparedStatement(); |
508 | } |
509 | |
510 | /** |
511 | Create a duplicate PreparedStatement to this, including state, from the passed in Connection. |
512 | */ |
513 | public PreparedStatement createDuplicateStatement(Connection conn, PreparedStatement oldStatement) throws SQLException { |
514 | |
515 | PreparedStatement newStatement = conn.prepareStatement(sql, resultSetType, resultSetConcurrency); |
516 | |
517 | setStatementState(oldStatement, newStatement); |
518 | |
519 | return newStatement; |
520 | } |
521 | } |