001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.sql;
031:
032: import com.caucho.log.Log;
033: import com.caucho.util.L10N;
034:
035: import java.io.InputStream;
036: import java.io.Reader;
037: import java.math.BigDecimal;
038: import java.net.URL;
039: import java.sql.*;
040: import java.util.Calendar;
041: import java.util.logging.Logger;
042:
043: /**
044: * User-view of prepared statements
045: */
046: public class UserPreparedStatement extends UserStatement implements
047: PreparedStatement {
048: protected final static Logger log = Log
049: .open(UserPreparedStatement.class);
050: protected static L10N L = new L10N(UserPreparedStatement.class);
051:
052: protected PreparedStatement _pstmt;
053: protected PreparedStatementCacheItem _cacheItem;
054:
055: private boolean _isClosed;
056:
057: UserPreparedStatement(UserConnection conn, PreparedStatement pStmt,
058: PreparedStatementCacheItem cacheItem) {
059: super (conn, pStmt);
060:
061: _pstmt = pStmt;
062: _cacheItem = cacheItem;
063:
064: if (pStmt == null)
065: throw new NullPointerException();
066: }
067:
068: UserPreparedStatement(UserConnection conn, PreparedStatement pStmt) {
069: this (conn, pStmt, null);
070: }
071:
072: /**
073: * Returns the underlying statement.
074: */
075: public PreparedStatement getPreparedStatement() {
076: return _pstmt;
077: }
078:
079: /**
080: * Executes the prepared statement's query.
081: */
082: public ResultSet executeQuery() throws SQLException {
083: try {
084: return _pstmt.executeQuery();
085: } catch (RuntimeException e) {
086: killPool();
087: throw e;
088: } catch (SQLException e) {
089: killPool();
090: throw e;
091: }
092: }
093:
094: /**
095: * Executes the prepared statement's sql as an update
096: */
097: public int executeUpdate() throws SQLException {
098: try {
099: return _pstmt.executeUpdate();
100: } catch (RuntimeException e) {
101: killPool();
102: throw e;
103: } catch (SQLException e) {
104: killPool();
105: throw e;
106: }
107: }
108:
109: /**
110: * Executes the prepared statement's sql as an update or query
111: */
112: public boolean execute() throws SQLException {
113: try {
114: return _pstmt.execute();
115: } catch (RuntimeException e) {
116: killPool();
117: throw e;
118: } catch (SQLException e) {
119: killPool();
120: throw e;
121: }
122: }
123:
124: /**
125: * Adds the statement as a batch.
126: */
127: public void addBatch() throws SQLException {
128: try {
129: _pstmt.addBatch();
130: } catch (RuntimeException e) {
131: killPool();
132: throw e;
133: } catch (SQLException e) {
134: killPool();
135: throw e;
136: }
137: }
138:
139: /**
140: * Clears the statement's parameters.
141: */
142: public void clearParameters() throws SQLException {
143: try {
144: _pstmt.clearParameters();
145: } catch (RuntimeException e) {
146: killPool();
147: throw e;
148: } catch (SQLException e) {
149: killPool();
150: throw e;
151: }
152: }
153:
154: /**
155: * Returns the result metadata.
156: */
157: public ResultSetMetaData getMetaData() throws SQLException {
158: try {
159: return _pstmt.getMetaData();
160: } catch (RuntimeException e) {
161: killPool();
162: throw e;
163: } catch (SQLException e) {
164: killPool();
165: throw e;
166: }
167: }
168:
169: /**
170: * Returns the prepared statement's meta data.
171: */
172: public ParameterMetaData getParameterMetaData() throws SQLException {
173: try {
174: return _pstmt.getParameterMetaData();
175: } catch (RuntimeException e) {
176: killPool();
177: throw e;
178: } catch (SQLException e) {
179: killPool();
180: throw e;
181: }
182: }
183:
184: /**
185: * Sets the parameter as a null.
186: */
187: public void setNull(int parameterIndex, int sqlType)
188: throws SQLException {
189: try {
190: _pstmt.setNull(parameterIndex, sqlType);
191: } catch (RuntimeException e) {
192: killPool();
193: throw e;
194: } catch (SQLException e) {
195: killPool();
196: throw e;
197: }
198: }
199:
200: /**
201: * Sets the parameter as a null.
202: */
203: public void setNull(int parameterIndex, int sqlType, String typeName)
204: throws SQLException {
205: try {
206: _pstmt.setNull(parameterIndex, sqlType, typeName);
207: } catch (RuntimeException e) {
208: killPool();
209: throw e;
210: } catch (SQLException e) {
211: killPool();
212: throw e;
213: }
214: }
215:
216: /**
217: * Sets the parameter as a boolean.
218: */
219: public void setBoolean(int index, boolean value)
220: throws SQLException {
221: try {
222: _pstmt.setBoolean(index, value);
223: } catch (RuntimeException e) {
224: killPool();
225: throw e;
226: } catch (SQLException e) {
227: killPool();
228: throw e;
229: }
230: }
231:
232: /**
233: * Sets the parameter as a byte.
234: */
235: public void setByte(int index, byte value) throws SQLException {
236: try {
237: _pstmt.setByte(index, value);
238: } catch (RuntimeException e) {
239: killPool();
240: throw e;
241: } catch (SQLException e) {
242: killPool();
243: throw e;
244: }
245: }
246:
247: /**
248: * Sets the parameter as a short.
249: */
250: public void setShort(int index, short value) throws SQLException {
251: try {
252: _pstmt.setShort(index, value);
253: } catch (RuntimeException e) {
254: killPool();
255: throw e;
256: } catch (SQLException e) {
257: killPool();
258: throw e;
259: }
260: }
261:
262: /**
263: * Sets the parameter as an int
264: */
265: public void setInt(int index, int value) throws SQLException {
266: try {
267: _pstmt.setInt(index, value);
268: } catch (RuntimeException e) {
269: killPool();
270: throw e;
271: } catch (SQLException e) {
272: killPool();
273: throw e;
274: }
275: }
276:
277: /**
278: * Sets the parameter as a long
279: */
280: public void setLong(int index, long value) throws SQLException {
281: try {
282: _pstmt.setLong(index, value);
283: } catch (RuntimeException e) {
284: killPool();
285: throw e;
286: } catch (SQLException e) {
287: killPool();
288: throw e;
289: }
290: }
291:
292: /**
293: * Sets the parameter as a float
294: */
295: public void setFloat(int index, float value) throws SQLException {
296: try {
297: _pstmt.setFloat(index, value);
298: } catch (RuntimeException e) {
299: killPool();
300: throw e;
301: } catch (SQLException e) {
302: killPool();
303: throw e;
304: }
305: }
306:
307: /**
308: * Sets the parameter as a double
309: */
310: public void setDouble(int index, double value) throws SQLException {
311: try {
312: _pstmt.setDouble(index, value);
313: } catch (RuntimeException e) {
314: killPool();
315: throw e;
316: } catch (SQLException e) {
317: killPool();
318: throw e;
319: }
320: }
321:
322: /**
323: * Sets the parameter as a BigDecimal
324: */
325: public void setBigDecimal(int index, BigDecimal value)
326: throws SQLException {
327: try {
328: _pstmt.setBigDecimal(index, value);
329: } catch (RuntimeException e) {
330: killPool();
331: throw e;
332: } catch (SQLException e) {
333: killPool();
334: throw e;
335: }
336: }
337:
338: /**
339: * Sets the parameter as a string
340: */
341: public void setString(int index, String value) throws SQLException {
342: try {
343: _pstmt.setString(index, value);
344: } catch (RuntimeException e) {
345: killPool();
346: throw e;
347: } catch (SQLException e) {
348: killPool();
349: throw e;
350: }
351: }
352:
353: /**
354: * Sets the parameter as a byte array.
355: */
356: public void setBytes(int index, byte[] value) throws SQLException {
357: try {
358: _pstmt.setBytes(index, value);
359: } catch (RuntimeException e) {
360: killPool();
361: throw e;
362: } catch (SQLException e) {
363: killPool();
364: throw e;
365: }
366: }
367:
368: /**
369: * Sets the parameter as a date
370: */
371: public void setDate(int index, Date value) throws SQLException {
372: try {
373: _pstmt.setDate(index, value);
374: } catch (RuntimeException e) {
375: killPool();
376: throw e;
377: } catch (SQLException e) {
378: killPool();
379: throw e;
380: }
381: }
382:
383: /**
384: * Sets the parameter as a time
385: */
386: public void setDate(int index, Date value, Calendar cal)
387: throws SQLException {
388: try {
389: _pstmt.setDate(index, value, cal);
390: } catch (RuntimeException e) {
391: killPool();
392: throw e;
393: } catch (SQLException e) {
394: killPool();
395: throw e;
396: }
397: }
398:
399: /**
400: * Sets the parameter as a time
401: */
402: public void setTime(int index, Time value) throws SQLException {
403: try {
404: _pstmt.setTime(index, value);
405: } catch (RuntimeException e) {
406: killPool();
407: throw e;
408: } catch (SQLException e) {
409: killPool();
410: throw e;
411: }
412: }
413:
414: /**
415: * Sets the parameter as a time
416: */
417: public void setTime(int index, Time value, Calendar cal)
418: throws SQLException {
419: try {
420: _pstmt.setTime(index, value, cal);
421: } catch (RuntimeException e) {
422: killPool();
423: throw e;
424: } catch (SQLException e) {
425: killPool();
426: throw e;
427: }
428: }
429:
430: /**
431: * Sets the parameter as a timestamp
432: */
433: public void setTimestamp(int index, Timestamp value)
434: throws SQLException {
435: try {
436: _pstmt.setTimestamp(index, value);
437: } catch (RuntimeException e) {
438: killPool();
439: throw e;
440: } catch (SQLException e) {
441: killPool();
442: throw e;
443: }
444: }
445:
446: /**
447: * Sets the parameter as a timestamp
448: */
449: public void setTimestamp(int index, Timestamp value, Calendar cal)
450: throws SQLException {
451: try {
452: _pstmt.setTimestamp(index, value, cal);
453: } catch (RuntimeException e) {
454: killPool();
455: throw e;
456: } catch (SQLException e) {
457: killPool();
458: throw e;
459: }
460: }
461:
462: /**
463: * Sets the parameter as an ascii stream.
464: */
465: public void setAsciiStream(int index, InputStream value, int length)
466: throws SQLException {
467: try {
468: _pstmt.setAsciiStream(index, value, length);
469: } catch (RuntimeException e) {
470: killPool();
471: throw e;
472: } catch (SQLException e) {
473: killPool();
474: throw e;
475: }
476: }
477:
478: /**
479: * Sets the parameter as a unicode stream.
480: */
481: public void setUnicodeStream(int index, InputStream value,
482: int length) throws SQLException {
483: try {
484: _pstmt.setUnicodeStream(index, value, length);
485: } catch (RuntimeException e) {
486: killPool();
487: throw e;
488: } catch (SQLException e) {
489: killPool();
490: throw e;
491: }
492: }
493:
494: /**
495: * Sets the parameter as a binary stream.
496: */
497: public void setBinaryStream(int index, InputStream value, int length)
498: throws SQLException {
499: try {
500: _pstmt.setBinaryStream(index, value, length);
501: } catch (RuntimeException e) {
502: killPool();
503: throw e;
504: } catch (SQLException e) {
505: killPool();
506: throw e;
507: }
508: }
509:
510: /**
511: * Sets the parameter as an character stream.
512: */
513: public void setCharacterStream(int index, Reader value, int length)
514: throws SQLException {
515: try {
516: _pstmt.setCharacterStream(index, value, length);
517: } catch (RuntimeException e) {
518: killPool();
519: throw e;
520: } catch (SQLException e) {
521: killPool();
522: throw e;
523: }
524: }
525:
526: /**
527: * Sets the parameter as an object with the given type and scale.
528: */
529: public void setObject(int index, Object value, int type, int scale)
530: throws SQLException {
531: try {
532: _pstmt.setObject(index, value, type, scale);
533: } catch (RuntimeException e) {
534: killPool();
535: throw e;
536: } catch (SQLException e) {
537: killPool();
538: throw e;
539: }
540: }
541:
542: /**
543: * Sets the parameter as an object with the given type.
544: */
545: public void setObject(int index, Object value, int type)
546: throws SQLException {
547: try {
548: _pstmt.setObject(index, value, type);
549: } catch (RuntimeException e) {
550: killPool();
551: throw e;
552: } catch (SQLException e) {
553: killPool();
554: throw e;
555: }
556: }
557:
558: /**
559: * Sets the parameter as a object.
560: */
561: public void setObject(int index, Object value) throws SQLException {
562: try {
563: _pstmt.setObject(index, value);
564: } catch (RuntimeException e) {
565: killPool();
566: throw e;
567: } catch (SQLException e) {
568: killPool();
569: throw e;
570: }
571: }
572:
573: /**
574: * Sets teh parameter as a ref.
575: */
576: public void setRef(int index, Ref value) throws SQLException {
577: try {
578: _pstmt.setRef(index, value);
579: } catch (RuntimeException e) {
580: killPool();
581: throw e;
582: } catch (SQLException e) {
583: killPool();
584: throw e;
585: }
586: }
587:
588: /**
589: * Sets the parameter as a blob.
590: */
591: public void setBlob(int index, Blob value) throws SQLException {
592: try {
593: _pstmt.setBlob(index, value);
594: } catch (RuntimeException e) {
595: killPool();
596: throw e;
597: } catch (SQLException e) {
598: killPool();
599: throw e;
600: }
601: }
602:
603: /**
604: * Sets the parameter as a clob.
605: */
606: public void setClob(int index, Clob value) throws SQLException {
607: try {
608: _pstmt.setClob(index, value);
609: } catch (RuntimeException e) {
610: killPool();
611: throw e;
612: } catch (SQLException e) {
613: killPool();
614: throw e;
615: }
616: }
617:
618: /**
619: * Sets the parameter as an array
620: */
621: public void setArray(int index, Array value) throws SQLException {
622: try {
623: _pstmt.setArray(index, value);
624: } catch (RuntimeException e) {
625: killPool();
626: throw e;
627: } catch (SQLException e) {
628: killPool();
629: throw e;
630: }
631: }
632:
633: /**
634: * Sets the parameter as a URL.
635: */
636: public void setURL(int index, URL value) throws SQLException {
637: try {
638: _pstmt.setURL(index, value);
639: } catch (RuntimeException e) {
640: killPool();
641: throw e;
642: } catch (SQLException e) {
643: killPool();
644: throw e;
645: }
646: }
647:
648: /**
649: * Closes the prepared statement.
650: */
651: public void close() throws SQLException {
652: synchronized (this ) {
653: if (_isClosed)
654: return;
655: _isClosed = true;
656: }
657:
658: clearParameters();
659:
660: if (_cacheItem == null)
661: super .close();
662: else if (!isPoolable())
663: _cacheItem.destroy();
664: else
665: _cacheItem.toIdle();
666: }
667:
668: public String toString() {
669: return "UserPreparedStatement[" + _pstmt + "]";
670: }
671:
672: public void setRowId(int parameterIndex, RowId x)
673: throws SQLException {
674: throw new UnsupportedOperationException("Not supported yet.");
675: }
676:
677: public void setNString(int parameterIndex, String value)
678: throws SQLException {
679: throw new UnsupportedOperationException("Not supported yet.");
680: }
681:
682: public void setNCharacterStream(int parameterIndex, Reader value,
683: long length) throws SQLException {
684: throw new UnsupportedOperationException("Not supported yet.");
685: }
686:
687: public void setNClob(int parameterIndex, NClob value)
688: throws SQLException {
689: throw new UnsupportedOperationException("Not supported yet.");
690: }
691:
692: public void setClob(int parameterIndex, Reader reader, long length)
693: throws SQLException {
694: throw new UnsupportedOperationException("Not supported yet.");
695: }
696:
697: public void setBlob(int parameterIndex, InputStream inputStream,
698: long length) throws SQLException {
699: throw new UnsupportedOperationException("Not supported yet.");
700: }
701:
702: public void setNClob(int parameterIndex, Reader reader, long length)
703: throws SQLException {
704: throw new UnsupportedOperationException("Not supported yet.");
705: }
706:
707: public void setSQLXML(int parameterIndex, SQLXML xmlObject)
708: throws SQLException {
709: throw new UnsupportedOperationException("Not supported yet.");
710: }
711:
712: public void setAsciiStream(int parameterIndex, InputStream x,
713: long length) throws SQLException {
714: throw new UnsupportedOperationException("Not supported yet.");
715: }
716:
717: public void setBinaryStream(int parameterIndex, InputStream x,
718: long length) throws SQLException {
719: throw new UnsupportedOperationException("Not supported yet.");
720: }
721:
722: public void setCharacterStream(int parameterIndex, Reader reader,
723: long length) throws SQLException {
724: throw new UnsupportedOperationException("Not supported yet.");
725: }
726:
727: public void setAsciiStream(int parameterIndex, InputStream x)
728: throws SQLException {
729: throw new UnsupportedOperationException("Not supported yet.");
730: }
731:
732: public void setBinaryStream(int parameterIndex, InputStream x)
733: throws SQLException {
734: throw new UnsupportedOperationException("Not supported yet.");
735: }
736:
737: public void setCharacterStream(int parameterIndex, Reader reader)
738: throws SQLException {
739: throw new UnsupportedOperationException("Not supported yet.");
740: }
741:
742: public void setNCharacterStream(int parameterIndex, Reader value)
743: throws SQLException {
744: throw new UnsupportedOperationException("Not supported yet.");
745: }
746:
747: public void setClob(int parameterIndex, Reader reader)
748: throws SQLException {
749: throw new UnsupportedOperationException("Not supported yet.");
750: }
751:
752: public void setBlob(int parameterIndex, InputStream inputStream)
753: throws SQLException {
754: throw new UnsupportedOperationException("Not supported yet.");
755: }
756:
757: public void setNClob(int parameterIndex, Reader reader)
758: throws SQLException {
759: throw new UnsupportedOperationException("Not supported yet.");
760: }
761: }
|