KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sqlmagic > tinysql > tinySQLStatement


1 /*
2  * tinySQLStatement
3  *
4  * Statement object for the tinySQL driver
5  *
6  * A lot of this code is based on or directly taken from
7  * George Reese's (borg@imaginary.com) mSQL driver.
8  *
9  * So, it's probably safe to say:
10  *
11  * Portions of this code Copyright (c) 1996 George Reese
12  *
13  * The rest of it:
14  *
15  * Copyright 1996, Brian C. Jepson
16  * (bjepson@ids.net)
17  * $Author: davis $
18  * $Date: 2004/12/18 21:28:47 $
19  * $Revision: 1.1 $
20  *
21  * This library is free software; you can redistribute it and/or
22  * modify it under the terms of the GNU Lesser General Public
23  * License as published by the Free Software Foundation; either
24  * version 2.1 of the License, or (at your option) any later version.
25  *
26  * This library is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29  * Lesser General Public License for more details.
30  *
31  * You should have received a copy of the GNU Lesser General Public
32  * License along with this library; if not, write to the Free Software
33  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34  *
35  */

36
37 package com.sqlmagic.tinysql;
38
39 import java.sql.SQLException JavaDoc;
40 import java.sql.SQLWarning JavaDoc;
41 import java.sql.ResultSet JavaDoc;
42 import java.sql.Connection JavaDoc;
43
44 /**
45  * @author Thomas Morgner <mgs@sherito.org> statementString contains the last
46  * used SQL-Query. Support for set/getFetchSize, ResultSets are created with a
47  * reference to the creating statement
48  */

