KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.sql99.expression.numericvalueexpression;
2
3 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
4 import com.daffodilwoods.daffodildb.server.serversystem.*;
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.*;
10 import com.daffodilwoods.daffodildb.utils.field.*;
11 import com.daffodilwoods.database.resource.*;
12 import com.daffodilwoods.database.utility.P;
13
14 public class powerfunction extends AbstractNumericValueFunction {
15
16    public parencommanumericvalueexpression _parencommanumericvalueexpression0;
17    public SNONRESERVEDWORD136444255 _SNONRESERVEDWORD1364442551;
18
19    public Object JavaDoc run(Object JavaDoc object) throws com.daffodilwoods.database.resource.DException {
20       FieldBase[] res = GeneralPurposeStaticClass.changeIntoFieldBase( (Object JavaDoc[]) _parencommanumericvalueexpression0.run(object));
21       FieldBase result1 = res[0];
22       FieldBase result2 = res[1];
23       if (result1.isNull() || result2.isNull()) {
24          return new FieldLiteral(FieldUtility.NULLBUFFERRANGE, DOUBLE);
25       }
26       return getResult(getDataType(result1), getDataType(result2),
27                        result1.getObject(), result2.getObject());
28    }
29
30    public ColumnDetails[] getExistingColumnDetails() throws DException {
31       return columnDetails;
32    }
33
34    public ParameterInfo[] getParameterInfo() throws DException {
35      ParameterInfo[] paramInfo = super.getParameterInfo();
36      for (int i = 0; i < paramInfo.length; i++) {
37        if (paramInfo[i].getQuestionMark()) {
38            paramInfo[i].setDataType(Datatypes.DOUBLE);
39            paramInfo[i].setName("POWER Arg");
40
41        }
42      }
43      return paramInfo;
44    }
45
46
47    private Object JavaDoc getResult(int type1, int type2, Object JavaDoc result1, Object JavaDoc result2) throws DException {
48       if (type1 <= 15) {
49          if (type2 <= 15) {
50             return power( ( (Number JavaDoc) result1).doubleValue(),
51                          ( (Number JavaDoc) result2).doubleValue());
52          }
53          throw new DException("DSE4108", new Object JavaDoc[] {StaticClass.getDataTypeName(type2), "POWER"});
54       }
55       throw new DException("DSE4108", new Object JavaDoc[] {StaticClass.getDataTypeName(type1), "POWER"});
56       /*switch(type1){
57         case BYTE : case TINYINT :
58         case SHORT : case SMALLINT :
59         case INTEGER : case INT :
60         case LONG : case BIGINT :
61         case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
62         case REAL :
63         case DOUBLE : case FLOAT : case DOUBLEPRECISION :
64          double operand0 = ((Number)result1).doubleValue();
65          switch(type2){
66            case BYTE : case TINYINT :
67            case SHORT : case SMALLINT :
68            case INTEGER : case INT :
69            case LONG : case BIGINT :
70            case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
71            case REAL :
72            case DOUBLE : case FLOAT : case DOUBLEPRECISION :
73              return power(operand0,((Number)result2).doubleValue());
74            default:
75              throw new DException("DSE4108",new Object[]{StaticClass.getDataTypeName(type2),"POWER"});
76          }
77          default:
78            throw new DException("DSE4108",new Object[]{StaticClass.getDataTypeName(type1),"POWER"});
79         }*/

80    }
81
82    /*private Object getResult(int type1,int type2,Object result1,Object result2) throws DException {
83      switch(type1){
84        case BYTE : case TINYINT :
85          double operand0 = ((Byte)result1).doubleValue();
86          switch(type2){
87            case BYTE : case TINYINT :
88              return power(operand0,((Byte)result2).doubleValue());
89            case SHORT : case SMALLINT :
90              return power(operand0,((Short)result2).doubleValue());
91            case INTEGER : case INT :
92              return power(operand0,((Integer)result2).doubleValue());
93            case LONG : case BIGINT :
94              return power(operand0,((Long)result2).doubleValue());
95            case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
96              return power(operand0,((BigDecimal)result2).doubleValue());
97            case REAL :
98              return power(operand0,((Float)result2).doubleValue());
99            case DOUBLE : case FLOAT : case DOUBLEPRECISION :
100              return power(operand0,((Double)result2).doubleValue());
101            default:
102              throw new DException("DSE4108",new Object[]{StaticClass.getDataTypeName(type2),"POWER"});
103          }
104        case SHORT : case SMALLINT :
105          double operand = ((Short)result1).doubleValue();
106          switch(type2){
107            case BYTE : case TINYINT :
108              return power(operand,((Byte)result2).doubleValue());
109            case SHORT : case SMALLINT :
110              return power(operand,((Short)result2).doubleValue());
111            case INTEGER : case INT :
112              return power(operand,((Integer)result2).doubleValue());
113            case LONG : case BIGINT :
114              return power(operand,((Long)result2).doubleValue());
115            case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
116              return power(operand,((BigDecimal)result2).doubleValue());
117            case REAL :
118              return power(operand,((Float)result2).doubleValue());
119            case DOUBLE : case FLOAT : case DOUBLEPRECISION :
120              return power(operand,((Double)result2).doubleValue());
121            default:
122              throw new DException("DSE4108",new Object[]{StaticClass.getDataTypeName(type2),"POWER"});
123          }
124        case INTEGER : case INT :
125          double operand1 = ((Integer)result1).doubleValue();
126          switch(type2){
127            case BYTE : case TINYINT :
128              return power(operand1,((Byte)result2).doubleValue());
129            case SHORT : case SMALLINT :
130              return power(operand1,((Short)result2).doubleValue());
131            case INTEGER : case INT :
132              return power(operand1,((Integer)result2).doubleValue());
133            case LONG : case BIGINT :
134              return power(operand1,((Long)result2).doubleValue());
135            case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
136              return power(operand1,((BigDecimal)result2).doubleValue());
137            case REAL :
138              return power(operand1,((Float)result2).doubleValue());
139            case DOUBLE : case FLOAT : case DOUBLEPRECISION :
140              return power(operand1,((Double)result2).doubleValue());
141            default:
142              throw new DException("DSE4108",new Object[]{StaticClass.getDataTypeName(type2),"POWER"});
143          }
144        case LONG : case BIGINT :
145          double operand2 = ((Long)result1).doubleValue();
146          switch(type2){
147            case BYTE : case TINYINT :
148              return power(operand2,((Byte)result2).doubleValue());
149            case SHORT : case SMALLINT :
150              return power(operand2,((Short)result2).doubleValue());
151            case INTEGER : case INT :
152              return power(operand2,((Integer)result2).doubleValue());
153            case LONG : case BIGINT :
154              return power(operand2,((Long)result2).doubleValue());
155            case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
156              return power(operand2,((BigDecimal)result2).doubleValue());
157            case REAL :
158              return power(operand2,((Float)result2).doubleValue());
159            case DOUBLE : case FLOAT : case DOUBLEPRECISION :
160              return power(operand2,((Double)result2).doubleValue());
161            default:
162              throw new DException("DSE4108",new Object[]{StaticClass.getDataTypeName(type2),"POWER"});
163          }
164        case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
165          double operand3 = ((BigDecimal)result1).doubleValue();
166          switch(type2){
167            case BYTE : case TINYINT :
168              return power(operand3,((Byte)result2).doubleValue());
169            case SHORT : case SMALLINT :
170              return power(operand3,((Short)result2).doubleValue());
171            case INTEGER : case INT :
172              return power(operand3,((Integer)result2).doubleValue());
173            case LONG : case BIGINT :
174              return power(operand3,((Long)result2).doubleValue());
175            case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
176              return power(operand3,((BigDecimal)result2).doubleValue());
177            case REAL :
178              return power(operand3,((Float)result2).doubleValue());
179            case DOUBLE : case FLOAT : case DOUBLEPRECISION :
180              return power(operand3,((Double)result2).doubleValue());
181            default:
182              throw new DException("DSE4108",new Object[]{StaticClass.getDataTypeName(type2),"POWER"});
183          }
184        case REAL :
185          double operand4 = ((Float)result1).doubleValue();
186              switch(type2){
187                case BYTE : case TINYINT :
188                  return power(operand4,((Byte)result2).doubleValue());
189                case SHORT : case SMALLINT :
190                  return power(operand4,((Short)result2).doubleValue());
191                case INTEGER : case INT :
192                  return power(operand4,((Integer)result2).doubleValue());
193                case LONG : case BIGINT :
194                  return power(operand4,((Long)result2).doubleValue());
195                case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
196                  return power(operand4,((BigDecimal)result2).doubleValue());
197                case REAL :
198                  return power(operand4,((Float)result2).doubleValue());
199                case DOUBLE : case FLOAT : case DOUBLEPRECISION :
200                  return power(operand4,((Double)result2).doubleValue());
201                default:
202                  throw new DException("DSE4108",new Object[]{StaticClass.getDataTypeName(type2),"POWER"});
203              }
204            case DOUBLE : case FLOAT : case DOUBLEPRECISION :
205              double operand5 = ((Double)result1).doubleValue();
206              switch(type2){
207                case BYTE : case TINYINT :
208                  return power(operand5,((Byte)result2).doubleValue());
209                case SHORT : case SMALLINT :
210                  return power(operand5,((Short)result2).doubleValue());
211                case INTEGER : case INT :
212                  return power(operand5,((Integer)result2).doubleValue());
213                case LONG : case BIGINT :
214                  return power(operand5,((Long)result2).doubleValue());
215                case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
216                  return power(operand5,((BigDecimal)result2).doubleValue());
217                case REAL :
218                  return power(operand5,((Float)result2).doubleValue());
219                case DOUBLE : case FLOAT : case DOUBLEPRECISION :
220                  return power(operand5,((Double)result2).doubleValue());
221                default:
222                  throw new DException("DSE4108",new Object[]{StaticClass.getDataTypeName(type2),"POWER"});
223              }
224              default:
225                throw new DException("DSE4108",new Object[]{StaticClass.getDataTypeName(type1),"POWER"});
226        }
227     }*/

228
229    private Object JavaDoc power(double value1, double value2) throws DException {
230       return new FieldLiteral(new Double JavaDoc(Math.pow(value1, value2)), Datatype.DOUBLE);
231    }
232
233    public AbstractRowValueExpression[] getChilds() {
234       AbstractRowValueExpression[] childs = new AbstractRowValueExpression[] {_parencommanumericvalueexpression0};
235       return childs;
236    }
237
238    public String JavaDoc getType() throws DException {
239       return (String JavaDoc) _SNONRESERVEDWORD1364442551.run(null);
240    }
241
242    public String JavaDoc toString() {
243       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
244       sb.append(" ");
245       sb.append(_SNONRESERVEDWORD1364442551);
246       sb.append(_parencommanumericvalueexpression0);
247       return sb.toString();
248    }
249
250    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
251       powerfunction tempClass = new powerfunction();
252       tempClass._parencommanumericvalueexpression0 = (parencommanumericvalueexpression) _parencommanumericvalueexpression0.clone();
253       tempClass._SNONRESERVEDWORD1364442551 = (SNONRESERVEDWORD136444255) _SNONRESERVEDWORD1364442551.clone();
254       return tempClass;
255    }
256
257    protected ParameterInfo[] getThisParameterInfo() throws DException {
258       ParameterInfo parameterInfo = new ParameterInfo();
259       parameterInfo.setName(toString());
260       parameterInfo.setDataType(Datatypes.DOUBLE);
261       return new ParameterInfo[] {
262           parameterInfo};
263    }
264
265    public ByteComparison getByteComparison(Object JavaDoc object) throws DException {
266    ByteComparison byteComparison = new ByteComparison(false, new int[] {DOUBLE});
267    byteComparison.setSize(getColumnSize(object));
268    return byteComparison;
269    }
270
271   /* private int getDatatype(Object object) throws DException {
272      ByteComparison b = _parencommanumericvalueexpression0.getByteComparison(object);
273      int types0 = b.getDataTypes()[0];
274      if(types0 == -1){
275        Object[] obj = (Object[])b.getField();
276        FieldBase field = (FieldBase)obj[0];
277        field.setDatatype(-1);
278        types0 = field.getDatatype();
279      }
280      int types1 = b.getDataTypes()[1];
281      if(types1 == -1){
282        Object[] obj = (Object[])b.getField();
283        FieldBase field = (FieldBase)obj[1];
284        field.setDatatype(-1);
285        types1 = field.getDatatype();
286      }
287      switch(types0){
288        case BYTE : case TINYINT :
289          switch(types1){
290            case BYTE : case TINYINT :
291            case SHORT : case SMALLINT :
292            case INTEGER : case INT :
293            case LONG : case BIGINT :
294            case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
295              return BYTE;
296            case REAL :
297            case DOUBLE : case FLOAT : case DOUBLEPRECISION :
298            case CHARACTER :case VARCHAR: case CHAR: case CHARACTERVARYING:
299              return DOUBLE;
300          }
301        case SHORT : case SMALLINT :
302          switch(types1){
303            case BYTE : case TINYINT :
304            case SHORT : case SMALLINT :
305            case INTEGER : case INT :
306            case LONG : case BIGINT :
307            case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
308              return SHORT;
309            case REAL :
310            case DOUBLE : case FLOAT : case DOUBLEPRECISION :
311            case CHARACTER :case VARCHAR: case CHAR: case CHARACTERVARYING:
312              return DOUBLE;
313          }
314        case INTEGER : case INT :
315          switch(types1){
316            case BYTE : case TINYINT :
317            case SHORT : case SMALLINT :
318            case INTEGER : case INT :
319            case LONG : case BIGINT :
320            case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
321            case DOUBLE : case FLOAT : case DOUBLEPRECISION :
322              return INTEGER;
323            case REAL :
324              return FLOAT;
325            case CHARACTER :case VARCHAR: case CHAR: case CHARACTERVARYING:
326              return DOUBLE;
327          }
328        case LONG : case BIGINT :
329          switch(types1){
330            case BYTE : case TINYINT :
331            case SHORT : case SMALLINT :
332            case INTEGER : case INT :
333            case LONG : case BIGINT :
334            case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
335              return LONG;
336            case REAL :
337            case DOUBLE : case FLOAT : case DOUBLEPRECISION :
338            case CHARACTER :case VARCHAR: case CHAR: case CHARACTERVARYING:
339              return DOUBLE;
340          }
341        case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
342          switch(types1){
343            case BYTE : case TINYINT :
344            case SHORT : case SMALLINT :
345            case INTEGER : case INT :
346            case LONG : case BIGINT :
347            case BIGDECIMAL : case DEC : case DECIMAL : case NUMERIC :
348              return BIGDECIMAL;
349            case REAL :
350            case DOUBLE : case FLOAT : case DOUBLEPRECISION :
351            case CHARACTER :case VARCHAR: case CHAR: case CHARACTERVARYING:
352              return DOUBLE;
353          }
354        case REAL :
355        case DOUBLE : case FLOAT : case DOUBLEPRECISION :
356        case CHARACTER :case VARCHAR: case CHAR: case CHARACTERVARYING:
357          return DOUBLE;
358        default:
359          throw new DException("DSE419",new Object[]{"POWER"});
360      }
361     }*/

362    public _Reference[] checkSemantic(_ServerSession parent) throws DException {
363       _Reference[] ref = super.checkSemantic(parent);
364       if(ref!=null) {
365            return ref;
366        }
367       int type[] = _parencommanumericvalueexpression0.getByteComparison(parent).getDataTypes();
368       if (type[0] == -1 || type[0] <= 15) {
369          if (type[1] == -1 || type[1] <= 15) {
370             return ref;
371          }
372          throw new DException("DSE4108",
373                               new Object JavaDoc[] {StaticClass.getDataTypeName(type[1]),
374                               "POWER"});
375       }
376       throw new DException("DSE4108",
377                            new Object JavaDoc[] {StaticClass.getDataTypeName(type[0]),
378                            "POWER"});
379
380    }
381  public int getColumnSize(Object JavaDoc object) throws DException {
382          return Datatypes.DOUBLESIZE;
383       }
384
385 }
386
Popular Tags