1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.types.DataValueFactory; 25 import org.apache.derby.iapi.types.DataTypeDescriptor; 26 import org.apache.derby.iapi.types.DataValueDescriptor; 27 import org.apache.derby.iapi.types.DateTimeDataValue; 28 import org.apache.derby.iapi.services.compiler.MethodBuilder; 29 import org.apache.derby.iapi.error.StandardException; 30 31 import org.apache.derby.iapi.sql.compile.C_NodeTypes; 32 33 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder; 34 35 import org.apache.derby.iapi.reference.ClassName; 36 import org.apache.derby.iapi.reference.SQLState; 37 38 import org.apache.derby.iapi.services.classfile.VMOpcode; 39 import org.apache.derby.iapi.services.sanity.SanityManager; 40 41 import java.sql.Types ; 42 43 import java.util.Vector ; 44 45 51 public class UnaryDateTimestampOperatorNode extends UnaryOperatorNode 52 { 53 private static final String TIMESTAMP_METHOD_NAME = "getTimestamp"; 54 private static final String DATE_METHOD_NAME = "getDate"; 55 56 62 63 public void init( Object operand, Object targetType) 64 throws StandardException 65 { 66 setType( (DataTypeDescriptor) targetType); 67 switch( getTypeServices().getJDBCTypeId()) 68 { 69 case Types.DATE: 70 super.init( operand, "date", DATE_METHOD_NAME); 71 break; 72 73 case Types.TIMESTAMP: 74 super.init( operand, "timestamp", TIMESTAMP_METHOD_NAME); 75 break; 76 77 default: 78 if( SanityManager.DEBUG) 79 SanityManager.NOTREACHED(); 80 super.init( operand); 81 } 82 } 83 84 100 protected ValueNode bindUnaryOperator( 101 FromList fromList, SubqueryList subqueryList, 102 Vector aggregateVector) 103 throws StandardException 104 { 105 boolean isIdentity = false; boolean operandIsNumber = false; 107 108 super.bindUnaryOperator( fromList, subqueryList, aggregateVector); 109 DataTypeDescriptor operandType = operand.getTypeServices(); 110 switch( operandType.getJDBCTypeId()) 111 { 112 case Types.BIGINT: 113 case Types.INTEGER: 114 case Types.SMALLINT: 115 case Types.TINYINT: 116 case Types.DECIMAL: 117 case Types.NUMERIC: 118 case Types.DOUBLE: 119 case Types.FLOAT: 120 if( TIMESTAMP_METHOD_NAME.equals( methodName)) 121 invalidOperandType(); 122 operandIsNumber = true; 123 break; 124 125 case Types.CHAR: 126 case Types.VARCHAR: 127 break; 128 129 case Types.DATE: 130 if( TIMESTAMP_METHOD_NAME.equals( methodName)) 131 invalidOperandType(); 132 isIdentity = true; 133 break; 134 135 case Types.NULL: 136 break; 137 138 case Types.TIMESTAMP: 139 if( TIMESTAMP_METHOD_NAME.equals( methodName)) 140 isIdentity = true; 141 break; 142 143 default: 144 invalidOperandType(); 145 } 146 147 if( operand instanceof ConstantNode) 148 { 149 DataValueFactory dvf = getLanguageConnectionContext().getDataValueFactory(); 150 DataValueDescriptor sourceValue = ((ConstantNode) operand).getValue(); 151 DataValueDescriptor destValue = null; 152 if( sourceValue.isNull()) 153 { 154 destValue = (TIMESTAMP_METHOD_NAME.equals( methodName)) 155 ? dvf.getNullTimestamp( (DateTimeDataValue) null) 156 : dvf.getNullDate( (DateTimeDataValue) null); 157 } 158 else 159 { 160 destValue = (TIMESTAMP_METHOD_NAME.equals( methodName)) 161 ? dvf.getTimestamp( sourceValue) : dvf.getDate( sourceValue); 162 } 163 return (ValueNode) getNodeFactory().getNode( C_NodeTypes.USERTYPE_CONSTANT_NODE, 164 destValue, getContextManager()); 165 } 166 167 if( isIdentity) 168 return operand; 169 return this; 170 } 172 private void invalidOperandType() throws StandardException 173 { 174 throw StandardException.newException( SQLState.LANG_UNARY_FUNCTION_BAD_TYPE, 175 getOperatorString(), getOperand().getTypeServices().getSQLstring()); 176 } 177 178 187 188 public void generateExpression( ExpressionClassBuilder acb, 189 MethodBuilder mb) 190 throws StandardException 191 { 192 acb.pushDataValueFactory( mb); 193 operand.generateExpression( acb, mb); 194 mb.cast( ClassName.DataValueDescriptor); 195 mb.callMethod( VMOpcode.INVOKEINTERFACE, (String ) null, methodName, getTypeCompiler().interfaceName(), 1); 196 } } 198 | Popular Tags |