49 public class tinySQLStatement implements java.sql.Statement JavaDoc {
50
51   /**
52    * Holds the last used queryString. execute() has to be synchronized,
53    * to guarantee thread-safety
54    */

55   private String JavaDoc statementString;
56   /**
57    *
58    * A connection object to execute queries and... stuff
59    *
60    */

61   private tinySQLConnection connection;
62
63   /**
64    *
65    * A result set returned from this query
66    *
67    */

68   private tinySQLResultSet result;
69
70   /**
71    *
72    * The max field size for tinySQL
73    * This can be pretty big, before things start to break.
74    *
75    */

76   private int max_field_size = 0;
77
78   /**
79    *
80    * The max rows supported by tinySQL
81    * I can't think of any limits, right now, but I'm sure some
82    * will crop up...
83    *
84    */

85   private int max_rows = 65536;
86
87   /**
88    *
89    * The number of seconds the driver will allow for a SQL statement to
90    * execute before giving up. The default is to wait forever (0).
91    *
92    */

93   private int timeout = 0;
94
95   /**
96    * How many rows to fetch in a single run. Default is now 4096 rows.
97    */

98   private int fetchsize = 4096;
99   /**
100    * Debug flag
101    */

102   private static boolean debug=false;
103   /**
104    *
105    * Constructs a new tinySQLStatement object.
106    * @param conn the tinySQLConnection object
107    *
108    */

109   public tinySQLStatement(tinySQLConnection conn) {
110
111     connection = conn;
112     if ( debug) System.out.println("connection is " + connection.toString());
113
114   }
115
116   /**
117    *
118    * Execute an SQL statement and return a result set.
119    * @see java.sql.Statement#executeQuery
120    * @exception SQLException raised for any errors
121    * @param sql the SQL statement string
122    * @return the result set from the query
123    *
124    */

125   public synchronized ResultSet JavaDoc executeQuery(String JavaDoc sql)
126        throws SQLException JavaDoc {
127
128     // tinySQL only supports one result set at a time, so
129
// don't let them get another one, just in case it's
130
// hanging out.
131
//
132
tinySQLResultSet trs;
133     result = null;
134     statementString = sql;
135
136     // create a new tinySQLResultSet with the tsResultSet
137
// returned from connection.executetinySQL()
138
//
139
if ( debug ) System.out.println("executeQuery conn is " + connection.toString());
140     trs = new tinySQLResultSet(connection.executetinySQL(this), this);
141     return trs;
142   }
143
144   /**
145    *
146    * Execute an update, insert, delete, create table, etc. This can
147    * be anything that doesn't return rows.
148    * @see java.sql.Statement#executeUpdate
149    * @exception java.sql.SQLException thrown when an error occurs executing
150    * the SQL
151    * @return either the row count for INSERT, UPDATE or DELETE or 0 for SQL statements that return nothing
152    */

153   public synchronized int executeUpdate(String JavaDoc sql) throws SQLException JavaDoc {
154
155     statementString = sql;
156     return connection.executetinyUpdate(this);
157
158   }
159
160   /**
161    *
162    * Executes some SQL and returns true or false, depending on
163    * the success. The result set is stored in result, and can
164    * be retrieved with getResultSet();
165    * @see java.sql.Statement#execute
166    * @exception SQLException raised for any errors
167    * @param sql the SQL to be executed
168    * @return true if there is a result set available
169    */

170   public boolean execute(String JavaDoc sql) throws SQLException JavaDoc {
171
172     // a result set object
173
//
174
tsResultSet r;
175
176     // execute the query
177
//
178
r = connection.executetinySQL(this);
179
180     // check for a null result set. If it wasn't null,
181
// use it to create a tinySQLResultSet, and return whether or
182
// not it is null (not null returns true).
183
//
184
if( r == null ) {
185       result = null;
186     } else {
187       result = new tinySQLResultSet(r, this);
188     }
189     return (result != null);
190
191   }
192
193   /**
194    * Returns the current query-String
195    */

196   public String JavaDoc getSQLString ()
197   {
198     return statementString;
199   }
200
201   /**
202    *
203    * Close any result sets. This is not used by tinySQL.
204    * @see java.sql.Statement#close
205    *
206    */

207   public void close() throws SQLException JavaDoc {
208   }
209
210   /**
211    *
212    * Returns the last result set
213    * @see java.sql.Statement#getResultSet
214    * @return null if no result set is available, otherwise a result set
215    *
216    */

217   public ResultSet JavaDoc getResultSet() throws SQLException JavaDoc {
218
219     ResultSet JavaDoc r;
220
221     r = result; // save the existing result set
222
result = null; // null out the existing result set
223
return r; // return the previously extant result set
224
}
225
226   /**
227    *
228    * Return the row count of the last operation. tinySQL does not support
229    * this, so it returns -1
230    * @see java.sql.Statement#getUpdateCount
231    * @return -1
232    */

233   public int getUpdateCount() throws SQLException JavaDoc {
234     return -1;
235   }
236
237   /**
238    *
239    * This returns true if there are any pending result sets. This
240    * should only be true after invoking execute()
241    * @see java.sql.Statement#getMoreResults
242    * @return true if rows are to be gotten
243    *
244    */

245   public boolean getMoreResults() throws SQLException JavaDoc {
246
247     return (result != null);
248
249   }
250
251   /**
252    *
253    * Get the maximum field size to return in a result set.
254    * @see java.sql.Statement#getMaxFieldSize
255    * @return the value of max field size
256    *
257    */

258   public int getMaxFieldSize() throws SQLException JavaDoc {
259     return max_field_size;
260   }
261
262   /**
263    *
264    * set the max field size.
265    * @see java.sql.Statement#setMaxFieldSize
266    * @param max the maximum field size
267    *
268    */

269   public void setMaxFieldSize(int max) throws SQLException JavaDoc {
270     max_field_size = max;
271   }
272
273   /**
274    *
275    * Get the maximum row count that can be returned by a result set.
276    * @see java.sql.Statement#getMaxRows
277    * @return the maximum rows
278    *
279    */

280   public int getMaxRows() throws SQLException JavaDoc {
281     return max_rows;
282   }
283
284   /**
285    *
286    * Get the maximum row count that can be returned by a result set.
287    * @see java.sql.Statement.setMaxRows
288    * @param max the max rows
289    *
290    */

291   public void setMaxRows(int max) throws SQLException JavaDoc {
292     max_rows = max;
293   }
294
295   /**
296    *
297    * If escape scanning is on (the default) the driver will do
298    * escape substitution before sending the SQL to the database.
299    * @see java.sql.Statement#setEscapeProcessing
300    * @param enable this does nothing right now
301    *
302    */

303   public void setEscapeProcessing(boolean enable)
304        throws SQLException JavaDoc {
305     throw new SQLException JavaDoc("The tinySQL Driver doesn't " +
306                            "support escape processing.");
307   }
308
309   /**
310    *
311    * Discover the query timeout.
312    * @see java.sql.Statement#getQueryTimeout
313    * @see setQueryTimeout
314    * @return the timeout value for this statement
315    *
316    */

317   public int getQueryTimeout() throws SQLException JavaDoc {
318     return timeout;
319   }
320
321   /**
322    *
323    * Set the query timeout.
324    * @see java.sql.Statement#setQueryTimeout
325    * @see getQueryTimeout
326    * @param x the new query timeout value
327    *
328    */

329   public void setQueryTimeout(int x) throws SQLException JavaDoc {
330     timeout = x;
331   }
332
333   /**
334    *
335    * This can be used by another thread to cancel a statement. This
336    * doesn't matter for tinySQL, as far as I can tell.
337    * @see java.sql.Statement#cancel
338    *
339    */

340   public void cancel() {
341   }
342
343   /**
344    *
345    * Get the warning chain associated with this Statement
346    * @see java.sql.Statement#getWarnings
347    * @return the chain of warnings
348    *
349    */

350   public final SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
351     return null;
352   }
353
354   /**
355    *
356    * Clear the warning chain associated with this Statement
357    * @see java.sql.Statement#clearWarnings
358    *
359    */

