KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * tsResultSet.java - Result Set object for tinySQL.
3  *
4  * Copyright 1996, Brian C. Jepson
5  * (bjepson@ids.net)
6  * $Author: davis $
7  * $Date: 2004/12/18 21:26:18 $
8  * $Revision: 1.1 $
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */

24
25 package com.sqlmagic.tinysql;
26
27 import java.util.*;
28 import java.lang.*;
29 import java.io.*;
30 import java.sql.*;
31
32 /*
33  *
34  * tsResultSet - object to hold query results
35  *
36  * @author Thomas Morgner <mgs@sherito.org> Changed tsResultSet to support java.sql.Types,
37  * a custom fetchsize, and storing a state for unfinished queries.
38  * I also marked all members as private and use access-Functions to set or query
39  * this ResultSets properties.
40  */

41 public class tsResultSet {
42
43    private Vector rows;// = new Vector(); // all the rows
44
private Vector tsColumns;// = new Vector(); // all the tsColumn objects
45
private Vector columns; // SQL-Query columns
46
private Vector tables; // SQL-Query information
47
private tinySQLWhere whereC;
48  
49    private int fetchsize;
50    private int windowStart;
51    private int level;
52    private tinySQL dbengine;
53    private Hashtable sTables;
54    private Vector groupByColumns;
55    private Vector orderByColumns;
56    private String JavaDoc orderType;
57    private int type;
58    private boolean eof;
59    private boolean groupedColumns = false;
60    public String JavaDoc newLine = System.getProperty("line.separator");
61    
62    public tsResultSet (Vector t, Vector c, tinySQLWhere w, tinySQL dbeng)
63    {
64       dbengine = dbeng;
65       windowStart = 0;
66       tables = t;
67       columns = c;
68       whereC = w;
69       rows = new Vector ();
70       tsColumns = new Vector ();
71    }
72    public void setState (int pstate, Hashtable ptables, Vector inputGroupBy,
73                          Vector inputOrderBy,String JavaDoc inputOrderType)
74    {
75       sTables = ptables;
76       groupByColumns = inputGroupBy;
77       orderByColumns = inputOrderBy;
78       orderType = inputOrderType;
79       level = pstate;
80    }
81    public void setType (int type)
82    {
83       if ((type == ResultSet.TYPE_FORWARD_ONLY) ||
84         (type == ResultSet.TYPE_SCROLL_SENSITIVE) ||
85         (type == ResultSet.TYPE_SCROLL_INSENSITIVE))
86      
87       this.type = type;
88    }
89    public int getType ()
90    {
91       return type;
92    }
93    public void setFetchSize (int i)
94    {
95       fetchsize = i;
96    }
97    public int getFetchSize ()
98    {
99       return fetchsize;
100    }
101    public void addColumn (tsColumn col)
102    {
103       tsColumns.addElement (col);
104       if ( col.isGroupedColumn() ) groupedColumns = true;
105    }
106    public boolean isGrouped()
107    {
108       return groupedColumns;
109    }
110    public boolean getMoreResults (int newPos, int fetchsize)
111    {
112       this.fetchsize = fetchsize;
113       if (dbengine != null)
114       {
115          try
116          {
117             if (type != ResultSet.TYPE_SCROLL_INSENSITIVE)
118             {
119                rows.removeAllElements();
120                windowStart = newPos;
121             }
122         dbengine.contSelectStatement (this);
123         if (level != 0)
124             {
125                eof = false;
126            return eof;
127             }
128          } catch (tinySQLException e) {
129          }
130       }
131       eof = true;
132       return eof;
133    }
134    public boolean addRow (tsRow row)
135    {
136       return addRow(row,"ASC");
137    }
138    public boolean addRow (tsRow row,String JavaDoc orderType)
139    {
140       int i;
141       boolean sortUp=true;
142       tsRow sortRow;
143       if ( orderType != (String JavaDoc)null )
144          if ( orderType.startsWith("DESC") ) sortUp = false;
145       if ( rows.size() > 0 )
146       {
147 /*
148  * Insert or append the row depending upon the ORDER BY
149  * conditions and a comparison of the new and the existing row.
150  */

151          if ( sortUp )
152          {
153             for ( i = rows.size() -1; i > -1; i-- )
154             {
155                sortRow = (tsRow)rows.elementAt(i);
156                if ( row.compareTo(sortRow) < 0 ) continue;
157                if ( i == rows.size() - 1 )
158                   rows.addElement(row);
159                else
160                   rows.insertElementAt(row,i+1);
161                return true;
162             }
163          } else {
164             for ( i = rows.size() -1; i > -1; i-- )
165             {
166                sortRow = (tsRow)rows.elementAt(i);
167                if ( row.compareTo(sortRow) > 0 ) continue;
168                if ( i == rows.size() - 1 )
169                   rows.addElement(row);
170                else
171                   rows.insertElementAt(row,i+1);
172                return true;
173             }
174          }
175          rows.insertElementAt(row,0);
176          return true;
177       }
178       rows.addElement (row);
179       if ((fetchsize > 0) && (rows.size () >= fetchsize)) return false;
180       return true;
181    }
182 /*
183  * The following methods update a particular row in the result set.
184  */

185    public void updateRow(tsRow inputRow)
186    {
187       updateRow(inputRow,rows.size()-1);
188    }
189    public void updateRow(tsRow inputRow, int rowIndex)
190    {
191       rows.setElementAt(inputRow,rowIndex);
192    }
193    public Vector getTables ()
194    {
195       return tables;
196    }
197    public int getLevel ()
198    {
199       return level;
200    }
201    public void setLevel (int l)
202    {
203       level = l;
204    }
205    public Hashtable getTableState ()
206    {
207       return sTables;
208    }
209    public Vector getGroupByColumns ()
210    {
211       return groupByColumns;
212    }
213    public Vector getOrderByColumns ()
214    {
215       return orderByColumns;
216    }
217    public String JavaDoc getOrderType()
218    {
219       return orderType;
220    }
221    public tinySQLWhere getWhereClause ()
222    {
223       return whereC;
224    }
225 /*
226  * Returns the number of columns in the result set.
227  */

228    public int numcols()
229    {
230       return tsColumns.size();
231    }
232 /*
233  * Update all the columns in the ResultSet.
234  */

235    public void updateColumns(String JavaDoc inputColumnName,String JavaDoc inputColumnValue)
236       throws tinySQLException
237    {
238       int i;
239       tsColumn tcol;
240       for ( i = 0; i < tsColumns.size(); i++ )
241       {
242          tcol = (tsColumn)tsColumns.elementAt(i);
243          tcol.update(inputColumnName,inputColumnValue);
244       }
245    }
246 /*
247  * Returns the number of rows in the result set.
248  */

249    public int size()
250    {
251       return rows.size();
252    }
253 /*
254  * Returns the tsRow at a given row offset (starts with zero).
255  *
256  * @param i the row offset/index
257  */

258    public tsRow rowAt(int row)
259    {
260       int i;
261       if (row >= (windowStart + rows.size()))
262       {
263          getMoreResults (row, fetchsize);
264       }
265       i = row - windowStart;
266       if ( i < rows.size () )
267       {
268          return (tsRow) rows.elementAt(i);
269       }
270       return null;
271    }
272 /*
273  * Returns the tsColumn at a given column offset (starts with zero).
274  *
275  * @param i the column offset/index
276  */

277    public tsColumn columnAtIndex (int i)
278    {
279       return (tsColumn) tsColumns.elementAt(i);
280    }
281 /*
282  * Debugging method to dump out the result set
283  */

284    public String JavaDoc toString()
285    {
286       int i;
287       tsColumn tcol;
288       StringBuffer JavaDoc outputBuffer;
289 /*
290  * Display columns
291  */

292       outputBuffer = new StringBuffer JavaDoc(newLine + "Columns in ResultSet"
293       + newLine);
294       for ( i = 0; i < tsColumns.size(); i++ )
295       {
296          tcol = (tsColumn)tsColumns.elementAt(i);
297          outputBuffer.append(tcol.toString());
298       }
299       outputBuffer.append(newLine + "Rows in tsResultSet" + newLine);
300       for (i = 0; i < size(); i++)
301       {
302          tsRow row = rowAt(i);
303          outputBuffer.append(row.toString() + newLine);
304       }
305       return outputBuffer.toString();
306    }
307 }
308
Popular Tags