1 package com.daffodilwoods.daffodildb.server.sql99.expression.numericvalueexpression; 2 3 import java.math.*; 4 5 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*; 6 import com.daffodilwoods.daffodildb.server.serversystem.*; 7 import com.daffodilwoods.daffodildb.server.sql99.common.*; 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.daffodildb.utils.*; 12 import com.daffodilwoods.daffodildb.utils.field.*; 13 import com.daffodilwoods.database.resource.*; 14 15 public class roundfunction extends AbstractNumericValueFunction { 16 17 public parencommanumericvalueexpression _parencommanumericvalueexpression0; 18 public SNONRESERVEDWORD136444255 _SNONRESERVEDWORD1364442551; 19 20 public Object run(Object object) throws com.daffodilwoods.database.resource.DException { 21 22 FieldBase[] res = GeneralPurposeStaticClass.changeIntoFieldBase( ( 23 Object []) _parencommanumericvalueexpression0.run(object)); 24 FieldBase result1 = res[0]; 25 FieldBase result2 = res[1]; 26 if (result1.isNull() || result2.isNull()) { 27 return new FieldLiteral(FieldUtility.NULLBUFFERRANGE, Datatype.BIGDECIMAL); 28 } 29 try { 30 return getResult(getDataType(result1), result1.getObject(), 31 getCount(result2, getDataType(result2))); 32 } 33 catch (DException ex) { 34 if(ex.getDseCode().equalsIgnoreCase("DSE5545")) { 35 throw new DException("DSE419", new Object [] {result2.getObject()}); 36 } 37 throw ex; 38 } 39 } 40 41 public ParameterInfo[] getParameterInfo() throws DException { 42 ParameterInfo[] paramInfo = super.getParameterInfo(); 43 for (int i = 0; i < paramInfo.length; i++) { 44 if (paramInfo[i].getQuestionMark()) { 45 paramInfo[i].setDataType(Datatypes.BIGDECIMAL); 46 paramInfo[i].setName("ROUND Arg"); 47 } 48 } 49 return paramInfo; 50 } 51 52 53 private Object getResult(int type, Object object, Integer result2) throws DException { 54 int places = result2.intValue(); 55 BigDecimal bigDecimal = null; 56 try { 57 switch (type) { 58 case BYTE: 59 case TINYINT: 60 case INTEGER: 61 case INT: 62 case REAL: 63 case DOUBLE: 64 case FLOAT: 65 case DOUBLEPRECISION: 66 case LONG: 67 case BIGINT: 68 case SHORT: 69 case SMALLINT: 70 return places <0 ? getResult((Number )object,places,false ):new FieldLiteral(new BigDecimal( ( (Number ) object).doubleValue()).setScale(places,BigDecimal.ROUND_HALF_EVEN),Datatype.BIGDECIMAL); 71 case BIGDECIMAL: 72 case DEC: 73 case DECIMAL: 74 case NUMERIC: 75 76 78 BigDecimal bigDecimal1 = (BigDecimal) object; 79 bigDecimal = new BigDecimal(bigDecimal1.doubleValue()); 80 int sc = bigDecimal1.scale(); 81 int u = places < sc ? places : sc; 82 return u < 0? getResult(bigDecimal,places,true) : new FieldLiteral( bigDecimal.setScale(places, bigDecimal.ROUND_HALF_EVEN), Datatype.BIGDECIMAL); 83 84 default: 85 throw new DException("DSE4108", new Object [] {StaticClass.getDataTypeName(type), "round"}); 86 } 87 } catch (Exception eee) { 88 throw new DException("DSE419", new Object [] {"ROUND"}); 89 } 90 } 91 92 133 134 private Integer getCount(FieldBase field, int type) throws DException { 135 Object object=field.getObject(); 136 switch (type) { 137 case BYTE: 138 case TINYINT: 139 case INTEGER: 140 case INT: 141 case SHORT: 142 case SMALLINT: 143 return new Integer (((Number )object).intValue()); 144 case REAL: 145 TypeValidityHandler.checkIntegerWithFloat(field); 146 return new Integer (((Number )object).intValue()); 147 case DOUBLE: 148 case FLOAT: 149 case DOUBLEPRECISION: 150 TypeValidityHandler.checkIntegerWithDouble(field); 151 return new Integer (((Number )object).intValue()); 152 case LONG: 153 case BIGINT: 154 TypeValidityHandler.checkIntegerWithNonDecimal(field); 155 return new Integer (((Number )object).intValue()); 156 case BIGDECIMAL: 157 case DEC: 158 case DECIMAL: 159 case NUMERIC: 160 TypeValidityHandler.checkIntegerWithBigDecimal(field); 161 return new Integer (((Number )object).intValue()); 162 case CHARACTER: 163 case VARCHAR: 164 case CHAR: 165 case CHARACTERVARYING: 166 String string = (String )object ; 167 Double d = null; 168 try { 169 d = new Double (string); 170 } catch (Exception e) { 171 throw new DException("DSE4104", new Object [] {"character string", "double"}); 172 } 173 return new Integer (d.intValue()); 174 default: 175 throw new DException("DSE4108", new Object [] {StaticClass.getDataTypeName(type), "round"}); 176 } 177 } 178 179 180 181 182 217 218 public AbstractRowValueExpression[] getChilds() { 219 AbstractRowValueExpression[] childs = new AbstractRowValueExpression[] {_parencommanumericvalueexpression0}; 220 return childs; 221 } 222 223 public String getType() throws DException { 224 return (String ) _SNONRESERVEDWORD1364442551.run(null); 225 } 226 227 public String toString() { 228 StringBuffer sb = new StringBuffer (); 229 sb.append(" "); 230 sb.append(_SNONRESERVEDWORD1364442551); 231 sb.append(_parencommanumericvalueexpression0); 232 return sb.toString(); 233 } 234 235 public Object clone() throws CloneNotSupportedException { 236 roundfunction tempClass = new roundfunction(); 237 tempClass._parencommanumericvalueexpression0 = (parencommanumericvalueexpression) _parencommanumericvalueexpression0.clone(); 238 tempClass._SNONRESERVEDWORD1364442551 = (SNONRESERVEDWORD136444255) _SNONRESERVEDWORD1364442551.clone(); 239 return tempClass; 240 } 241 242 public ByteComparison getByteComparison(Object object) throws DException { 243 ByteComparison byteComparison = new ByteComparison(false, new int[] {BIGDECIMAL}); 244 byteComparison.setSize(getColumnSize(object)); 245 return byteComparison; 246 } 247 248 249 257 258 private int getDatatype(Object object) throws DException { 259 int type = getDataTypeForByte(_parencommanumericvalueexpression0.getByteComparison(object)); 260 switch (type) { 261 case TINYINT: 262 case BYTE: 263 case SHORT: 264 case SMALLINT: 265 case INT: 266 case INTEGER: 267 return INTEGER; 268 case BIGINT: 269 case LONG: 270 return LONG; 271 case REAL: 272 return FLOAT; 273 case DOUBLE: 274 case FLOAT: 275 case DOUBLEPRECISION: 276 return DOUBLE; 277 281 case -1 : 282 case BIGDECIMAL: 283 case DEC: 284 case DECIMAL: 285 case NUMERIC: 286 return BIGDECIMAL; 287 default: 288 throw new DException("DSE4108", new Object [] {StaticClass.getDataTypeName(type), "ROUND"}); 289 } 290 } 291 292 293 public _Reference[] checkSemantic(_ServerSession parent) throws DException { 294 _Reference[] ref = super.checkSemantic(parent); 295 if(ref!=null) { 296 return ref; 297 } 298 int type[] = _parencommanumericvalueexpression0.getByteComparison(parent).getDataTypes(); 299 300 if (type[0] == -1 || type[0] <= 15) { 301 if (type[1] == -1 || type[1] <= 15) { 302 return ref; 303 } 304 throw new DException("DSE4108", 305 new Object [] {StaticClass.getDataTypeName(type[1]), 306 "ROUND"}); 307 } 308 throw new DException("DSE4108", 309 new Object [] {StaticClass.getDataTypeName(type[0]), 310 "ROUND"}); 311 312 } 313 314 public int getColumnSize(Object object) throws DException { 315 if(columnDetails[0].getType()!=TypeConstants.CONSTANT) { 316 int datatype = columnDetails[0].getDatatype(); 317 switch (datatype) { 318 case BYTE: 319 case TINYINT: 320 case SHORT: 321 case SMALLINT: 322 return SHORTSIZE; 323 case INTEGER: 324 case INT: 325 return INTSIZE; 326 case REAL: 327 return FLOATSIZE; 328 case DOUBLE: 329 case FLOAT: 330 case DOUBLEPRECISION: 331 return DOUBLESIZE; 332 case LONG: 333 case BIGINT: 334 return LONGSIZE; 335 default : 336 if (columnDetails[0].getSize() != -5) { 337 return columnDetails[0].getSize(); 338 } 339 340 } 341 } else if(columnDetails[0].getQuestion()){ 342 return Datatypes.DOUBLESIZE; 343 } else { 344 FieldBase[] field = GeneralPurposeStaticClass.changeIntoFieldBase( ( 345 Object []) _parencommanumericvalueexpression0.run(object)); 346 field[0].setDatatype(columnDetails[0].getDatatype()); 347 return field[0].getLength(); 348 } 349 return -1; 350 } 351 352 362 363 private Object getResult(Number number, int scale,boolean flag) { 364 Object obj=null; 365 if (flag) { 366 BigDecimal val = ( (BigDecimal) number); 367 BigDecimal D = new BigDecimal( (long) Math.pow(10, Math.abs(scale)) + ""); 368 obj = val.signum() == -1 ? 369 val.negate().divide(D, BigDecimal.ROUND_HALF_UP).setScale(0, 370 BigDecimal.ROUND_HALF_EVEN).multiply(D).negate() 371 : 372 val.divide(D, BigDecimal.ROUND_HALF_UP).setScale(0, BigDecimal.ROUND_HALF_EVEN). 373 multiply(D); 374 375 } 376 else { 377 double val = number.doubleValue(); 378 long D = (long) Math.pow(10, Math.abs(scale)); 379 obj = val < 0 ? new BigDecimal(Math.round(Math.abs(val) / D) * -D) : 380 new BigDecimal(Math.round(val / D) * D); 381 } 382 383 384 return new FieldLiteral(obj, Datatype.BIGDECIMAL); 385 } 386 387 } 388 | Popular Tags |