1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.jdbc; |
7 | |
8 | import java.io.InputStream; |
9 | import java.io.Reader; |
10 | import java.math.BigDecimal; |
11 | import java.net.URL; |
12 | import java.sql.Array; |
13 | import java.sql.Blob; |
14 | import java.sql.Clob; |
15 | import java.sql.NClob; |
16 | import java.sql.ParameterMetaData; |
17 | import java.sql.PreparedStatement; |
18 | import java.sql.Ref; |
19 | import java.sql.ResultSet; |
20 | import java.sql.ResultSetMetaData; |
21 | import java.sql.RowId; |
22 | import java.sql.SQLException; |
23 | import java.sql.SQLXML; |
24 | import java.sql.Statement; |
25 | import java.util.ArrayList; |
26 | import java.util.Calendar; |
27 | import java.util.HashMap; |
28 | |
29 | import org.h2.api.ErrorCode; |
30 | import org.h2.command.CommandInterface; |
31 | import org.h2.expression.ParameterInterface; |
32 | import org.h2.message.DbException; |
33 | import org.h2.message.TraceObject; |
34 | import org.h2.result.ResultInterface; |
35 | import org.h2.util.DateTimeUtils; |
36 | import org.h2.util.IOUtils; |
37 | import org.h2.util.New; |
38 | import org.h2.value.DataType; |
39 | import org.h2.value.Value; |
40 | import org.h2.value.ValueBoolean; |
41 | import org.h2.value.ValueByte; |
42 | import org.h2.value.ValueBytes; |
43 | import org.h2.value.ValueDate; |
44 | import org.h2.value.ValueDecimal; |
45 | import org.h2.value.ValueDouble; |
46 | import org.h2.value.ValueFloat; |
47 | import org.h2.value.ValueInt; |
48 | import org.h2.value.ValueLong; |
49 | import org.h2.value.ValueNull; |
50 | import org.h2.value.ValueShort; |
51 | import org.h2.value.ValueString; |
52 | import org.h2.value.ValueTime; |
53 | import org.h2.value.ValueTimestamp; |
54 | |
55 | /** |
56 | * Represents a prepared statement. |
57 | */ |
58 | public class JdbcPreparedStatement extends JdbcStatement implements |
59 | PreparedStatement { |
60 | |
61 | protected CommandInterface command; |
62 | private final String sqlStatement; |
63 | private ArrayList<Value[]> batchParameters; |
64 | private HashMap<String, Integer> cachedColumnLabelMap; |
65 | |
66 | JdbcPreparedStatement(JdbcConnection conn, String sql, int id, |
67 | int resultSetType, int resultSetConcurrency, |
68 | boolean closeWithResultSet) { |
69 | super(conn, id, resultSetType, resultSetConcurrency, closeWithResultSet); |
70 | setTrace(session.getTrace(), TraceObject.PREPARED_STATEMENT, id); |
71 | this.sqlStatement = sql; |
72 | command = conn.prepareCommand(sql, fetchSize); |
73 | } |
74 | |
75 | /** |
76 | * Cache the column labels (looking up the column index can sometimes show |
77 | * up on the performance profile). |
78 | * |
79 | * @param cachedColumnLabelMap the column map |
80 | */ |
81 | void setCachedColumnLabelMap(HashMap<String, Integer> cachedColumnLabelMap) { |
82 | this.cachedColumnLabelMap = cachedColumnLabelMap; |
83 | } |
84 | |
85 | /** |
86 | * Executes a query (select statement) and returns the result set. If |
87 | * another result set exists for this statement, this will be closed (even |
88 | * if this statement fails). |
89 | * |
90 | * @return the result set |
91 | * @throws SQLException if this object is closed or invalid |
92 | */ |
93 | @Override |
94 | public ResultSet executeQuery() throws SQLException { |
95 | try { |
96 | int id = getNextId(TraceObject.RESULT_SET); |
97 | if (isDebugEnabled()) { |
98 | debugCodeAssign("ResultSet", TraceObject.RESULT_SET, id, "executeQuery()"); |
99 | } |
100 | synchronized (session) { |
101 | checkClosed(); |
102 | closeOldResultSet(); |
103 | ResultInterface result; |
104 | boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY; |
105 | boolean updatable = resultSetConcurrency == ResultSet.CONCUR_UPDATABLE; |
106 | try { |
107 | setExecutingStatement(command); |
108 | result = command.executeQuery(maxRows, scrollable); |
109 | } finally { |
110 | setExecutingStatement(null); |
111 | } |
112 | resultSet = new JdbcResultSet(conn, this, result, id, |
113 | closedByResultSet, scrollable, updatable, cachedColumnLabelMap); |
114 | } |
115 | return resultSet; |
116 | } catch (Exception e) { |
117 | throw logAndConvert(e); |
118 | } |
119 | } |
120 | |
121 | /** |
122 | * Executes a statement (insert, update, delete, create, drop) |
123 | * and returns the update count. |
124 | * If another result set exists for this statement, this will be closed |
125 | * (even if this statement fails). |
126 | * |
127 | * If auto commit is on, this statement will be committed. |
128 | * If the statement is a DDL statement (create, drop, alter) and does not |
129 | * throw an exception, the current transaction (if any) is committed after |
130 | * executing the statement. |
131 | * |
132 | * @return the update count (number of row affected by an insert, update or |
133 | * delete, or 0 if no rows or the statement was a create, drop, |
134 | * commit or rollback) |
135 | * @throws SQLException if this object is closed or invalid |
136 | */ |
137 | @Override |
138 | public int executeUpdate() throws SQLException { |
139 | try { |
140 | debugCodeCall("executeUpdate"); |
141 | checkClosedForWrite(); |
142 | try { |
143 | return executeUpdateInternal(); |
144 | } finally { |
145 | afterWriting(); |
146 | } |
147 | } catch (Exception e) { |
148 | throw logAndConvert(e); |
149 | } |
150 | } |
151 | |
152 | private int executeUpdateInternal() throws SQLException { |
153 | closeOldResultSet(); |
154 | synchronized (session) { |
155 | try { |
156 | setExecutingStatement(command); |
157 | updateCount = command.executeUpdate(); |
158 | } finally { |
159 | setExecutingStatement(null); |
160 | } |
161 | } |
162 | return updateCount; |
163 | } |
164 | |
165 | /** |
166 | * Executes an arbitrary statement. If another result set exists for this |
167 | * statement, this will be closed (even if this statement fails). If auto |
168 | * commit is on, and the statement is not a select, this statement will be |
169 | * committed. |
170 | * |
171 | * @return true if a result set is available, false if not |
172 | * @throws SQLException if this object is closed or invalid |
173 | */ |
174 | @Override |
175 | public boolean execute() throws SQLException { |
176 | try { |
177 | int id = getNextId(TraceObject.RESULT_SET); |
178 | if (isDebugEnabled()) { |
179 | debugCodeCall("execute"); |
180 | } |
181 | checkClosedForWrite(); |
182 | try { |
183 | boolean returnsResultSet; |
184 | synchronized (conn.getSession()) { |
185 | closeOldResultSet(); |
186 | try { |
187 | setExecutingStatement(command); |
188 | if (command.isQuery()) { |
189 | returnsResultSet = true; |
190 | boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY; |
191 | boolean updatable = resultSetConcurrency == ResultSet.CONCUR_UPDATABLE; |
192 | ResultInterface result = command.executeQuery(maxRows, scrollable); |
193 | resultSet = new JdbcResultSet(conn, this, result, |
194 | id, closedByResultSet, scrollable, |
195 | updatable); |
196 | } else { |
197 | returnsResultSet = false; |
198 | updateCount = command.executeUpdate(); |
199 | } |
200 | } finally { |
201 | setExecutingStatement(null); |
202 | } |
203 | } |
204 | return returnsResultSet; |
205 | } finally { |
206 | afterWriting(); |
207 | } |
208 | } catch (Exception e) { |
209 | throw logAndConvert(e); |
210 | } |
211 | } |
212 | |
213 | /** |
214 | * Clears all parameters. |
215 | * |
216 | * @throws SQLException if this object is closed or invalid |
217 | */ |
218 | @Override |
219 | public void clearParameters() throws SQLException { |
220 | try { |
221 | debugCodeCall("clearParameters"); |
222 | checkClosed(); |
223 | ArrayList<? extends ParameterInterface> parameters = command.getParameters(); |
224 | for (int i = 0, size = parameters.size(); i < size; i++) { |
225 | ParameterInterface param = parameters.get(i); |
226 | // can only delete old temp files if they are not in the batch |
227 | param.setValue(null, batchParameters == null); |
228 | } |
229 | } catch (Exception e) { |
230 | throw logAndConvert(e); |
231 | } |
232 | } |
233 | |
234 | /** |
235 | * Calling this method is not legal on a PreparedStatement. |
236 | * |
237 | * @param sql ignored |
238 | * @throws SQLException Unsupported Feature |
239 | */ |
240 | @Override |
241 | public ResultSet executeQuery(String sql) throws SQLException { |
242 | try { |
243 | debugCodeCall("executeQuery", sql); |
244 | throw DbException.get(ErrorCode.METHOD_NOT_ALLOWED_FOR_PREPARED_STATEMENT); |
245 | } catch (Exception e) { |
246 | throw logAndConvert(e); |
247 | } |
248 | } |
249 | |
250 | /** |
251 | * Calling this method is not legal on a PreparedStatement. |
252 | * |
253 | * @param sql ignored |
254 | * @throws SQLException Unsupported Feature |
255 | */ |
256 | @Override |
257 | public void addBatch(String sql) throws SQLException { |
258 | try { |
259 | debugCodeCall("addBatch", sql); |
260 | throw DbException.get(ErrorCode.METHOD_NOT_ALLOWED_FOR_PREPARED_STATEMENT); |
261 | } catch (Exception e) { |
262 | throw logAndConvert(e); |
263 | } |
264 | } |
265 | |
266 | /** |
267 | * Calling this method is not legal on a PreparedStatement. |
268 | * |
269 | * @param sql ignored |
270 | * @throws SQLException Unsupported Feature |
271 | */ |
272 | @Override |
273 | public int executeUpdate(String sql) throws SQLException { |
274 | try { |
275 | debugCodeCall("executeUpdate", sql); |
276 | throw DbException.get(ErrorCode.METHOD_NOT_ALLOWED_FOR_PREPARED_STATEMENT); |
277 | } catch (Exception e) { |
278 | throw logAndConvert(e); |
279 | } |
280 | } |
281 | |
282 | /** |
283 | * Calling this method is not legal on a PreparedStatement. |
284 | * |
285 | * @param sql ignored |
286 | * @throws SQLException Unsupported Feature |
287 | */ |
288 | @Override |
289 | public boolean execute(String sql) throws SQLException { |
290 | try { |
291 | debugCodeCall("execute", sql); |
292 | throw DbException.get(ErrorCode.METHOD_NOT_ALLOWED_FOR_PREPARED_STATEMENT); |
293 | } catch (Exception e) { |
294 | throw logAndConvert(e); |
295 | } |
296 | } |
297 | |
298 | // ============================================================= |
299 | |
300 | /** |
301 | * Sets a parameter to null. |
302 | * |
303 | * @param parameterIndex the parameter index (1, 2, ...) |
304 | * @param sqlType the data type (Types.x) |
305 | * @throws SQLException if this object is closed |
306 | */ |
307 | @Override |
308 | public void setNull(int parameterIndex, int sqlType) throws SQLException { |
309 | try { |
310 | if (isDebugEnabled()) { |
311 | debugCode("setNull("+parameterIndex+", "+sqlType+");"); |
312 | } |
313 | setParameter(parameterIndex, ValueNull.INSTANCE); |
314 | } catch (Exception e) { |
315 | throw logAndConvert(e); |
316 | } |
317 | } |
318 | |
319 | /** |
320 | * Sets the value of a parameter. |
321 | * |
322 | * @param parameterIndex the parameter index (1, 2, ...) |
323 | * @param x the value |
324 | * @throws SQLException if this object is closed |
325 | */ |
326 | @Override |
327 | public void setInt(int parameterIndex, int x) throws SQLException { |
328 | try { |
329 | if (isDebugEnabled()) { |
330 | debugCode("setInt("+parameterIndex+", "+x+");"); |
331 | } |
332 | setParameter(parameterIndex, ValueInt.get(x)); |
333 | } catch (Exception e) { |
334 | throw logAndConvert(e); |
335 | } |
336 | } |
337 | |
338 | /** |
339 | * Sets the value of a parameter. |
340 | * |
341 | * @param parameterIndex the parameter index (1, 2, ...) |
342 | * @param x the value |
343 | * @throws SQLException if this object is closed |
344 | */ |
345 | @Override |
346 | public void setString(int parameterIndex, String x) throws SQLException { |
347 | try { |
348 | if (isDebugEnabled()) { |
349 | debugCode("setString("+parameterIndex+", "+quote(x)+");"); |
350 | } |
351 | Value v = x == null ? (Value) ValueNull.INSTANCE : ValueString.get(x); |
352 | setParameter(parameterIndex, v); |
353 | } catch (Exception e) { |
354 | throw logAndConvert(e); |
355 | } |
356 | } |
357 | |
358 | /** |
359 | * Sets the value of a parameter. |
360 | * |
361 | * @param parameterIndex the parameter index (1, 2, ...) |
362 | * @param x the value |
363 | * @throws SQLException if this object is closed |
364 | */ |
365 | @Override |
366 | public void setBigDecimal(int parameterIndex, BigDecimal x) |
367 | throws SQLException { |
368 | try { |
369 | if (isDebugEnabled()) { |
370 | debugCode("setBigDecimal("+parameterIndex+", " + quoteBigDecimal(x) + ");"); |
371 | } |
372 | Value v = x == null ? (Value) ValueNull.INSTANCE : ValueDecimal.get(x); |
373 | setParameter(parameterIndex, v); |
374 | } catch (Exception e) { |
375 | throw logAndConvert(e); |
376 | } |
377 | } |
378 | |
379 | /** |
380 | * Sets the value of a parameter. |
381 | * |
382 | * @param parameterIndex the parameter index (1, 2, ...) |
383 | * @param x the value |
384 | * @throws SQLException if this object is closed |
385 | */ |
386 | @Override |
387 | public void setDate(int parameterIndex, java.sql.Date x) |
388 | throws SQLException { |
389 | try { |
390 | if (isDebugEnabled()) { |
391 | debugCode("setDate("+parameterIndex+", " + quoteDate(x) + ");"); |
392 | } |
393 | Value v = x == null ? (Value) ValueNull.INSTANCE : ValueDate.get(x); |
394 | setParameter(parameterIndex, v); |
395 | } catch (Exception e) { |
396 | throw logAndConvert(e); |
397 | } |
398 | } |
399 | |
400 | /** |
401 | * Sets the value of a parameter. |
402 | * |
403 | * @param parameterIndex the parameter index (1, 2, ...) |
404 | * @param x the value |
405 | * @throws SQLException if this object is closed |
406 | */ |
407 | @Override |
408 | public void setTime(int parameterIndex, java.sql.Time x) |
409 | throws SQLException { |
410 | try { |
411 | if (isDebugEnabled()) { |
412 | debugCode("setTime("+parameterIndex+", " + quoteTime(x) + ");"); |
413 | } |
414 | Value v = x == null ? (Value) ValueNull.INSTANCE : ValueTime.get(x); |
415 | setParameter(parameterIndex, v); |
416 | } catch (Exception e) { |
417 | throw logAndConvert(e); |
418 | } |
419 | } |
420 | |
421 | /** |
422 | * Sets the value of a parameter. |
423 | * |
424 | * @param parameterIndex the parameter index (1, 2, ...) |
425 | * @param x the value |
426 | * @throws SQLException if this object is closed |
427 | */ |
428 | @Override |
429 | public void setTimestamp(int parameterIndex, java.sql.Timestamp x) |
430 | throws SQLException { |
431 | try { |
432 | if (isDebugEnabled()) { |
433 | debugCode("setTimestamp("+parameterIndex+", " + quoteTimestamp(x) + ");"); |
434 | } |
435 | Value v = x == null ? (Value) ValueNull.INSTANCE : ValueTimestamp.get(x); |
436 | setParameter(parameterIndex, v); |
437 | } catch (Exception e) { |
438 | throw logAndConvert(e); |
439 | } |
440 | } |
441 | |
442 | /** |
443 | * Sets the value of a parameter. |
444 | * Objects of unknown classes are serialized (on the client side). |
445 | * |
446 | * @param parameterIndex the parameter index (1, 2, ...) |
447 | * @param x the value |
448 | * @throws SQLException if this object is closed |
449 | */ |
450 | @Override |
451 | public void setObject(int parameterIndex, Object x) throws SQLException { |
452 | try { |
453 | if (isDebugEnabled()) { |
454 | debugCode("setObject("+parameterIndex+", x);"); |
455 | } |
456 | if (x == null) { |
457 | // throw Errors.getInvalidValueException("null", "x"); |
458 | setParameter(parameterIndex, ValueNull.INSTANCE); |
459 | } else { |
460 | setParameter(parameterIndex, |
461 | DataType.convertToValue(session, x, Value.UNKNOWN)); |
462 | } |
463 | } catch (Exception e) { |
464 | throw logAndConvert(e); |
465 | } |
466 | } |
467 | |
468 | /** |
469 | * Sets the value of a parameter. The object is converted, if required, to |
470 | * the specified data type before sending to the database. |
471 | * Objects of unknown classes are serialized (on the client side). |
472 | * |
473 | * @param parameterIndex the parameter index (1, 2, ...) |
474 | * @param x the value, null is allowed |
475 | * @param targetSqlType the type as defined in java.sql.Types |
476 | * @throws SQLException if this object is closed |
477 | */ |
478 | @Override |
479 | public void setObject(int parameterIndex, Object x, int targetSqlType) |
480 | throws SQLException { |
481 | try { |
482 | if (isDebugEnabled()) { |
483 | debugCode("setObject("+parameterIndex+", x, "+targetSqlType+");"); |
484 | } |
485 | int type = DataType.convertSQLTypeToValueType(targetSqlType); |
486 | if (x == null) { |
487 | setParameter(parameterIndex, ValueNull.INSTANCE); |
488 | } else { |
489 | Value v = DataType.convertToValue(conn.getSession(), x, type); |
490 | setParameter(parameterIndex, v.convertTo(type)); |
491 | } |
492 | } catch (Exception e) { |
493 | throw logAndConvert(e); |
494 | } |
495 | } |
496 | |
497 | /** |
498 | * Sets the value of a parameter. The object is converted, if required, to |
499 | * the specified data type before sending to the database. |
500 | * Objects of unknown classes are serialized (on the client side). |
501 | * |
502 | * @param parameterIndex the parameter index (1, 2, ...) |
503 | * @param x the value, null is allowed |
504 | * @param targetSqlType the type as defined in java.sql.Types |
505 | * @param scale is ignored |
506 | * @throws SQLException if this object is closed |
507 | */ |
508 | @Override |
509 | public void setObject(int parameterIndex, Object x, int targetSqlType, |
510 | int scale) throws SQLException { |
511 | try { |
512 | if (isDebugEnabled()) { |
513 | debugCode("setObject("+parameterIndex+", x, "+targetSqlType+", "+scale+");"); |
514 | } |
515 | setObject(parameterIndex, x, targetSqlType); |
516 | } catch (Exception e) { |
517 | throw logAndConvert(e); |
518 | } |
519 | } |
520 | |
521 | /** |
522 | * Sets the value of a parameter. |
523 | * |
524 | * @param parameterIndex the parameter index (1, 2, ...) |
525 | * @param x the value |
526 | * @throws SQLException if this object is closed |
527 | */ |
528 | @Override |
529 | public void setBoolean(int parameterIndex, boolean x) throws SQLException { |
530 | try { |
531 | if (isDebugEnabled()) { |
532 | debugCode("setBoolean("+parameterIndex+", "+x+");"); |
533 | } |
534 | setParameter(parameterIndex, ValueBoolean.get(x)); |
535 | } catch (Exception e) { |
536 | throw logAndConvert(e); |
537 | } |
538 | } |
539 | |
540 | /** |
541 | * Sets the value of a parameter. |
542 | * |
543 | * @param parameterIndex the parameter index (1, 2, ...) |
544 | * @param x the value |
545 | * @throws SQLException if this object is closed |
546 | */ |
547 | @Override |
548 | public void setByte(int parameterIndex, byte x) throws SQLException { |
549 | try { |
550 | if (isDebugEnabled()) { |
551 | debugCode("setByte("+parameterIndex+", "+x+");"); |
552 | } |
553 | setParameter(parameterIndex, ValueByte.get(x)); |
554 | } catch (Exception e) { |
555 | throw logAndConvert(e); |
556 | } |
557 | } |
558 | |
559 | /** |
560 | * Sets the value of a parameter. |
561 | * |
562 | * @param parameterIndex the parameter index (1, 2, ...) |
563 | * @param x the value |
564 | * @throws SQLException if this object is closed |
565 | */ |
566 | @Override |
567 | public void setShort(int parameterIndex, short x) throws SQLException { |
568 | try { |
569 | if (isDebugEnabled()) { |
570 | debugCode("setShort("+parameterIndex+", (short) "+x+");"); |
571 | } |
572 | setParameter(parameterIndex, ValueShort.get(x)); |
573 | } catch (Exception e) { |
574 | throw logAndConvert(e); |
575 | } |
576 | } |
577 | |
578 | /** |
579 | * Sets the value of a parameter. |
580 | * |
581 | * @param parameterIndex the parameter index (1, 2, ...) |
582 | * @param x the value |
583 | * @throws SQLException if this object is closed |
584 | */ |
585 | @Override |
586 | public void setLong(int parameterIndex, long x) throws SQLException { |
587 | try { |
588 | if (isDebugEnabled()) { |
589 | debugCode("setLong("+parameterIndex+", "+x+"L);"); |
590 | } |
591 | setParameter(parameterIndex, ValueLong.get(x)); |
592 | } catch (Exception e) { |
593 | throw logAndConvert(e); |
594 | } |
595 | } |
596 | |
597 | /** |
598 | * Sets the value of a parameter. |
599 | * |
600 | * @param parameterIndex the parameter index (1, 2, ...) |
601 | * @param x the value |
602 | * @throws SQLException if this object is closed |
603 | */ |
604 | @Override |
605 | public void setFloat(int parameterIndex, float x) throws SQLException { |
606 | try { |
607 | if (isDebugEnabled()) { |
608 | debugCode("setFloat("+parameterIndex+", "+x+"f);"); |
609 | } |
610 | setParameter(parameterIndex, ValueFloat.get(x)); |
611 | } catch (Exception e) { |
612 | throw logAndConvert(e); |
613 | } |
614 | } |
615 | |
616 | /** |
617 | * Sets the value of a parameter. |
618 | * |
619 | * @param parameterIndex the parameter index (1, 2, ...) |
620 | * @param x the value |
621 | * @throws SQLException if this object is closed |
622 | */ |
623 | @Override |
624 | public void setDouble(int parameterIndex, double x) throws SQLException { |
625 | try { |
626 | if (isDebugEnabled()) { |
627 | debugCode("setDouble("+parameterIndex+", "+x+"d);"); |
628 | } |
629 | setParameter(parameterIndex, ValueDouble.get(x)); |
630 | } catch (Exception e) { |
631 | throw logAndConvert(e); |
632 | } |
633 | } |
634 | |
635 | /** |
636 | * [Not supported] Sets the value of a column as a reference. |
637 | */ |
638 | @Override |
639 | public void setRef(int parameterIndex, Ref x) throws SQLException { |
640 | throw unsupported("ref"); |
641 | } |
642 | |
643 | /** |
644 | * Sets the date using a specified time zone. The value will be converted to |
645 | * the local time zone. |
646 | * |
647 | * @param parameterIndex the parameter index (1, 2, ...) |
648 | * @param x the value |
649 | * @param calendar the calendar |
650 | * @throws SQLException if this object is closed |
651 | */ |
652 | @Override |
653 | public void setDate(int parameterIndex, java.sql.Date x, Calendar calendar) |
654 | throws SQLException { |
655 | try { |
656 | if (isDebugEnabled()) { |
657 | debugCode("setDate("+parameterIndex+", " + quoteDate(x) + ", calendar);"); |
658 | } |
659 | if (x == null) { |
660 | setParameter(parameterIndex, ValueNull.INSTANCE); |
661 | } else { |
662 | setParameter(parameterIndex, DateTimeUtils.convertDate(x, calendar)); |
663 | } |
664 | } catch (Exception e) { |
665 | throw logAndConvert(e); |
666 | } |
667 | } |
668 | |
669 | /** |
670 | * Sets the time using a specified time zone. The value will be converted to |
671 | * the local time zone. |
672 | * |
673 | * @param parameterIndex the parameter index (1, 2, ...) |
674 | * @param x the value |
675 | * @param calendar the calendar |
676 | * @throws SQLException if this object is closed |
677 | */ |
678 | @Override |
679 | public void setTime(int parameterIndex, java.sql.Time x, Calendar calendar) |
680 | throws SQLException { |
681 | try { |
682 | if (isDebugEnabled()) { |
683 | debugCode("setTime("+parameterIndex+", " + quoteTime(x) + ", calendar);"); |
684 | } |
685 | if (x == null) { |
686 | setParameter(parameterIndex, ValueNull.INSTANCE); |
687 | } else { |
688 | setParameter(parameterIndex, DateTimeUtils.convertTime(x, calendar)); |
689 | } |
690 | } catch (Exception e) { |
691 | throw logAndConvert(e); |
692 | } |
693 | } |
694 | |
695 | /** |
696 | * Sets the timestamp using a specified time zone. The value will be |
697 | * converted to the local time zone. |
698 | * |
699 | * @param parameterIndex the parameter index (1, 2, ...) |
700 | * @param x the value |
701 | * @param calendar the calendar |
702 | * @throws SQLException if this object is closed |
703 | */ |
704 | @Override |
705 | public void setTimestamp(int parameterIndex, java.sql.Timestamp x, |
706 | Calendar calendar) throws SQLException { |
707 | try { |
708 | if (isDebugEnabled()) { |
709 | debugCode("setTimestamp(" + parameterIndex + ", " + |
710 | quoteTimestamp(x) + ", calendar);"); |
711 | } |
712 | if (x == null) { |
713 | setParameter(parameterIndex, ValueNull.INSTANCE); |
714 | } else { |
715 | setParameter(parameterIndex, DateTimeUtils.convertTimestamp(x, calendar)); |
716 | } |
717 | } catch (Exception e) { |
718 | throw logAndConvert(e); |
719 | } |
720 | } |
721 | |
722 | /** |
723 | * [Not supported] This feature is deprecated and not supported. |
724 | * |
725 | * @deprecated since JDBC 2.0, use setCharacterStream |
726 | */ |
727 | @Override |
728 | public void setUnicodeStream(int parameterIndex, InputStream x, int length) |
729 | throws SQLException { |
730 | throw unsupported("unicodeStream"); |
731 | } |
732 | |
733 | /** |
734 | * Sets a parameter to null. |
735 | * |
736 | * @param parameterIndex the parameter index (1, 2, ...) |
737 | * @param sqlType the data type (Types.x) |
738 | * @param typeName this parameter is ignored |
739 | * @throws SQLException if this object is closed |
740 | */ |
741 | @Override |
742 | public void setNull(int parameterIndex, int sqlType, String typeName) |
743 | throws SQLException { |
744 | try { |
745 | if (isDebugEnabled()) { |
746 | debugCode("setNull("+parameterIndex+", "+sqlType+", "+quote(typeName)+");"); |
747 | } |
748 | setNull(parameterIndex, sqlType); |
749 | } catch (Exception e) { |
750 | throw logAndConvert(e); |
751 | } |
752 | } |
753 | |
754 | /** |
755 | * Sets the value of a parameter as a Blob. |
756 | * |
757 | * @param parameterIndex the parameter index (1, 2, ...) |
758 | * @param x the value |
759 | * @throws SQLException if this object is closed |
760 | */ |
761 | @Override |
762 | public void setBlob(int parameterIndex, Blob x) throws SQLException { |
763 | try { |
764 | if (isDebugEnabled()) { |
765 | debugCode("setBlob("+parameterIndex+", x);"); |
766 | } |
767 | checkClosedForWrite(); |
768 | try { |
769 | Value v; |
770 | if (x == null) { |
771 | v = ValueNull.INSTANCE; |
772 | } else { |
773 | v = conn.createBlob(x.getBinaryStream(), -1); |
774 | } |
775 | setParameter(parameterIndex, v); |
776 | } finally { |
777 | afterWriting(); |
778 | } |
779 | } catch (Exception e) { |
780 | throw logAndConvert(e); |
781 | } |
782 | } |
783 | |
784 | /** |
785 | * Sets the value of a parameter as a Blob. |
786 | * This method does not close the stream. |
787 | * The stream may be closed after executing the statement. |
788 | * |
789 | * @param parameterIndex the parameter index (1, 2, ...) |
790 | * @param x the value |
791 | * @throws SQLException if this object is closed |
792 | */ |
793 | @Override |
794 | public void setBlob(int parameterIndex, InputStream x) throws SQLException { |
795 | try { |
796 | if (isDebugEnabled()) { |
797 | debugCode("setBlob("+parameterIndex+", x);"); |
798 | } |
799 | checkClosedForWrite(); |
800 | try { |
801 | Value v = conn.createBlob(x, -1); |
802 | setParameter(parameterIndex, v); |
803 | } finally { |
804 | afterWriting(); |
805 | } |
806 | } catch (Exception e) { |
807 | throw logAndConvert(e); |
808 | } |
809 | } |
810 | |
811 | /** |
812 | * Sets the value of a parameter as a Clob. |
813 | * |
814 | * @param parameterIndex the parameter index (1, 2, ...) |
815 | * @param x the value |
816 | * @throws SQLException if this object is closed |
817 | */ |
818 | @Override |
819 | public void setClob(int parameterIndex, Clob x) throws SQLException { |
820 | try { |
821 | if (isDebugEnabled()) { |
822 | debugCode("setClob("+parameterIndex+", x);"); |
823 | } |
824 | checkClosedForWrite(); |
825 | try { |
826 | Value v; |
827 | if (x == null) { |
828 | v = ValueNull.INSTANCE; |
829 | } else { |
830 | v = conn.createClob(x.getCharacterStream(), -1); |
831 | } |
832 | setParameter(parameterIndex, v); |
833 | } finally { |
834 | afterWriting(); |
835 | } |
836 | } catch (Exception e) { |
837 | throw logAndConvert(e); |
838 | } |
839 | } |
840 | |
841 | /** |
842 | * Sets the value of a parameter as a Clob. |
843 | * This method does not close the reader. |
844 | * The reader may be closed after executing the statement. |
845 | * |
846 | * @param parameterIndex the parameter index (1, 2, ...) |
847 | * @param x the value |
848 | * @throws SQLException if this object is closed |
849 | */ |
850 | @Override |
851 | public void setClob(int parameterIndex, Reader x) throws SQLException { |
852 | try { |
853 | if (isDebugEnabled()) { |
854 | debugCode("setClob("+parameterIndex+", x);"); |
855 | } |
856 | checkClosedForWrite(); |
857 | try { |
858 | Value v; |
859 | if (x == null) { |
860 | v = ValueNull.INSTANCE; |
861 | } else { |
862 | v = conn.createClob(x, -1); |
863 | } |
864 | setParameter(parameterIndex, v); |
865 | } finally { |
866 | afterWriting(); |
867 | } |
868 | } catch (Exception e) { |
869 | throw logAndConvert(e); |
870 | } |
871 | } |
872 | |
873 | /** |
874 | * [Not supported] Sets the value of a parameter as a Array. |
875 | */ |
876 | @Override |
877 | public void setArray(int parameterIndex, Array x) throws SQLException { |
878 | throw unsupported("setArray"); |
879 | } |
880 | |
881 | /** |
882 | * Sets the value of a parameter as a byte array. |
883 | * |
884 | * @param parameterIndex the parameter index (1, 2, ...) |
885 | * @param x the value |
886 | * @throws SQLException if this object is closed |
887 | */ |
888 | @Override |
889 | public void setBytes(int parameterIndex, byte[] x) throws SQLException { |
890 | try { |
891 | if (isDebugEnabled()) { |
892 | debugCode("setBytes("+parameterIndex+", "+quoteBytes(x)+");"); |
893 | } |
894 | Value v = x == null ? (Value) ValueNull.INSTANCE : ValueBytes.get(x); |
895 | setParameter(parameterIndex, v); |
896 | } catch (Exception e) { |
897 | throw logAndConvert(e); |
898 | } |
899 | } |
900 | |
901 | /** |
902 | * Sets the value of a parameter as an input stream. |
903 | * This method does not close the stream. |
904 | * The stream may be closed after executing the statement. |
905 | * |
906 | * @param parameterIndex the parameter index (1, 2, ...) |
907 | * @param x the value |
908 | * @param length the maximum number of bytes |
909 | * @throws SQLException if this object is closed |
910 | */ |
911 | @Override |
912 | public void setBinaryStream(int parameterIndex, InputStream x, long length) |
913 | throws SQLException { |
914 | try { |
915 | if (isDebugEnabled()) { |
916 | debugCode("setBinaryStream("+parameterIndex+", x, "+length+"L);"); |
917 | } |
918 | checkClosedForWrite(); |
919 | try { |
920 | Value v = conn.createBlob(x, length); |
921 | setParameter(parameterIndex, v); |
922 | } finally { |
923 | afterWriting(); |
924 | } |
925 | } catch (Exception e) { |
926 | throw logAndConvert(e); |
927 | } |
928 | } |
929 | |
930 | /** |
931 | * Sets the value of a parameter as an input stream. |
932 | * This method does not close the stream. |
933 | * The stream may be closed after executing the statement. |
934 | * |
935 | * @param parameterIndex the parameter index (1, 2, ...) |
936 | * @param x the value |
937 | * @param length the maximum number of bytes |
938 | * @throws SQLException if this object is closed |
939 | */ |
940 | @Override |
941 | public void setBinaryStream(int parameterIndex, InputStream x, int length) |
942 | throws SQLException { |
943 | setBinaryStream(parameterIndex, x, (long) length); |
944 | } |
945 | |
946 | /** |
947 | * Sets the value of a parameter as an input stream. |
948 | * This method does not close the stream. |
949 | * The stream may be closed after executing the statement. |
950 | * |
951 | * @param parameterIndex the parameter index (1, 2, ...) |
952 | * @param x the value |
953 | * @throws SQLException if this object is closed |
954 | */ |
955 | @Override |
956 | public void setBinaryStream(int parameterIndex, InputStream x) |
957 | throws SQLException { |
958 | setBinaryStream(parameterIndex, x, -1); |
959 | } |
960 | |
961 | /** |
962 | * Sets the value of a parameter as an ASCII stream. |
963 | * This method does not close the stream. |
964 | * The stream may be closed after executing the statement. |
965 | * |
966 | * @param parameterIndex the parameter index (1, 2, ...) |
967 | * @param x the value |
968 | * @param length the maximum number of bytes |
969 | * @throws SQLException if this object is closed |
970 | */ |
971 | @Override |
972 | public void setAsciiStream(int parameterIndex, InputStream x, int length) |
973 | throws SQLException { |
974 | setAsciiStream(parameterIndex, x, (long) length); |
975 | } |
976 | |
977 | /** |
978 | * Sets the value of a parameter as an ASCII stream. |
979 | * This method does not close the stream. |
980 | * The stream may be closed after executing the statement. |
981 | * |
982 | * @param parameterIndex the parameter index (1, 2, ...) |
983 | * @param x the value |
984 | * @param length the maximum number of bytes |
985 | * @throws SQLException if this object is closed |
986 | */ |
987 | @Override |
988 | public void setAsciiStream(int parameterIndex, InputStream x, long length) |
989 | throws SQLException { |
990 | try { |
991 | if (isDebugEnabled()) { |
992 | debugCode("setAsciiStream("+parameterIndex+", x, "+length+"L);"); |
993 | } |
994 | checkClosedForWrite(); |
995 | try { |
996 | Value v = conn.createClob(IOUtils.getAsciiReader(x), length); |
997 | setParameter(parameterIndex, v); |
998 | } finally { |
999 | afterWriting(); |
1000 | } |
1001 | } catch (Exception e) { |
1002 | throw logAndConvert(e); |
1003 | } |
1004 | } |
1005 | |
1006 | /** |
1007 | * Sets the value of a parameter as an ASCII stream. |
1008 | * This method does not close the stream. |
1009 | * The stream may be closed after executing the statement. |
1010 | * |
1011 | * @param parameterIndex the parameter index (1, 2, ...) |
1012 | * @param x the value |
1013 | * @throws SQLException if this object is closed |
1014 | */ |
1015 | @Override |
1016 | public void setAsciiStream(int parameterIndex, InputStream x) |
1017 | throws SQLException { |
1018 | setAsciiStream(parameterIndex, x, -1); |
1019 | } |
1020 | |
1021 | /** |
1022 | * Sets the value of a parameter as a character stream. |
1023 | * This method does not close the reader. |
1024 | * The reader may be closed after executing the statement. |
1025 | * |
1026 | * @param parameterIndex the parameter index (1, 2, ...) |
1027 | * @param x the value |
1028 | * @param length the maximum number of characters |
1029 | * @throws SQLException if this object is closed |
1030 | */ |
1031 | @Override |
1032 | public void setCharacterStream(int parameterIndex, Reader x, int length) |
1033 | throws SQLException { |
1034 | setCharacterStream(parameterIndex, x, (long) length); |
1035 | } |
1036 | |
1037 | /** |
1038 | * Sets the value of a parameter as a character stream. |
1039 | * This method does not close the reader. |
1040 | * The reader may be closed after executing the statement. |
1041 | * |
1042 | * @param parameterIndex the parameter index (1, 2, ...) |
1043 | * @param x the value |
1044 | * @throws SQLException if this object is closed |
1045 | */ |
1046 | @Override |
1047 | public void setCharacterStream(int parameterIndex, Reader x) |
1048 | throws SQLException { |
1049 | setCharacterStream(parameterIndex, x, -1); |
1050 | } |
1051 | |
1052 | /** |
1053 | * Sets the value of a parameter as a character stream. |
1054 | * This method does not close the reader. |
1055 | * The reader may be closed after executing the statement. |
1056 | * |
1057 | * @param parameterIndex the parameter index (1, 2, ...) |
1058 | * @param x the value |
1059 | * @param length the maximum number of characters |
1060 | * @throws SQLException if this object is closed |
1061 | */ |
1062 | @Override |
1063 | public void setCharacterStream(int parameterIndex, Reader x, long length) |
1064 | throws SQLException { |
1065 | try { |
1066 | if (isDebugEnabled()) { |
1067 | debugCode("setCharacterStream("+parameterIndex+", x, "+length+"L);"); |
1068 | } |
1069 | checkClosedForWrite(); |
1070 | try { |
1071 | Value v = conn.createClob(x, length); |
1072 | setParameter(parameterIndex, v); |
1073 | } finally { |
1074 | afterWriting(); |
1075 | } |
1076 | } catch (Exception e) { |
1077 | throw logAndConvert(e); |
1078 | } |
1079 | } |
1080 | |
1081 | /** |
1082 | * [Not supported] |
1083 | */ |
1084 | @Override |
1085 | public void setURL(int parameterIndex, URL x) throws SQLException { |
1086 | throw unsupported("url"); |
1087 | } |
1088 | |
1089 | /** |
1090 | * Gets the result set metadata of the query returned when the statement is |
1091 | * executed. If this is not a query, this method returns null. |
1092 | * |
1093 | * @return the meta data or null if this is not a query |
1094 | * @throws SQLException if this object is closed |
1095 | */ |
1096 | @Override |
1097 | public ResultSetMetaData getMetaData() throws SQLException { |
1098 | try { |
1099 | debugCodeCall("getMetaData"); |
1100 | checkClosed(); |
1101 | ResultInterface result = command.getMetaData(); |
1102 | if (result == null) { |
1103 | return null; |
1104 | } |
1105 | int id = getNextId(TraceObject.RESULT_SET_META_DATA); |
1106 | if (isDebugEnabled()) { |
1107 | debugCodeAssign("ResultSetMetaData", |
1108 | TraceObject.RESULT_SET_META_DATA, id, "getMetaData()"); |
1109 | } |
1110 | String catalog = conn.getCatalog(); |
1111 | JdbcResultSetMetaData meta = new JdbcResultSetMetaData( |
1112 | null, this, result, catalog, session.getTrace(), id); |
1113 | return meta; |
1114 | } catch (Exception e) { |
1115 | throw logAndConvert(e); |
1116 | } |
1117 | } |
1118 | |
1119 | /** |
1120 | * Clears the batch. |
1121 | */ |
1122 | @Override |
1123 | public void clearBatch() throws SQLException { |
1124 | try { |
1125 | debugCodeCall("clearBatch"); |
1126 | checkClosed(); |
1127 | batchParameters = null; |
1128 | } catch (Exception e) { |
1129 | throw logAndConvert(e); |
1130 | } |
1131 | } |
1132 | |
1133 | /** |
1134 | * Closes this statement. |
1135 | * All result sets that where created by this statement |
1136 | * become invalid after calling this method. |
1137 | */ |
1138 | @Override |
1139 | public void close() throws SQLException { |
1140 | try { |
1141 | super.close(); |
1142 | batchParameters = null; |
1143 | if (command != null) { |
1144 | command.close(); |
1145 | command = null; |
1146 | } |
1147 | } catch (Exception e) { |
1148 | throw logAndConvert(e); |
1149 | } |
1150 | } |
1151 | |
1152 | /** |
1153 | * Executes the batch. |
1154 | * If one of the batched statements fails, this database will continue. |
1155 | * |
1156 | * @return the array of update counts |
1157 | */ |
1158 | @Override |
1159 | public int[] executeBatch() throws SQLException { |
1160 | try { |
1161 | debugCodeCall("executeBatch"); |
1162 | if (batchParameters == null) { |
1163 | // TODO batch: check what other database do if no parameters are |
1164 | // set |
1165 | batchParameters = New.arrayList(); |
1166 | } |
1167 | int size = batchParameters.size(); |
1168 | int[] result = new int[size]; |
1169 | boolean error = false; |
1170 | SQLException next = null; |
1171 | checkClosedForWrite(); |
1172 | try { |
1173 | for (int i = 0; i < size; i++) { |
1174 | Value[] set = batchParameters.get(i); |
1175 | ArrayList<? extends ParameterInterface> parameters = |
1176 | command.getParameters(); |
1177 | for (int j = 0; j < set.length; j++) { |
1178 | Value value = set[j]; |
1179 | ParameterInterface param = parameters.get(j); |
1180 | param.setValue(value, false); |
1181 | } |
1182 | try { |
1183 | result[i] = executeUpdateInternal(); |
1184 | } catch (Exception re) { |
1185 | SQLException e = logAndConvert(re); |
1186 | if (next == null) { |
1187 | next = e; |
1188 | } else { |
1189 | e.setNextException(next); |
1190 | next = e; |
1191 | } |
1192 | result[i] = Statement.EXECUTE_FAILED; |
1193 | error = true; |
1194 | } |
1195 | } |
1196 | batchParameters = null; |
1197 | if (error) { |
1198 | JdbcBatchUpdateException e = new JdbcBatchUpdateException(next, result); |
1199 | throw e; |
1200 | } |
1201 | return result; |
1202 | } finally { |
1203 | afterWriting(); |
1204 | } |
1205 | } catch (Exception e) { |
1206 | throw logAndConvert(e); |
1207 | } |
1208 | } |
1209 | |
1210 | /** |
1211 | * Adds the current settings to the batch. |
1212 | */ |
1213 | @Override |
1214 | public void addBatch() throws SQLException { |
1215 | try { |
1216 | debugCodeCall("addBatch"); |
1217 | checkClosedForWrite(); |
1218 | try { |
1219 | ArrayList<? extends ParameterInterface> parameters = |
1220 | command.getParameters(); |
1221 | int size = parameters.size(); |
1222 | Value[] set = new Value[size]; |
1223 | for (int i = 0; i < size; i++) { |
1224 | ParameterInterface param = parameters.get(i); |
1225 | Value value = param.getParamValue(); |
1226 | set[i] = value; |
1227 | } |
1228 | if (batchParameters == null) { |
1229 | batchParameters = New.arrayList(); |
1230 | } |
1231 | batchParameters.add(set); |
1232 | } finally { |
1233 | afterWriting(); |
1234 | } |
1235 | } catch (Exception e) { |
1236 | throw logAndConvert(e); |
1237 | } |
1238 | } |
1239 | |
1240 | /** |
1241 | * Calling this method is not legal on a PreparedStatement. |
1242 | * |
1243 | * @param sql ignored |
1244 | * @param autoGeneratedKeys ignored |
1245 | * @throws SQLException Unsupported Feature |
1246 | */ |
1247 | @Override |
1248 | public int executeUpdate(String sql, int autoGeneratedKeys) |
1249 | throws SQLException { |
1250 | try { |
1251 | debugCode("executeUpdate("+quote(sql)+", "+autoGeneratedKeys+");"); |
1252 | throw DbException.get(ErrorCode.METHOD_NOT_ALLOWED_FOR_PREPARED_STATEMENT); |
1253 | } catch (Exception e) { |
1254 | throw logAndConvert(e); |
1255 | } |
1256 | } |
1257 | |
1258 | |
1259 | /** |
1260 | * Calling this method is not legal on a PreparedStatement. |
1261 | * |
1262 | * @param sql ignored |
1263 | * @param columnIndexes ignored |
1264 | * @throws SQLException Unsupported Feature |
1265 | */ |
1266 | @Override |
1267 | public int executeUpdate(String sql, int[] columnIndexes) |
1268 | throws SQLException { |
1269 | try { |
1270 | debugCode("executeUpdate(" + quote(sql) + ", " + |
1271 | quoteIntArray(columnIndexes) + ");"); |
1272 | throw DbException.get(ErrorCode.METHOD_NOT_ALLOWED_FOR_PREPARED_STATEMENT); |
1273 | } catch (Exception e) { |
1274 | throw logAndConvert(e); |
1275 | } |
1276 | } |
1277 | |
1278 | /** |
1279 | * Calling this method is not legal on a PreparedStatement. |
1280 | * |
1281 | * @param sql ignored |
1282 | * @param columnNames ignored |
1283 | * @throws SQLException Unsupported Feature |
1284 | */ |
1285 | @Override |
1286 | public int executeUpdate(String sql, String[] columnNames) |
1287 | throws SQLException { |
1288 | try { |
1289 | debugCode("executeUpdate(" + quote(sql) + ", " + |
1290 | quoteArray(columnNames) + ");"); |
1291 | throw DbException.get( |
1292 | ErrorCode.METHOD_NOT_ALLOWED_FOR_PREPARED_STATEMENT); |
1293 | } catch (Exception e) { |
1294 | throw logAndConvert(e); |
1295 | } |
1296 | } |
1297 | |
1298 | /** |
1299 | * Calling this method is not legal on a PreparedStatement. |
1300 | * |
1301 | * @param sql ignored |
1302 | * @param autoGeneratedKeys ignored |
1303 | * @throws SQLException Unsupported Feature |
1304 | */ |
1305 | @Override |
1306 | public boolean execute(String sql, int autoGeneratedKeys) |
1307 | throws SQLException { |
1308 | try { |
1309 | debugCode("execute(" + quote(sql) + ", " + autoGeneratedKeys + ");"); |
1310 | throw DbException.get( |
1311 | ErrorCode.METHOD_NOT_ALLOWED_FOR_PREPARED_STATEMENT); |
1312 | } catch (Exception e) { |
1313 | throw logAndConvert(e); |
1314 | } |
1315 | } |
1316 | |
1317 | /** |
1318 | * Calling this method is not legal on a PreparedStatement. |
1319 | * |
1320 | * @param sql ignored |
1321 | * @param columnIndexes ignored |
1322 | * @throws SQLException Unsupported Feature |
1323 | */ |
1324 | @Override |
1325 | public boolean execute(String sql, int[] columnIndexes) throws SQLException { |
1326 | try { |
1327 | debugCode("execute(" + quote(sql) + ", " + quoteIntArray(columnIndexes) + ");"); |
1328 | throw DbException.get(ErrorCode.METHOD_NOT_ALLOWED_FOR_PREPARED_STATEMENT); |
1329 | } catch (Exception e) { |
1330 | throw logAndConvert(e); |
1331 | } |
1332 | } |
1333 | |
1334 | /** |
1335 | * Calling this method is not legal on a PreparedStatement. |
1336 | * |
1337 | * @param sql ignored |
1338 | * @param columnNames ignored |
1339 | * @throws SQLException Unsupported Feature |
1340 | */ |
1341 | @Override |
1342 | public boolean execute(String sql, String[] columnNames) |
1343 | throws SQLException { |
1344 | try { |
1345 | debugCode("execute(" + quote(sql) + ", " + quoteArray(columnNames) + ");"); |
1346 | throw DbException.get( |
1347 | ErrorCode.METHOD_NOT_ALLOWED_FOR_PREPARED_STATEMENT); |
1348 | } catch (Exception e) { |
1349 | throw logAndConvert(e); |
1350 | } |
1351 | } |
1352 | |
1353 | /** |
1354 | * Get the parameter meta data of this prepared statement. |
1355 | * |
1356 | * @return the meta data |
1357 | */ |
1358 | @Override |
1359 | public ParameterMetaData getParameterMetaData() throws SQLException { |
1360 | try { |
1361 | int id = getNextId(TraceObject.PARAMETER_META_DATA); |
1362 | if (isDebugEnabled()) { |
1363 | debugCodeAssign("ParameterMetaData", |
1364 | TraceObject.PARAMETER_META_DATA, id, "getParameterMetaData()"); |
1365 | } |
1366 | checkClosed(); |
1367 | JdbcParameterMetaData meta = new JdbcParameterMetaData( |
1368 | session.getTrace(), this, command, id); |
1369 | return meta; |
1370 | } catch (Exception e) { |
1371 | throw logAndConvert(e); |
1372 | } |
1373 | } |
1374 | |
1375 | // ============================================================= |
1376 | |
1377 | private void setParameter(int parameterIndex, Value value) { |
1378 | checkClosed(); |
1379 | parameterIndex--; |
1380 | ArrayList<? extends ParameterInterface> parameters = command.getParameters(); |
1381 | if (parameterIndex < 0 || parameterIndex >= parameters.size()) { |
1382 | throw DbException.getInvalidValueException("parameterIndex", |
1383 | parameterIndex + 1); |
1384 | } |
1385 | ParameterInterface param = parameters.get(parameterIndex); |
1386 | // can only delete old temp files if they are not in the batch |
1387 | param.setValue(value, batchParameters == null); |
1388 | } |
1389 | |
1390 | /** |
1391 | * [Not supported] Sets the value of a parameter as a row id. |
1392 | */ |
1393 | @Override |
1394 | public void setRowId(int parameterIndex, RowId x) throws SQLException { |
1395 | throw unsupported("rowId"); |
1396 | } |
1397 | |
1398 | /** |
1399 | * Sets the value of a parameter. |
1400 | * |
1401 | * @param parameterIndex the parameter index (1, 2, ...) |
1402 | * @param x the value |
1403 | * @throws SQLException if this object is closed |
1404 | */ |
1405 | @Override |
1406 | public void setNString(int parameterIndex, String x) throws SQLException { |
1407 | try { |
1408 | if (isDebugEnabled()) { |
1409 | debugCode("setNString("+parameterIndex+", "+quote(x)+");"); |
1410 | } |
1411 | Value v = x == null ? (Value) ValueNull.INSTANCE : ValueString.get(x); |
1412 | setParameter(parameterIndex, v); |
1413 | } catch (Exception e) { |
1414 | throw logAndConvert(e); |
1415 | } |
1416 | } |
1417 | |
1418 | /** |
1419 | * Sets the value of a parameter as a character stream. |
1420 | * This method does not close the reader. |
1421 | * The reader may be closed after executing the statement. |
1422 | * |
1423 | * @param parameterIndex the parameter index (1, 2, ...) |
1424 | * @param x the value |
1425 | * @param length the maximum number of characters |
1426 | * @throws SQLException if this object is closed |
1427 | */ |
1428 | @Override |
1429 | public void setNCharacterStream(int parameterIndex, Reader x, long length) |
1430 | throws SQLException { |
1431 | try { |
1432 | if (isDebugEnabled()) { |
1433 | debugCode("setNCharacterStream("+ |
1434 | parameterIndex+", x, "+length+"L);"); |
1435 | } |
1436 | checkClosedForWrite(); |
1437 | try { |
1438 | Value v = conn.createClob(x, length); |
1439 | setParameter(parameterIndex, v); |
1440 | } finally { |
1441 | afterWriting(); |
1442 | } |
1443 | } catch (Exception e) { |
1444 | throw logAndConvert(e); |
1445 | } |
1446 | } |
1447 | |
1448 | /** |
1449 | * Sets the value of a parameter as a character stream. |
1450 | * This method does not close the reader. |
1451 | * The reader may be closed after executing the statement. |
1452 | * |
1453 | * @param parameterIndex the parameter index (1, 2, ...) |
1454 | * @param x the value |
1455 | * @throws SQLException if this object is closed |
1456 | */ |
1457 | @Override |
1458 | public void setNCharacterStream(int parameterIndex, Reader x) |
1459 | throws SQLException { |
1460 | setNCharacterStream(parameterIndex, x, -1); |
1461 | } |
1462 | |
1463 | /** |
1464 | * Sets the value of a parameter as a Clob. |
1465 | * |
1466 | * @param parameterIndex the parameter index (1, 2, ...) |
1467 | * @param x the value |
1468 | * @throws SQLException if this object is closed |
1469 | */ |
1470 | @Override |
1471 | public void setNClob(int parameterIndex, NClob x) throws SQLException { |
1472 | try { |
1473 | if (isDebugEnabled()) { |
1474 | debugCode("setNClob("+parameterIndex+", x);"); |
1475 | } |
1476 | checkClosedForWrite(); |
1477 | Value v; |
1478 | if (x == null) { |
1479 | v = ValueNull.INSTANCE; |
1480 | } else { |
1481 | v = conn.createClob(x.getCharacterStream(), -1); |
1482 | } |
1483 | setParameter(parameterIndex, v); |
1484 | } catch (Exception e) { |
1485 | throw logAndConvert(e); |
1486 | } |
1487 | } |
1488 | |
1489 | /** |
1490 | * Sets the value of a parameter as a Clob. |
1491 | * This method does not close the reader. |
1492 | * The reader may be closed after executing the statement. |
1493 | * |
1494 | * @param parameterIndex the parameter index (1, 2, ...) |
1495 | * @param x the value |
1496 | * @throws SQLException if this object is closed |
1497 | */ |
1498 | @Override |
1499 | public void setNClob(int parameterIndex, Reader x) throws SQLException { |
1500 | try { |
1501 | if (isDebugEnabled()) { |
1502 | debugCode("setNClob("+parameterIndex+", x);"); |
1503 | } |
1504 | checkClosedForWrite(); |
1505 | try { |
1506 | Value v = conn.createClob(x, -1); |
1507 | setParameter(parameterIndex, v); |
1508 | } finally { |
1509 | afterWriting(); |
1510 | } |
1511 | } catch (Exception e) { |
1512 | throw logAndConvert(e); |
1513 | } |
1514 | } |
1515 | |
1516 | /** |
1517 | * Sets the value of a parameter as a Clob. This method does not close the |
1518 | * reader. The reader may be closed after executing the statement. |
1519 | * |
1520 | * @param parameterIndex the parameter index (1, 2, ...) |
1521 | * @param x the value |
1522 | * @param length the maximum number of characters |
1523 | * @throws SQLException if this object is closed |
1524 | */ |
1525 | @Override |
1526 | public void setClob(int parameterIndex, Reader x, long length) |
1527 | throws SQLException { |
1528 | try { |
1529 | if (isDebugEnabled()) { |
1530 | debugCode("setClob("+parameterIndex+", x, "+length+"L);"); |
1531 | } |
1532 | checkClosedForWrite(); |
1533 | try { |
1534 | Value v = conn.createClob(x, length); |
1535 | setParameter(parameterIndex, v); |
1536 | } finally { |
1537 | afterWriting(); |
1538 | } |
1539 | } catch (Exception e) { |
1540 | throw logAndConvert(e); |
1541 | } |
1542 | } |
1543 | |
1544 | /** |
1545 | * Sets the value of a parameter as a Blob. |
1546 | * This method does not close the stream. |
1547 | * The stream may be closed after executing the statement. |
1548 | * |
1549 | * @param parameterIndex the parameter index (1, 2, ...) |
1550 | * @param x the value |
1551 | * @param length the maximum number of bytes |
1552 | * @throws SQLException if this object is closed |
1553 | */ |
1554 | @Override |
1555 | public void setBlob(int parameterIndex, InputStream x, long length) |
1556 | throws SQLException { |
1557 | try { |
1558 | if (isDebugEnabled()) { |
1559 | debugCode("setBlob("+parameterIndex+", x, "+length+"L);"); |
1560 | } |
1561 | checkClosedForWrite(); |
1562 | try { |
1563 | Value v = conn.createBlob(x, length); |
1564 | setParameter(parameterIndex, v); |
1565 | } finally { |
1566 | afterWriting(); |
1567 | } |
1568 | } catch (Exception e) { |
1569 | throw logAndConvert(e); |
1570 | } |
1571 | } |
1572 | |
1573 | /** |
1574 | * Sets the value of a parameter as a Clob. |
1575 | * This method does not close the reader. |
1576 | * The reader may be closed after executing the statement. |
1577 | * |
1578 | * @param parameterIndex the parameter index (1, 2, ...) |
1579 | * @param x the value |
1580 | * @param length the maximum number of characters |
1581 | * @throws SQLException if this object is closed |
1582 | */ |
1583 | @Override |
1584 | public void setNClob(int parameterIndex, Reader x, long length) |
1585 | throws SQLException { |
1586 | try { |
1587 | if (isDebugEnabled()) { |
1588 | debugCode("setNClob("+parameterIndex+", x, "+length+"L);"); |
1589 | } |
1590 | checkClosedForWrite(); |
1591 | try { |
1592 | Value v = conn.createClob(x, length); |
1593 | setParameter(parameterIndex, v); |
1594 | } finally { |
1595 | afterWriting(); |
1596 | } |
1597 | } catch (Exception e) { |
1598 | throw logAndConvert(e); |
1599 | } |
1600 | } |
1601 | |
1602 | /** |
1603 | * [Not supported] Sets the value of a parameter as a SQLXML object. |
1604 | */ |
1605 | @Override |
1606 | public void setSQLXML(int parameterIndex, SQLXML x) throws SQLException { |
1607 | throw unsupported("SQLXML"); |
1608 | } |
1609 | |
1610 | /** |
1611 | * INTERNAL |
1612 | */ |
1613 | @Override |
1614 | public String toString() { |
1615 | return getTraceObjectName() + ": " + command; |
1616 | } |
1617 | |
1618 | @Override |
1619 | protected boolean checkClosed(boolean write) { |
1620 | if (super.checkClosed(write)) { |
1621 | // if the session was re-connected, re-prepare the statement |
1622 | ArrayList<? extends ParameterInterface> oldParams = command.getParameters(); |
1623 | command = conn.prepareCommand(sqlStatement, fetchSize); |
1624 | ArrayList<? extends ParameterInterface> newParams = command.getParameters(); |
1625 | for (int i = 0, size = oldParams.size(); i < size; i++) { |
1626 | ParameterInterface old = oldParams.get(i); |
1627 | Value value = old.getParamValue(); |
1628 | if (value != null) { |
1629 | ParameterInterface n = newParams.get(i); |
1630 | n.setValue(value, false); |
1631 | } |
1632 | } |
1633 | return true; |
1634 | } |
1635 | return false; |
1636 | } |
1637 | |
1638 | } |