1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.error.StandardException; 25 26 import org.apache.derby.iapi.types.TypeId; 27 import org.apache.derby.iapi.types.UserDataValue; 28 29 import org.apache.derby.iapi.sql.compile.TypeCompiler; 30 31 import org.apache.derby.iapi.services.compiler.MethodBuilder; 32 import org.apache.derby.iapi.services.compiler.LocalField; 33 import org.apache.derby.iapi.services.io.StoredFormatIds; 34 35 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 36 37 import org.apache.derby.iapi.types.DataValueFactory; 38 39 import org.apache.derby.iapi.types.TypeId; 40 41 import org.apache.derby.iapi.types.TypeId; 42 43 import org.apache.derby.iapi.types.DataValueDescriptor; 44 45 import org.apache.derby.iapi.services.sanity.SanityManager; 46 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder; 47 48 import org.apache.derby.iapi.reference.ClassName; 49 50 import org.apache.derby.catalog.TypeDescriptor; 51 52 import org.apache.derby.iapi.util.ReuseFactory; 53 import org.apache.derby.iapi.services.classfile.VMOpcode; 54 import org.apache.derby.iapi.types.*; 55 56 import java.lang.reflect.Modifier ; 57 import java.sql.Date ; 58 import java.sql.Time ; 59 import java.sql.Timestamp ; 60 import java.sql.Types ; 61 62 74 public class UserTypeConstantNode extends ConstantNode { 75 84 Object value; 85 86 105 public void init(Object arg1) 106 throws StandardException { 107 DataValueDescriptor dvd = null; 108 109 if (arg1 instanceof TypeId) 110 { 111 super.init( 112 arg1, 113 Boolean.TRUE, 114 ReuseFactory.getInteger( 115 TypeDescriptor.MAXIMUM_WIDTH_UNKNOWN)); 116 } 117 else 118 { 119 Integer maxWidth = null; 120 TypeId typeId = null; 121 122 if( arg1 instanceof DataValueDescriptor) 123 dvd = (DataValueDescriptor) arg1; 124 if (arg1 instanceof Date 125 || (dvd != null && dvd.getTypeFormatId() == StoredFormatIds.SQL_DATE_ID)) 126 { 127 maxWidth = ReuseFactory.getInteger(TypeId.DATE_MAXWIDTH); 128 typeId = TypeId.getBuiltInTypeId(Types.DATE); 129 } 130 else if (arg1 instanceof Time 131 || (dvd != null && dvd.getTypeFormatId() == StoredFormatIds.SQL_TIME_ID)) 132 { 133 maxWidth = ReuseFactory.getInteger(TypeId.TIME_MAXWIDTH); 134 typeId = TypeId.getBuiltInTypeId(Types.TIME); 135 } 136 else if (arg1 instanceof Timestamp 137 || (dvd != null && dvd.getTypeFormatId() == StoredFormatIds.SQL_TIMESTAMP_ID)) 138 { 139 maxWidth = ReuseFactory.getInteger(TypeId.TIMESTAMP_MAXWIDTH); 140 typeId = TypeId.getBuiltInTypeId(Types.TIMESTAMP); 141 } 142 else 143 { 144 if (SanityManager.DEBUG) 145 { 146 SanityManager.THROWASSERT( 147 "Unexpected class " + arg1.getClass().getName()); 148 } 149 } 150 151 super.init( 152 typeId, 153 (arg1 == null) ? Boolean.TRUE : Boolean.FALSE, 154 maxWidth); 155 156 if( dvd != null) 157 setValue( dvd); 158 else if (arg1 instanceof Date ) 159 { 160 setValue(getDataValueFactory().getDataValue((Date ) arg1)); 161 } 162 else if (arg1 instanceof Time ) 163 { 164 setValue(getDataValueFactory().getDataValue((Time ) arg1)); 165 } 166 else if (arg1 instanceof Timestamp ) 167 { 168 setValue(getDataValueFactory().getDataValue((Timestamp ) arg1)); 169 } 170 171 value = arg1; 172 } 173 } 174 175 182 public Object getObjectValue() { return value; } 183 184 188 public boolean isNull() 189 { 190 return (value == null); 191 } 192 193 199 public DataValueDescriptor getStorableValue() 200 throws StandardException 201 { 202 if( value instanceof DataValueDescriptor) 203 return ((DataValueDescriptor) value).getClone(); 204 205 DataValueFactory dvf = getDataValueFactory(); 206 TypeId typeID = getTypeId(); 207 String typeName = typeID.getSQLTypeName(); 208 209 if ( typeName.equals( TypeId.DATE_NAME ) ) 210 { 211 return new SQLDate((Date ) value); 212 } 213 else if ( typeName.equals( TypeId.TIME_NAME ) ) 214 { 215 return new SQLTime( (Time ) value); 216 } 217 else if ( typeName.equals( TypeId.TIMESTAMP_NAME ) ) 218 { 219 return new SQLTimestamp( (Timestamp ) value); 220 } 221 else 222 { 223 return dvf.getDataValue( value, (UserDataValue) null ); 224 } 225 } 226 227 232 public void setObjectValue( Object newValue ) { value = newValue; } 233 234 241 245 256 public Object getConstantValueAsObject() 257 { 258 return value; 259 } 260 261 287 public void generateExpression(ExpressionClassBuilder acb, 288 MethodBuilder mb) 289 throws StandardException { 290 291 TypeCompiler tc = getTypeCompiler(); 292 String fieldType = tc.interfaceName(); 293 294 298 299 300 if (value == null) 301 { 302 acb.generateNull(mb, tc); 303 } 304 else 324 { 325 338 String typeName = getTypeId().getCorrespondingJavaTypeName(); 339 340 mb.push(value.toString()); 341 mb.callMethod(VMOpcode.INVOKESTATIC, typeName, "valueOf", typeName, 1); 342 343 LocalField field = acb.newFieldDeclaration(Modifier.PRIVATE, fieldType); 344 345 acb.generateDataValue(mb, tc, field); 346 } 347 } 348 349 350 359 void generateConstant(ExpressionClassBuilder acb, MethodBuilder mb) 360 throws StandardException 361 { 362 if (SanityManager.DEBUG) 363 { 364 SanityManager.THROWASSERT("geneateConstant() not expected to be called for UserTypeConstantNode because we have implemented our own generateExpression()."); 365 } 366 } 367 } 368 | Popular Tags |