360   public void clearWarnings() throws SQLException JavaDoc {
361   }
362
363   /**
364    *
365    * Sets the cursor name for this connection. Presently unsupported.
366    *
367    */

368   public void setCursorName(String JavaDoc unused) throws SQLException JavaDoc {
369     throw new SQLException JavaDoc("tinySQL does not support cursors.");
370   }
371
372     //--------------------------JDBC 2.0-----------------------------
373

374
375     /**
376      * JDBC 2.0
377      *
378      * Gives the driver a hint as to the direction in which
379          * the rows in a result set
380      * will be processed. The hint applies only to result sets created
381      * using this Statement object. The default value is
382      * ResultSet.FETCH_FORWARD.
383      * <p>Note that this method sets the default fetch direction for
384          * result sets generated by this <code>Statement</code> object.
385          * Each result set has its own methods for getting and setting
386          * its own fetch direction.
387      * @param direction the initial direction for processing rows
388      * @exception SQLException if a database access error occurs
389          * or the given direction
390      * is not one of ResultSet.FETCH_FORWARD, ResultSet.FETCH_REVERSE, or
391      * ResultSet.FETCH_UNKNOWN
392      */

393     public void setFetchDirection(int direction) throws SQLException JavaDoc {
394       throw new SQLException JavaDoc("tinySQL does not support setFetchDirection.");
395     }
396
397     /**
398      * JDBC 2.0
399      *
400      * Retrieves the direction for fetching rows from
401          * database tables that is the default for result sets
402          * generated from this <code>Statement</code> object.
403          * If this <code>Statement</code> object has not set
404          * a fetch direction by calling the method <code>setFetchDirection</code>,
405          * the return value is implementation-specific.
406      *
407      * @return the default fetch direction for result sets generated
408          * from this <code>Statement</code> object
409      * @exception SQLException if a database access error occurs
410      */

