AggregateCursor.java :  » Database-DBMS » Quadcap-Embeddable-Database » com » quadcap » sql » Java Open Source

Java Open Source » Database DBMS » Quadcap Embeddable Database 
Quadcap Embeddable Database » com » quadcap » sql » AggregateCursor.java
package com.quadcap.sql;

/* Copyright 1999 - 2003 Quadcap Software.  All rights reserved.
 *
 * This software is distributed under the Quadcap Free Software License.
 * This software may be used or modified for any purpose, personal or
 * commercial.  Open Source redistributions are permitted.  Commercial
 * redistribution of larger works derived from, or works which bundle
 * this software requires a "Commercial Redistribution License"; see
 * http://www.quadcap.com/purchase.
 *
 * Redistributions qualify as "Open Source" under  one of the following terms:
 *   
 *    Redistributions are made at no charge beyond the reasonable cost of
 *    materials and delivery.
 *
 *    Redistributions are accompanied by a copy of the Source Code or by an
 *    irrevocable offer to provide a copy of the Source Code for up to three
 *    years at the cost of materials and delivery.  Such redistributions
 *    must allow further use, modification, and redistribution of the Source
 *    Code under substantially the same terms as this license.
 *
 * Redistributions of source code must retain the copyright notices as they
 * appear in each source code file, these license terms, and the
 * disclaimer/limitation of liability set forth as paragraph 6 below.
 *
 * Redistributions in binary form must reproduce this Copyright Notice,
 * these license terms, and the disclaimer/limitation of liability set
 * forth as paragraph 6 below, in the documentation and/or other materials
 * provided with the distribution.
 *
 * The Software is provided on an "AS IS" basis.  No warranty is
 * provided that the Software is free of defects, or fit for a
 * particular purpose.  
 *
 * Limitation of Liability. Quadcap Software shall not be liable
 * for any damages suffered by the Licensee or any third party resulting
 * from use of the Software.
 */

import java.io.IOException;

import java.util.Vector;

import java.sql.SQLException;

import com.quadcap.util.Debug;

/**
 * Used in conjunction with a <code>GroupByCursor</code> to handle
 * aggregate functions (e.g., <b>SUM</b>, <b>AVG</b>, etc.) with
 * a <b>GROUP BY</b> clause.
 *
 * @author Stan Bailes
 */
public class AggregateCursor extends FilterCursor {
    GroupByCursor  gcursor        = null;
    Vector    aggregates      = null;
    Row      row        = null;
    int      currentRowNum      = 0;
    boolean     first        = true;
    boolean             isItemCursor                    = false;
    ItemsCursor         itemsCursor                     = null;
    Cursor              baseCursor                      = null;

    public AggregateCursor(Session session, Cursor cursor,
         GroupByCursor gcursor,
         Vector aggregates) throws SQLException {
  super(session, cursor);
        isItemCursor = (cursor instanceof ItemsCursor);
        if (isItemCursor) {
            itemsCursor = (ItemsCursor)cursor;
            baseCursor = itemsCursor.getBaseCursor();
        } else {
            baseCursor = cursor;
        }
  this.gcursor = gcursor;
  this.aggregates = aggregates;
    }

    public void resetAggregates() throws SQLException {
  if (aggregates != null) {
      for (int i = 0; i < aggregates.size(); i++) {
    try {
        ((AggregateExpression)aggregates.elementAt(i)).reset(session);
    } catch (IOException e) {
                    throw DbException.wrapThrowable(e);
    }
      }
  }
    }
    
    public void updateAggregates(Cursor c) throws SQLException {
  if (aggregates != null) {
      for (int i = 0; i < aggregates.size(); i++) {
                AggregateExpression ae =
                    (AggregateExpression)aggregates.elementAt(i);
                ae.updateAggregate(session, c);
      }
  }
    }
    
    public Row getRow() throws SQLException {
  return row;
    }

    public void updateRow(Row row) throws SQLException {
  throw new SQLException("Aggregate expressions aren't updateable",
             "42000");
    }

    public void deleteRow() throws SQLException {
  throw new SQLException("Aggregate expressions aren't updateable",
             "42000");
    }
    
    public void beforeFirst() throws SQLException {
        cursor.beforeFirst();
        first = true;
    }

    /**
     * This routine returns the next element of an aggregate cursor,
     * i.e., one which uses SUM, COUNT, AVG, MAX, or MIN.
     *
     * <p>
     * If GROUP BY is not specified, 'next()' simply fetches all of
     * the underlying rows, updating the aggregate expressions after
     * each row.  If there is a GROUP BY clause, we have to keep track
     * of when a new group starts using our associated group-by cursor.
     */
    public boolean next() throws SQLException {
        boolean any = false;
  boolean ret = first;
  first = false;
  resetAggregates();
  while (cursor.next()) {
            updateAggregates(baseCursor);
      ret = true;
      if (gcursor != null && gcursor.lastRowOfGroup()) {
                any = true;
    break;
      }
  }
  if (ret) {
            if (isItemCursor) {
                if (any) {
                    row = cursor.getRow();
                    if (row == null) {
                        row = itemsCursor.getEmptyAggregate();
                    }
                } else if (gcursor == null) {
                    row = itemsCursor.getEmptyAggregate();
                } else {
                    ret = false;
                }
            } else {
                ret = false;
            }
  }
  return ret;
    }

    public boolean isWritable(int col) throws SQLException {
  return false;
    }

    /**
     * We don't know the size.
     */
    public long size() { return -1; }

    public void close() throws SQLException {
  resetAggregates();
  cursor.close();
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.