001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029: package com.caucho.db.jdbc;
030:
031: import com.caucho.db.sql.Query;
032: import com.caucho.db.sql.QueryContext;
033: import com.caucho.db.store.Transaction;
034:
035: import java.io.ByteArrayInputStream;
036: import java.io.InputStream;
037: import java.io.Reader;
038: import java.sql.*;
039: import java.util.Calendar;
040:
041: /**
042: * The JDBC statement implementation.
043: */
044: public class PreparedStatementImpl extends StatementImpl implements
045: PreparedStatement {
046:
047: private Query _query;
048: private int _updateCount;
049: private boolean _wasResultSet;
050: private ResultSet _resultSet;
051:
052: private boolean _isReturnGeneratedKeys;
053: private int _count;
054:
055: PreparedStatementImpl(ConnectionImpl conn, Query query) {
056: super (conn);
057:
058: _query = query;
059: }
060:
061: void setReturnGeneratedKeys(boolean isReturnGeneratedKeys) {
062: _isReturnGeneratedKeys = isReturnGeneratedKeys;
063: }
064:
065: public java.sql.ResultSetMetaData getMetaData() {
066: return null;
067: }
068:
069: public void clearParameters() throws SQLException {
070: _query.clearParameters();
071: // throw new UnsupportedOperationException();
072: }
073:
074: public void setNull(int parameter, int sqlType) throws SQLException {
075: _query.setString(parameter, null);
076: }
077:
078: public void setNull(int parameter, int sqlType, String typeName)
079: throws SQLException {
080: _query.setString(parameter, null);
081: }
082:
083: public void setBoolean(int parameter, boolean x)
084: throws SQLException {
085: _query.setBoolean(parameter, x);
086: }
087:
088: public void setByte(int parameter, byte x) throws SQLException {
089: _query.setLong(parameter, x);
090: }
091:
092: public void setShort(int parameter, short x) throws SQLException {
093: _query.setLong(parameter, x);
094: }
095:
096: public void setInt(int parameter, int x) throws SQLException {
097: _query.setLong(parameter, x);
098: }
099:
100: public void setLong(int parameter, long x) throws SQLException {
101: _query.setLong(parameter, x);
102: }
103:
104: public void setFloat(int parameter, float x) throws SQLException {
105: _query.setDouble(parameter, x);
106: }
107:
108: public void setDouble(int parameter, double x) throws SQLException {
109: _query.setDouble(parameter, x);
110: }
111:
112: public void setBigDecimal(int parameter, java.math.BigDecimal x)
113: throws SQLException {
114: throw new UnsupportedOperationException();
115: }
116:
117: public void setString(int parameter, String x) throws SQLException {
118: _query.setString(parameter, x);
119: }
120:
121: public void setBytes(int parameter, byte[] x) throws SQLException {
122: if (x != null) {
123: ByteArrayInputStream bis = new ByteArrayInputStream(x);
124:
125: _query.setBinaryStream(parameter, bis, x.length);
126: } else
127: setNull(parameter, 0);
128: }
129:
130: public void setDate(int parameter, java.sql.Date x,
131: Calendar calendar) throws SQLException {
132: setDate(parameter, x);
133: }
134:
135: public void setDate(int parameter, java.sql.Date x)
136: throws SQLException {
137: if (x != null)
138: setTime(parameter, x.getTime());
139: else
140: setNull(parameter, 0);
141: }
142:
143: public void setTime(int parameter, Time x, Calendar calendar)
144: throws SQLException {
145: setTime(parameter, x);
146: }
147:
148: public void setTime(int parameter, Time x) throws SQLException {
149: if (x != null)
150: setTime(parameter, x.getTime());
151: else
152: setNull(parameter, 0);
153: }
154:
155: public void setTimestamp(int parameter, Timestamp x,
156: Calendar calendar) throws SQLException {
157: setTimestamp(parameter, x);
158: }
159:
160: public void setTimestamp(int parameter, Timestamp x)
161: throws SQLException {
162: if (x != null)
163: setTime(parameter, x.getTime());
164: else
165: setNull(parameter, 0);
166: }
167:
168: private void setTime(int parameter, long now) throws SQLException {
169: _query.setDate(parameter, now);
170: }
171:
172: public void setAsciiStream(int parameter, InputStream is, int len)
173: throws SQLException {
174: throw new UnsupportedOperationException();
175: }
176:
177: public void setUnicodeStream(int parameter, InputStream is, int len)
178: throws SQLException {
179: throw new UnsupportedOperationException();
180: }
181:
182: public void setBinaryStream(int parameter, InputStream is, int len)
183: throws SQLException {
184: _query.setBinaryStream(parameter, is, len);
185: }
186:
187: public void setCharacterStream(int parameter, Reader is, int len)
188: throws SQLException {
189: throw new UnsupportedOperationException();
190: }
191:
192: public void setObject(int parameter, Object x, int target, int scale)
193: throws SQLException {
194: throw new UnsupportedOperationException();
195: }
196:
197: public void setObject(int parameter, Object x, int target)
198: throws SQLException {
199: throw new UnsupportedOperationException();
200: }
201:
202: public void setObject(int parameter, Object x) throws SQLException {
203: if (x instanceof String)
204: setString(parameter, (String) x);
205: else if (x instanceof Number) {
206: Number number = (Number) x;
207:
208: if (x instanceof Double)
209: setDouble(parameter, number.doubleValue());
210: else if (x instanceof java.lang.Float)
211: setDouble(parameter, number.doubleValue());
212: else
213: setLong(parameter, number.longValue());
214: } else if (x instanceof java.sql.Time)
215: setTime(parameter, (java.sql.Time) x);
216: else if (x instanceof java.sql.Timestamp)
217: setTimestamp(parameter, (java.sql.Timestamp) x);
218: else if (x instanceof java.sql.Date)
219: setDate(parameter, (java.sql.Date) x);
220: else {
221: throw new UnsupportedOperationException();
222: }
223: }
224:
225: public void setRef(int parameter, Ref x) throws SQLException {
226: throw new UnsupportedOperationException();
227: }
228:
229: public void setBlob(int parameter, Blob x) throws SQLException {
230: throw new UnsupportedOperationException();
231: }
232:
233: public void setClob(int parameter, Clob x) throws SQLException {
234: throw new UnsupportedOperationException();
235: }
236:
237: public void setArray(int parameter, Array x) throws SQLException {
238: throw new UnsupportedOperationException();
239: }
240:
241: public void addBatch() throws SQLException {
242: }
243:
244: public java.sql.ResultSet executeQuery() throws SQLException {
245: execute();
246:
247: if (_wasResultSet)
248: return _resultSet;
249: else
250: throw new SQLException("missing result set");
251: }
252:
253: public int executeUpdate() throws SQLException {
254: execute();
255:
256: return getUpdateCount();
257: }
258:
259: public boolean execute() throws SQLException {
260: _count++;
261:
262: Transaction xa = null;
263:
264: try {
265: if (_count != 1)
266: throw new IllegalStateException(
267: "Multithreading execute");
268:
269: xa = _conn.getTransaction();
270: QueryContext queryContext = getQueryContext();
271:
272: if (_query.isSelect()) {
273: com.caucho.db.ResultSetImpl rs = null;
274:
275: _query.execute(queryContext, xa);
276:
277: _wasResultSet = true;
278: _resultSet = new ResultSetImpl(this , queryContext
279: .getResult());
280:
281: return true;
282: } else {
283: queryContext
284: .setReturnGeneratedKeys(_isReturnGeneratedKeys);
285:
286: _query.execute(queryContext, xa);
287:
288: _wasResultSet = false;
289: return false;
290: }
291: } finally {
292: _count--;
293:
294: if (xa != null && xa.isAutoCommit())
295: xa.rollback();
296: }
297: }
298:
299: // JDK 1.4
300: public void setURL(int foo, java.net.URL url) {
301: throw new UnsupportedOperationException();
302: }
303:
304: public ParameterMetaData getParameterMetaData() {
305: throw new UnsupportedOperationException();
306: }
307:
308: public boolean isClosed() throws SQLException {
309: throw new UnsupportedOperationException("Not supported yet.");
310: }
311:
312: public void setPoolable(boolean poolable) throws SQLException {
313: throw new UnsupportedOperationException("Not supported yet.");
314: }
315:
316: public boolean isPoolable() throws SQLException {
317: throw new UnsupportedOperationException("Not supported yet.");
318: }
319:
320: public <T> T unwrap(Class<T> iface) throws SQLException {
321: throw new UnsupportedOperationException("Not supported yet.");
322: }
323:
324: public boolean isWrapperFor(Class<?> iface) throws SQLException {
325: throw new UnsupportedOperationException("Not supported yet.");
326: }
327:
328: public void setRowId(int parameterIndex, RowId x)
329: throws SQLException {
330: throw new UnsupportedOperationException("Not supported yet.");
331: }
332:
333: public void setNString(int parameterIndex, String value)
334: throws SQLException {
335: throw new UnsupportedOperationException("Not supported yet.");
336: }
337:
338: public void setNCharacterStream(int parameterIndex, Reader value,
339: long length) throws SQLException {
340: throw new UnsupportedOperationException("Not supported yet.");
341: }
342:
343: public void setNClob(int parameterIndex, NClob value)
344: throws SQLException {
345: throw new UnsupportedOperationException("Not supported yet.");
346: }
347:
348: public void setClob(int parameterIndex, Reader reader, long length)
349: throws SQLException {
350: throw new UnsupportedOperationException("Not supported yet.");
351: }
352:
353: public void setBlob(int parameterIndex, InputStream inputStream,
354: long length) throws SQLException {
355: throw new UnsupportedOperationException("Not supported yet.");
356: }
357:
358: public void setNClob(int parameterIndex, Reader reader, long length)
359: throws SQLException {
360: throw new UnsupportedOperationException("Not supported yet.");
361: }
362:
363: public void setSQLXML(int parameterIndex, SQLXML xmlObject)
364: throws SQLException {
365: throw new UnsupportedOperationException("Not supported yet.");
366: }
367:
368: public void setAsciiStream(int parameterIndex, InputStream x,
369: long length) throws SQLException {
370: throw new UnsupportedOperationException("Not supported yet.");
371: }
372:
373: public void setBinaryStream(int parameterIndex, InputStream x,
374: long length) throws SQLException {
375: throw new UnsupportedOperationException("Not supported yet.");
376: }
377:
378: public void setCharacterStream(int parameterIndex, Reader reader,
379: long length) throws SQLException {
380: throw new UnsupportedOperationException("Not supported yet.");
381: }
382:
383: public void setAsciiStream(int parameterIndex, InputStream x)
384: throws SQLException {
385: throw new UnsupportedOperationException("Not supported yet.");
386: }
387:
388: public void setBinaryStream(int parameterIndex, InputStream x)
389: throws SQLException {
390: throw new UnsupportedOperationException("Not supported yet.");
391: }
392:
393: public void setCharacterStream(int parameterIndex, Reader reader)
394: throws SQLException {
395: throw new UnsupportedOperationException("Not supported yet.");
396: }
397:
398: public void setNCharacterStream(int parameterIndex, Reader value)
399: throws SQLException {
400: throw new UnsupportedOperationException("Not supported yet.");
401: }
402:
403: public void setClob(int parameterIndex, Reader reader)
404: throws SQLException {
405: throw new UnsupportedOperationException("Not supported yet.");
406: }
407:
408: public void setBlob(int parameterIndex, InputStream inputStream)
409: throws SQLException {
410: throw new UnsupportedOperationException("Not supported yet.");
411: }
412:
413: public void setNClob(int parameterIndex, Reader reader)
414: throws SQLException {
415: throw new UnsupportedOperationException("Not supported yet.");
416: }
417: }
|