001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.sql;
024:
025: import java.sql.Connection;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028: import java.sql.SQLWarning;
029: import java.sql.Statement;
030: import java.util.Iterator;
031: import java.util.Vector;
032:
033: import org.isqlviewer.util.LocalMessages;
034:
035: /**
036: * Wrapper class for generic statements produced by the underlying connection object.
037: * <p>
038: *
039: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
040: * @version 1.0
041: */
042: class StatementWrapper implements Statement {
043:
044: protected static final LocalMessages messages = new LocalMessages(
045: ConnectionWrapper.BUNDLE_NAME);
046: protected Statement stmt;
047: protected ConnectionWrapper connection;
048: protected String lastStatement;
049: protected Vector<ResultSetWrapper> openCursors = new Vector<ResultSetWrapper>();
050:
051: public StatementWrapper(ConnectionWrapper connection,
052: Statement stmt, String currentSQL) {
053:
054: this .stmt = stmt;
055: this .connection = connection;
056: this .lastStatement = currentSQL;
057: }
058:
059: public ConnectionWrapper getOwner() {
060:
061: return connection;
062: }
063:
064: public Statement getParent() {
065:
066: return stmt;
067: }
068:
069: public void close() throws SQLException {
070:
071: connection.updateLastAccess();
072: try {
073: stmt.setMaxRows(0);
074: } catch (Throwable t) {
075: error(messages.format("DataSource.GeneralMethodError",
076: "setMaxRows()"), t);
077: }
078: try {
079: stmt.clearWarnings();
080: } catch (Throwable t) {
081: error(messages.format("DataSource.GeneralMethodError",
082: "clearWarnings()"), t);
083: }
084: try {
085: stmt.clearBatch();
086: } catch (Throwable t) {
087: error(messages.format("DataSource.GeneralMethodError",
088: "clearBatch()"), t);
089: }
090: if (!openCursors.isEmpty()) {
091: Iterator itr = openCursors.iterator();
092: warn(messages.format("DataSource.DirtyCursorsError"));
093: while (itr.hasNext()) {
094: ResultSetWrapper next = (ResultSetWrapper) itr.next();
095: itr.remove();
096: try {
097: next.close();
098: } catch (Throwable t) {
099: }
100: try {
101: StatementWrapper ps = (StatementWrapper) next
102: .getStatement();
103: if (ps != this ) {
104: ps.stmt.close();
105: }
106: } catch (Throwable t) {
107: }
108: }
109: }
110: }
111:
112: public ResultSet executeQuery(String sql) throws SQLException {
113:
114: connection.updateLastAccess();
115: try {
116: lastStatement = sql;
117: String nsql = connection.nativeSQL(sql);
118: info(messages.format("DataSource.StatementExec", nsql));
119: ResultSetWrapper wrs = new ResultSetWrapper(this , stmt
120: .executeQuery(sql));
121: openCursors.add(wrs);
122: return wrs;
123: } catch (SQLException sqle) {
124: error(messages.format("DataSource.GeneralMethodError",
125: "executeQuery(String)"), sqle);
126: throw sqle;
127: } catch (Throwable t) {
128: SQLException sqle = connection.createWrappedSQLException(t,
129: "executeQuery(String)");
130: throw sqle;
131: }
132: }
133:
134: public boolean execute(String sql) throws SQLException {
135:
136: connection.updateLastAccess();
137: try {
138: lastStatement = sql;
139: return stmt.execute(sql);
140: } catch (SQLException sqle) {
141: error(messages.format("DataSource.GeneralMethodError",
142: "getResultSet()"), sqle);
143: throw sqle;
144: } catch (Throwable t) {
145: SQLException sqle = connection.createWrappedSQLException(t,
146: "getResultSet()");
147: throw sqle;
148: }
149: }
150:
151: public boolean execute(String sql, int autoGeneratedKeys)
152: throws SQLException {
153:
154: connection.updateLastAccess();
155: try {
156: lastStatement = sql;
157: return stmt.execute(sql, autoGeneratedKeys);
158: } catch (SQLException sqle) {
159: error(messages.format("DataSource.GeneralMethodError",
160: "execute(String,int)"), sqle);
161: throw sqle;
162: } catch (Throwable t) {
163: SQLException sqle = connection.createWrappedSQLException(t,
164: "execute(String,int)");
165: throw sqle;
166: }
167: }
168:
169: public boolean execute(String sql, int[] columnIndexes)
170: throws SQLException {
171:
172: connection.updateLastAccess();
173: try {
174: lastStatement = sql;
175: return stmt.execute(sql, columnIndexes);
176: } catch (SQLException sqle) {
177: error(messages.format("DataSource.GeneralMethodError",
178: "execute(String,int[])"), sqle);
179: throw sqle;
180: } catch (Throwable t) {
181: SQLException sqle = connection.createWrappedSQLException(t,
182: "execute(String,int[])");
183: throw sqle;
184: }
185: }
186:
187: public boolean execute(String sql, String[] columnNames)
188: throws SQLException {
189:
190: connection.updateLastAccess();
191: try {
192: lastStatement = sql;
193: return stmt.execute(sql, columnNames);
194: } catch (SQLException sqle) {
195: error(messages.format("DataSource.GeneralMethodError",
196: "execute(String,String[])"), sqle);
197: throw sqle;
198: } catch (Throwable t) {
199: SQLException sqle = connection.createWrappedSQLException(t,
200: "execute(String,String[])");
201: throw sqle;
202: }
203: }
204:
205: public int executeUpdate(String sql, int autoGeneratedKeys)
206: throws SQLException {
207:
208: connection.updateLastAccess();
209: try {
210: lastStatement = sql;
211: return stmt.executeUpdate(sql, autoGeneratedKeys);
212: } catch (SQLException sqle) {
213: error(messages.format("DataSource.GeneralMethodError",
214: "executeUpdate(String,int)"), sqle);
215: throw sqle;
216: } catch (Throwable t) {
217: SQLException sqle = connection.createWrappedSQLException(t,
218: "executeUpdate(String,int)");
219: throw sqle;
220: }
221: }
222:
223: public int executeUpdate(String sql, int[] columnIndexes)
224: throws SQLException {
225:
226: connection.updateLastAccess();
227: try {
228: lastStatement = sql;
229: return stmt.executeUpdate(sql, columnIndexes);
230: } catch (SQLException sqle) {
231: error(messages.format("DataSource.GeneralMethodError",
232: "executeUpdate(String,int[])"), sqle);
233: throw sqle;
234: } catch (Throwable t) {
235: SQLException sqle = connection.createWrappedSQLException(t,
236: "executeUpdate(String,int[])");
237: throw sqle;
238: }
239: }
240:
241: public int executeUpdate(String sql, String[] columnNames)
242: throws SQLException {
243:
244: connection.updateLastAccess();
245: try {
246: lastStatement = sql;
247: return stmt.executeUpdate(sql, columnNames);
248: } catch (SQLException sqle) {
249: error(messages.format("DataSource.GeneralMethodError",
250: "executeUpdate(String,String[])"), sqle);
251: throw sqle;
252: } catch (Throwable t) {
253: SQLException sqle = connection.createWrappedSQLException(t,
254: "executeUpdate(String,String[])");
255: throw sqle;
256: }
257: }
258:
259: public int executeUpdate(String sql) throws SQLException {
260:
261: connection.updateLastAccess();
262: try {
263: lastStatement = sql;
264: return stmt.executeUpdate(sql);
265: } catch (SQLException sqle) {
266: error(messages.format("DataSource.GeneralMethodError",
267: "executeUpdate(String)"), sqle);
268: throw sqle;
269: } catch (Throwable t) {
270: SQLException sqle = connection.createWrappedSQLException(t,
271: "executeUpdate(String)");
272: throw sqle;
273: }
274: }
275:
276: public int getMaxFieldSize() throws SQLException {
277:
278: connection.updateLastAccess();
279: try {
280: return stmt.getMaxFieldSize();
281: } catch (SQLException sqle) {
282: error(messages.format("DataSource.GeneralMethodError",
283: "getMaxFieldSize(int)"), sqle);
284: throw sqle;
285: } catch (Throwable t) {
286: SQLException sqle = connection.createWrappedSQLException(t,
287: "getMaxFieldSize(int)");
288: throw sqle;
289: }
290: }
291:
292: public void setMaxFieldSize(int max) throws SQLException {
293:
294: connection.updateLastAccess();
295: try {
296: stmt.setMaxFieldSize(max);
297: } catch (SQLException sqle) {
298: error(messages.format("DataSource.GeneralMethodError",
299: "setMaxFieldSize(int)"), sqle);
300: throw sqle;
301: } catch (Throwable t) {
302: SQLException sqle = connection.createWrappedSQLException(t,
303: "setMaxFieldSize(int)");
304: throw sqle;
305: }
306: }
307:
308: public int getMaxRows() throws SQLException {
309:
310: connection.updateLastAccess();
311: try {
312: return stmt.getMaxRows();
313: } catch (SQLException sqle) {
314: error(messages.format("DataSource.GeneralMethodError",
315: "getMaxRows()"), sqle);
316: throw sqle;
317: } catch (Throwable t) {
318: SQLException sqle = connection.createWrappedSQLException(t,
319: "getMaxRows()");
320: throw sqle;
321: }
322: }
323:
324: public void setMaxRows(int max) throws SQLException {
325:
326: connection.updateLastAccess();
327: try {
328: stmt.setMaxRows(max);
329: } catch (SQLException sqle) {
330: error(messages.format("DataSource.GeneralMethodError",
331: "setMaxRows(int)"), sqle);
332: throw sqle;
333: } catch (Throwable t) {
334: SQLException sqle = connection.createWrappedSQLException(t,
335: "setMaxRows(int)");
336: throw sqle;
337: }
338: }
339:
340: public void setEscapeProcessing(boolean enable) throws SQLException {
341:
342: connection.updateLastAccess();
343: try {
344: stmt.setEscapeProcessing(enable);
345: } catch (SQLException sqle) {
346: error(messages.format("DataSource.GeneralMethodError",
347: "setEscapeProcessing(boolean)"), sqle);
348: throw sqle;
349: } catch (Throwable t) {
350: SQLException sqle = connection.createWrappedSQLException(t,
351: "setEscapeProcessing(boolean)");
352: throw sqle;
353: }
354: }
355:
356: public int getQueryTimeout() throws SQLException {
357:
358: connection.updateLastAccess();
359: try {
360: return stmt.getQueryTimeout();
361: } catch (SQLException sqle) {
362: error(messages.format("DataSource.GeneralMethodError",
363: "getQueryTimeout()"), sqle);
364: throw sqle;
365: } catch (Throwable t) {
366: SQLException sqle = connection.createWrappedSQLException(t,
367: "getQueryTimeout()");
368: throw sqle;
369: }
370: }
371:
372: public void setQueryTimeout(int seconds) throws SQLException {
373:
374: connection.updateLastAccess();
375: try {
376: stmt.setQueryTimeout(seconds);
377: } catch (SQLException sqle) {
378: error(messages.format("DataSource.GeneralMethodError",
379: "setQueryTimeout(int)"), sqle);
380: throw sqle;
381: } catch (Throwable t) {
382: SQLException sqle = connection.createWrappedSQLException(t,
383: "setQueryTimeout(int)");
384: throw sqle;
385: }
386: }
387:
388: public void cancel() throws SQLException {
389:
390: connection.updateLastAccess();
391: try {
392: stmt.cancel();
393: } catch (SQLException sqle) {
394: error(messages.format("DataSource.GeneralMethodError",
395: "cancel()"), sqle);
396: throw sqle;
397: } catch (Throwable t) {
398: SQLException sqle = connection.createWrappedSQLException(t,
399: "cancel()");
400: throw sqle;
401: }
402: }
403:
404: public SQLWarning getWarnings() throws SQLException {
405:
406: connection.updateLastAccess();
407: try {
408: return stmt.getWarnings();
409: } catch (SQLException sqle) {
410: error(messages.format("DataSource.GeneralMethodError",
411: "getWarnings()"), sqle);
412: throw sqle;
413: } catch (Throwable t) {
414: SQLException sqle = connection.createWrappedSQLException(t,
415: "getWarnings()");
416: throw sqle;
417: }
418: }
419:
420: public void clearWarnings() throws SQLException {
421:
422: connection.updateLastAccess();
423: try {
424: stmt.clearWarnings();
425: } catch (SQLException sqle) {
426: error(messages.format("DataSource.GeneralMethodError",
427: "clearWarnings()"), sqle);
428: throw sqle;
429: } catch (Throwable t) {
430: SQLException sqle = connection.createWrappedSQLException(t,
431: "clearWarnings()");
432: throw sqle;
433: }
434: }
435:
436: public void setCursorName(String name) throws SQLException {
437:
438: connection.updateLastAccess();
439: try {
440: stmt.setCursorName(name);
441: } catch (SQLException sqle) {
442: error(messages.format("DataSource.GeneralMethodError",
443: "setCursorName(String)"), sqle);
444: throw sqle;
445: } catch (Throwable t) {
446: SQLException sqle = connection.createWrappedSQLException(t,
447: "setCursorName(String)");
448: throw sqle;
449: }
450: }
451:
452: public ResultSet getResultSet() throws SQLException {
453:
454: connection.updateLastAccess();
455: try {
456: ResultSetWrapper wrs = new ResultSetWrapper(this , stmt
457: .getResultSet());
458: openCursors.add(wrs);
459: return wrs;
460: } catch (SQLException sqle) {
461: error(messages.format("DataSource.GeneralMethodError",
462: "getResultSet()"), sqle);
463: throw sqle;
464: } catch (Throwable t) {
465: SQLException sqle = connection.createWrappedSQLException(t,
466: "getResultSet()");
467: throw sqle;
468: }
469: }
470:
471: public int getUpdateCount() throws SQLException {
472:
473: connection.updateLastAccess();
474: try {
475: return stmt.getUpdateCount();
476: } catch (SQLException sqle) {
477: error(messages.format("DataSource.GeneralMethodError",
478: "getUpdateCount()"), sqle);
479: throw sqle;
480: } catch (Throwable t) {
481: SQLException sqle = connection.createWrappedSQLException(t,
482: "getUpdateCount()");
483: throw sqle;
484: }
485: }
486:
487: public boolean getMoreResults() throws SQLException {
488:
489: connection.updateLastAccess();
490: try {
491: return stmt.getMoreResults();
492: } catch (SQLException sqle) {
493: error(messages.format("DataSource.GeneralMethodError",
494: "getMoreResults()"), sqle);
495: throw sqle;
496: } catch (Throwable t) {
497: SQLException sqle = connection.createWrappedSQLException(t,
498: "getMoreResults()");
499: throw sqle;
500: }
501: }
502:
503: public int getFetchDirection() throws SQLException {
504:
505: connection.updateLastAccess();
506: try {
507: return stmt.getFetchDirection();
508: } catch (SQLException sqle) {
509: error(messages.format("DataSource.GeneralMethodError",
510: "getFetchDirection()"), sqle);
511: throw sqle;
512: } catch (Throwable t) {
513: SQLException sqle = connection.createWrappedSQLException(t,
514: "getFetchDirection()");
515: throw sqle;
516: }
517: }
518:
519: public int[] executeBatch() throws SQLException {
520:
521: connection.updateLastAccess();
522: try {
523: return stmt.executeBatch();
524: } catch (SQLException sqle) {
525: error(messages.format("DataSource.GeneralMethodError",
526: "executeBatch()"), sqle);
527: throw sqle;
528: } catch (Throwable t) {
529: SQLException sqle = connection.createWrappedSQLException(t,
530: "executeBatch()");
531: throw sqle;
532: }
533: }
534:
535: public void setFetchSize(int i) throws SQLException {
536:
537: connection.updateLastAccess();
538: try {
539: stmt.setFetchSize(i);
540: } catch (SQLException sqle) {
541: error(messages.format("DataSource.GeneralMethodError",
542: "setFetchSize(int)"), sqle);
543: throw sqle;
544: } catch (Throwable t) {
545: SQLException sqle = connection.createWrappedSQLException(t,
546: "setFetchSize(int)");
547: throw sqle;
548: }
549: }
550:
551: public void clearBatch() throws SQLException {
552:
553: connection.updateLastAccess();
554: try {
555: stmt.clearBatch();
556: } catch (SQLException sqle) {
557: error(messages.format("DataSource.GeneralMethodError",
558: "clearBatch()"), sqle);
559: throw sqle;
560: } catch (Throwable t) {
561: SQLException sqle = connection.createWrappedSQLException(t,
562: "clearBatch()");
563: throw sqle;
564: }
565: }
566:
567: public void addBatch(String s) throws SQLException {
568:
569: connection.updateLastAccess();
570: try {
571: stmt.addBatch(s);
572: } catch (SQLException sqle) {
573: error(messages.format("DataSource.GeneralMethodError",
574: "addBatch(String)"), sqle);
575: throw sqle;
576: } catch (Throwable t) {
577: SQLException sqle = connection.createWrappedSQLException(t,
578: "addBatch(String)");
579: throw sqle;
580: }
581: }
582:
583: public int getResultSetConcurrency() throws SQLException {
584:
585: connection.updateLastAccess();
586: try {
587: return stmt.getResultSetConcurrency();
588: } catch (SQLException sqle) {
589: error(messages.format("DataSource.GeneralMethodError",
590: "getResultSetConcurrency()"), sqle);
591: throw sqle;
592: } catch (Throwable t) {
593: SQLException sqle = connection.createWrappedSQLException(t,
594: "getResultSetConcurrency()");
595: throw sqle;
596: }
597: }
598:
599: public void setFetchDirection(int i) throws SQLException {
600:
601: connection.updateLastAccess();
602: try {
603: stmt.setFetchDirection(i);
604: } catch (SQLException sqle) {
605: error(messages.format("DataSource.GeneralMethodError",
606: "setFetchDirection(int)"), sqle);
607: throw sqle;
608: } catch (Throwable t) {
609: SQLException sqle = connection.createWrappedSQLException(t,
610: "setFetchDirection(int)");
611: throw sqle;
612: }
613: }
614:
615: public int getFetchSize() throws SQLException {
616:
617: connection.updateLastAccess();
618: try {
619: return stmt.getFetchSize();
620: } catch (SQLException sqle) {
621: error(messages.format("DataSource.GeneralMethodError",
622: "getFetchSize()"), sqle);
623: throw sqle;
624: } catch (Throwable t) {
625: SQLException sqle = connection.createWrappedSQLException(t,
626: "getFetchSize()");
627: throw sqle;
628: }
629: }
630:
631: public Connection getConnection() throws SQLException {
632:
633: return connection;
634: }
635:
636: public int getResultSetType() throws SQLException {
637:
638: connection.updateLastAccess();
639: try {
640: return stmt.getResultSetType();
641: } catch (SQLException sqle) {
642: error(messages.format("DataSource.GeneralMethodError",
643: "getResultSetType()"), sqle);
644: throw sqle;
645: } catch (Throwable t) {
646: SQLException sqle = connection.createWrappedSQLException(t,
647: "getResultSetType()");
648: throw sqle;
649: }
650: }
651:
652: public ResultSet getGeneratedKeys() throws SQLException {
653:
654: connection.updateLastAccess();
655: try {
656: ResultSetWrapper wrs = new ResultSetWrapper(this , stmt
657: .getGeneratedKeys());
658: openCursors.add(wrs);
659: return wrs;
660: } catch (SQLException sqle) {
661: error(messages.format("DataSource.GeneralMethodError",
662: "getGeneratedKeys()"), sqle);
663: throw sqle;
664: } catch (Throwable t) {
665: SQLException sqle = connection.createWrappedSQLException(t,
666: "getGeneratedKeys()");
667: throw sqle;
668: }
669: }
670:
671: public boolean getMoreResults(int current) throws SQLException {
672:
673: connection.updateLastAccess();
674: try {
675: return stmt.getMoreResults(current);
676: } catch (SQLException sqle) {
677: error(messages.format("DataSource.GeneralMethodError",
678: "getMoreResults(int)"), sqle);
679: throw sqle;
680: } catch (Throwable t) {
681: SQLException sqle = connection.createWrappedSQLException(t,
682: "getMoreResults(int)");
683: throw sqle;
684: }
685: }
686:
687: public int getResultSetHoldability() throws SQLException {
688:
689: connection.updateLastAccess();
690: try {
691: return stmt.getResultSetHoldability();
692: } catch (SQLException sqle) {
693: error(messages.format("DataSource.GeneralMethodError",
694: "getResultSetHoldability()"), sqle);
695: throw sqle;
696: } catch (Throwable t) {
697: SQLException sqle = connection.createWrappedSQLException(t,
698: "getResultSetHoldability()");
699: throw sqle;
700: }
701: }
702:
703: protected void info(Object message, Throwable error) {
704:
705: connection.info(message, error);
706: }
707:
708: protected void info(Object message) {
709:
710: connection.info(message);
711: }
712:
713: protected void debug(Object message, Throwable error) {
714:
715: connection.trace(message, error);
716: }
717:
718: protected void debug(Object message) {
719:
720: connection.trace(message);
721: }
722:
723: protected void error(Object message, Throwable error) {
724:
725: connection.error(message, error);
726: }
727:
728: protected void error(Object message) {
729:
730: connection.error(message);
731: }
732:
733: protected void warn(Object message, Throwable error) {
734:
735: connection.warn(message, error);
736: }
737:
738: protected void warn(Object message) {
739:
740: connection.warn(message);
741: }
742:
743: void removeOpenCursor(ResultSetWrapper set) {
744:
745: openCursors.remove(set);
746: }
747: }
|