| 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 24 public class AggregateSumAll implements _Aggregate, Datatypes, IntegerPool { 25 26 30 protected Object result; 31 35 protected valueexpression column; 36 40 protected int dataType; 41 42 43 private boolean forQuestion; 44 45 55 public AggregateSumAll(valueexpression column0) throws DException { 56 column = column0; 57 if (column0.getCardinality() > 1) { 58 throw new DException("DSE4119", new Object [] {"AGGREGATE FUNCTION SUM"}); 59 } 60 61 try { 62 dataType=Datatypes.DOUBLE; 63 64 } catch (NullPointerException ex) { 65 throw ex; 66 } 67 68 if (column0.getCardinality() == -1) { 69 return; 70 } 71 72 73 74 75 76 GeneralPurposeStaticClass.checkForValidColumnsInAggregatesSumAndAvg(column0.getColumnDetails(),"SUM"); 77 forQuestion=true; 78 79 80 } 81 82 93 protected void getSum(Object 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 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 140 public Object 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 ) result).intValue()), BIGDECIMAL); 149 case LONG: 150 case BIGINT: 151 return new FieldLiteral(new BigDecimal("" + ( (Number ) result).longValue()), BIGDECIMAL); 152 case REAL: 153 case DOUBLE: 154 case FLOAT: 155 case DOUBLEPRECISION: 156 return new FieldLiteral(new BigDecimal("" + ( (Number ) 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 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 (0); 179 break; 180 case LONG: 181 case BIGINT: 182 result = new Long (0); 183 break; 184 case REAL: 185 result = new Float (0.0); 186 break; 187 case DOUBLE: 188 case FLOAT: 189 case DOUBLEPRECISION: 190 result = new Double (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 [] {new Integer (dataType)}); 206 } 207 } 208 209 public void releaseResource() throws DException { 210 } 211 212 public valueexpression getValueExpression() { 213 return column; 214 } 215 216 223 224 public void addRecord(Object 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 |