001: /*
002: DBPool - JDBC Connection Pool Manager
003: Copyright (c) Giles Winstanley
004: */
005: package snaq.db;
006:
007: import snaq.util.Reusable;
008: import java.sql.*;
009:
010: /**
011: * Statement wrapper that provides methods for caching support.
012: * @author Giles Winstanley
013: */
014: public class CachedStatement implements Statement, Reusable {
015: protected StatementListener listener;
016: protected Statement st;
017: protected boolean open = true; // Exists to avoid repeat cleaning of statement
018:
019: /**
020: * Creates a new CachedStatement object, using the supplied Statement.
021: */
022: public CachedStatement(Statement st) {
023: this .st = st;
024: }
025:
026: /** Returns a string descriptions of the ResultSet parameters. */
027: protected String getParametersString() {
028: StringBuffer sb = new StringBuffer();
029: try {
030: switch (getResultSetType()) {
031: case ResultSet.TYPE_SCROLL_INSENSITIVE:
032: sb.append("TYPE_SCROLL_INSENSITIVE");
033: break;
034: case ResultSet.TYPE_SCROLL_SENSITIVE:
035: sb.append("TYPE_SCROLL_SENSITIVE");
036: break;
037: default:
038: sb.append("TYPE_FORWARD_ONLY");
039: }
040: } catch (SQLException sqle) {
041: sb.append("TYPE_UNKNOWN");
042: }
043: sb.append(',');
044: try {
045: switch (getResultSetConcurrency()) {
046: case ResultSet.CONCUR_UPDATABLE:
047: sb.append("CONCUR_UPDATABLE");
048: break;
049: default:
050: sb.append("CONCUR_READ_ONLY");
051: }
052: } catch (SQLException sqle) {
053: sb.append("CONCUR_UNKNOWN");
054: }
055: sb.append(',');
056: try {
057: switch (getResultSetHoldability()) {
058: case ResultSet.CLOSE_CURSORS_AT_COMMIT:
059: sb.append("CLOSE_CURSORS_AT_COMMIT");
060: break;
061: case ResultSet.HOLD_CURSORS_OVER_COMMIT:
062: sb.append("HOLD_CURSORS_OVER_COMMIT");
063: }
064: } catch (SQLException sqle) {
065: sb.append("HOLD_UNKNOWN");
066: }
067: return sb.toString();
068: }
069:
070: // Cleans up the statement ready to be reused or closed.
071: public void recycle() throws SQLException {
072: ResultSet rs = st.getResultSet();
073: if (rs != null)
074: rs.close();
075:
076: try {
077: st.clearWarnings();
078: } catch (SQLException sqle) {
079: } // Caught to fix bug in some drivers
080:
081: try {
082: st.clearBatch();
083: } catch (SQLException sqle) {
084: } // Caught to fix bug in some drivers
085: }
086:
087: /**
088: * Overridden to provide caching support.
089: */
090: public void close() throws SQLException {
091: if (!open)
092: return;
093: open = false;
094: // If listener registered, do callback, otherwise release statement
095: if (listener != null)
096: listener.statementClosed(this );
097: else
098: release();
099: }
100:
101: /**
102: * Overridden to provide caching support.
103: */
104: public void release() throws SQLException {
105: st.close();
106: }
107:
108: /**
109: * Added to provide caching support.
110: */
111: void setOpen() {
112: open = true;
113: }
114:
115: /**
116: * Added to provide caching support.
117: */
118: void setStatementListener(StatementListener x) {
119: this .listener = x;
120: }
121:
122: //*****************************
123: // Statement interface methods
124: //*****************************
125:
126: public ResultSet executeQuery(String sql) throws SQLException {
127: return st.executeQuery(sql);
128: }
129:
130: public int executeUpdate(String sql) throws SQLException {
131: return st.executeUpdate(sql);
132: }
133:
134: public int getMaxFieldSize() throws SQLException {
135: return st.getMaxFieldSize();
136: }
137:
138: public void setMaxFieldSize(int max) throws SQLException {
139: st.setMaxFieldSize(max);
140: }
141:
142: public int getMaxRows() throws SQLException {
143: return st.getMaxRows();
144: }
145:
146: public void setMaxRows(int max) throws SQLException {
147: st.setMaxRows(max);
148: }
149:
150: public void setEscapeProcessing(boolean enable) throws SQLException {
151: st.setEscapeProcessing(enable);
152: }
153:
154: public int getQueryTimeout() throws SQLException {
155: return st.getQueryTimeout();
156: }
157:
158: public void setQueryTimeout(int seconds) throws SQLException {
159: st.setQueryTimeout(seconds);
160: }
161:
162: public void cancel() throws SQLException {
163: st.cancel();
164: }
165:
166: public SQLWarning getWarnings() throws SQLException {
167: return st.getWarnings();
168: }
169:
170: public void clearWarnings() throws SQLException {
171: st.clearWarnings();
172: }
173:
174: public void setCursorName(String name) throws SQLException {
175: st.setCursorName(name);
176: }
177:
178: public boolean execute(String sql) throws SQLException {
179: return st.execute(sql);
180: }
181:
182: public ResultSet getResultSet() throws SQLException {
183: return st.getResultSet();
184: }
185:
186: public int getUpdateCount() throws SQLException {
187: return st.getUpdateCount();
188: }
189:
190: public boolean getMoreResults() throws SQLException {
191: return st.getMoreResults();
192: }
193:
194: public void setFetchDirection(int direction) throws SQLException {
195: st.setFetchDirection(direction);
196: }
197:
198: public int getFetchDirection() throws SQLException {
199: return st.getFetchDirection();
200: }
201:
202: public void setFetchSize(int rows) throws SQLException {
203: st.setFetchSize(rows);
204: }
205:
206: public int getFetchSize() throws SQLException {
207: return st.getFetchSize();
208: }
209:
210: public int getResultSetConcurrency() throws SQLException {
211: return st.getResultSetConcurrency();
212: }
213:
214: public int getResultSetType() throws SQLException {
215: return st.getResultSetType();
216: }
217:
218: public void addBatch(String sql) throws SQLException {
219: st.addBatch(sql);
220: }
221:
222: public void clearBatch() throws SQLException {
223: st.clearBatch();
224: }
225:
226: public int[] executeBatch() throws SQLException {
227: return st.executeBatch();
228: }
229:
230: public Connection getConnection() throws SQLException {
231: return st.getConnection();
232: }
233:
234: //**********************************
235: // Interface methods from JDBC 3.0
236: //**********************************
237:
238: public boolean getMoreResults(int current) throws SQLException {
239: return st.getMoreResults(current);
240: }
241:
242: public ResultSet getGeneratedKeys() throws SQLException {
243: return st.getGeneratedKeys();
244: }
245:
246: public int executeUpdate(String sql, int autoGeneratedKeys)
247: throws SQLException {
248: return st.executeUpdate(sql, autoGeneratedKeys);
249: }
250:
251: public int executeUpdate(String sql, int[] columnIndexes)
252: throws SQLException {
253: return st.executeUpdate(sql, columnIndexes);
254: }
255:
256: public int executeUpdate(String sql, String[] columnNames)
257: throws SQLException {
258: return st.executeUpdate(sql, columnNames);
259: }
260:
261: public boolean execute(String sql, int autoGeneratedKeys)
262: throws SQLException {
263: return st.execute(sql, autoGeneratedKeys);
264: }
265:
266: public boolean execute(String sql, int[] columnIndexes)
267: throws SQLException {
268: return st.execute(sql, columnIndexes);
269: }
270:
271: public boolean execute(String sql, String[] columnNames)
272: throws SQLException {
273: return st.execute(sql, columnNames);
274: }
275:
276: public int getResultSetHoldability() throws SQLException {
277: return st.getResultSetHoldability();
278: }
279: }
|