KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > expression > numericvalueexpression > truncatefunction


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 truncatefunction extends AbstractNumericValueFunction {
16
17    public parencommanumericvalueexpression _parencommanumericvalueexpression0;
18    public SNONRESERVEDWORD136444255 _SNONRESERVEDWORD1364442551;
19
20    public Object JavaDoc run(Object JavaDoc object) throws com.daffodilwoods.database.resource.DException {
21       FieldBase[] res = GeneralPurposeStaticClass.changeIntoFieldBase( (Object JavaDoc[]) _parencommanumericvalueexpression0.run(object));
22       FieldBase result1 = res[0];
23       FieldBase result2 = res[1];
24       if (result1.isNull() || result2.isNull()) {
25          return new FieldLiteral(FieldUtility.NULLBUFFERRANGE, DOUBLE);
26       }
27       return getResult(getDataType(result1), result1.getObject(),
28                        getCount(result2.getObject(), getDataType(result2)));
29    }
30
31    public double truncate(double number, int places) throws DException {
32       double d = Math.pow(10, places);
33       return Math.floor(number * d) / d;
34    }
35
36    public BigDecimal truncate(BigDecimal number, int places) throws DException {
37      return number.setScale(places,BigDecimal.ROUND_DOWN);
38    }
39
40    public ColumnDetails[] getExistingColumnDetails() throws DException {
41       return columnDetails;
42    }
43
44    public ParameterInfo[] getParameterInfo() throws DException {
45      ParameterInfo[] paramInfo = super.getParameterInfo();
46      for (int i = 0; i < paramInfo.length; i++) {
47        if (paramInfo[i].getQuestionMark()) {
48          paramInfo[i].setDataType(Datatypes.BIGDECIMAL);
49          paramInfo[i].setName("TRUNCATE Arg");
50        }
51      }
52      return paramInfo;
53    }
54
55
56    protected ParameterInfo[] getThisParameterInfo() throws DException {
57       ParameterInfo parameterInfo = new ParameterInfo();
58       parameterInfo.setName(toString());
59       parameterInfo.setDataType(Datatypes.BIGDECIMAL);
60       return new ParameterInfo[] {
61           parameterInfo};
62    }
63
64
65    private Object JavaDoc getResult(int type, Object JavaDoc object, Integer JavaDoc object2) throws DException {
66       int value = object2.intValue();
67       switch (type) {
68          case TINYINT:
69          case BYTE:
70          case SHORT:
71          case SMALLINT:
72          case INT:
73          case INTEGER:
74            Number JavaDoc number = (Number JavaDoc) object;
75            return new FieldLiteral(new Integer JavaDoc(number.intValue()), Datatype.INTEGER);
76          case BIGINT:
77          case LONG:
78             Long JavaDoc longV = (Long JavaDoc) object;
79             return new FieldLiteral(longV, Datatype.LONG);
80          case REAL:
81             Float JavaDoc floatV = (Float JavaDoc) object;
82             return new FieldLiteral(new Float JavaDoc(truncate(floatV.doubleValue(), value)), Datatype.FLOAT);
83          case DOUBLE:
84          case FLOAT:
85          case DOUBLEPRECISION:
86             Double JavaDoc doubleV = (Double JavaDoc) object;
87             return new FieldLiteral(new Double JavaDoc(truncate(doubleV.doubleValue(), value)), Datatype.DOUBLE);
88          case BIGDECIMAL:
89          case DEC:
90          case DECIMAL:
91          case NUMERIC:
92             BigDecimal bigDecimal = (BigDecimal) object;
93             return new FieldLiteral(truncate(bigDecimal, value), Datatype.BIGDECIMAL);
94          default:
95             throw new DException("DSE514", new Object JavaDoc[] {StaticClass.getDataTypeName(type)});
96       }
97    }
98
99    private Integer JavaDoc getCount(Object JavaDoc object, int type) throws DException {
100       switch (type) {
101          case BYTE:
102          case TINYINT:
103          case INTEGER:
104          case INT:
105          case REAL:
106          case DOUBLE:
107          case FLOAT:
108          case DOUBLEPRECISION:
109          case LONG:
110          case BIGINT:
111          case SHORT:
112          case SMALLINT:
113          case BIGDECIMAL:
114          case DEC:
115          case DECIMAL:
116          case NUMERIC:
117             return new Integer JavaDoc( ( (Number JavaDoc) object).intValue());
118          case CHARACTER:
119          case VARCHAR:
120          case CHAR:
121          case CHARACTERVARYING:
122             String JavaDoc string = (String JavaDoc) object;
123             Double JavaDoc d = null;
124             try {
125                d = new Double JavaDoc(string);
126             } catch (Exception JavaDoc e) {
127                throw new DException("DSE4104", null);
128             }
129             return new Integer JavaDoc(d.intValue());
130       }
131       throw new DException("DSE4108",
132                            new Object JavaDoc[] {StaticClass.getDataTypeName(type),
133                            "TRUNCATE"});
134       /*switch(type){
135        case BYTE : case TINYINT :
136        case INTEGER : case INT :
137        case REAL :
138        case DOUBLE : case FLOAT : case DOUBLEPRECISION :
139        case LONG : case BIGINT :
140        case SHORT : case SMALLINT :
141        case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
142          Number operand = (Number)object;
143          return new Integer(operand.intValue());
144                 default :
145          throw new DException("DSE4108",new Object[]{StaticClass.getDataTypeName(type),"TRUNCATE"});
146              } */

147    }
148
149    /* private Integer getCount(Object object, int type) throws DException {
150         switch(type){
151           case BYTE : case TINYINT :
152               return new Integer(((Byte)object).intValue());
153             case INTEGER : case INT :
154               Integer integer = (Integer)object;
155               return new Integer(integer.intValue());
156            case REAL :
157               Float floatV = (Float)object;
158               return new Integer(floatV.intValue());
159             case DOUBLE : case FLOAT : case DOUBLEPRECISION :
160               Double doubleV = (Double)object;
161               return new Integer(doubleV.intValue());
162             case LONG : case BIGINT :
163               Long longV = (Long)object;
164               return new Integer(longV.intValue());
165             case SHORT : case SMALLINT :
166               Short shortV = (Short)object;
167               return new Integer(shortV.intValue());
168             case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
169               BigDecimal bigDecimal = (BigDecimal)object;
170               return new Integer(bigDecimal.intValue());
171            default :
172               throw new DException("DSE4108",new Object[]{StaticClass.getDataTypeName(type),"TRUNCATE"});
173         }
174      } */

175
176    public AbstractRowValueExpression[] getChilds() {
177       AbstractRowValueExpression[] childs = new AbstractRowValueExpression[] {_parencommanumericvalueexpression0};
178       return childs;
179    }
180
181    public String JavaDoc getType() throws DException {
182       return (String JavaDoc) _SNONRESERVEDWORD1364442551.run(null);
183    }
184
185    public String JavaDoc toString() {
186       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
187       sb.append(" ");
188       sb.append(_SNONRESERVEDWORD1364442551);
189       sb.append(_parencommanumericvalueexpression0);
190       return sb.toString();
191    }
192
193    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
194       truncatefunction tempClass = new truncatefunction();
195       tempClass._parencommanumericvalueexpression0 = (parencommanumericvalueexpression) _parencommanumericvalueexpression0.clone();
196       tempClass._SNONRESERVEDWORD1364442551 = (SNONRESERVEDWORD136444255) _SNONRESERVEDWORD1364442551.clone();
197       return tempClass;
198    }
199
200    public ByteComparison getByteComparison(Object JavaDoc object) throws DException {
201     ByteComparison byteComparison = new ByteComparison(false, new int[] {getDatatype(object)});
202     byteComparison.setSize(getColumnSize(object));
203     return byteComparison;
204
205    }
206
207
208    private int getDatatype(Object JavaDoc object) throws DException {
209           int type = getDataTypeForByte(_parencommanumericvalueexpression0.getByteComparison(object));
210           switch (type) {
211              case TINYINT:
212              case BYTE:
213              case SHORT:
214              case SMALLINT:
215              case INT:
216              case INTEGER:
217                 return INTEGER;
218              case BIGINT:
219              case LONG:
220                 return LONG;
221              case REAL:
222                 return FLOAT;
223              case DOUBLE:
224              case FLOAT:
225              case DOUBLEPRECISION:
226                 return DOUBLE;
227              /*In case of question mark I get data type -1
228                So to resolve the bug 11823 data type BigDecimal
229               is set. ---- Sube Singh
230               */

231              case -1 :
232              case BIGDECIMAL:
233              case DEC:
234              case DECIMAL:
235              case NUMERIC:
236                 return BIGDECIMAL;
237              default:
238                 throw new DException("DSE4108", new Object JavaDoc[] {StaticClass.getDataTypeName(type), "TRUNCATE"});
239           }
240        }
241
242
243    public _Reference[] checkSemantic(_ServerSession parent) throws DException {
244       _Reference[] ref = super.checkSemantic(parent);
245       if(ref!=null) {
246         return ref;
247        }
248       int type[] = _parencommanumericvalueexpression0.getByteComparison(parent).getDataTypes();
249
250       if (type[0] == -1 || type[0] <= 15) {
251          if (type[1] == -1 || type[1] <= 15) {
252             return ref;
253          }
254          throw new DException("DSE4108",
255                               new Object JavaDoc[] {StaticClass.getDataTypeName(type[1]),
256                               "TRUNCATE"});
257       }
258       throw new DException("DSE4108",
259                            new Object JavaDoc[] {StaticClass.getDataTypeName(type[0]),
260                            "TRUNCATE"});
261
262    }
263
264   public int getColumnSize(Object JavaDoc object) throws DException {
265        ColumnDetails[] columnDetails = getChildColumnDetails();
266        int type =columnDetails[0].getType();
267        if(type!=TypeConstants.CONSTANT) {
268          int datatype = columnDetails[0].getDatatype();
269           switch (datatype) {
270             case BYTE:
271               case TINYINT:
272               case SHORT:
273               case SMALLINT:
274                return SHORTSIZE;
275               case INTEGER:
276               case INT:
277                 return INTSIZE;
278               case REAL:
279                 return FLOATSIZE;
280               case DOUBLE:
281               case FLOAT:
282               case DOUBLEPRECISION:
283                return DOUBLESIZE;
284               case LONG:
285               case BIGINT:
286                 return LONGSIZE;
287                 default :
288               if (columnDetails[0].getSize() != -5) {
289                   return columnDetails[0].getSize();
290               }
291          }
292         } else if(columnDetails[0].getQuestion()) {
293           return Datatypes.DOUBLESIZE;
294         } else {
295           FieldBase[] field = GeneralPurposeStaticClass.changeIntoFieldBase( (
296               Object JavaDoc[]) _parencommanumericvalueexpression0.run(object));
297           field[0].setDatatype(columnDetails[0].getDatatype());
298           return field[0].getLength();
299         }
300         return -1;
301         }
302
303
304 }
305
Popular Tags