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.sql.dictionary.DataDictionary; 27 28 import org.apache.derby.iapi.types.TypeId; 29 import org.apache.derby.iapi.types.DateTimeDataValue; 30 import org.apache.derby.iapi.types.DataTypeDescriptor; 31 32 import org.apache.derby.iapi.sql.compile.TypeCompiler; 33 34 import org.apache.derby.iapi.reference.SQLState; 35 import org.apache.derby.iapi.error.StandardException; 36 37 import org.apache.derby.iapi.services.sanity.SanityManager; 38 39 import java.sql.Types ; 40 41 import java.util.Vector ; 42 43 49 public class ExtractOperatorNode extends UnaryOperatorNode { 50 51 static private final String fieldName[] = { 52 "YEAR", "MONTH", "DAY", "HOUR", "MINUTE", "SECOND" 53 }; 54 static private final String fieldMethod[] = { 55 "getYear","getMonth","getDate","getHours","getMinutes","getSeconds" 56 }; 57 58 private int extractField; 59 60 66 public void init(Object field, Object operand) { 67 extractField = ((Integer ) field).intValue(); 68 super.init( operand, 69 "EXTRACT "+fieldName[extractField], 70 fieldMethod[extractField] ); 71 } 72 73 84 85 public ValueNode bindExpression( 86 FromList fromList, 87 SubqueryList subqueryList, 88 Vector aggregateVector) 89 throws StandardException 90 { 91 int operandType; 92 TypeId opTypeId; 93 94 super.bindExpression(fromList, subqueryList, 95 aggregateVector); 96 97 opTypeId = operand.getTypeId(); 98 operandType = opTypeId.getJDBCTypeId(); 99 TypeCompiler tc = operand.getTypeCompiler(); 100 101 107 if (opTypeId.isStringTypeId()) 108 { 109 int castType = (extractField < 3) ? Types.DATE : Types.TIME; 110 operand = (ValueNode) 111 getNodeFactory().getNode( 112 C_NodeTypes.CAST_NODE, 113 operand, 114 DataTypeDescriptor.getBuiltInDataTypeDescriptor(castType, true, 115 tc.getCastToCharWidth( 116 operand.getTypeServices())), 117 getContextManager()); 118 ((CastNode) operand).bindCastNodeOnly(); 119 120 opTypeId = operand.getTypeId(); 121 operandType = opTypeId.getJDBCTypeId(); 122 } 123 124 if ( ! ( ( operandType == Types.DATE ) 125 || ( operandType == Types.TIME ) 126 || ( operandType == Types.TIMESTAMP ) 127 ) ) { 128 throw StandardException.newException(SQLState.LANG_UNARY_FUNCTION_BAD_TYPE, 129 "EXTRACT "+fieldName[extractField], 130 opTypeId.getSQLTypeName()); 131 } 132 133 136 if ( (operandType == Types.DATE) 137 && (extractField > DateTimeDataValue.DAY_FIELD) ) { 138 throw StandardException.newException(SQLState.LANG_UNARY_FUNCTION_BAD_TYPE, 139 "EXTRACT "+fieldName[extractField], 140 opTypeId.getSQLTypeName()); 141 } 142 143 146 if ( (operandType == Types.TIME) 147 && (extractField < DateTimeDataValue.HOUR_FIELD) ) { 148 throw StandardException.newException(SQLState.LANG_UNARY_FUNCTION_BAD_TYPE, 149 "EXTRACT "+fieldName[extractField], 150 opTypeId.getSQLTypeName()); 151 } 152 153 159 if ( (operandType == Types.TIMESTAMP) 160 && (extractField == DateTimeDataValue.SECOND_FIELD) ) { 161 setType(new DataTypeDescriptor( 162 TypeId.getBuiltInTypeId(Types.DOUBLE), 163 operand.getTypeServices().isNullable() 164 ) 165 ); 166 } else { 167 setType(new DataTypeDescriptor( 168 TypeId.INTEGER_ID, 169 operand.getTypeServices().isNullable() 170 ) 171 ); 172 } 173 174 return this; 175 } 176 177 public String toString() { 178 if (SanityManager.DEBUG) 179 { 180 return super.toString() + "field is "+fieldName[extractField]+"\n"; 181 } 182 else 183 { 184 return ""; 185 } 186 } 187 } 188 | Popular Tags |