Quadcap Embeddable Database

com/quadcap/sql/AggregateCursor.java

Go to the documentation of this file.
00001 package com.quadcap.sql; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.IOException; 00042 00043 import java.util.Vector; 00044 00045 import java.sql.SQLException; 00046 00047 import com.quadcap.util.Debug; 00048 00049 /** 00050 * Used in conjunction with a <code>GroupByCursor</code> to handle 00051 * aggregate functions (e.g., <b>SUM</b>, <b>AVG</b>, etc.) with 00052 * a <b>GROUP BY</b> clause. 00053 * 00054 * @author Stan Bailes 00055 */ 00056 public class AggregateCursor extends FilterCursor { 00057 GroupByCursor gcursor = null; 00058 Vector aggregates = null; 00059 Row row = null; 00060 int currentRowNum = 0; 00061 boolean first = true; 00062 boolean isItemCursor = false; 00063 ItemsCursor itemsCursor = null; 00064 Cursor baseCursor = null; 00065 00066 public AggregateCursor(Session session, Cursor cursor, 00067 GroupByCursor gcursor, 00068 Vector aggregates) throws SQLException { 00069 super(session, cursor); 00070 isItemCursor = (cursor instanceof ItemsCursor); 00071 if (isItemCursor) { 00072 itemsCursor = (ItemsCursor)cursor; 00073 baseCursor = itemsCursor.getBaseCursor(); 00074 } else { 00075 baseCursor = cursor; 00076 } 00077 this.gcursor = gcursor; 00078 this.aggregates = aggregates; 00079 } 00080 00081 public void resetAggregates() throws SQLException { 00082 if (aggregates != null) { 00083 for (int i = 0; i < aggregates.size(); i++) { 00084 try { 00085 ((AggregateExpression)aggregates.elementAt(i)).reset(session); 00086 } catch (IOException e) { 00087 throw DbException.wrapThrowable(e); 00088 } 00089 } 00090 } 00091 } 00092 00093 public void updateAggregates(Cursor c) throws SQLException { 00094 if (aggregates != null) { 00095 for (int i = 0; i < aggregates.size(); i++) { 00096 AggregateExpression ae = 00097 (AggregateExpression)aggregates.elementAt(i); 00098 ae.updateAggregate(session, c); 00099 } 00100 } 00101 } 00102 00103 public Row getRow() throws SQLException { 00104 return row; 00105 } 00106 00107 public void updateRow(Row row) throws SQLException { 00108 throw new SQLException("Aggregate expressions aren't updateable", 00109 "42000"); 00110 } 00111 00112 public void deleteRow() throws SQLException { 00113 throw new SQLException("Aggregate expressions aren't updateable", 00114 "42000"); 00115 } 00116 00117 public void beforeFirst() throws SQLException { 00118 cursor.beforeFirst(); 00119 first = true; 00120 } 00121 00122 /** 00123 * This routine returns the next element of an aggregate cursor, 00124 * i.e., one which uses SUM, COUNT, AVG, MAX, or MIN. 00125 * 00126 * <p> 00127 * If GROUP BY is not specified, 'next()' simply fetches all of 00128 * the underlying rows, updating the aggregate expressions after 00129 * each row. If there is a GROUP BY clause, we have to keep track 00130 * of when a new group starts using our associated group-by cursor. 00131 */ 00132 public boolean next() throws SQLException { 00133 boolean any = false; 00134 boolean ret = first; 00135 first = false; 00136 resetAggregates(); 00137 while (cursor.next()) { 00138 updateAggregates(baseCursor); 00139 ret = true; 00140 if (gcursor != null && gcursor.lastRowOfGroup()) { 00141 any = true; 00142 break; 00143 } 00144 } 00145 if (ret) { 00146 if (isItemCursor) { 00147 if (any) { 00148 row = cursor.getRow(); 00149 if (row == null) { 00150 row = itemsCursor.getEmptyAggregate(); 00151 } 00152 } else if (gcursor == null) { 00153 row = itemsCursor.getEmptyAggregate(); 00154 } else { 00155 ret = false; 00156 } 00157 } else { 00158 ret = false; 00159 } 00160 } 00161 return ret; 00162 } 00163 00164 public boolean isWritable(int col) throws SQLException { 00165 return false; 00166 } 00167 00168 /** 00169 * We don't know the size. 00170 */ 00171 public long size() { return -1; } 00172 00173 public void close() throws SQLException { 00174 resetAggregates(); 00175 cursor.close(); 00176 } 00177 }