KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.sql99.dql.execution;
2
3 import java.math.*;
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.database.utility.*;
11 import com.daffodilwoods.daffodildb.server.sql99.expression.valueexpression;
12 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference;
13
14 /**
15  * <p>Title: AggregateSumAll </p>
16  * <p>Description:
17  * This class is responsible for computing SUM ALL aggregate
18  * function for a particular group of rows. </p>
19  * <p>Copyright: Copyright (c) 2004</p>
20  * <p>Company: </p>
21  * @author not attributable
22  * @version 1.0
23  */

24 public class AggregateSumAll implements _Aggregate, Datatypes, IntegerPool {
25
26   /**
27    * Instance variable representing the sum of all the values
28    * belonging to one group.
29    */

30    protected Object JavaDoc result;
31    /**
32     * Instance variable representing the expression/column involed
33     * in the sum function argument.
34     */

35    protected valueexpression column;
36    /**
37     * data type of valueexpression involed in aggregate
38     * function computation.
39     */

40    protected int dataType;
41
42
43   private boolean forQuestion;
44
45    /**
46     * Initializes the value expression and its data type.
47     * @param column0 represents the value expression passed as argument to
48     * SUM ALL function
49     * @throws DException throws an exception
50     * 1) when the cardinality of the value expression is more than one. SUM
51     * function can be performed on one column at one time.
52     * 2) when aggregate function is performed on column of
53     * data type character string, datetime and BLOB, CLOB and Boolean data type.
54     */

55    public AggregateSumAll(valueexpression column0) throws DException {
56       column = column0;
57        if (column0.getCardinality() > 1) {
58          throw new DException("DSE4119", new Object JavaDoc[] {"AGGREGATE FUNCTION SUM"});
59       }
60
61       try {
62             dataType=Datatypes.DOUBLE;
63
64           } catch (NullPointerException JavaDoc ex) {
65              throw ex;
66           }
67
68           if (column0.getCardinality() == -1) {
69                 return;
70               }
71
72 /*done by vibha to solve problem of ? in summ*/
73
74
75
76  GeneralPurposeStaticClass.checkForValidColumnsInAggregatesSumAndAvg(column0.getColumnDetails(),"SUM");
77  forQuestion=true;
78
79
80    }
81
82    /**
83     * This method performs the addition (sum) of value passed to the result i.e.
84     * sum of the values corresponding to one group. If the result value exceeds
85     * the maximum limit of result data type, data type of the result is promoted
86     * to next bigger datatype to retrive correct result.
87     * If there is numeric overflow after addition, an exception is thrown, which
88     * is catched, and value is added into sum after promoting data type.
89     * @param result0 previous sum of the values involved in the group
90     * @param newObject new value to be added into the sum
91     * @throws DException
92     */

93    protected void getSum(Object JavaDoc result0, FieldBase newObject) throws DException {
94       try {
95          result = GeneralPurposeStaticClass.computeSum(result0, newObject, dataType);
96       } catch (DException ex) {
97          if (ex.getDseCode().equals("DSE8101")) {
98             promoteDatatype();
99             getSum(result0, newObject);
100          } else {
101             throw ex;
102          }
103       }
104    }
105
106    /**
107     * This method is used, when addtion of values causes numeric overflow, this
108     * method sets the data types to next bigger range data type.
109     * @throws DException
110     */

111    protected void promoteDatatype() throws DException {
112       switch (dataType) {
113          case BYTE:
114          case TINYINT:
115          case INTEGER:
116          case INT:
117          case SHORT:
118          case SMALLINT:
119             dataType = LONG;
120             break;
121          case LONG:
122          case BIGINT:
123          case REAL:
124             dataType = DOUBLE;
125             break;
126          case DOUBLE:
127          case FLOAT:
128          case DOUBLEPRECISION:
129             dataType = BIGDECIMAL;
130             break;
131       }
132    }
133
134    /**
135     * This method is required to retrieve the SUM of all the values
136     * belonging to a particular group.
137     * @return the sum of all the records involved in the group.
138     * @throws DException
139     */

140    public Object JavaDoc getResult() throws DException {
141       switch (dataType) {
142          case BYTE:
143          case TINYINT:
144          case INTEGER:
145          case INT:
146          case SHORT:
147          case SMALLINT:
148             return new FieldLiteral(new BigDecimal("" + ( (Number JavaDoc) result).intValue()), BIGDECIMAL);
149          case LONG:
150          case BIGINT:
151             return new FieldLiteral(new BigDecimal("" + ( (Number JavaDoc) result).longValue()), BIGDECIMAL);
152          case REAL:
153          case DOUBLE:
154          case FLOAT:
155          case DOUBLEPRECISION:
156             return new FieldLiteral(new BigDecimal("" + ( (Number JavaDoc) result).doubleValue()), BIGDECIMAL);
157          case BIGDECIMAL:
158             return new FieldLiteral(result, dataType);
159          default:
160             return new FieldLiteral(result, dataType);
161       }
162    }
163
164
165    /**
166     * This method is used to initialize the sum of values involved in the group.
167     * It initializes the numeric values according to the result data type.
168     * @throws DException
169     */

170    public void initialize() throws DException {
171       switch (dataType) {
172          case BYTE:
173          case TINYINT:
174          case INTEGER:
175          case INT:
176          case SHORT:
177          case SMALLINT:
178             result = new Integer JavaDoc(0);
179             break;
180          case LONG:
181          case BIGINT:
182             result = new Long JavaDoc(0);
183             break;
184          case REAL:
185             result = new Float JavaDoc(0.0);
186             break;
187          case DOUBLE:
188          case FLOAT:
189          case DOUBLEPRECISION:
190             result = new Double JavaDoc(0.0);
191             break;
192          case DEC:
193          case DECIMAL:
194          case NUMERIC:
195          case BIGDECIMAL:
196             result = new BigDecimal(0.0);
197             break;
198          case CHARACTER:
199          case CHAR:
200          case VARCHAR:
201          case CHARACTERVARYING:
202          case CHARVARYING:
203             throw new DException("DSE945", null);
204          default:
205             throw new DException("DSE416", new Object JavaDoc[] {new Integer JavaDoc(dataType)});
206       }
207    }
208
209    public void releaseResource() throws DException {
210    }
211
212    public valueexpression getValueExpression() {
213       return column;
214    }
215
216    /**
217       * This method is responsible to add a new record into group and correspondingly
218       * updates the sum of the values.
219       * @param iterator group by iterator retriving the values of columns
220       * involved in aggregate computation.
221       * @throws DException
222       */

223
224    public void addRecord(Object JavaDoc newObject) throws DException{
225       if (((FieldBase)newObject).isNull()) {
226          return;
227       }
228        if(forQuestion){
229       if(!GeneralPurposeStaticClass.checkForIntAndDouble(((FieldBase)newObject).getDatatype())){
230         throw new DException("DSE773", null);
231       }
232     }
233
234     getSum(result, (FieldBase)newObject);
235
236  }
237 }
238
Popular Tags