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.types.TypeId; 27 import org.apache.derby.iapi.types.DataTypeDescriptor; 28 import org.apache.derby.iapi.error.StandardException; 29 import org.apache.derby.iapi.services.compiler.MethodBuilder; 30 import org.apache.derby.iapi.services.compiler.LocalField; 31 import org.apache.derby.iapi.types.DataValueDescriptor; 32 import org.apache.derby.iapi.types.DataValueFactory; 33 import org.apache.derby.iapi.types.DataTypeDescriptor; 34 35 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder; 36 37 import org.apache.derby.iapi.reference.SQLState; 38 import org.apache.derby.iapi.reference.ClassName; 39 40 import org.apache.derby.iapi.services.classfile.VMOpcode; 41 42 import java.lang.reflect.Modifier ; 43 44 import java.sql.Types ; 45 46 import java.util.Vector ; 47 48 53 54 public final class DB2LengthOperatorNode extends UnaryOperatorNode 55 { 56 57 62 public void init(Object operand) 63 { 64 super.init( operand, "length", "getDB2Length"); 65 } 66 67 68 79 80 public ValueNode bindExpression( 81 FromList fromList, SubqueryList subqueryList, 82 Vector aggregateVector) 83 throws StandardException 84 { 85 ValueNode boundExpression = super.bindExpression( fromList, subqueryList, aggregateVector); 86 87 TypeId operandType = operand.getTypeId(); 89 if (operandType.isXMLTypeId()) { 90 throw StandardException.newException(SQLState.LANG_UNARY_FUNCTION_BAD_TYPE, 91 getOperatorString(), 92 operandType.getSQLTypeName()); 93 } 94 95 setType( new DataTypeDescriptor( TypeId.getBuiltInTypeId( Types.INTEGER), 96 operand.getTypeServices().isNullable())); 97 return boundExpression; 98 } 99 100 104 public String getReceiverInterfaceName() { 105 return ClassName.ConcatableDataValue; 106 } 107 108 117 118 public void generateExpression(ExpressionClassBuilder acb, 119 MethodBuilder mb) 120 throws StandardException 121 { 122 if (operand == null) 123 return; 124 125 int constantLength = getConstantLength(); 126 128 String resultTypeName = getTypeCompiler().interfaceName(); 129 130 mb.pushThis(); 131 operand.generateExpression(acb, mb); 132 mb.upCast( ClassName.DataValueDescriptor); 133 mb.push( constantLength); 134 135 136 LocalField field = acb.newFieldDeclaration(Modifier.PRIVATE, resultTypeName); 137 mb.getField(field); 138 mb.callMethod(VMOpcode.INVOKEVIRTUAL, ClassName.BaseActivation, methodName, resultTypeName, 3); 139 140 144 mb.putField(field); 145 } 147 private int getConstantLength( ) throws StandardException 148 { 149 DataTypeDescriptor typeDescriptor = operand.getTypeServices(); 150 151 switch( typeDescriptor.getJDBCTypeId()) 152 { 153 case Types.BIGINT: 154 return 8; 155 case org.apache.derby.iapi.reference.JDBC30Translation.SQL_TYPES_BOOLEAN: 156 case Types.BIT: 157 return 1; 158 case Types.BINARY: 159 case Types.CHAR: 160 return typeDescriptor.getMaximumWidth(); 161 case Types.DATE: 162 return 4; 163 case Types.DECIMAL: 164 case Types.NUMERIC: 165 return typeDescriptor.getPrecision()/2 + 1; 166 case Types.DOUBLE: 167 return 8; 168 case Types.FLOAT: 169 case Types.REAL: 170 case Types.INTEGER: 171 return 4; 172 case Types.SMALLINT: 173 return 2; 174 case Types.TIME: 175 return 3; 176 case Types.TIMESTAMP: 177 return 10; 178 case Types.TINYINT: 179 return 1; 180 case Types.LONGVARCHAR: 181 case Types.VARCHAR: 182 case Types.LONGVARBINARY: 183 case Types.VARBINARY: 184 case Types.BLOB: 185 return getConstantNodeLength(); 186 default: 187 return -1; 188 } 189 } 191 private int getConstantNodeLength() throws StandardException 192 { 193 if( operand instanceof ConstantNode) 194 return ((ConstantNode) operand).getValue().getLength(); 195 return -1; 196 } 197 } 198 | Popular Tags |