1 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 ; 43 44 import java.util.Vector ; 45 46 51 52 public final class BinaryArithmeticOperatorNode extends BinaryOperatorNode 53 { 54 60 61 public void init( 62 Object leftOperand, 63 Object rightOperand) 64 { 65 super.init(leftOperand, rightOperand, 66 ClassName.NumberDataValue, ClassName.NumberDataValue); 67 } 68 69 public void setNodeType(int nodeType) 70 { 71 String operator = null; 72 String 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 124 125 public ValueNode bindExpression( 126 FromList fromList, SubqueryList subqueryList, 127 Vector 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 139 if (leftType.isStringTypeId() && rightType.isNumericTypeId()) 140 { 141 boolean nullableResult; 142 nullableResult = leftDTS.isNullable() || 143 rightDTS.isNullable(); 144 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 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 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 |