KickJava   Java API By Example, From Geeks To Geeks.

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


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.Database;
32 import com.caucho.db.sql.Query;
33 import com.caucho.db.sql.QueryContext;
34 import com.caucho.db.sql.SelectQuery;
35 import com.caucho.db.store.Transaction;
36 import com.caucho.util.L10N;
37
38 import java.sql.SQLException JavaDoc;
39 import java.sql.SQLWarning JavaDoc;
40
41 /**
42  * The JDBC statement implementation.
43  */

44 public class StatementImpl implements java.sql.Statement JavaDoc {
45   private final static L10N L = new L10N(StatementImpl.class);
46   
47   protected Database _db;
48   protected final ConnectionImpl _conn;
49
50   protected ResultSetImpl _rs;
51
52   protected QueryContext _queryContext;
53   
54   private boolean _isClosed;
55   
56   StatementImpl(ConnectionImpl conn)
57   {
58     _conn = conn;
59     _db = conn.getDatabase();
60
61     if (_db == null)
62       throw new NullPointerException JavaDoc();
63
64     init();
65   }
66
67   protected void init()
68   {
69     if (_queryContext == null)
70       _queryContext = QueryContext.allocate();
71   }
72
73   protected QueryContext getQueryContext()
74   {
75     if (_queryContext == null)
76       _queryContext = QueryContext.allocate();
77
78     return _queryContext;
79   }
80
81   public void addBatch(String JavaDoc sql)
82   {
83   }
84
85   public void cancel()
86   {
87   }
88
89   public void clearBatch()
90   {
91   }
92
93   public void clearWarnings()
94   {
95   }
96
97   public java.sql.ResultSet JavaDoc executeQuery(String JavaDoc sql)
98     throws SQLException JavaDoc
99   {
100     if (_db == null)
101       throw new SQLException JavaDoc(L.l("statement is closed"));
102     
103     Query query = _db.parseQuery(sql);
104
105     return executeQuery(query);
106   }
107   
108   private java.sql.ResultSet JavaDoc executeQuery(Query query)
109     throws SQLException JavaDoc
110   {
111     Transaction xa = _conn.getTransaction();
112     
113     boolean isOkay = false;
114     try {
115       query.execute(_queryContext, xa);
116       isOkay = true;
117     } finally {
118       if (! xa.isAutoCommit()) {
119       }
120       else if (isOkay)
121     xa.commit();
122       else
123     xa.rollback();
124     }
125     
126     _rs = new ResultSetImpl(this, _queryContext.getResult());
127
128     return _rs;
129   }
130
131   /**
132    * Executes an update statement with the given SQL.
133    *
134    * @param sql the SQL to execute.
135    *
136    * @return the number of rows modified.
137    */

138   public int executeUpdate(String JavaDoc sql)
139     throws SQLException JavaDoc
140   {
141     Query query = _db.parseQuery(sql);
142
143     return executeUpdate(query);
144   }
145   
146   public int executeUpdate(String JavaDoc sql, int autoGeneratedKeys)
147     throws SQLException JavaDoc
148   {
149     Query query = _db.parseQuery(sql);
150
151     _queryContext.setReturnGeneratedKeys(true);
152
153     return executeUpdate(query);
154   }
155   
156   public int executeUpdate(String JavaDoc sql, int []columnIndexes)
157     throws SQLException JavaDoc
158   {
159     Query query = _db.parseQuery(sql);
160
161     _queryContext.setReturnGeneratedKeys(true);
162
163     return executeUpdate(query);
164   }
165   
166   public int executeUpdate(String JavaDoc sql, String JavaDoc []columnNames)
167     throws SQLException JavaDoc
168   {
169     Query query = _db.parseQuery(sql);
170
171     _queryContext.setReturnGeneratedKeys(true);
172
173     return executeUpdate(query);
174   }
175
176   /**
177    * Executes the update for the given query.
178    *
179    * @return the number of rows modified
180    */

181   private int executeUpdate(Query query)
182     throws SQLException JavaDoc
183   {
184     Transaction xa = _conn.getTransaction();
185     boolean isOkay = false;
186     _queryContext.setTransaction(xa);
187
188     try {
189       query.execute(_queryContext, xa);
190       isOkay = true;
191     } finally {
192       if (! xa.isAutoCommit()) {
193       }
194       else if (isOkay)
195     xa.commit();
196       else
197     xa.rollback();
198     }
199     
200     return _queryContext.getRowUpdateCount();
201   }
202   
203   public java.sql.ResultSet JavaDoc getGeneratedKeys()
204   {
205     java.sql.ResultSet JavaDoc rs = _queryContext.getGeneratedKeysResultSet();
206
207     if (rs != null)
208       return rs;
209     else
210       return new NullResultSet();
211   }
212
213   public boolean execute(String JavaDoc sql)
214     throws SQLException JavaDoc
215   {
216     Query query = _db.parseQuery(sql);
217
218     if (query instanceof SelectQuery) {
219       executeQuery(query);
220
221       return true;
222     }
223     else {
224       executeUpdate(query);
225
226       return false;
227     }
228   }
229
230   public int[]executeBatch()
231     throws SQLException JavaDoc
232   {
233     return null;
234   }
235
236   public java.sql.ResultSet JavaDoc getResultSet()
237   {
238     return _rs;
239   }
240
241   public int getUpdateCount()
242   {
243     return _queryContext.getRowUpdateCount();
244   }
245
246   public java.sql.Connection JavaDoc getConnection()
247   {
248     return _conn;
249   }
250
251   public int getFetchDirection()
252   {
253     return 0;
254   }
255
256   public int getFetchSize()
257   {
258     return 0;
259   }
260
261   public int getMaxFieldSize()
262   {
263     return 0;
264   }
265
266   public int getMaxRows()
267   {
268     return 0;
269   }
270   
271   public void setMaxRows(int max)
272   {
273   }
274
275   public boolean getMoreResults()
276   {
277     return false;
278   }
279
280   public int getQueryTimeout()
281   {
282     return 0;
283   }
284
285   public int getResultSetConcurrency()
286   {
287     return 0;
288   }
289
290   public int getResultSetType()
291   {
292     return 0;
293   }
294
295   public SQLWarning JavaDoc getWarnings()
296   {
297     return null;
298   }
299
300   public void setCursorName(String JavaDoc name)
301   {
302   }
303
304   public void setEscapeProcessing(boolean enable)
305   {
306   }
307
308   public void setFetchDirection(int direction)
309   {
310   }
311
312   public void setFetchSize(int rows)
313   {
314   }
315
316   public void setMaxFieldSize(int max)
317   {
318   }
319   
320   public void setQueryTimeout(int seconds)
321   {
322   }
323
324   // jdk 1.4
325
public boolean getMoreResults(int count)
326   {
327     throw new UnsupportedOperationException JavaDoc();
328   }
329   
330   public boolean execute(String JavaDoc query, int foo)
331   {
332     throw new UnsupportedOperationException JavaDoc();
333   }
334   
335   public boolean execute(String JavaDoc query, int []foo)
336   {
337     throw new UnsupportedOperationException JavaDoc();
338   }
339   
340   public boolean execute(String JavaDoc query, String JavaDoc []foo)
341   {
342     throw new UnsupportedOperationException JavaDoc();
343   }
344
345   public int getResultSetHoldability()
346   {
347     throw new UnsupportedOperationException JavaDoc();
348   }
349
350   public void close()
351     throws SQLException JavaDoc
352   {
353     _db = null;
354
355     ResultSetImpl rs = _rs;
356     _rs = null;
357
358     QueryContext queryContext = _queryContext;
359     _queryContext = null;
360
361     if (rs != null)
362       rs.close();
363
364     _conn.closeStatement(this);
365
366     if (queryContext != null)
367       QueryContext.free(queryContext);
368   }
369 }
370
Popular Tags