1 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 ; 44 45 public final class NumericConstantNode extends ConstantNode 46 { 47 54 public void init(Object arg1) 55 throws StandardException 56 { 57 int precision = 0, scal = 0, maxwidth = 0; 58 Boolean isNullable; 59 boolean valueInP; 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 ) 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 ) 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 ) 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 ) arg1)); 121 } 122 break; 123 124 case C_NodeTypes.DECIMAL_CONSTANT_NODE: 125 if (valueInP) 126 { 127 128 NumberDataValue constantDecimal = getDataValueFactory().getDecimalDataValue((String ) arg1); 129 130 typeid = Types.DECIMAL; 131 precision = constantDecimal.getDecimalValuePrecision(); 132 scal = constantDecimal.getDecimalValueScale(); 133 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 ) 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 ) arg1)); 167 } 168 break; 169 170 default: 171 if (SanityManager.DEBUG) 172 { 173 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 203 Object getConstantValueAsObject() 204 throws StandardException 205 { 206 return value.getObject(); 207 } 208 209 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 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 SanityManager.THROWASSERT( 256 "Unexpected nodeType = " + getNodeType()); 257 } 258 } 259 } 260 } 261 | Popular Tags |