KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > GroupByCursor


1 package com.quadcap.sql;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.ByteArrayInputStream JavaDoc;
42 import java.io.ByteArrayOutputStream JavaDoc;
43 import java.io.IOException JavaDoc;
44
45 import java.util.Vector JavaDoc;
46
47 import java.sql.SQLException JavaDoc;
48
49 import com.quadcap.sql.io.ObjectInputStream;
50 import com.quadcap.sql.io.ObjectOutputStream;
51
52 import com.quadcap.sql.index.BCursor;
53 import com.quadcap.sql.index.Btree;
54
55 import com.quadcap.sql.file.BlockFile;
56 import com.quadcap.sql.file.PageManager;
57
58 import com.quadcap.sql.types.Op;
59 import com.quadcap.sql.types.Value;
60
61 import com.quadcap.util.Debug;
62 import com.quadcap.util.Util;
63
64 /**
65  * Cursor to support SQL <b>GROUP BY</b>.
66  *
67  * @author Stan Bailes
68  */

69
70 public class GroupByCursor extends FilterCursor {
71     Vector JavaDoc groupBy = null;
72     int currentRowNum = 0;
73     byte[] prevKey = new byte[32];
74     int prevKeyLen = -1;
75     int[] gMap = null;
76     BCursor bc = null;
77     LazyRow prevRow = null;
78     LazyRow currRow = null;
79     boolean sameGroup = false;
80     boolean prev = false;
81     Key compare = null;
82     TempTable tempTable = null;
83
84     public GroupByCursor(Session session, Cursor cursor, Vector JavaDoc groupBy)
85     throws SQLException JavaDoc
86     {
87     super(session, cursor);
88     
89     this.currRow = new LazyRow(cursor.getColumnCount());
90     this.prevRow = new LazyRow(cursor.getColumnCount());
91     this.groupBy = groupBy;
92     this.gMap = new int[groupBy.size()];
93     this.compare = new Key(gMap.length);
94     for (int i = 0; i < gMap.length; i++) {
95         Column c = cursor.getColumn((String JavaDoc)groupBy.elementAt(i));
96         gMap[i] = c.getColumn();
97     }
98     try {
99         this.tempTable = new TempTable(session, new Key(gMap.length+1),
100                                            gMap);
101         tempTable.addRows(cursor);
102         this.bc = tempTable.getCursor();
103             beforeFirst();
104     } catch (IOException JavaDoc e) {
105         throw DbException.wrapThrowable(e);
106     }
107     }
108
109     public Row getRow() throws SQLException JavaDoc {
110     //Debug.println("getRow: " + prevRow);
111
return prevRow;
112     }
113
114     public void updateRow(Row row) throws SQLException JavaDoc {
115     throw new SQLException JavaDoc("GroupBy expressions aren't updateable",
116                    "42000");
117     }
118
119     public void deleteRow() throws SQLException JavaDoc {
120     throw new SQLException JavaDoc("GroupBy expressions aren't updateable",
121                    "42000");
122     }
123     
124     public void beforeFirst() throws SQLException JavaDoc {
125     try {
126         bc.beforeFirst();
127             prev = nextRow();
128     } catch (IOException JavaDoc e) {
129         throw DbException.wrapThrowable(e);
130     } catch (ClassNotFoundException JavaDoc e) {
131         throw DbException.wrapThrowable(e);
132     }
133     }
134
135     public boolean lastRowOfGroup() {
136     //Debug.println("lastRow: " + !sameGroup);
137
return !sameGroup;
138     }
139
140     public boolean next() throws SQLException JavaDoc {
141     try {
142         boolean ret = prev;
143         prev = nextRow();
144         return ret;
145     } catch (IOException JavaDoc e) {
146         throw DbException.wrapThrowable(e);
147     } catch (ClassNotFoundException JavaDoc e) {
148         throw DbException.wrapThrowable(e);
149     }
150     }
151     
152     public boolean nextRow()
153     throws SQLException JavaDoc, IOException JavaDoc, ClassNotFoundException JavaDoc
154     {
155     swapRows();
156         sameGroup = false;
157     if (!bc.next()) {
158         return false;
159     }
160         final byte[] key = bc.getKeyBuf();
161         final int len = bc.getKeyLen();
162         
163     if (prevKeyLen != -1) {
164         sameGroup = compare.compare(key, 0, len,
165                                         prevKey, 0, prevKeyLen) == 0;
166     }
167         prevKey = (byte[])Util.checkCapacity(prevKey, len);
168     prevKeyLen = len;
169         
170         System.arraycopy(key, 0, prevKey, 0, len);
171
172     tempTable.getRow(bc.getValBuf(), currRow);
173     return true;
174     }
175
176     final void swapRows() {
177     LazyRow temp = prevRow;
178     prevRow = currRow;
179     currRow = temp;
180
181     }
182
183     public boolean isWritable(int col) throws SQLException JavaDoc {
184     return false;
185     }
186
187     /**
188      * I honestly have no idea.
189      */

190     public long size() { return -1; }
191
192     public void close() throws SQLException JavaDoc {
193     try {
194         super.close();
195     } finally {
196             try {
197                 if (bc != null) bc.release();
198             } finally {
199                 bc = null;
200                 try {
201                     if (tempTable != null) tempTable.release();
202                 } catch (IOException JavaDoc e2) {
203                     throw DbException.wrapThrowable(e2);
204                 } finally {
205                     tempTable = null;
206                 }
207             }
208         }
209     }
210 }
211
Popular Tags