001: /* ====================================================================
002: * The LateralNZ Software License, Version 1.0
003: *
004: * Copyright (c) 2003 LateralNZ. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by
021: * LateralNZ (http://www.lateralnz.org/) and other third parties."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "LateralNZ" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please
028: * contact oss@lateralnz.org.
029: *
030: * 5. Products derived from this software may not be called "Panther",
031: * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
032: * "LATERALNZ" appear in their name, without prior written
033: * permission of LateralNZ.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of LateralNZ. For more
051: * information on Lateral, please see http://www.lateralnz.com/ or
052: * http://www.lateralnz.org
053: *
054: */
055: package org.lateralnz.c3d;
056:
057: import java.io.Serializable;
058: import java.sql.Connection;
059: import java.sql.ResultSet;
060: import java.sql.SQLException;
061: import java.sql.SQLWarning;
062: import java.sql.Statement;
063:
064: import org.apache.log4j.Logger;
065:
066: import org.lateralnz.common.util.Constants;
067: import org.lateralnz.common.util.DAOUtils;
068:
069: import org.lateralnz.c3d.util.ResultWrapper;
070:
071: public class DCStatement implements Statement, Serializable, Constants {
072: private static final Logger log = Logger
073: .getLogger(DCStatement.class.getName());
074:
075: protected static final int STATEMENT = 1;
076: protected static final int PREPARED = 2;
077: protected static final int CALLABLE = 3;
078:
079: protected DCConnection conn; // connection used to create this statement
080: private String sql = EMPTY; // SQL we will execute
081: Statement realStatement = null; // a real statement for the actual db
082: protected int statementType; // what type of statement is this
083: protected ResultSet rs; // the result produced by execution
084: protected int updateCount = -1; // the num of rows updated
085: protected boolean nocaching = false; // should data not be cached
086:
087: protected int[] insertKeyColumnIndexes = null; // passthrough vars
088: protected String[] insertKeyColumns = null;
089: protected int autoGeneratedKeys = Integer.MIN_VALUE;
090: private String cursorName = null;
091: private boolean escapeProcessing = true;
092: private int fetchDirection = Integer.MIN_VALUE;
093: private int fetchSize = Integer.MIN_VALUE;
094: private int maxFieldSize = Integer.MIN_VALUE;
095: private int maxRows = Integer.MIN_VALUE;
096: private int queryTimeout = Integer.MIN_VALUE;
097: protected int resultsetType = Integer.MIN_VALUE;
098: protected int resultsetConcurrency = Integer.MIN_VALUE;
099: protected int resultsetHoldability = Integer.MIN_VALUE;
100:
101: /**
102: * create a statement using the specified parameters. Note that resultset type,
103: * concurrency and holdability are basically ignored by this statement, but passed
104: * through if a real statement (connection to the DB) is required.
105: */
106: DCStatement(DCConnection conn, int resultsetType,
107: int resultsetConcurrency, int resultsetHoldability) {
108: this .conn = conn;
109: this .resultsetType = resultsetType;
110: this .resultsetConcurrency = resultsetConcurrency;
111: this .resultsetHoldability = resultsetHoldability;
112: this .statementType = STATEMENT;
113: }
114:
115: protected Statement getRealStatement() throws SQLException {
116: if (realStatement == null) {
117: if (resultsetHoldability != Integer.MIN_VALUE
118: && resultsetType != Integer.MIN_VALUE
119: && resultsetConcurrency != Integer.MIN_VALUE) {
120: realStatement = conn.getRealConnection()
121: .createStatement(resultsetType,
122: resultsetConcurrency,
123: resultsetHoldability);
124: } else if (resultsetType != Integer.MIN_VALUE
125: && resultsetConcurrency != Integer.MIN_VALUE) {
126: realStatement = conn.getRealConnection()
127: .createStatement(resultsetType,
128: resultsetConcurrency);
129: } else {
130: realStatement = conn.getRealConnection()
131: .createStatement();
132: }
133: if (cursorName != null) {
134: realStatement.setCursorName(cursorName);
135: }
136:
137: realStatement.setEscapeProcessing(escapeProcessing);
138:
139: if (fetchDirection != Integer.MIN_VALUE) {
140: realStatement.setFetchDirection(fetchDirection);
141: }
142:
143: if (fetchSize != Integer.MIN_VALUE) {
144: realStatement.setFetchSize(fetchSize);
145: }
146:
147: if (maxFieldSize != Integer.MIN_VALUE) {
148: realStatement.setMaxFieldSize(maxFieldSize);
149: }
150:
151: if (maxRows != Integer.MIN_VALUE) {
152: realStatement.setMaxRows(maxRows);
153: }
154:
155: if (queryTimeout != Integer.MIN_VALUE) {
156: realStatement.setQueryTimeout(queryTimeout);
157: }
158: }
159: return realStatement;
160: }
161:
162: public void addBatch(String str) throws SQLException {
163: throw new SQLException("batch not supported");
164: }
165:
166: public void cancel() throws SQLException {
167: if (realStatement != null) {
168: realStatement.cancel();
169: }
170: }
171:
172: public void clearBatch() throws SQLException {
173: throw new SQLException("batch not supported");
174: }
175:
176: public void clearWarnings() throws SQLException {
177: if (realStatement != null) {
178: realStatement.clearWarnings();
179: }
180: }
181:
182: public void close() throws SQLException {
183: autoGeneratedKeys = Integer.MIN_VALUE;
184: insertKeyColumnIndexes = null;
185: insertKeyColumns = null;
186:
187: DAOUtils.close(realStatement);
188: }
189:
190: public boolean execute(String sql) throws SQLException {
191: setSQL(sql);
192: DatabaseEngine dbengine = conn.getDatabaseEngine();
193:
194: ResultWrapper rw = dbengine.execute(this );
195: if (rw != null) {
196: this .rs = rw.rs;
197: this .updateCount = rw.updateCount;
198: if (log.isDebugEnabled()) {
199: log.debug("execute returned rs " + rs
200: + ", updatecount " + updateCount);
201: }
202: }
203:
204: if (rs != null) {
205: return true;
206: } else {
207: return false;
208: }
209: }
210:
211: public boolean execute(String sql, String[] columnNames)
212: throws SQLException {
213: this .insertKeyColumns = columnNames;
214: return execute(sql);
215: }
216:
217: public boolean execute(String sql, int[] columnIndexes)
218: throws SQLException {
219: this .insertKeyColumnIndexes = columnIndexes;
220: return execute(sql);
221: }
222:
223: public boolean execute(String sql, int autoGeneratedKeys)
224: throws SQLException {
225: this .autoGeneratedKeys = autoGeneratedKeys;
226: return execute(sql);
227: }
228:
229: public int[] executeBatch() throws SQLException {
230: throw new SQLException("batch not supported");
231: }
232:
233: public ResultSet executeQuery(String sql) throws SQLException {
234: if (execute(sql)) {
235: return getResultSet();
236: } else {
237: throw new SQLException("invalid query");
238: }
239: }
240:
241: public int executeUpdate(String sql) throws SQLException {
242: execute(sql);
243: return getUpdateCount();
244: }
245:
246: public int executeUpdate(String sql, String[] columnNames)
247: throws SQLException {
248: execute(sql, columnNames);
249: return getUpdateCount();
250: }
251:
252: public int executeUpdate(String sql, int autoGeneratedKeys)
253: throws SQLException {
254: execute(sql, autoGeneratedKeys);
255: return getUpdateCount();
256: }
257:
258: public int executeUpdate(String sql, int[] columnIndexes)
259: throws SQLException {
260: execute(sql, columnIndexes);
261: return getUpdateCount();
262: }
263:
264: public Connection getConnection() throws SQLException {
265: return conn;
266: }
267:
268: public int getFetchDirection() throws SQLException {
269: return ResultSet.FETCH_FORWARD;
270: }
271:
272: public int getFetchSize() throws SQLException {
273: if (realStatement != null) {
274: return realStatement.getFetchSize();
275: } else {
276: return 10;
277: }
278: }
279:
280: public ResultSet getGeneratedKeys() throws SQLException {
281: if (realStatement != null) {
282: return realStatement.getGeneratedKeys();
283: } else {
284: return new DCResultSet(null, null, null, null,
285: ResultSet.CONCUR_READ_ONLY,
286: ResultSet.TYPE_FORWARD_ONLY);
287: }
288: }
289:
290: public int getMaxFieldSize() throws SQLException {
291: return 0;
292: }
293:
294: public int getMaxRows() throws SQLException {
295: return 0;
296: }
297:
298: public boolean getMoreResults() throws SQLException {
299: return false;
300: }
301:
302: public boolean getMoreResults(int param) throws SQLException {
303: return false;
304: }
305:
306: public int getQueryTimeout() throws SQLException {
307: if (realStatement != null) {
308: return realStatement.getQueryTimeout();
309: } else {
310: return 0;
311: }
312: }
313:
314: public ResultSet getResultSet() throws SQLException {
315: return rs;
316: }
317:
318: public int getResultSetConcurrency() throws SQLException {
319: return resultsetConcurrency;
320: }
321:
322: public int getResultSetHoldability() throws SQLException {
323: return resultsetHoldability;
324: }
325:
326: public int getResultSetType() throws SQLException {
327: return resultsetType;
328: }
329:
330: protected String getSQL() {
331: return sql;
332: }
333:
334: public int getUpdateCount() throws SQLException {
335: return updateCount;
336: }
337:
338: public SQLWarning getWarnings() throws SQLException {
339: if (realStatement != null) {
340: return realStatement.getWarnings();
341: } else {
342: return null;
343: }
344: }
345:
346: public void setCursorName(String str) throws SQLException {
347: this .cursorName = str;
348: if (realStatement != null) {
349: realStatement.setCursorName(str);
350: }
351: }
352:
353: public void setEscapeProcessing(boolean param) throws SQLException {
354: this .escapeProcessing = param;
355: if (realStatement != null) {
356: realStatement.setEscapeProcessing(param);
357: }
358: }
359:
360: public void setFetchDirection(int param) throws SQLException {
361: this .fetchDirection = param;
362: if (realStatement != null) {
363: realStatement.setFetchDirection(param);
364: }
365: }
366:
367: public void setFetchSize(int param) throws SQLException {
368: this .fetchSize = param;
369: if (realStatement != null) {
370: realStatement.setFetchSize(param);
371: }
372: }
373:
374: public void setMaxFieldSize(int param) throws SQLException {
375: this .maxFieldSize = param;
376: if (realStatement != null) {
377: realStatement.setMaxFieldSize(maxFieldSize);
378: }
379: }
380:
381: public void setMaxRows(int param) throws SQLException {
382: this .maxRows = param;
383: if (realStatement != null) {
384: realStatement.setMaxRows(param);
385: }
386: }
387:
388: public void setQueryTimeout(int param) throws SQLException {
389: this .queryTimeout = param;
390: if (realStatement != null) {
391: realStatement.setQueryTimeout(param);
392: }
393: }
394:
395: protected void setSQL(String sql) {
396: this.sql = sql;
397: }
398: }
|