KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > c3d > DCStatement


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.c3d;
56
57 import java.io.Serializable JavaDoc;
58 import java.sql.Connection JavaDoc;
59 import java.sql.ResultSet JavaDoc;
60 import java.sql.SQLException JavaDoc;
61 import java.sql.SQLWarning JavaDoc;
62 import java.sql.Statement JavaDoc;
63
64 import org.apache.log4j.Logger;
65
66 import org.lateralnz.common.util.Constants;
67 import org.lateralnz.common.util.DAOUtils;
68
69 import org.lateralnz.c3d.util.ResultWrapper;
70
71 public class DCStatement implements Statement JavaDoc, Serializable JavaDoc, Constants {
72   private static final Logger log = Logger.getLogger(DCStatement.class.getName());
73   
74   protected static final int STATEMENT = 1;
75   protected static final int PREPARED = 2;
76   protected static final int CALLABLE = 3;
77   
78   protected DCConnection conn; // connection used to create this statement
79
private String JavaDoc sql = EMPTY; // SQL we will execute
80
Statement JavaDoc realStatement = null; // a real statement for the actual db
81
protected int statementType; // what type of statement is this
82
protected ResultSet JavaDoc rs; // the result produced by execution
83
protected int updateCount = -1; // the num of rows updated
84
protected boolean nocaching = false; // should data not be cached
85

86   protected int[] insertKeyColumnIndexes = null; // passthrough vars
87
protected String JavaDoc[] insertKeyColumns = null;
88   protected int autoGeneratedKeys = Integer.MIN_VALUE;
89   private String JavaDoc cursorName = null;
90   private boolean escapeProcessing = true;
91   private int fetchDirection = Integer.MIN_VALUE;
92   private int fetchSize = Integer.MIN_VALUE;
93   private int maxFieldSize = Integer.MIN_VALUE;
94   private int maxRows = Integer.MIN_VALUE;
95   private int queryTimeout = Integer.MIN_VALUE;
96   protected int resultsetType = Integer.MIN_VALUE;
97   protected int resultsetConcurrency = Integer.MIN_VALUE;
98   protected int resultsetHoldability = Integer.MIN_VALUE;
99   
100  /**
101   * create a statement using the specified parameters. Note that resultset type,
102   * concurrency and holdability are basically ignored by this statement, but passed
103   * through if a real statement (connection to the DB) is required.
104   */

105   DCStatement(DCConnection conn, int resultsetType, int resultsetConcurrency, int resultsetHoldability) {
106     this.conn = conn;
107     this.resultsetType = resultsetType;
108     this.resultsetConcurrency = resultsetConcurrency;
109     this.resultsetHoldability = resultsetHoldability;
110     this.statementType = STATEMENT;
111   }
112   
113   protected Statement JavaDoc getRealStatement() throws SQLException JavaDoc {
114     if (realStatement == null) {
115       if (resultsetHoldability != Integer.MIN_VALUE && resultsetType != Integer.MIN_VALUE && resultsetConcurrency != Integer.MIN_VALUE) {
116         realStatement = conn.getRealConnection().createStatement(resultsetType, resultsetConcurrency, resultsetHoldability);
117       }
118       else if (resultsetType != Integer.MIN_VALUE && resultsetConcurrency != Integer.MIN_VALUE) {
119         realStatement = conn.getRealConnection().createStatement(resultsetType, resultsetConcurrency);
120       }
121       else {
122         realStatement = conn.getRealConnection().createStatement();
123       }
124       if (cursorName != null) {
125         realStatement.setCursorName(cursorName);
126       }
127       
128       realStatement.setEscapeProcessing(escapeProcessing);
129       
130       if (fetchDirection != Integer.MIN_VALUE) {
131         realStatement.setFetchDirection(fetchDirection);
132       }
133       
134       if (fetchSize != Integer.MIN_VALUE) {
135         realStatement.setFetchSize(fetchSize);
136       }
137       
138       if (maxFieldSize != Integer.MIN_VALUE) {
139         realStatement.setMaxFieldSize(maxFieldSize);
140       }
141       
142       if (maxRows != Integer.MIN_VALUE) {
143         realStatement.setMaxRows(maxRows);
144       }
145       
146       if (queryTimeout != Integer.MIN_VALUE) {
147         realStatement.setQueryTimeout(queryTimeout);
148       }
149     }
150     return realStatement;
151   }
152   
153   public void addBatch(String JavaDoc str) throws SQLException JavaDoc {
154     throw new SQLException JavaDoc("batch not supported");
155   }
156   
157   public void cancel() throws SQLException JavaDoc {
158     if (realStatement != null) {
159       realStatement.cancel();
160     }
161   }
162   
163   public void clearBatch() throws SQLException JavaDoc {
164     throw new SQLException JavaDoc("batch not supported");
165   }
166   
167   public void clearWarnings() throws SQLException JavaDoc {
168     if (realStatement != null) {
169       realStatement.clearWarnings();
170     }
171   }
172   
173   public void close() throws SQLException JavaDoc {
174     autoGeneratedKeys = Integer.MIN_VALUE;
175     insertKeyColumnIndexes = null;
176     insertKeyColumns = null;
177     
178     DAOUtils.close(realStatement);
179   }
180   
181   public boolean execute(String JavaDoc sql) throws SQLException JavaDoc {
182     setSQL(sql);
183     DatabaseEngine dbengine = conn.getDatabaseEngine();
184
185     ResultWrapper rw = dbengine.execute(this);
186     if (rw != null) {
187       this.rs = rw.rs;
188       this.updateCount = rw.updateCount;
189       if (log.isDebugEnabled()) {
190         log.debug("execute returned rs " + rs + ", updatecount " + updateCount);
191       }
192     }
193     
194     if (rs != null) {
195       return true;
196     }
197     else {
198       return false;
199     }
200   }
201   
202   public boolean execute(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc {
203     this.insertKeyColumns = columnNames;
204     return execute(sql);
205   }
206   
207   public boolean execute(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc {
208     this.insertKeyColumnIndexes = columnIndexes;
209     return execute(sql);
210   }
211   
212   public boolean execute(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc {
213     this.autoGeneratedKeys = autoGeneratedKeys;
214     return execute(sql);
215   }
216   
217   public int[] executeBatch() throws SQLException JavaDoc {
218     throw new SQLException JavaDoc("batch not supported");
219   }
220   
221   public ResultSet JavaDoc executeQuery(String JavaDoc sql) throws SQLException JavaDoc {
222     if (execute(sql)) {
223       return getResultSet();
224     }
225     else {
226       throw new SQLException JavaDoc("invalid query");
227     }
228   }
229   
230   public int executeUpdate(String JavaDoc sql) throws SQLException JavaDoc {
231     execute(sql);
232     return getUpdateCount();
233   }
234   
235   public int executeUpdate(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc {
236     execute(sql, columnNames);
237     return getUpdateCount();
238   }
239   
240   public int executeUpdate(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc {
241     execute(sql, autoGeneratedKeys);
242     return getUpdateCount();
243   }
244   
245   public int executeUpdate(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc {
246     execute(sql, columnIndexes);
247     return getUpdateCount();
248   }
249   
250   public Connection JavaDoc getConnection() throws SQLException JavaDoc {
251     return conn;
252   }
253   
254   public int getFetchDirection() throws SQLException JavaDoc {
255     return ResultSet.FETCH_FORWARD;
256   }
257   
258   public int getFetchSize() throws SQLException JavaDoc {
259     if (realStatement != null) {
260       return realStatement.getFetchSize();
261     }
262     else {
263       return 10;
264     }
265   }
266   
267   public ResultSet JavaDoc getGeneratedKeys() throws SQLException JavaDoc {
268     if (realStatement != null) {
269       return realStatement.getGeneratedKeys();
270     }
271     else {
272       return new DCResultSet(null, null, null, null, ResultSet.CONCUR_READ_ONLY, ResultSet.TYPE_FORWARD_ONLY);
273     }
274   }
275   
276   public int getMaxFieldSize() throws SQLException JavaDoc {
277     return 0;
278   }
279   
280   public int getMaxRows() throws SQLException JavaDoc {
281     return 0;
282   }
283   
284   public boolean getMoreResults() throws SQLException JavaDoc {
285     return false;
286   }
287   
288   public boolean getMoreResults(int param) throws SQLException JavaDoc {
289     return false;
290   }
291   
292   public int getQueryTimeout() throws SQLException JavaDoc {
293     if (realStatement != null) {
294       return realStatement.getQueryTimeout();
295     }
296     else {
297       return 0;
298     }
299   }
300   
301   public ResultSet JavaDoc getResultSet() throws SQLException JavaDoc {
302     return rs;
303   }
304   
305   public int getResultSetConcurrency() throws SQLException JavaDoc {
306     return resultsetConcurrency;
307   }
308   
309   public int getResultSetHoldability() throws SQLException JavaDoc {
310     return resultsetHoldability;
311   }
312   
313   public int getResultSetType() throws SQLException JavaDoc {
314     return resultsetType;
315   }
316   
317   protected String JavaDoc getSQL() {
318     return sql;
319   }
320   
321   public int getUpdateCount() throws SQLException JavaDoc {
322     return updateCount;
323   }
324   
325   public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
326     if (realStatement != null) {
327       return realStatement.getWarnings();
328     }
329     else {
330       return null;
331     }
332   }
333   
334   public void setCursorName(String JavaDoc str) throws SQLException JavaDoc {
335     this.cursorName = str;
336     if (realStatement != null) {
337       realStatement.setCursorName(str);
338     }
339   }
340   
341   public void setEscapeProcessing(boolean param) throws SQLException JavaDoc {
342     this.escapeProcessing = param;
343     if (realStatement != null) {
344       realStatement.setEscapeProcessing(param);
345     }
346   }
347   
348   public void setFetchDirection(int param) throws SQLException JavaDoc {
349     this.fetchDirection = param;
350     if (realStatement != null) {
351       realStatement.setFetchDirection(param);
352     }
353   }
354   
355   public void setFetchSize(int param) throws SQLException JavaDoc {
356     this.fetchSize = param;
357     if (realStatement != null) {
358       realStatement.setFetchSize(param);
359     }
360   }
361   
362   public void setMaxFieldSize(int param) throws SQLException JavaDoc {
363     this.maxFieldSize = param;
364     if (realStatement != null) {
365       realStatement.setMaxFieldSize(maxFieldSize);
366     }
367   }
368   
369   public void setMaxRows(int param) throws SQLException JavaDoc {
370     this.maxRows = param;
371     if (realStatement != null) {
372       realStatement.setMaxRows(param);
373     }
374   }
375   
376   public void setQueryTimeout(int param) throws SQLException JavaDoc {
377         this.queryTimeout = param;
378     if (realStatement != null) {
379       realStatement.setQueryTimeout(param);
380     }
381   }
382   
383   protected void setSQL(String JavaDoc sql) {
384     this.sql = sql;
385   }
386 }
Popular Tags