KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > sql > UserStatement


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.sql;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.L10N;
33
34 import java.sql.Connection JavaDoc;
35 import java.sql.ResultSet JavaDoc;
36 import java.sql.SQLException JavaDoc;
37 import java.sql.SQLWarning JavaDoc;
38 import java.sql.Statement JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41
42 /**
43  * User-view of a statement;
44  */

45 public class UserStatement implements Statement JavaDoc {
46   protected final static Logger JavaDoc log = Log.open(UserStatement.class);
47   protected final static L10N L = new L10N(UserStatement.class);
48
49   // The connection
50
protected UserConnection _conn;
51
52   // The underlying connection
53
protected Statement JavaDoc _stmt;
54
55   // True if the statement is changed in a way that forbids its caching.
56
protected boolean _isChanged;
57
58   UserStatement(UserConnection conn, Statement JavaDoc stmt)
59   {
60     _conn = conn;
61     _stmt = stmt;
62   }
63
64   /**
65    * Returns true if the statement is chanced in a way that forbids caching.
66    */

67   boolean isChanged()
68   {
69     return _isChanged;
70   }
71
72   /**
73    * Returns the underlying statement.
74    */

75   public Statement JavaDoc getStatement()
76   {
77     Statement JavaDoc stmt = _stmt;
78
79     if (stmt instanceof com.caucho.sql.spy.SpyStatement) {
80       stmt = ((com.caucho.sql.spy.SpyStatement)stmt).getStatement();
81     }
82
83     return stmt;
84   }
85
86   public void addBatch(String JavaDoc sql)
87     throws SQLException JavaDoc
88   {
89     _stmt.addBatch(sql);
90   }
91
92   public void cancel()
93     throws SQLException JavaDoc
94   {
95     _stmt.cancel();
96   }
97
98   public void clearBatch()
99     throws SQLException JavaDoc
100   {
101     _stmt.clearBatch();
102   }
103
104   public void clearWarnings()
105     throws SQLException JavaDoc
106   {
107     _stmt.clearWarnings();
108   }
109
110   /**
111    * Closes the statement.
112    */

113   public void close()
114     throws SQLException JavaDoc
115   {
116     Statement JavaDoc stmt = _stmt;
117     _stmt = null;
118
119     if (stmt != null) {
120       _conn.closeStatement(stmt);
121
122       stmt.close();
123     }
124   }
125
126   /**
127    * queries the database with the given sql.
128    */

129   public ResultSet JavaDoc executeQuery(String JavaDoc sql)
130     throws SQLException JavaDoc
131   {
132     return _stmt.executeQuery(sql);
133   }
134
135   /**
136    * updates the database with the given sql.
137    */

138   public int executeUpdate(String JavaDoc sql)
139     throws SQLException JavaDoc
140   {
141     return _stmt.executeUpdate(sql);
142   }
143
144   /**
145    * Execute an update with the given result type.
146    */

147   public int executeUpdate(String JavaDoc query, int resultType)
148     throws SQLException JavaDoc
149   {
150     return _stmt.executeUpdate(query, resultType);
151   }
152
153   /**
154    * Execute an update checking the given columns for primary keys.
155    */

156   public int executeUpdate(String JavaDoc query, int []columns)
157     throws SQLException JavaDoc
158   {
159     return _stmt.executeUpdate(query, columns);
160   }
161
162   /**
163    * Execute an update checking the given columns for primary keys.
164    */

165   public int executeUpdate(String JavaDoc query, String JavaDoc []columns)
166     throws SQLException JavaDoc
167   {
168     return _stmt.executeUpdate(query, columns);
169   }
170
171   /**
172    * executes the given sql.
173    */

174   public boolean execute(String JavaDoc sql)
175     throws SQLException JavaDoc
176   {
177     return _stmt.execute(sql);
178   }
179
180   /**
181    * executes the given query with its result type.
182    */

183   public boolean execute(String JavaDoc query, int resultType)
184     throws SQLException JavaDoc
185   {
186     return _stmt.execute(query, resultType);
187   }
188
189   /**
190    * executes the given query with the columns given for
191    * primary key generation.
192    */

193   public boolean execute(String JavaDoc query, int []columns)
194     throws SQLException JavaDoc
195   {
196     return _stmt.execute(query, columns);
197   }
198
199   /**
200    * executes the given query with the columns given for
201    * primary key generation.
202    */

203   public boolean execute(String JavaDoc query, String JavaDoc []columns)
204     throws SQLException JavaDoc
205   {
206     return _stmt.execute(query, columns);
207   }
208
209   /**
210    * Executes the batched sql.
211    */

212   public int[]executeBatch()
213     throws SQLException JavaDoc
214   {
215     return _stmt.executeBatch();
216   }
217
218   /**
219    * Returns the result set of the last query.
220    */

221   public java.sql.ResultSet JavaDoc getResultSet()
222     throws SQLException JavaDoc
223   {
224     return _stmt.getResultSet();
225   }
226
227   /**
228    * Returns the update count of the last query.
229    */

230   public int getUpdateCount()
231     throws SQLException JavaDoc
232   {
233     return _stmt.getUpdateCount();
234   }
235
236   /**
237    * Returns the underlying connection.
238    */

239   public Connection JavaDoc getConnection()
240     throws SQLException JavaDoc
241   {
242     return _conn;
243   }
244
245   /**
246    * Returns the current fetch direction.
247    */

248   public int getFetchDirection()
249     throws SQLException JavaDoc
250   {
251     return _stmt.getFetchDirection();
252   }
253
254   /**
255    * Sets the fetch direction.
256    */

257   public void setFetchDirection(int direction)
258     throws SQLException JavaDoc
259   {
260     _isChanged = true;
261
262     _stmt.setFetchDirection(direction);
263   }
264
265   /**
266    * Returns the fetch size.
267    */

268   public int getFetchSize()
269     throws SQLException JavaDoc
270   {
271     return _stmt.getFetchSize();
272   }
273
274   /**
275    * Sets the fetch size.
276    */

277   public void setFetchSize(int rows)
278     throws SQLException JavaDoc
279   {
280     _isChanged = true;
281
282     _stmt.setFetchSize(rows);
283   }
284
285   /**
286    * Returns the maximum field size.
287    */

288   public int getMaxFieldSize()
289     throws SQLException JavaDoc
290   {
291     return _stmt.getMaxFieldSize();
292   }
293
294   /**
295    * Sets the maximum field size.
296    */

297   public void setMaxFieldSize(int max)
298     throws SQLException JavaDoc
299   {
300     _isChanged = true;
301
302     _stmt.setMaxFieldSize(max);
303   }
304
305   /**
306    * Returns the maximum rows returned by a query.
307    */

308   public int getMaxRows()
309     throws SQLException JavaDoc
310   {
311     return _stmt.getMaxRows();
312   }
313
314   /**
315    * Sets the maximum rows returned by a query.
316    */

317   public void setMaxRows(int max)
318     throws SQLException JavaDoc
319   {
320     _isChanged = true;
321
322     _stmt.setMaxRows(max);
323   }
324
325   /**
326    * Returns true if more results are available.
327    */

328   public boolean getMoreResults()
329     throws SQLException JavaDoc
330   {
331     return _stmt.getMoreResults();
332   }
333
334   /**
335    * Returns the current query timeout.
336    */

337   public int getQueryTimeout()
338     throws SQLException JavaDoc
339   {
340     return _stmt.getQueryTimeout();
341   }
342
343   /**
344    * Sets the query timeout.
345    */

346   public void setQueryTimeout(int seconds)
347     throws SQLException JavaDoc
348   {
349     _isChanged = true;
350
351     _stmt.setQueryTimeout(seconds);
352   }
353
354   /**
355    * Returns the statement's result set concurrency setting.
356    */

357   public int getResultSetConcurrency()
358     throws SQLException JavaDoc
359   {
360     return _stmt.getResultSetConcurrency();
361   }
362
363   /**
364    * Returns the statement's result set type.
365    */

366   public int getResultSetType()
367     throws SQLException JavaDoc
368   {
369     return _stmt.getResultSetType();
370   }
371
372   /**
373    * Returns the current sql warnings.
374    */

375   public SQLWarning JavaDoc getWarnings()
376     throws SQLException JavaDoc
377   {
378     return _stmt.getWarnings();
379   }
380
381   /**
382    * Sets the current cursor name.
383    */

384   public void setCursorName(String JavaDoc name)
385     throws SQLException JavaDoc
386   {
387     _isChanged = true;
388
389     _stmt.setCursorName(name);
390   }
391
392   /**
393    * Enables escape processing.
394    */

395   public void setEscapeProcessing(boolean enable)
396     throws SQLException JavaDoc
397   {
398     _isChanged = true;
399
400     _stmt.setEscapeProcessing(enable);
401   }
402
403   /**
404    * Returns the next count results.
405    */

406   public boolean getMoreResults(int count)
407     throws SQLException JavaDoc
408   {
409     return _stmt.getMoreResults(count);
410   }
411
412   /**
413    * Returns the generated keys for the update.
414    */

415   public java.sql.ResultSet JavaDoc getGeneratedKeys()
416     throws SQLException JavaDoc
417   {
418     return _stmt.getGeneratedKeys();
419   }
420
421   /**
422    * Returns the result set holdability.
423    */

424   public int getResultSetHoldability()
425     throws SQLException JavaDoc
426   {
427     return _stmt.getResultSetHoldability();
428   }
429
430   public String JavaDoc toString()
431   {
432     return "UserStatement[" + _stmt + "]";
433   }
434 }
435
Popular Tags