1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.sql.compile.CompilerContext; 25 26 import org.apache.derby.iapi.services.sanity.SanityManager; 27 28 import org.apache.derby.iapi.services.context.ContextManager; 29 30 import org.apache.derby.iapi.types.DataTypeDescriptor; 31 32 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 33 34 import org.apache.derby.iapi.types.TypeId; 35 36 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 37 import org.apache.derby.iapi.error.StandardException; 38 import org.apache.derby.iapi.services.i18n.MessageService; 39 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder; 40 import org.apache.derby.impl.sql.compile.ActivationClassBuilder; 41 42 import org.apache.derby.iapi.types.JSQLType; 43 44 import org.apache.derby.iapi.services.compiler.LocalField; 45 import org.apache.derby.iapi.services.compiler.MethodBuilder; 46 47 48 import org.apache.derby.iapi.services.loader.ClassInspector; 49 50 import org.apache.derby.iapi.store.access.Qualifier; 51 52 import org.apache.derby.iapi.util.JBitSet; 53 54 import org.apache.derby.iapi.reference.SQLState; 55 56 import java.lang.reflect.Modifier ; 57 58 import java.util.Vector ; 59 60 63 64 abstract class JavaValueNode extends QueryTreeNode 65 { 66 private boolean mustCastToPrimitive; 67 68 protected boolean forCallStatement; 69 private boolean valueReturnedToSQLDomain; 70 private boolean returnValueDiscarded; 71 72 protected JSQLType jsqlType; 73 74 75 private LocalField receiverField; 76 77 public boolean isPrimitiveType() throws StandardException 78 { 79 JSQLType myType = getJSQLType(); 80 81 if ( myType == null ) { return false; } 82 else { return ( myType.getCategory() == JSQLType.JAVA_PRIMITIVE ); } 83 } 84 85 public String getJavaTypeName() throws StandardException 86 { 87 JSQLType myType = getJSQLType(); 88 89 if ( myType == null ) { return ""; } 90 91 switch( myType.getCategory() ) 92 { 93 case JSQLType.JAVA_CLASS: return myType.getJavaClassName(); 94 95 case JSQLType.JAVA_PRIMITIVE: return JSQLType.primitiveNames[ myType.getPrimitiveKind() ]; 96 97 default: 98 99 if (SanityManager.DEBUG) 100 { SanityManager.THROWASSERT( "Inappropriate JSQLType: " + myType ); } 101 } 102 103 return ""; 104 } 105 106 public void setJavaTypeName(String javaTypeName) 107 { 108 jsqlType = new JSQLType( javaTypeName ); 109 } 110 111 public String getPrimitiveTypeName() 112 throws StandardException 113 { 114 JSQLType myType = getJSQLType(); 115 116 if ( myType == null ) { return ""; } 117 118 switch( myType.getCategory() ) 119 { 120 case JSQLType.JAVA_PRIMITIVE: return JSQLType.primitiveNames[ myType.getPrimitiveKind() ]; 121 122 default: 123 124 if (SanityManager.DEBUG) 125 { SanityManager.THROWASSERT( "Inappropriate JSQLType: " + myType ); } 126 } 127 128 return ""; 129 } 130 131 138 public void castToPrimitive(boolean booleanValue) 139 { 140 mustCastToPrimitive = booleanValue; 141 } 142 143 150 public boolean mustCastToPrimitive() { return mustCastToPrimitive; } 151 152 159 public JSQLType getJSQLType() throws StandardException 160 { return jsqlType; } 161 162 163 171 public TypeId mapToTypeID( JSQLType jsqlType ) 172 { 173 DataTypeDescriptor dts = jsqlType.getSQLType(); 174 175 if ( dts == null ) { return null; } 176 177 return dts.getTypeId(); 178 } 179 180 185 public void setClause(int clause) 186 { 187 clause = clause; 188 } 189 190 194 public void markForCallStatement() 195 { 196 forCallStatement = true; 197 } 198 199 204 abstract public JavaValueNode remapColumnReferencesToExpressions() 205 throws StandardException; 206 207 212 abstract public boolean categorize(JBitSet referencedTabs, boolean simplePredsOnly) 213 throws StandardException; 214 215 222 abstract JavaValueNode bindExpression(FromList fromList, SubqueryList subqueryList, 223 Vector aggregateVector) 224 throws StandardException; 225 230 abstract public void preprocess(int numTables, 231 FromList outerFromList, 232 SubqueryList outerSubqueryList, 233 PredicateList outerPredicateList) 234 throws StandardException; 235 236 240 Object getConstantValueAsObject() 241 throws StandardException 242 { 243 return null; 244 } 245 246 255 256 protected final void generate(ActivationClassBuilder acb, 257 MethodBuilder mb) 258 throws StandardException 259 { 260 generateExpression( acb, mb ); 261 } 262 263 285 protected boolean generateReceiver(ExpressionClassBuilder acb, 286 MethodBuilder mb) 287 throws StandardException 288 { 289 return false; 290 } 291 292 304 protected int getOrderableVariantType() throws StandardException 305 { 306 return Qualifier.VARIANT; 308 } 310 311 320 321 protected abstract void generateExpression( 322 ExpressionClassBuilder acb, 323 MethodBuilder mb) 324 throws StandardException; 325 326 358 protected final boolean generateReceiver(ExpressionClassBuilder acb, 359 MethodBuilder mb, 360 JavaValueNode receiver) 361 throws StandardException 362 { 363 ClassInspector classInspector = getClassFactory().getClassInspector(); 364 365 369 if ( (! valueReturnedToSQLDomain()) && 370 classInspector.primitiveType(getJavaTypeName())) 371 { 372 return false; 373 } 374 375 383 384 String receiverClassName = receiver.getJavaTypeName(); 385 receiverField = 386 acb.newFieldDeclaration(Modifier.PRIVATE, receiverClassName); 387 388 receiver.generateExpression(acb, mb); 389 mb.putField(receiverField); 390 return true; 391 } 392 393 405 protected final void getReceiverExpression(ExpressionClassBuilder acb, 406 MethodBuilder mb, 407 JavaValueNode receiver) 408 throws StandardException 409 { 410 if (receiverField != null) 411 { 412 mb.getField(receiverField); 413 } 414 else 415 { 416 receiver.generateExpression(acb, mb); 417 } 418 } 419 420 421 protected void returnValueToSQLDomain() 422 { 423 valueReturnedToSQLDomain = true; 424 } 425 426 427 protected boolean valueReturnedToSQLDomain() 428 { 429 return valueReturnedToSQLDomain; 430 } 431 432 433 protected void markReturnValueDiscarded() 434 { 435 returnValueDiscarded = true; 436 } 437 438 439 protected boolean returnValueDiscarded() 440 { 441 return returnValueDiscarded; 442 } 443 444 451 public void checkReliability(ValueNode sqlNode) throws StandardException { 452 sqlNode.checkReliability( 453 CompilerContext.FUNCTION_CALL_ILLEGAL, 454 SQLState.LANG_JAVA_METHOD_CALL_OR_FIELD_REF 455 ); 456 } 457 } 458 | Popular Tags |