KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > execution > AggregateCountDistinct


1 package com.daffodilwoods.daffodildb.server.sql99.dql.execution;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.server.sql99.common.*;
6 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
7 import com.daffodilwoods.daffodildb.server.sql99.expression.*;
8 import com.daffodilwoods.daffodildb.utils.field.*;
9 import com.daffodilwoods.database.resource.*;
10 import com.daffodilwoods.daffodildb.server.sql99.expression.valueexpression;
11
12 /**
13  * <p>Title: AggregateCountDistinct </p>
14  * <p>Description:
15  * This class is responsible for retriving the number of distinct rows belonging to one
16  * group. Duplicate and Null values are ignored in count function evaluation. </p>
17  * <p>Copyright: Copyright (c) 2004</p>
18  * <p>Company: </p>
19  * @author not attributable
20  * @version 1.0
21  */

22 public class AggregateCountDistinct implements _Aggregate, Datatypes {
23
24   /**
25    * Variable representing the number of records involved in one group
26    */

27    private long count = 0;
28    /**
29     * List for caching distinct values involved in count function computation.
30     */

31    private ArrayList aList;
32    /**
33     * Instance variable representing the expression/column involed
34     * in the count function argument.
35     */

36    private valueexpression column;
37
38    /**
39     * Initailizes the value expression passed to COUNT function as a argument and
40     * a list for caching of distinct values taking part in count function evaluation
41     * for a particular group.
42     * @param column0 expression passed as argument
43     * @throws DException throws an exception when aggregate function is
44     * performed on column of data type BLOB, CLOB and Boolean data type.
45     */

46    public AggregateCountDistinct(valueexpression column0) throws DException {
47       column = column0;
48       GeneralPurposeStaticClass.checkForValidColumnsInAggregatesMaxMinAndCount(column0.getColumnDetails(), "COUNT");
49       if (column0.getCardinality() != 1) {
50          throw new DException("DSE4119", new Object JavaDoc[] {"AGGREGATE FUNCTION COUNT"});
51       }
52       aList = new ArrayList();
53    }
54
55    /**
56     * This method is required to initialize the number of row and to empty distinct
57     * values list maintained for a particular group.
58     * @throws DException
59     */

60    public void initialize() throws DException {
61       count = 0;
62       aList.clear();
63    }
64
65    /**
66     * This method is required to retrive distinct number of rows belonging to
67     * a particular group, corresponding to the column involved in count.
68     * @return the number of the records involved in the group.
69     * @throws DException
70     */

71    public Object JavaDoc getResult() throws DException {
72       return new FieldLiteral(new Long JavaDoc(count), LONG);
73    }
74
75    public void releaseResource() throws DException {
76    }
77    public valueexpression getValueExpression() {
78       return column;
79    }
80
81    /**
82     * This method is responsible to add a new record in to a group and to
83     * correspondingly update the number of rows in the group. DUPLICATE and
84     * NULL values are ingnored in count distinct function computation.
85     * @param iterator group by iterator retriving the values of columns
86     * involved in aggregate computation.
87     * @throws DException
88     */

89    public void addRecord(Object JavaDoc newObject) throws DException{
90      if (((FieldBase)newObject).isNull()) {
91         return;
92      }
93      if (!aList.contains(((FieldBase)newObject).getObject())) {
94         count++;
95         aList.add(((FieldBase)newObject).getObject());
96      }
97   }
98 }
99
Popular Tags