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.sql.Array;
058: import java.sql.ParameterMetaData;
059: import java.sql.PreparedStatement;
060: import java.sql.ResultSet;
061: import java.sql.ResultSetMetaData;
062: import java.sql.Statement;
063: import java.sql.SQLException;
064: import java.sql.Types;
065:
066: import org.lateralnz.common.util.Constants;
067: import org.lateralnz.common.util.DAOUtils;
068: import org.lateralnz.common.util.StringUtils;
069:
070: import org.lateralnz.c3d.util.Column;
071: import org.lateralnz.c3d.util.ResultWrapper;
072:
073: public class DCPreparedStatement extends DCStatement implements
074: PreparedStatement, Constants {
075: private static final byte[] NULL_BYTES = "null".getBytes();
076: protected PreparedStatement realPS;
077: protected PreparedStatement queryUpdateRowsPS = null;
078: protected String queryUpdateRowsSQL = EMPTY;
079:
080: private int paramCount = 0;
081: protected Column[] params;
082:
083: public DCPreparedStatement(DCConnection conn, String sql,
084: int resultsetType, int resultsetConcurrency,
085: int resultsetHoldability, int autoGeneratedKeys,
086: int[] columnIndexes, String[] columnNames)
087: throws SQLException {
088: super (conn, resultsetType, resultsetConcurrency,
089: resultsetHoldability);
090: this .statementType = PREPARED;
091: setSQL(sql);
092:
093: if (resultsetType != Integer.MIN_VALUE
094: && resultsetConcurrency != Integer.MIN_VALUE
095: && resultsetHoldability != Integer.MIN_VALUE) {
096: realPS = conn.getRealConnection().prepareStatement(sql,
097: resultsetType, resultsetConcurrency,
098: resultsetHoldability);
099: } else if (resultsetType != Integer.MIN_VALUE
100: && resultsetConcurrency != Integer.MIN_VALUE) {
101: realPS = conn.getRealConnection().prepareStatement(sql,
102: resultsetType, resultsetConcurrency);
103: } else if (autoGeneratedKeys != Integer.MIN_VALUE) {
104: realPS = conn.getRealConnection().prepareStatement(sql,
105: autoGeneratedKeys);
106: } else if (columnIndexes != null) {
107: realPS = conn.getRealConnection().prepareStatement(sql,
108: columnIndexes);
109: } else if (columnNames != null) {
110: realPS = conn.getRealConnection().prepareStatement(sql,
111: columnNames);
112: } else {
113: realPS = conn.getRealConnection().prepareStatement(sql);
114: }
115:
116: realStatement = realPS;
117:
118: paramCount = StringUtils.countOccurrences(sql, '?');
119: clearParameters();
120: }
121:
122: public void addBatch() throws SQLException {
123: }
124:
125: public void clearParameters() throws SQLException {
126: realPS.clearParameters();
127: params = new Column[paramCount];
128: for (int i = 0; i < paramCount; i++) {
129: params[i] = new Column();
130: }
131: }
132:
133: public void close() throws SQLException {
134: try {
135: super .close();
136: } finally {
137: DAOUtils.close(queryUpdateRowsPS);
138: }
139: }
140:
141: public boolean execute() throws SQLException {
142: ResultWrapper rw = conn.getDatabaseEngine().execute(this );
143: if (rw != null) {
144: rs = rw.rs;
145: updateCount = rw.updateCount;
146: }
147:
148: if (rs != null) {
149: return true;
150: } else {
151: return false;
152: }
153: }
154:
155: public ResultSet executeQuery() throws SQLException {
156: if (execute()) {
157: return getResultSet();
158: } else {
159: throw new SQLException("query failed");
160: }
161: }
162:
163: public int executeUpdate() throws SQLException {
164: if (!execute()) {
165: return getUpdateCount();
166: } else {
167: return -1;
168: }
169: }
170:
171: public ResultSetMetaData getMetaData() throws SQLException {
172: if (getResultSet() != null) {
173: return getResultSet().getMetaData();
174: } else {
175: throw new SQLException("operation not supported");
176: }
177: }
178:
179: public ParameterMetaData getParameterMetaData() throws SQLException {
180: return realPS.getParameterMetaData();
181: }
182:
183: public Statement getRealStatement() {
184: return realPS;
185: }
186:
187: public void setArray(int param, Array array) throws SQLException {
188: realPS.setArray(param, array);
189: nocaching = true;
190: }
191:
192: public void setAsciiStream(int param,
193: java.io.InputStream inputStream, int param2)
194: throws SQLException {
195: realPS.setAsciiStream(param, inputStream, param2);
196: nocaching = true;
197: }
198:
199: public void setBigDecimal(int param, java.math.BigDecimal bigDecimal)
200: throws SQLException {
201: realPS.setBigDecimal(param, bigDecimal);
202: param--;
203: params[param].setObjectValue(bigDecimal);
204: params[param].setType(Types.NUMERIC);
205: }
206:
207: public void setBinaryStream(int param,
208: java.io.InputStream inputStream, int param2)
209: throws SQLException {
210: realPS.setBinaryStream(param, inputStream, param2);
211: nocaching = true;
212: }
213:
214: public void setBlob(int param, java.sql.Blob blob)
215: throws SQLException {
216: realPS.setBlob(param, blob);
217: nocaching = true;
218: }
219:
220: public void setBoolean(int param, boolean param1)
221: throws SQLException {
222: realPS.setBoolean(param, param1);
223: param--;
224: if (param1) {
225: params[param].setObjectValue(TRUE);
226: } else {
227: params[param].setObjectValue(FALSE);
228: }
229: params[param].setType(Types.BOOLEAN);
230: }
231:
232: public void setByte(int param, byte param1) throws SQLException {
233: realPS.setByte(param, param1);
234: param--;
235: params[param].setObjectValue(new Byte(param1));
236: params[param].setType(Types.TINYINT);
237: }
238:
239: public void setBytes(int param, byte[] values) throws SQLException {
240: realPS.setBytes(param, values);
241: param--;
242: params[param].setValue(values);
243: params[param].setType(Types.VARBINARY);
244: }
245:
246: public void setCharacterStream(int param, java.io.Reader reader,
247: int param2) throws SQLException {
248: realPS.setCharacterStream(param, reader, param2);
249: nocaching = true;
250: }
251:
252: public void setClob(int param, java.sql.Clob clob)
253: throws SQLException {
254: realPS.setClob(param, clob);
255: nocaching = true;
256: }
257:
258: public void setDate(int param, java.sql.Date date)
259: throws SQLException {
260: realPS.setDate(param, date);
261: param--;
262: String s = date.toString();
263: params[param].setObjectValue(s);
264: params[param].setType(Types.DATE);
265: }
266:
267: public void setDate(int param, java.sql.Date date,
268: java.util.Calendar calendar) throws SQLException {
269: realPS.setDate(param, date, calendar);
270: param--;
271: String s = date.toString();
272: params[param].setObjectValue(s);
273: params[param].setType(Types.DATE);
274: }
275:
276: public void setDouble(int param, double param1) throws SQLException {
277: realPS.setDouble(param, param1);
278: param--;
279: params[param].setObjectValue(new Double(param1));
280: params[param].setType(Types.DOUBLE);
281: }
282:
283: public void setFloat(int param, float param1) throws SQLException {
284: realPS.setFloat(param, param1);
285: param--;
286: params[param].setObjectValue(new Float(param1));
287: params[param].setType(Types.FLOAT);
288: }
289:
290: public void setInt(int param, int param1) throws SQLException {
291: realPS.setInt(param, param1);
292: param--;
293: params[param].setObjectValue(new Integer(param1));
294: params[param].setType(Types.INTEGER);
295: }
296:
297: public void setLong(int param, long param1) throws SQLException {
298: realPS.setLong(param, param1);
299: param--;
300: params[param].setObjectValue(new Long(param1));
301: params[param].setType(Types.BIGINT);
302: }
303:
304: public void setNull(int param, int param1) throws SQLException {
305: realPS.setNull(param, param1);
306: param--;
307: params[param].setType(param1);
308: params[param].setNull(true);
309: }
310:
311: public void setNull(int param, int param1, String str)
312: throws SQLException {
313: realPS.setNull(param, param1, str);
314: param--;
315: params[param].setObjectValue(str);
316: params[param].setType(param1);
317: params[param].setNull(true);
318: }
319:
320: public void setObject(int param, Object obj) throws SQLException {
321: realPS.setObject(param, obj);
322: nocaching = true;
323: }
324:
325: public void setObject(int param, Object obj, int param2)
326: throws SQLException {
327: realPS.setObject(param, obj, param2);
328: nocaching = true;
329: }
330:
331: public void setObject(int param, Object obj, int param2, int param3)
332: throws SQLException {
333: realPS.setObject(param, obj, param2, param3);
334: nocaching = true;
335: }
336:
337: public void setRef(int param, java.sql.Ref ref) throws SQLException {
338: realPS.setRef(param, ref);
339: nocaching = true;
340: }
341:
342: public void setShort(int param, short param1) throws SQLException {
343: realPS.setShort(param, param1);
344: param--;
345: params[param].setObjectValue(new Short(param1));
346: params[param].setType(Types.SMALLINT);
347: }
348:
349: public void setString(int param, String str) throws SQLException {
350: realPS.setString(param, str);
351: param--;
352: params[param].setObjectValue(str);
353: params[param].setType(Types.VARCHAR);
354: }
355:
356: public void setTime(int param, java.sql.Time time)
357: throws SQLException {
358: realPS.setTime(param, time);
359: param--;
360: params[param].setObjectValue(time);
361: params[param].setType(Types.TIME);
362: }
363:
364: public void setTime(int param, java.sql.Time time,
365: java.util.Calendar calendar) throws SQLException {
366: realPS.setTime(param, time, calendar);
367: param--;
368: params[param].setObjectValue(time);
369: params[param].setType(Types.TIME);
370: }
371:
372: public void setTimestamp(int param, java.sql.Timestamp timestamp)
373: throws SQLException {
374: realPS.setTimestamp(param, timestamp);
375: param--;
376: params[param].setObjectValue(timestamp);
377: params[param].setType(Types.TIMESTAMP);
378: }
379:
380: public void setTimestamp(int param, java.sql.Timestamp timestamp,
381: java.util.Calendar calendar) throws SQLException {
382: realPS.setTimestamp(param, timestamp, calendar);
383: param--;
384: params[param].setObjectValue(timestamp);
385: params[param].setType(Types.TIMESTAMP);
386: }
387:
388: public void setURL(int param, java.net.URL url) throws SQLException {
389: realPS.setURL(param, url);
390: nocaching = true;
391: }
392:
393: public void setUnicodeStream(int param,
394: java.io.InputStream inputStream, int param2)
395: throws SQLException {
396: realPS.setUnicodeStream(param, inputStream, param2);
397: nocaching = true;
398: }
399:
400: }
|