Debian Codegraph @

src/main/org/h2/jdbc/JdbcPreparedStatement.java

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