1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.services.compiler.MethodBuilder; 25 import org.apache.derby.iapi.services.compiler.LocalField; 26 27 import org.apache.derby.iapi.services.sanity.SanityManager; 28 29 import org.apache.derby.iapi.types.JSQLType; 30 31 import org.apache.derby.iapi.types.DataValueDescriptor; 32 import org.apache.derby.iapi.types.DataTypeDescriptor; 33 34 import org.apache.derby.iapi.sql.compile.TypeCompiler; 35 36 import org.apache.derby.iapi.sql.Activation; 37 38 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 39 import org.apache.derby.iapi.error.StandardException; 40 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder; 41 import org.apache.derby.iapi.sql.compile.Visitable; 42 import org.apache.derby.iapi.sql.compile.Visitor; 43 44 import org.apache.derby.iapi.reference.ClassName; 45 46 import org.apache.derby.iapi.util.JBitSet; 47 import org.apache.derby.iapi.services.classfile.VMOpcode; 48 49 import java.lang.reflect.Modifier ; 50 51 import java.util.Vector ; 52 53 57 58 public class SQLToJavaValueNode extends JavaValueNode 59 { 60 ValueNode value; 61 62 LocalField returnsNullOnNullState; 63 64 70 71 public void init(Object value) 72 { 73 this.value = (ValueNode) value; 74 } 75 76 82 83 public void printSubNodes(int depth) 84 { 85 if (SanityManager.DEBUG) 86 { 87 int parm; 88 89 super.printSubNodes(depth); 90 if (value != null) 91 { 92 printLabel(depth, "value: "); 93 value.treePrint(depth + 1); 94 } 95 } 96 } 97 98 104 public String getJavaTypeName() 105 throws StandardException 106 { 107 JSQLType myType = getJSQLType(); 108 109 if ( myType == null ) { return ""; } 110 else { return mapToTypeID( myType ).getCorrespondingJavaTypeName(); } 111 } 112 113 120 public String getPrimitiveTypeName() 121 throws StandardException 122 { 123 JSQLType myType = getJSQLType(); 124 125 if ( myType == null ) 126 { 127 return ""; 128 } 129 else 130 { 131 return 132 getTypeCompiler(mapToTypeID( myType )). 133 getCorrespondingPrimitiveTypeName(); 134 } 135 } 136 137 146 public JSQLType getJSQLType () throws StandardException 147 { 148 if ( jsqlType == null ) 149 { 150 if ( value.requiresTypeFromContext()) 151 { 152 ParameterNode pn; 153 if (value instanceof UnaryOperatorNode) 154 pn = ((UnaryOperatorNode)value).getParameterOperand(); 155 else 156 pn = (ParameterNode) (value); 157 jsqlType = pn.getJSQLType(); 158 } 159 else 160 { 161 DataTypeDescriptor dtd = value.getTypeServices(); 162 if (dtd != null) 163 jsqlType = new JSQLType( dtd ); 164 } 165 } 166 167 return jsqlType; 168 } 169 170 171 176 public void setClause(int clause) 177 { 178 super.setClause(clause); 179 value.setClause(clause); 180 } 181 182 196 197 public JavaValueNode bindExpression( 198 FromList fromList, SubqueryList subqueryList, 199 Vector aggregateVector) 200 throws StandardException 201 { 202 203 value = value.bindExpression(fromList, subqueryList, 204 aggregateVector); 205 206 return this; 207 } 208 209 217 public JavaValueNode remapColumnReferencesToExpressions() 218 throws StandardException 219 { 220 value = value.remapColumnReferencesToExpressions(); 221 return this; 222 } 223 224 250 public boolean categorize(JBitSet referencedTabs, boolean simplePredsOnly) 251 throws StandardException 252 { 253 return value.categorize(referencedTabs, simplePredsOnly); 254 } 255 256 269 public void preprocess(int numTables, 270 FromList outerFromList, 271 SubqueryList outerSubqueryList, 272 PredicateList outerPredicateList) 273 throws StandardException 274 { 275 value.preprocess(numTables, 276 outerFromList, outerSubqueryList, 277 outerPredicateList); 278 } 279 280 293 protected int getOrderableVariantType() throws StandardException 294 { 295 return value.getOrderableVariantType(); 296 } 297 298 304 305 325 326 public void generateExpression(ExpressionClassBuilder acb, 327 MethodBuilder mb) 328 throws StandardException 329 { 330 331 generateSQLValue( acb, mb ); 332 333 334 generateJavaValue( acb, mb); 335 } 336 337 346 347 public void generateSQLValue(ExpressionClassBuilder acb, 348 MethodBuilder mb) 349 throws StandardException 350 { 351 value.generateExpression(acb, mb); 352 } 353 354 364 365 public void generateJavaValue 366 ( 367 ExpressionClassBuilder acb, 368 MethodBuilder mbex 369 ) 370 throws StandardException 371 { 372 378 if ( isPrimitiveType() || mustCastToPrimitive() ) 379 { 380 String primitiveTN = value.getTypeCompiler().getCorrespondingPrimitiveTypeName(); 381 382 388 String [] pd = new String [1]; 389 pd[0] = getSQLValueInterfaceName(); 391 MethodBuilder mb = acb.newGeneratedFun(primitiveTN, Modifier.PRIVATE, pd); 392 393 mb.getParameter(0); 394 395 if (returnsNullOnNullState != null) 396 { 397 generateReturnsNullOnNullCheck(mb); 398 } 399 else 400 { 401 mb.dup(); 402 mb.upCast(ClassName.DataValueDescriptor); 403 mb.push(primitiveTN); 404 mb.callMethod(VMOpcode.INVOKESTATIC, ClassName.BaseActivation, "nullToPrimitiveTest", "void", 2); 405 } 406 407 409 410 mb.callMethod(VMOpcode.INVOKEINTERFACE, ClassName.DataValueDescriptor, 411 value.getTypeCompiler().getPrimitiveMethodName(), primitiveTN, 0); 412 413 mb.methodReturn(); 414 mb.complete(); 415 416 417 418 mbex.pushThis(); 419 mbex.swap(); mbex.callMethod(VMOpcode.INVOKEVIRTUAL, (String ) null, mb.getName(), primitiveTN, 1); 421 } 422 else 423 { 424 if (returnsNullOnNullState != null) 425 generateReturnsNullOnNullCheck(mbex); 426 427 428 mbex.callMethod(VMOpcode.INVOKEINTERFACE, ClassName.DataValueDescriptor, "getObject", 429 "java.lang.Object", 0); 430 431 mbex.cast(value.getTypeId().getCorrespondingJavaTypeName()); 432 } 433 } 434 435 439 440 private void generateReturnsNullOnNullCheck(MethodBuilder mb) 441 { 442 mb.dup(); 443 mb.callMethod(VMOpcode.INVOKEINTERFACE, ClassName.Storable, 444 "isNull", "boolean", 0); 445 446 mb.conditionalIf(); 447 mb.push(true); 448 mb.startElseCode(); 449 mb.getField(returnsNullOnNullState); 450 mb.completeConditional(); 451 452 mb.setField(returnsNullOnNullState); 453 } 454 455 456 464 public String getSQLValueInterfaceName() 465 throws StandardException 466 { 467 return value.getTypeCompiler().interfaceName(); 468 } 469 470 476 477 482 ValueNode getSQLValueNode() 483 { 484 return value; 485 } 486 487 491 Object getConstantValueAsObject() 492 throws StandardException 493 { 494 return value.getConstantValueAsObject(); 495 } 496 497 505 public Visitable accept(Visitor v) 506 throws StandardException 507 { 508 Visitable returnNode = v.visit(this); 509 510 if (v.skipChildren(this)) 511 { 512 return returnNode; 513 } 514 515 if (value != null && !v.stopTraversal()) 516 { 517 value = (ValueNode)value.accept(v); 518 } 519 520 return returnNode; 521 } 522 } 523 | Popular Tags |