411     public int getFetchDirection() throws SQLException JavaDoc {
412       throw new SQLException JavaDoc("tinySQL does not support getFetchDirection.");
413     }
414
415     /**
416      * JDBC 2.0
417      *
418      * Gives the JDBC driver a hint as to the number of rows that should
419      * be fetched from the database when more rows are needed. The number
420      * of rows specified affects only result sets created using this
421      * statement. If the value specified is zero, then the hint is ignored.
422      * The default value is zero.
423      *
424      * @param rows the number of rows to fetch
425      * @exception SQLException if a database access error occurs, or the
426      * condition 0 <= rows <= this.getMaxRows() is not satisfied.
427      */

428     public void setFetchSize(int rows) throws SQLException JavaDoc {
429       if ((rows <= 0) || (rows >= this.getMaxRows ()))
430               throw new SQLException JavaDoc ("Condition 0 <= rows <= this.getMaxRows() is not satisfied");
431     
432       fetchsize = rows;
433     }
434
435     /**
436      * JDBC 2.0
437      *
438      * Retrieves the number of result set rows that is the default
439          * fetch size for result sets
440          * generated from this <code>Statement</code> object.
441          * If this <code>Statement</code> object has not set
442          * a fetch size by calling the method <code>setFetchSize</code>,
443          * the return value is implementation-specific.
444      * @return the default fetch size for result sets generated
445          * from this <code>Statement</code> object
446      * @exception SQLException if a database access error occurs
447      */

448     public int getFetchSize() throws SQLException JavaDoc {
449       return fetchsize;
450     }
451
452     /**
453      * JDBC 2.0
454      *
455      * Retrieves the result set concurrency.
456      */

457     public int getResultSetConcurrency() throws SQLException JavaDoc {
458       throw new SQLException JavaDoc("tinySQL does not support ResultSet concurrency.");
459     }
460
461     /**
462      * JDBC 2.0
463      *
464      * Determine the result set type.
465      */

466     public int getResultSetType() throws SQLException JavaDoc {
467       throw new SQLException JavaDoc("tinySQL does not support getResultSetType.");
468     }
469
470     /**
471      * JDBC 2.0
472      *
473      * Adds a SQL command to the current batch of commmands for the statement.
474      * This method is optional.
475      *
476      * @param sql typically this is a static SQL INSERT or UPDATE statement
477      * @exception SQLException if a database access error occurs, or the
478      * driver does not support batch statements
479      */

480     public void addBatch( String JavaDoc sql ) throws SQLException JavaDoc {
481       throw new SQLException JavaDoc("tinySQL does not support addBatch.");
482     }
483
484     /**
485      * JDBC 2.0
486      *
487      * Makes the set of commands in the current batch empty.
488      * This method is optional.
489      *
490      * @exception SQLException if a database access error occurs or the
491      * driver does not support batch statements
492      */

493     public void clearBatch() throws SQLException JavaDoc {
494       throw new SQLException JavaDoc("tinySQL does not support clearBatch.");
495     }
496
497     /**
498      * JDBC 2.0
499      *
500      * Submits a batch of commands to the database for execution.
501      * This method is optional.
502      *
503      * @return an array of update counts containing one element for each
504      * command in the batch. The array is ordered according
505      * to the order in which commands were inserted into the batch.
506      * @exception SQLException if a database access error occurs or the
507      * driver does not support batch statements
508      */

509     public int[] executeBatch() throws SQLException JavaDoc {
510       throw new SQLException JavaDoc("tinySQL does not support executeBatch.");
511     }
512
513     /**
514      * JDBC 2.0
515      *
516      * Returns the <code>Connection</code> object
517          * that produced this <code>Statement</code> object.
518          * @return the connection that produced this statement
519      * @exception SQLException if a database access error occurs
520      */

521     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
522       throw new SQLException JavaDoc("tinySQL does not support getConnection.");
523     }
524
525 }
526
Popular Tags