001: /*
002: * Licensed under the X license (see http://www.x.org/terms.htm)
003: */
004: package org.ofbiz.minerva.pool.jdbc;
005:
006: import java.io.InputStream;
007: import java.io.Reader;
008: import java.math.BigDecimal;
009: import java.net.URL;
010: import java.sql.Array;
011: import java.sql.Blob;
012: import java.sql.Clob;
013: import java.sql.Date;
014: import java.sql.ParameterMetaData;
015: import java.sql.PreparedStatement;
016: import java.sql.Ref;
017: import java.sql.ResultSet;
018: import java.sql.ResultSetMetaData;
019: import java.sql.SQLException;
020: import java.sql.Time;
021: import java.sql.Timestamp;
022: import java.util.Calendar;
023:
024: /**
025: * Wrapper around a PreparedStatement that supports error-handling
026: * and caching.
027: *
028: * @author Aaron Mulder ammulder@alumni.princeton.edu
029: */
030: public class PreparedStatementInPool extends StatementInPool implements
031: PreparedStatement {
032:
033: private final static String CLOSED = "PreparedStatement has been closed!";
034: private PreparedStatement impl;
035: private ConnectionWrapper con;
036: private String sql;
037:
038: /**
039: * Creates a new statement from a source statement and wrapper connection.
040: */
041: public PreparedStatementInPool(PreparedStatement source,
042: ConnectionWrapper owner, String sql) {
043: super (source, owner);
044: if (source == null || owner == null)
045: throw new NullPointerException();
046: impl = source;
047: con = owner;
048: this .sql = sql;
049: }
050:
051: /**
052: * Gets a reference to the "real" Statement. This should only be used if
053: * you need to cast that to a specific type to call a proprietary method -
054: * you will defeat all the pooling if you use the underlying Statement
055: * directly.
056: */
057: public PreparedStatement getUnderlyingPreparedStatement() {
058: return impl;
059: }
060:
061: /**
062: * Returns the SQL Statement string.
063: */
064: public String getSql() {
065: return sql;
066: }
067:
068: // ---- Implementation of java.sql.Statement ----
069:
070: public ResultSet executeQuery() throws SQLException {
071: if (impl == null)
072: throw new SQLException(CLOSED);
073: try {
074: setLastUsed();
075: return new ResultSetInPool(impl.executeQuery(), this );
076: } catch (SQLException e) {
077: setError(e);
078: throw e;
079: }
080: }
081:
082: public int executeUpdate() throws SQLException {
083: if (impl == null)
084: throw new SQLException(CLOSED);
085: try {
086: setLastUsed();
087: return impl.executeUpdate();
088: } catch (SQLException e) {
089: setError(e);
090: throw e;
091: }
092: }
093:
094: public void setNull(int parameterIndex, int sqlType)
095: throws SQLException {
096: if (impl == null)
097: throw new SQLException(CLOSED);
098: try {
099: impl.setNull(parameterIndex, sqlType);
100: } catch (SQLException e) {
101: setError(e);
102: throw e;
103: }
104: }
105:
106: public void setBoolean(int parameterIndex, boolean x)
107: throws SQLException {
108: if (impl == null)
109: throw new SQLException(CLOSED);
110: try {
111: impl.setBoolean(parameterIndex, x);
112: } catch (SQLException e) {
113: setError(e);
114: throw e;
115: }
116: }
117:
118: public void setByte(int parameterIndex, byte x) throws SQLException {
119: if (impl == null)
120: throw new SQLException(CLOSED);
121: try {
122: impl.setByte(parameterIndex, x);
123: } catch (SQLException e) {
124: setError(e);
125: throw e;
126: }
127: }
128:
129: public void setShort(int parameterIndex, short x)
130: throws SQLException {
131: if (impl == null)
132: throw new SQLException(CLOSED);
133: try {
134: impl.setShort(parameterIndex, x);
135: } catch (SQLException e) {
136: setError(e);
137: throw e;
138: }
139: }
140:
141: public void setInt(int parameterIndex, int x) throws SQLException {
142: if (impl == null)
143: throw new SQLException(CLOSED);
144: try {
145: impl.setInt(parameterIndex, x);
146: } catch (SQLException e) {
147: setError(e);
148: throw e;
149: }
150: }
151:
152: public void setLong(int parameterIndex, long x) throws SQLException {
153: if (impl == null)
154: throw new SQLException(CLOSED);
155: try {
156: impl.setLong(parameterIndex, x);
157: } catch (SQLException e) {
158: setError(e);
159: throw e;
160: }
161: }
162:
163: public void setFloat(int parameterIndex, float x)
164: throws SQLException {
165: if (impl == null)
166: throw new SQLException(CLOSED);
167: try {
168: impl.setFloat(parameterIndex, x);
169: } catch (SQLException e) {
170: setError(e);
171: throw e;
172: }
173: }
174:
175: public void setDouble(int parameterIndex, double x)
176: throws SQLException {
177: if (impl == null)
178: throw new SQLException(CLOSED);
179: try {
180: impl.setDouble(parameterIndex, x);
181: } catch (SQLException e) {
182: setError(e);
183: throw e;
184: }
185: }
186:
187: public void setBigDecimal(int parameterIndex, BigDecimal x)
188: throws SQLException {
189: if (impl == null)
190: throw new SQLException(CLOSED);
191: try {
192: impl.setBigDecimal(parameterIndex, x);
193: } catch (SQLException e) {
194: setError(e);
195: throw e;
196: }
197: }
198:
199: public void setString(int parameterIndex, String x)
200: throws SQLException {
201: if (impl == null)
202: throw new SQLException(CLOSED);
203: try {
204: impl.setString(parameterIndex, x);
205: } catch (SQLException e) {
206: setError(e);
207: throw e;
208: }
209: }
210:
211: public void setBytes(int parameterIndex, byte[] x)
212: throws SQLException {
213: if (impl == null)
214: throw new SQLException(CLOSED);
215: try {
216: impl.setBytes(parameterIndex, x);
217: } catch (SQLException e) {
218: setError(e);
219: throw e;
220: }
221: }
222:
223: public void setDate(int parameterIndex, Date x) throws SQLException {
224: if (impl == null)
225: throw new SQLException(CLOSED);
226: try {
227: impl.setDate(parameterIndex, x);
228: } catch (SQLException e) {
229: setError(e);
230: throw e;
231: }
232: }
233:
234: public void setTime(int parameterIndex, Time x) throws SQLException {
235: if (impl == null)
236: throw new SQLException(CLOSED);
237: try {
238: impl.setTime(parameterIndex, x);
239: } catch (SQLException e) {
240: setError(e);
241: throw e;
242: }
243: }
244:
245: public void setTimestamp(int parameterIndex, Timestamp x)
246: throws SQLException {
247: if (impl == null)
248: throw new SQLException(CLOSED);
249: try {
250: impl.setTimestamp(parameterIndex, x);
251: } catch (SQLException e) {
252: setError(e);
253: throw e;
254: }
255: }
256:
257: public void setAsciiStream(int parameterIndex, InputStream x,
258: int length) throws SQLException {
259: if (impl == null)
260: throw new SQLException(CLOSED);
261: try {
262: impl.setAsciiStream(parameterIndex, x, length);
263: } catch (SQLException e) {
264: setError(e);
265: throw e;
266: }
267: }
268:
269: public void setUnicodeStream(int parameterIndex, InputStream x,
270: int length) throws SQLException {
271: if (impl == null)
272: throw new SQLException(CLOSED);
273: try {
274: impl.setUnicodeStream(parameterIndex, x, length);
275: } catch (SQLException e) {
276: setError(e);
277: throw e;
278: }
279: }
280:
281: public void setBinaryStream(int parameterIndex, InputStream x,
282: int length) throws SQLException {
283: if (impl == null)
284: throw new SQLException(CLOSED);
285: try {
286: impl.setBinaryStream(parameterIndex, x, length);
287: } catch (SQLException e) {
288: setError(e);
289: throw e;
290: }
291: }
292:
293: public void clearParameters() throws SQLException {
294: if (impl == null)
295: throw new SQLException(CLOSED);
296: try {
297: impl.clearParameters();
298: } catch (SQLException e) {
299: setError(e);
300: throw e;
301: }
302: }
303:
304: public void setObject(int parameterIndex, Object x,
305: int targetSqlType, int scale) throws SQLException {
306: if (impl == null)
307: throw new SQLException(CLOSED);
308: try {
309: impl.setObject(parameterIndex, x, targetSqlType, scale);
310: } catch (SQLException e) {
311: setError(e);
312: throw e;
313: }
314: }
315:
316: public void setObject(int parameterIndex, Object x,
317: int targetSqlType) throws SQLException {
318: if (impl == null)
319: throw new SQLException(CLOSED);
320: try {
321: impl.setObject(parameterIndex, x, targetSqlType);
322: } catch (SQLException e) {
323: setError(e);
324: throw e;
325: }
326: }
327:
328: public void setObject(int parameterIndex, Object x)
329: throws SQLException {
330: if (impl == null)
331: throw new SQLException(CLOSED);
332: try {
333: impl.setObject(parameterIndex, x);
334: } catch (SQLException e) {
335: setError(e);
336: throw e;
337: }
338: }
339:
340: public boolean execute() throws SQLException {
341: if (impl == null)
342: throw new SQLException(CLOSED);
343: try {
344: setLastUsed();
345: return impl.execute();
346: } catch (SQLException e) {
347: setError(e);
348: throw e;
349: }
350: }
351:
352: public void addBatch() throws SQLException {
353: if (impl == null)
354: throw new SQLException(CLOSED);
355: try {
356: impl.addBatch();
357: } catch (SQLException e) {
358: setError(e);
359: throw e;
360: }
361: }
362:
363: public void setCharacterStream(int parameterIndex, Reader reader,
364: int length) throws SQLException {
365: if (impl == null)
366: throw new SQLException(CLOSED);
367: try {
368: impl.setCharacterStream(parameterIndex, reader, length);
369: } catch (SQLException e) {
370: setError(e);
371: throw e;
372: }
373: }
374:
375: public void setRef(int i, Ref x) throws SQLException {
376: if (impl == null)
377: throw new SQLException(CLOSED);
378: try {
379: impl.setRef(i, x);
380: } catch (SQLException e) {
381: setError(e);
382: throw e;
383: }
384: }
385:
386: public void setBlob(int i, Blob x) throws SQLException {
387: if (impl == null)
388: throw new SQLException(CLOSED);
389: try {
390: impl.setBlob(i, x);
391: } catch (SQLException e) {
392: setError(e);
393: throw e;
394: }
395: }
396:
397: public void setClob(int i, Clob x) throws SQLException {
398: if (impl == null)
399: throw new SQLException(CLOSED);
400: try {
401: impl.setClob(i, x);
402: } catch (SQLException e) {
403: setError(e);
404: throw e;
405: }
406: }
407:
408: public void setArray(int i, Array x) throws SQLException {
409: if (impl == null)
410: throw new SQLException(CLOSED);
411: try {
412: impl.setArray(i, x);
413: } catch (SQLException e) {
414: setError(e);
415: throw e;
416: }
417: }
418:
419: public ResultSetMetaData getMetaData() throws SQLException {
420: if (impl == null)
421: throw new SQLException(CLOSED);
422: try {
423: return impl.getMetaData();
424: } catch (SQLException e) {
425: setError(e);
426: throw e;
427: }
428: }
429:
430: public void setDate(int parameterIndex, Date x, Calendar cal)
431: throws SQLException {
432: if (impl == null)
433: throw new SQLException(CLOSED);
434: try {
435: impl.setDate(parameterIndex, x, cal);
436: } catch (SQLException e) {
437: setError(e);
438: throw e;
439: }
440: }
441:
442: public void setTime(int parameterIndex, Time x, Calendar cal)
443: throws SQLException {
444: if (impl == null)
445: throw new SQLException(CLOSED);
446: try {
447: impl.setTime(parameterIndex, x, cal);
448: } catch (SQLException e) {
449: setError(e);
450: throw e;
451: }
452: }
453:
454: public void setTimestamp(int parameterIndex, Timestamp x,
455: Calendar cal) throws SQLException {
456: if (impl == null)
457: throw new SQLException(CLOSED);
458: try {
459: impl.setTimestamp(parameterIndex, x, cal);
460: } catch (SQLException e) {
461: setError(e);
462: throw e;
463: }
464: }
465:
466: public void setNull(int paramIndex, int sqlType, String typeName)
467: throws SQLException {
468: if (impl == null)
469: throw new SQLException(CLOSED);
470: try {
471: impl.setNull(paramIndex, sqlType, typeName);
472: } catch (SQLException e) {
473: setError(e);
474: throw e;
475: }
476: }
477:
478: public void close() throws SQLException {
479: if (con != null) {
480: con.statementClosed(this );
481: }
482: super .clearFields();
483: con = null;
484: impl = null;
485: sql = null;
486: }
487:
488: // JDK 1.4 methods
489:
490: /* (non-Javadoc)
491: * @see java.sql.PreparedStatement#setURL(int, java.net.URL)
492: */
493: public void setURL(int arg0, URL arg1) throws SQLException {
494: // TODO Auto-generated method stub
495:
496: }
497:
498: /* (non-Javadoc)
499: * @see java.sql.PreparedStatement#getParameterMetaData()
500: */
501: public ParameterMetaData getParameterMetaData() throws SQLException {
502: // TODO Auto-generated method stub
503: return null;
504: }
505: }
|