KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > jdbc > PreparedStatementImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29 package com.caucho.db.jdbc;
30
31 import com.caucho.db.sql.Query;
32 import com.caucho.db.sql.QueryContext;
33 import com.caucho.db.store.Transaction;
34
35 import java.io.ByteArrayInputStream JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.io.Reader JavaDoc;
38 import java.sql.*;
39 import java.util.Calendar JavaDoc;
40
41 /**
42  * The JDBC statement implementation.
43  */

44 public class PreparedStatementImpl extends StatementImpl
45   implements PreparedStatement {
46   
47   private Query _query;
48   private int _updateCount;
49   private boolean _wasResultSet;
50   private ResultSet _resultSet;
51
52   private boolean _isReturnGeneratedKeys;
53   
54   PreparedStatementImpl(ConnectionImpl conn, Query query)
55   {
56     super(conn);
57
58     _query = query;
59   }
60
61   void setReturnGeneratedKeys(boolean isReturnGeneratedKeys)
62   {
63     _isReturnGeneratedKeys = isReturnGeneratedKeys;
64   }
65
66   public java.sql.ResultSetMetaData JavaDoc getMetaData()
67   {
68     return null;
69   }
70   
71   public void clearParameters()
72     throws SQLException
73   {
74     _query.clearParameters();
75     // throw new UnsupportedOperationException();
76
}
77
78   public void setNull(int parameter, int sqlType)
79     throws SQLException
80   {
81     _query.setString(parameter, null);
82   }
83
84   public void setNull(int parameter, int sqlType, String JavaDoc typeName)
85     throws SQLException
86   {
87     _query.setString(parameter, null);
88   }
89
90   public void setBoolean(int parameter, boolean x)
91     throws SQLException
92   {
93     _query.setBoolean(parameter, x);
94   }
95
96   public void setByte(int parameter, byte x)
97     throws SQLException
98   {
99     _query.setLong(parameter, x);
100   }
101
102   public void setShort(int parameter, short x)
103     throws SQLException
104   {
105     _query.setLong(parameter, x);
106   }
107
108   public void setInt(int parameter, int x)
109     throws SQLException
110   {
111     _query.setLong(parameter, x);
112   }
113
114   public void setLong(int parameter, long x)
115     throws SQLException
116   {
117     _query.setLong(parameter, x);
118   }
119
120   public void setFloat(int parameter, float x)
121     throws SQLException
122   {
123     _query.setDouble(parameter, x);
124   }
125
126   public void setDouble(int parameter, double x)
127     throws SQLException
128   {
129     _query.setDouble(parameter, x);
130   }
131
132   public void setBigDecimal(int parameter, java.math.BigDecimal JavaDoc x)
133     throws SQLException
134   {
135     throw new UnsupportedOperationException JavaDoc();
136   }
137
138   public void setString(int parameter, String JavaDoc x)
139     throws SQLException
140   {
141     _query.setString(parameter, x);
142   }
143
144   public void setBytes(int parameter, byte []x)
145     throws SQLException
146   {
147     if (x != null) {
148       ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(x);
149     
150       _query.setBinaryStream(parameter, bis, x.length);
151     }
152     else
153       setNull(parameter, 0);
154   }
155
156   public void setDate(int parameter, java.sql.Date JavaDoc x, Calendar JavaDoc calendar)
157     throws SQLException
158   {
159     setDate(parameter, x);
160   }
161
162   public void setDate(int parameter, java.sql.Date JavaDoc x)
163     throws SQLException
164   {
165     if (x != null)
166       setTime(parameter, x.getTime());
167     else
168       setNull(parameter, 0);
169   }
170
171   public void setTime(int parameter, Time x, Calendar JavaDoc calendar)
172     throws SQLException
173   {
174     setTime(parameter, x);
175   }
176
177   public void setTime(int parameter, Time x)
178     throws SQLException
179   {
180     if (x != null)
181       setTime(parameter, x.getTime());
182     else
183       setNull(parameter, 0);
184   }
185
186   public void setTimestamp(int parameter, Timestamp x, Calendar JavaDoc calendar)
187     throws SQLException
188   {
189     setTimestamp(parameter, x);
190   }
191
192   public void setTimestamp(int parameter, Timestamp x)
193     throws SQLException
194   {
195     if (x != null)
196       setTime(parameter, x.getTime());
197     else
198       setNull(parameter, 0);
199   }
200
201   private void setTime(int parameter, long now)
202     throws SQLException
203   {
204     _query.setDate(parameter, now);
205   }
206
207   public void setAsciiStream(int parameter, InputStream JavaDoc is, int len)
208     throws SQLException
209   {
210     throw new UnsupportedOperationException JavaDoc();
211   }
212
213   public void setUnicodeStream(int parameter, InputStream JavaDoc is, int len)
214     throws SQLException
215   {
216     throw new UnsupportedOperationException JavaDoc();
217   }
218
219   public void setBinaryStream(int parameter, InputStream JavaDoc is, int len)
220     throws SQLException
221   {
222     _query.setBinaryStream(parameter, is, len);
223   }
224
225   public void setCharacterStream(int parameter, Reader JavaDoc is, int len)
226     throws SQLException
227   {
228     throw new UnsupportedOperationException JavaDoc();
229   }
230
231   public void setObject(int parameter, Object JavaDoc x, int target, int scale)
232     throws SQLException
233   {
234     throw new UnsupportedOperationException JavaDoc();
235   }
236
237   public void setObject(int parameter, Object JavaDoc x, int target)
238     throws SQLException
239   {
240     throw new UnsupportedOperationException JavaDoc();
241   }
242
243   public void setObject(int parameter, Object JavaDoc x)
244     throws SQLException
245   {
246     if (x instanceof String JavaDoc)
247       setString(parameter, (String JavaDoc) x);
248     else if (x instanceof Number JavaDoc) {
249       Number JavaDoc number = (Number JavaDoc) x;
250       
251       if (x instanceof Double JavaDoc)
252     setDouble(parameter, number.doubleValue());
253       else if (x instanceof java.lang.Float JavaDoc)
254     setDouble(parameter, number.doubleValue());
255       else
256     setLong(parameter, number.longValue());
257     }
258     else if (x instanceof java.sql.Time JavaDoc)
259       setTime(parameter, (java.sql.Time JavaDoc) x);
260     else if (x instanceof java.sql.Timestamp JavaDoc)
261       setTimestamp(parameter, (java.sql.Timestamp JavaDoc) x);
262     else if (x instanceof java.sql.Date JavaDoc)
263       setDate(parameter, (java.sql.Date JavaDoc) x);
264     else {
265       throw new UnsupportedOperationException JavaDoc();
266     }
267   }
268
269   public void setRef(int parameter, Ref x)
270     throws SQLException
271   {
272     throw new UnsupportedOperationException JavaDoc();
273   }
274
275   public void setBlob(int parameter, Blob x)
276     throws SQLException
277   {
278     throw new UnsupportedOperationException JavaDoc();
279   }
280
281   public void setClob(int parameter, Clob x)
282     throws SQLException
283   {
284     throw new UnsupportedOperationException JavaDoc();
285   }
286
287   public void setArray(int parameter, Array x)
288     throws SQLException
289   {
290     throw new UnsupportedOperationException JavaDoc();
291   }
292
293   public void addBatch()
294     throws SQLException
295   {
296   }
297   
298   public java.sql.ResultSet JavaDoc executeQuery()
299     throws SQLException
300   {
301     execute();
302
303     if (_wasResultSet)
304       return _resultSet;
305     else
306       throw new SQLException("missing result set");
307   }
308
309   public int executeUpdate()
310     throws SQLException
311   {
312     execute();
313     
314     return getUpdateCount();
315   }
316
317   private int _count;
318
319   public boolean execute()
320     throws SQLException
321   {
322     _count++;
323
324     Transaction xa = null;
325
326     try {
327       if (_count != 1)
328     throw new IllegalStateException JavaDoc("Multithreading execute");
329       
330       xa = _conn.getTransaction();
331       QueryContext queryContext = getQueryContext();
332     
333       if (_query.isSelect()) {
334     com.caucho.db.ResultSetImpl rs = null;
335     
336     _query.execute(queryContext, xa);
337
338     _wasResultSet = true;
339     _resultSet = new ResultSetImpl(this, queryContext.getResult());
340
341     return true;
342       }
343       else {
344     queryContext.setReturnGeneratedKeys(_isReturnGeneratedKeys);
345     
346     _query.execute(queryContext, xa);
347
348     _wasResultSet = false;
349     return false;
350       }
351     } finally {
352       _count--;
353
354       if (xa != null && xa.isAutoCommit())
355     xa.rollback();
356     }
357   }
358
359   // JDK 1.4
360
public void setURL(int foo, java.net.URL JavaDoc url)
361   {
362     throw new UnsupportedOperationException JavaDoc();
363   }
364   
365   public ParameterMetaData getParameterMetaData()
366   {
367     throw new UnsupportedOperationException JavaDoc();
368   }
369 }
370
Popular Tags