KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > NumericConstantNode


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.NumericConstantNode
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.compile;
23
24 import org.apache.derby.iapi.sql.compile.C_NodeTypes;
25
26 import org.apache.derby.iapi.services.sanity.SanityManager;
27
28 import org.apache.derby.iapi.sql.compile.TypeCompiler;
29
30 import org.apache.derby.iapi.error.StandardException;
31
32 import org.apache.derby.iapi.services.compiler.MethodBuilder;
33 import org.apache.derby.iapi.services.info.JVMInfo;
34
35 import org.apache.derby.iapi.types.TypeId;
36 import org.apache.derby.iapi.types.DataTypeUtilities;
37 import org.apache.derby.iapi.types.NumberDataValue;
38
39 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
40
41 import org.apache.derby.iapi.util.ReuseFactory;
42
43 import java.sql.Types JavaDoc;
44
45 public final class NumericConstantNode extends ConstantNode
46 {
47     /**
48      * Initializer for a typed null node
49      *
50      * @param arg1 The TypeId for the type of node OR An object containing the value of the constant.
51      *
52      * @exception StandardException
53      */

54     public void init(Object JavaDoc arg1)
55         throws StandardException
56     {
57         int precision = 0, scal = 0, maxwidth = 0;
58         Boolean JavaDoc isNullable;
59         boolean valueInP; // value in Predicate-- if TRUE a value was passed in
60
TypeId typeId = null;
61         int typeid = 0;
62
63         if (arg1 instanceof TypeId)
64         {
65             typeId = (TypeId)arg1;
66             isNullable = Boolean.TRUE;
67             valueInP = false;
68             maxwidth = 0;
69         }
70
71         else
72         {
73             isNullable = Boolean.FALSE;
74             valueInP = true;
75         }
76
77         
78         switch (getNodeType())
79         {
80         case C_NodeTypes.TINYINT_CONSTANT_NODE:
81             precision = TypeId.SMALLINT_PRECISION;
82             scal = TypeId.SMALLINT_SCALE;
83             if (valueInP)
84             {
85                 maxwidth = TypeId.SMALLINT_MAXWIDTH;
86                 typeid = Types.TINYINT;
87                 setValue(getDataValueFactory().getDataValue((Byte JavaDoc) arg1));
88             }
89             break;
90
91         case C_NodeTypes.INT_CONSTANT_NODE:
92             precision = TypeId.INT_PRECISION;
93             scal = TypeId.INT_SCALE;
94             if (valueInP)
95             {
96                 maxwidth = TypeId.INT_MAXWIDTH;
97                 typeid = Types.INTEGER;
98                 setValue(getDataValueFactory().getDataValue((Integer JavaDoc) arg1));
99             }
100             break;
101
102         case C_NodeTypes.SMALLINT_CONSTANT_NODE:
103             precision = TypeId.SMALLINT_PRECISION;
104             scal = TypeId.SMALLINT_SCALE;
105             if (valueInP)
106             {
107                 maxwidth = TypeId.SMALLINT_MAXWIDTH;
108                 typeid = Types.SMALLINT;
109                 setValue(getDataValueFactory().getDataValue((Short JavaDoc) arg1));
110             }
111             break;
112
113         case C_NodeTypes.LONGINT_CONSTANT_NODE:
114             precision = TypeId.LONGINT_PRECISION;
115             scal = TypeId.LONGINT_SCALE;
116             if (valueInP)
117             {
118                 maxwidth = TypeId.LONGINT_MAXWIDTH;
119                 typeid = Types.BIGINT;
120                 setValue(getDataValueFactory().getDataValue((Long JavaDoc) arg1));
121             }
122             break;
123             
124         case C_NodeTypes.DECIMAL_CONSTANT_NODE:
125             if (valueInP)
126             {
127
128                 NumberDataValue constantDecimal = getDataValueFactory().getDecimalDataValue((String JavaDoc) arg1);
129
130                 typeid = Types.DECIMAL;
131                 precision = constantDecimal.getDecimalValuePrecision();
132                 scal = constantDecimal.getDecimalValueScale();
133                 /* be consistent with our convention on maxwidth, see also
134                  * exactNumericType(), otherwise we get format problem, b 3923
135                  */

136                 maxwidth = DataTypeUtilities.computeMaxWidth(precision, scal);
137                 setValue(constantDecimal);
138             }
139             else
140             {
141                 precision = TypeCompiler.DEFAULT_DECIMAL_PRECISION;
142                 scal = TypeCompiler.DEFAULT_DECIMAL_SCALE;
143                 maxwidth = TypeId.DECIMAL_MAXWIDTH;
144             }
145             break;
146                                                    
147         case C_NodeTypes.DOUBLE_CONSTANT_NODE:
148             precision = TypeId.DOUBLE_PRECISION;
149             scal = TypeId.DOUBLE_SCALE;
150             if (valueInP)
151             {
152                 maxwidth = TypeId.DOUBLE_MAXWIDTH;
153                 typeid = Types.DOUBLE;
154                 setValue(getDataValueFactory().getDataValue((Double JavaDoc) arg1));
155             }
156             break;
157
158         case C_NodeTypes.FLOAT_CONSTANT_NODE:
159             precision = TypeId.REAL_PRECISION;
160             scal = TypeId.REAL_SCALE;
161             if (valueInP)
162             {
163                 maxwidth = TypeId.REAL_MAXWIDTH;
164                 typeid = Types.REAL;
165                 setValue(
166                     getDataValueFactory().getDataValue((Float JavaDoc) arg1));
167             }
168             break;
169             
170         default:
171             if (SanityManager.DEBUG)
172             {
173                 // we should never really come here-- when the class is created
174
// it should have the correct nodeType set.
175
SanityManager.THROWASSERT(
176                                 "Unexpected nodeType = " + getNodeType());
177             }
178             break;
179         }
180         
181         super.init(
182                    (typeId != null) ? typeId :
183                      TypeId.getBuiltInTypeId(typeid),
184
185                    ReuseFactory.getInteger(precision),
186                    ReuseFactory.getInteger(scal),
187                    isNullable,
188                    ReuseFactory.getInteger(maxwidth));
189     }
190
191     /**
192      * Return an Object representing the bind time value of this
193      * expression tree. If the expression tree does not evaluate to
194      * a constant at bind time then we return null.
195      * This is useful for bind time resolution of VTIs.
196      * RESOLVE: What do we do for primitives?
197      *
198      * @return An Object representing the bind time value of this expression tree.
199      * (null if not a bind time constant.)
200      *
201      * @exception StandardException Thrown on error
202      */

203     Object JavaDoc getConstantValueAsObject()
204         throws StandardException
205     {
206         return value.getObject();
207     }
208
209         /**
210      * This generates the proper constant. It is implemented
211      * by every specific constant node (e.g. IntConstantNode).
212      *
213      * @param acb The ExpressionClassBuilder for the class being built
214      * @param mb The method the expression will go into
215      *
216      * @exception StandardException Thrown on error
217      */

218     void generateConstant(ExpressionClassBuilder acb, MethodBuilder mb)
219         throws StandardException
220     {
221         switch (getNodeType())
222         {
223         case C_NodeTypes.INT_CONSTANT_NODE:
224             mb.push(value.getInt());
225             break;
226         case C_NodeTypes.TINYINT_CONSTANT_NODE:
227             mb.push(value.getByte());
228             break;
229         case C_NodeTypes.SMALLINT_CONSTANT_NODE:
230             mb.push(value.getShort());
231             break;
232         case C_NodeTypes.DECIMAL_CONSTANT_NODE:
233             // No java.math.BigDecimal class in J2ME so the constant
234
// from the input SQL is handled directly as a String.
235
if (!JVMInfo.J2ME)
236                 mb.pushNewStart("java.math.BigDecimal");
237             mb.push(value.getString());
238             if (!JVMInfo.J2ME)
239                 mb.pushNewComplete(1);
240             break;
241         case C_NodeTypes.DOUBLE_CONSTANT_NODE:
242             mb.push(value.getDouble());
243             break;
244         case C_NodeTypes.FLOAT_CONSTANT_NODE:
245             mb.push(value.getFloat());
246             break;
247         case C_NodeTypes.LONGINT_CONSTANT_NODE:
248             mb.push(value.getLong());
249             break;
250         default:
251             if (SanityManager.DEBUG)
252             {
253                 // we should never really come here-- when the class is created
254
// it should have the correct nodeType set.
255
SanityManager.THROWASSERT(
256                           "Unexpected nodeType = " + getNodeType());
257             }
258         }
259     }
260 }
261
Popular Tags