KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > expression > expressionprimary > generalsetfunction


1 package com.daffodilwoods.daffodildb.server.sql99.expression.expressionprimary;
2
3 import com.daffodilwoods.daffodildb.server.sql99.common.*;
4 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*;
5 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.set.*;
6 import com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression.queryspecification.*;
7 import com.daffodilwoods.daffodildb.server.sql99.expression.*;
8 import com.daffodilwoods.daffodildb.server.sql99.expression.rowvalueexpression.*;
9 import com.daffodilwoods.daffodildb.server.sql99.token.*;
10 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
11 import com.daffodilwoods.database.resource.*;
12
13 public class generalsetfunction extends AbstractValueExpression implements setfunctionspecification, TypeConstants, SetOperatorConstants {
14    public Srightparen_1874859514 _Srightparen_18748595140;
15    public valueexpression _valueexpression1;
16    public setquantifier _Optsetquantifier2;
17    public Sleftparen653880241 _Sleftparen6538802413;
18    public setfunctiontype _setfunctiontype4;
19    public ColumnDetails[] columnDetails;
20    public ColumnDetails column;
21
22    public Object JavaDoc run(Object JavaDoc object) throws com.daffodilwoods.database.resource.DException {
23       try {
24         return ( (_BaseVariableValues) object).getColumnValues(column);
25       }
26       catch (DException ex) {
27         throw ex;
28       }
29    }
30
31    public ParameterInfo[] getParameterInfo() throws DException {
32       /* Done by Kaushik on 15/09/2004 to solve Bug No. 12004
33        * dst*/

34       ParameterInfo[] info=_valueexpression1.getParameterInfo();
35       String JavaDoc str=_setfunctiontype4.run(null).toString().toUpperCase();
36       if(str.startsWith("SUM") || str.startsWith("AVG")){
37          for (int i = 0; i < info.length; i++) {
38             if(info[i].getDataType()==-1)
39                info[i].setDataType(Datatypes.BIGDECIMAL);
40          }
41       }
42       return info;
43       /*dend*/
44
45   }
46
47    public _Aggregate getExecutable() throws com.daffodilwoods.database.resource.DException {
48       String JavaDoc typeOfFunction = (String JavaDoc) _setfunctiontype4.run(null);
49       int quantifier = ALL;
50       if (_Optsetquantifier2 != null) {
51          if ( ( (String JavaDoc) _Optsetquantifier2.run(null)).equalsIgnoreCase("Distinct")) {
52             quantifier = DISTINCT;
53          }
54       }
55       if (typeOfFunction.equalsIgnoreCase("SUM")) {
56          return getSumAggregate(quantifier);
57       }
58       if (typeOfFunction.equalsIgnoreCase("AVG")) {
59          return getAvgAggregate(quantifier);
60       }
61       if (typeOfFunction.equalsIgnoreCase("Count")) {
62          return getCountAggregate(quantifier);
63       }
64       if (typeOfFunction.equalsIgnoreCase("MAX")) {
65          return new AggregateMax(_valueexpression1);
66       }
67       if (typeOfFunction.equalsIgnoreCase("MIN")) {
68          return new AggregateMin(_valueexpression1);
69       }
70       throw new DException("DSE3560", null);
71    }
72
73    private _Aggregate getSumAggregate(int quantifier) throws DException {
74       switch (quantifier) {
75          case ALL:
76             return new AggregateSumAll(_valueexpression1);
77          case DISTINCT:
78             return new AggregateSumDistinct(_valueexpression1);
79       }
80       throw new DException("DSE3561", null);
81    }
82
83    private _Aggregate getAvgAggregate(int quantifier) throws DException {
84       switch (quantifier) {
85          case ALL:
86             return new AggregateAvgAll(_valueexpression1);
87          case DISTINCT:
88             return new AggregateAvgDistinct(_valueexpression1);
89       }
90       throw new DException("DSE3561", null);
91    }
92
93    private _Aggregate getCountAggregate(int quantifier) throws DException {
94       switch (quantifier) {
95          case ALL:
96             return new AggregateCountAll(_valueexpression1);
97          case DISTINCT:
98             return new AggregateCountDistinct(_valueexpression1);
99       }
100       throw new DException("DSE3561", null);
101    }
102
103    public ColumnDetails[] getChildColumnDetails() throws DException {
104       return columnDetails;
105    }
106
107    public AbstractRowValueExpression[] getChilds() {
108       AbstractRowValueExpression[] childs = new AbstractRowValueExpression[] { (AbstractRowValueExpression) (_valueexpression1)};
109       return childs;
110
111    }
112
113    public ColumnDetails[] getColumnDetails() throws DException {
114       if (column == null) {
115          column = new ColumnDetails();
116          column.setType(GROUPING);
117          column.setObject(this);
118          column.setColumn(new String JavaDoc[] {toString()}); // done by Gaurav check bug 4279 for that
119
column.setFunctionType( (String JavaDoc) _setfunctiontype4.run(null));
120          if (_Optsetquantifier2 != null) {
121             column.setQuantifier( (String JavaDoc) _Optsetquantifier2.run(null));
122          } else {
123             column.setQuantifier("ALL");
124          }
125          columnDetails = _valueexpression1.getColumnDetails();
126          checkForAggregateColumns(columnDetails);
127       }
128       return new ColumnDetails[] {column};
129    }
130
131    private void checkForAggregateColumns(ColumnDetails[] columnDetails) throws DException {
132       for (int i = 0, length = columnDetails.length; i < length; i++) {
133          int type = columnDetails[i].getType();
134          switch (type) {
135             case FUNCTIONAL:
136             case SCALARFUNCTION:
137             case CASEEXPRESSION:
138            case USERFUNCTION:
139                checkForAggregateColumns(columnDetails[i].getExistingColumnDetails());
140                break;
141             case GROUPING:
142                throw new DException("DSE4122", null);
143          }
144       }
145    }
146
147    public String JavaDoc toString() {
148       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
149       sb.append(" ");
150       sb.append(_setfunctiontype4);
151       sb.append(" ");
152       sb.append(_Sleftparen6538802413);
153       sb.append(" ");
154       if (_Optsetquantifier2 != null) {
155          sb.append(_Optsetquantifier2);
156       }
157       sb.append(" ");
158       sb.append(_valueexpression1);
159       sb.append(" ");
160       sb.append(_Srightparen_18748595140);
161       return sb.toString();
162    }
163
164    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
165
166       generalsetfunction tempClass = new generalsetfunction();
167       tempClass._Srightparen_18748595140 = (Srightparen_1874859514) _Srightparen_18748595140.clone();
168       tempClass._valueexpression1 = (valueexpression) _valueexpression1.clone();
169       if (_Optsetquantifier2 != null) {
170          tempClass._Optsetquantifier2 = (setquantifier) _Optsetquantifier2.clone();
171       }
172       tempClass._Sleftparen6538802413 = (Sleftparen653880241) _Sleftparen6538802413.clone();
173       tempClass._setfunctiontype4 = (setfunctiontype) _setfunctiontype4.clone();
174       return tempClass;
175    }
176
177    public ByteComparison getByteComparison(Object JavaDoc object) throws DException {
178       String JavaDoc functionType = (String JavaDoc) _setfunctiontype4.run(null);
179       int dataType = -1;
180       if (functionType.equalsIgnoreCase("Count")) {
181          dataType = LONG;
182       } else if (functionType.equalsIgnoreCase("Avg") || functionType.equalsIgnoreCase("Sum")) {
183          dataType = BIGDECIMAL;
184       } else {
185          dataType = getDataTypeForByte(_valueexpression1.getByteComparison(object));
186       }
187       return new ByteComparison(false, new int[] {dataType});
188    }
189
190    public void releaseResource() throws DException {
191
192    }
193 }
194
Popular Tags