1 package com.daffodilwoods.daffodildb.server.sql99.expression.numericvalueexpression; 2 3 import java.math.*; 4 5 import com.daffodilwoods.daffodildb.server.sql99.common.*; 6 import com.daffodilwoods.daffodildb.server.sql99.expression.rowvalueexpression.*; 7 import com.daffodilwoods.daffodildb.server.sql99.token.*; 8 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 9 import com.daffodilwoods.daffodildb.utils.field.*; 10 import com.daffodilwoods.database.resource.*; 11 import com.daffodilwoods.daffodildb.server.serversystem._ServerSession; 12 13 public class absolutevalueexpression extends AbstractNumericValueFunction { 14 15 public parennumericvalueexpression _parennumericvalueexpression0; 16 public SNONRESERVEDWORD136444255 _SNONRESERVEDWORD1364442551; 17 private int dataType = -1; 18 19 public Object run(Object object) throws com.daffodilwoods.database.resource.DException { 20 FieldBase field = (FieldBase) _parennumericvalueexpression0.run(object); 21 if (field.isNull()) { 22 return field; 23 } 24 Object result1 = field.getObject(); 25 dataType = getDataType(field); 26 switch (dataType) { 27 case TINYINT: 28 case BYTE: 29 return new FieldLiteral(new Byte ( (byte) Math.abs( ( (Byte ) result1).byteValue())), dataType); 30 case SHORT: 31 case SMALLINT: 32 return new FieldLiteral(new Short ( (short) Math.abs( ( (Short ) result1).shortValue())), dataType); 33 case INT: 34 case INTEGER: 35 return new FieldLiteral(new Integer (Math.abs( ( (Integer ) result1).intValue())), dataType); 36 case BIGINT: 37 case LONG: 38 return new FieldLiteral(new Long (Math.abs( ( (Long ) result1).longValue())), dataType); 39 case REAL: 40 return new FieldLiteral(new Float (Math.abs( ( (Float ) result1).floatValue())), dataType); 41 case DOUBLE: 42 case DOUBLEPRECISION: 43 case FLOAT: 44 return new FieldLiteral(new Double (Math.abs( ( (Double ) result1).doubleValue())), dataType); 45 case BIGDECIMAL: 46 case DEC: 47 case DECIMAL: 48 case NUMERIC: 49 String value =getAbsoluteValueForBigDecimal(result1); 50 return new FieldLiteral(new BigDecimal(value), dataType); 51 } 52 throw new DException("DSE419", new Object [] {"ABS"}); 53 } 54 55 protected ParameterInfo[] getThisParameterInfo() throws DException { 56 ParameterInfo parameterInfo = new ParameterInfo(); 57 parameterInfo.setName(toString()); 58 parameterInfo.setDataType(BIGDECIMAL); 59 return new ParameterInfo[] {parameterInfo}; 60 } 61 62 public AbstractRowValueExpression[] getChilds() { 63 AbstractRowValueExpression[] childs = new AbstractRowValueExpression[] {_parennumericvalueexpression0}; 64 return childs; 65 } 66 67 public String getType() throws DException { 68 return (String ) _SNONRESERVEDWORD1364442551.run(null); 69 } 70 71 public String toString() { 72 StringBuffer sb = new StringBuffer (); 73 sb.append(" "); 74 sb.append(_SNONRESERVEDWORD1364442551); 75 sb.append(_parennumericvalueexpression0); 76 return sb.toString(); 77 } 78 79 public Object clone() throws CloneNotSupportedException { 80 absolutevalueexpression tempClass = new absolutevalueexpression(); 81 tempClass._parennumericvalueexpression0 = (parennumericvalueexpression) _parennumericvalueexpression0.clone(); 82 tempClass._SNONRESERVEDWORD1364442551 = (SNONRESERVEDWORD136444255) _SNONRESERVEDWORD1364442551.clone(); 83 return tempClass; 84 } 85 86 public ParameterInfo[] getParameterInfo() throws DException { 87 ParameterInfo[] paramInfo = super.getParameterInfo(); 88 for (int i = 0; i < paramInfo.length; i++) { 89 if (paramInfo[i].getQuestionMark()) { 90 paramInfo[i].setDataType(Datatypes.BIGDECIMAL); 91 paramInfo[i].setName("ABS Arg"); 92 } 93 } 94 return paramInfo; 95 } 96 97 public ByteComparison getByteComparison(Object object) throws DException { 98 if (dataType == -1) { 99 dataType = getDataTypeForByte(_parennumericvalueexpression0.getByteComparison(object)); 100 } 101 ByteComparison byteComparison = new ByteComparison(false, new int[] { dataType}); 102 byteComparison.setSize(getColumnSize(object)); 103 return byteComparison; 104 } 105 106 110 111 private int getDatatype(Object object) throws DException { 112 int type = getDataTypeForByte(_parennumericvalueexpression0.getByteComparison(object)); 113 switch (type) { 114 case TINYINT: 115 case BYTE: 116 case SHORT: 117 case SMALLINT: 118 case INT: 119 case INTEGER: 120 return INTEGER; 121 case BIGINT: 122 case LONG: 123 return LONG; 124 case REAL: 125 return FLOAT; 126 case DOUBLE: 127 case FLOAT: 128 case DOUBLEPRECISION: 129 return DOUBLE; 130 134 case -1 : 135 case BIGDECIMAL: 136 case DEC: 137 case DECIMAL: 138 case NUMERIC: 139 return BIGDECIMAL; 140 default: 141 throw new DException("DSE4108", new Object [] {StaticClass.getDataTypeName(type), "ABS"}); 142 } 143 } 144 145 146 public ColumnDetails[] getExistingColumnDetails() throws DException { 147 return columnDetails; 148 } 149 150 public int getColumnSize(Object object) throws DException { 151 ColumnDetails[] columnDetails = getChildColumnDetails(); 152 int size=-1; 153 if(columnDetails[0].getQuestion()) { 154 size = Datatypes.DOUBLESIZE; 155 } else if(columnDetails[0].getType()!=TypeConstants.CONSTANT) { 156 int datatype = columnDetails[0].getDatatype(); 157 switch (datatype) { 158 case BYTE: 159 case TINYINT: 160 case INTEGER: 161 case INT: 162 case REAL: 163 case DOUBLE: 164 case FLOAT: 165 case DOUBLEPRECISION: 166 size = INTSIZE; 167 case LONG: 168 case BIGINT: 169 size = LONGSIZE; 170 case SHORT: 171 case SMALLINT: 172 size = SHORTSIZE; 173 case BIGDECIMAL: 174 case DEC: 175 case DECIMAL: 176 case NUMERIC: 177 size = columnDetails[0].getSize(); 178 } 179 } 180 else { 181 FieldBase field = (FieldBase) _parennumericvalueexpression0.run(object); 182 field.setDatatype(columnDetails[0].getDatatype()); 183 size = field.getLength(); 184 } 185 return size; 186 } 187 188 private String getAbsoluteValueForBigDecimal(Object object) { 189 String number=object.toString(); 190 boolean minuscheck =number.startsWith("-"); 191 boolean puluscheck =number.startsWith("+"); 192 if(minuscheck) { 193 number = number.substring(number.indexOf("-")+1); 194 } else if(puluscheck) { 195 number = number.substring(number.indexOf("+")); 196 } 197 return number; 198 } 199 public _Reference[] checkSemantic(_ServerSession parent) throws DException { 200 _Reference[] ref = super.checkSemantic(parent); 201 if(ref!=null) { 202 return ref; 203 } 204 int type = _parennumericvalueexpression0.getByteComparison(parent).getDataTypes()[0]; 205 if (type == -1 || type <= 15) { 206 return ref; 207 } 208 throw new DException("DSE4108", new Object [] {StaticClass.getDataTypeName(type), "ABS"}); 209 } 210 211 } 212 | Popular Tags |