KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.BinaryArithmeticOperatorNode
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.services.sanity.SanityManager;
25
26 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
27
28 import org.apache.derby.iapi.sql.compile.C_NodeTypes;
29
30 import org.apache.derby.iapi.types.TypeId;
31
32 import org.apache.derby.iapi.types.DataTypeDescriptor;
33 import org.apache.derby.iapi.types.NumberDataValue;
34
35 import org.apache.derby.iapi.sql.compile.TypeCompiler;
36
37 import org.apache.derby.iapi.error.StandardException;
38
39 import org.apache.derby.impl.sql.compile.ActivationClassBuilder;
40 import org.apache.derby.iapi.reference.ClassName;
41
42 import java.sql.Types JavaDoc;
43
44 import java.util.Vector JavaDoc;
45
46 /**
47  * This node represents a binary arithmetic operator, like + or *.
48  *
49  * @author Jeff Lichtman
50  */

51
52 public final class BinaryArithmeticOperatorNode extends BinaryOperatorNode
53 {
54     /**
55      * Initializer for a BinaryArithmeticOperatorNode
56      *
57      * @param leftOperand The left operand
58      * @param rightOperand The right operand
59      */

60
61     public void init(
62                     Object JavaDoc leftOperand,
63                     Object JavaDoc rightOperand)
64     {
65         super.init(leftOperand, rightOperand,
66                 ClassName.NumberDataValue, ClassName.NumberDataValue);
67     }
68
69     public void setNodeType(int nodeType)
70     {
71         String JavaDoc operator = null;
72         String JavaDoc methodName = null;
73
74         switch (nodeType)
75         {
76             case C_NodeTypes.BINARY_DIVIDE_OPERATOR_NODE:
77                 operator = TypeCompiler.DIVIDE_OP;
78                 methodName = "divide";
79                 break;
80
81             case C_NodeTypes.BINARY_MINUS_OPERATOR_NODE:
82                 operator = TypeCompiler.MINUS_OP;
83                 methodName = "minus";
84                 break;
85
86             case C_NodeTypes.BINARY_PLUS_OPERATOR_NODE:
87                 operator = TypeCompiler.PLUS_OP;
88                 methodName = "plus";
89                 break;
90
91             case C_NodeTypes.BINARY_TIMES_OPERATOR_NODE:
92                 operator = TypeCompiler.TIMES_OP;
93                 methodName = "times";
94                 break;
95
96             case C_NodeTypes.MOD_OPERATOR_NODE:
97                 operator = TypeCompiler.MOD_OP;
98                 methodName = "mod";
99                 break;
100
101             default:
102                 if (SanityManager.DEBUG)
103                 {
104                     SanityManager.THROWASSERT(
105                         "Unexpected nodeType = " + nodeType);
106                 }
107         }
108         setOperator(operator);
109         setMethodName(methodName);
110         super.setNodeType(nodeType);
111     }
112
113     /**
114      * Bind this operator
115      *
116      * @param fromList The query's FROM list
117      * @param subqueryList The subquery list being built as we find SubqueryNodes
118      * @param aggregateVector The aggregate vector being built as we find AggregateNodes
119      *
120      * @return The new top of the expression tree.
121      *
122      * @exception StandardException Thrown on error
123      */

124
125     public ValueNode bindExpression(
126         FromList fromList, SubqueryList subqueryList,
127         Vector JavaDoc aggregateVector)
128             throws StandardException
129     {
130         super.bindExpression(fromList, subqueryList,
131                 aggregateVector);
132
133         TypeId leftType = leftOperand.getTypeId();
134         TypeId rightType = rightOperand.getTypeId();
135         DataTypeDescriptor leftDTS = leftOperand.getTypeServices();
136         DataTypeDescriptor rightDTS = rightOperand.getTypeServices();
137
138         /* Do any implicit conversions from (long) (var)char. */
139         if (leftType.isStringTypeId() && rightType.isNumericTypeId())
140         {
141             boolean nullableResult;
142             nullableResult = leftDTS.isNullable() ||
143                              rightDTS.isNullable();
144             /* If other side is decimal/numeric, then we need to diddle
145              * with the precision, scale and max width in order to handle
146              * computations like: 1.1 + '0.111'
147              */

148             int precision = rightDTS.getPrecision();
149             int scale = rightDTS.getScale();
150             int maxWidth = rightDTS.getMaximumWidth();
151
152             if (rightType.isDecimalTypeId())
153             {
154                 int charMaxWidth = leftDTS.getMaximumWidth();
155                 precision += (2 * charMaxWidth);
156                 scale += charMaxWidth;
157                 maxWidth = precision + 3;
158             }
159
160             leftOperand = (ValueNode)
161                     getNodeFactory().getNode(
162                         C_NodeTypes.CAST_NODE,
163                         leftOperand,
164                         new DataTypeDescriptor(rightType, precision,
165                                             scale, nullableResult,
166                                             maxWidth),
167                         getContextManager());
168             ((CastNode) leftOperand).bindCastNodeOnly();
169         }
170         else if (rightType.isStringTypeId() && leftType.isNumericTypeId())
171         {
172             boolean nullableResult;
173             nullableResult = leftDTS.isNullable() ||
174                              rightDTS.isNullable();
175             /* If other side is decimal/numeric, then we need to diddle
176              * with the precision, scale and max width in order to handle
177              * computations like: 1.1 + '0.111'
178              */

179             int precision = leftDTS.getPrecision();
180             int scale = leftDTS.getScale();
181             int maxWidth = leftDTS.getMaximumWidth();
182
183             if (leftType.isDecimalTypeId())
184             {
185                 int charMaxWidth = rightDTS.getMaximumWidth();
186                 precision += (2 * charMaxWidth);
187                 scale += charMaxWidth;
188                 maxWidth = precision + 3;
189             }
190
191             rightOperand = (ValueNode)
192                     getNodeFactory().getNode(
193                         C_NodeTypes.CAST_NODE,
194                         rightOperand,
195                         new DataTypeDescriptor(leftType, precision,
196                                             scale, nullableResult,
197                                             maxWidth),
198                         getContextManager());
199             ((CastNode) rightOperand).bindCastNodeOnly();
200         }
201
202         /*
203         ** Set the result type of this operator based on the operands.
204         ** By convention, the left operand gets to decide the result type
205         ** of a binary operator.
206         */

207         setType(leftOperand.getTypeCompiler().
208                     resolveArithmeticOperation(
209                         leftOperand.getTypeServices(),
210                         rightOperand.getTypeServices(),
211                         operator
212                             )
213                 );
214
215         return this;
216     }
217 }
218
Popular Tags