KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
42
43 import java.util.Vector JavaDoc;
44
45 import java.sql.SQLException JavaDoc;
46
47 import com.quadcap.util.Debug;
48
49 /**
50  * Used in conjunction with a <code>GroupByCursor</code> to handle
51  * aggregate functions (e.g., <b>SUM</b>, <b>AVG</b>, etc.) with
52  * a <b>GROUP BY</b> clause.
53  *
54  * @author Stan Bailes
55  */

56 public class AggregateCursor extends FilterCursor {
57     GroupByCursor gcursor = null;
58     Vector JavaDoc aggregates = null;
59     Row row = null;
60     int currentRowNum = 0;
61     boolean first = true;
62     boolean isItemCursor = false;
63     ItemsCursor itemsCursor = null;
64     Cursor baseCursor = null;
65
66     public AggregateCursor(Session session, Cursor cursor,
67                GroupByCursor gcursor,
68                Vector JavaDoc aggregates) throws SQLException JavaDoc {
69     super(session, cursor);
70         isItemCursor = (cursor instanceof ItemsCursor);
71         if (isItemCursor) {
72             itemsCursor = (ItemsCursor)cursor;
73             baseCursor = itemsCursor.getBaseCursor();
74         } else {
75             baseCursor = cursor;
76         }
77     this.gcursor = gcursor;
78     this.aggregates = aggregates;
79     }
80
81     public void resetAggregates() throws SQLException JavaDoc {
82     if (aggregates != null) {
83         for (int i = 0; i < aggregates.size(); i++) {
84         try {
85             ((AggregateExpression)aggregates.elementAt(i)).reset(session);
86         } catch (IOException JavaDoc e) {
87                     throw DbException.wrapThrowable(e);
88         }
89         }
90     }
91     }
92     
93     public void updateAggregates(Cursor c) throws SQLException JavaDoc {
94     if (aggregates != null) {
95         for (int i = 0; i < aggregates.size(); i++) {
96                 AggregateExpression ae =
97                     (AggregateExpression)aggregates.elementAt(i);
98                 ae.updateAggregate(session, c);
99         }
100     }
101     }
102     
103     public Row getRow() throws SQLException JavaDoc {
104     return row;
105     }
106
107     public void updateRow(Row row) throws SQLException JavaDoc {
108     throw new SQLException JavaDoc("Aggregate expressions aren't updateable",
109                    "42000");
110     }
111
112     public void deleteRow() throws SQLException JavaDoc {
113     throw new SQLException JavaDoc("Aggregate expressions aren't updateable",
114                    "42000");
115     }
116     
117     public void beforeFirst() throws SQLException JavaDoc {
118         cursor.beforeFirst();
119         first = true;
120     }
121
122     /**
123      * This routine returns the next element of an aggregate cursor,
124      * i.e., one which uses SUM, COUNT, AVG, MAX, or MIN.
125      *
126      * <p>
127      * If GROUP BY is not specified, 'next()' simply fetches all of
128      * the underlying rows, updating the aggregate expressions after
129      * each row. If there is a GROUP BY clause, we have to keep track
130      * of when a new group starts using our associated group-by cursor.
131      */

132     public boolean next() throws SQLException JavaDoc {
133         boolean any = false;
134     boolean ret = first;
135     first = false;
136     resetAggregates();
137     while (cursor.next()) {
138             updateAggregates(baseCursor);
139         ret = true;
140         if (gcursor != null && gcursor.lastRowOfGroup()) {
141                 any = true;
142         break;
143         }
144     }
145     if (ret) {
146             if (isItemCursor) {
147                 if (any) {
148                     row = cursor.getRow();
149                     if (row == null) {
150                         row = itemsCursor.getEmptyAggregate();
151                     }
152                 } else if (gcursor == null) {
153                     row = itemsCursor.getEmptyAggregate();
154                 } else {
155                     ret = false;
156                 }
157             } else {
158                 ret = false;
159             }
160     }
161     return ret;
162     }
163
164     public boolean isWritable(int col) throws SQLException JavaDoc {
165     return false;
166     }
167
168     /**
169      * We don't know the size.
170      */

171     public long size() { return -1; }
172
173     public void close() throws SQLException JavaDoc {
174     resetAggregates();
175     cursor.close();
176     }
177 }
178
Popular Tags