1 21 22 package org.apache.derby.impl.sql; 23 24 import org.apache.derby.iapi.services.stream.HeaderPrintWriter; 25 26 import org.apache.derby.iapi.reference.JDBC20Translation; 27 import org.apache.derby.iapi.reference.JDBC30Translation; 28 import org.apache.derby.iapi.reference.SQLState; 29 30 import org.apache.derby.iapi.sql.Activation; 31 import org.apache.derby.iapi.types.DataTypeDescriptor; 32 import org.apache.derby.iapi.sql.ResultSet; 33 import org.apache.derby.iapi.sql.Statement; 34 import org.apache.derby.iapi.sql.PreparedStatement; 35 import org.apache.derby.iapi.sql.execute.ConstantAction; 36 import org.apache.derby.iapi.sql.execute.ExecutionContext; 37 import org.apache.derby.iapi.sql.execute.ExecPreparedStatement; 38 import org.apache.derby.iapi.sql.ParameterValueSet; 39 40 import org.apache.derby.iapi.sql.conn.LanguageConnectionFactory; 41 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 42 import org.apache.derby.iapi.sql.conn.StatementContext; 43 44 import org.apache.derby.iapi.sql.depend.Dependent; 45 46 import org.apache.derby.iapi.sql.compile.CompilerContext; 47 import org.apache.derby.iapi.sql.compile.NodeFactory; 48 import org.apache.derby.iapi.sql.compile.Parser; 49 50 import org.apache.derby.impl.sql.compile.QueryTreeNode; 51 import org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext; 52 53 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 54 import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext; 55 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 56 57 import org.apache.derby.iapi.services.compiler.JavaFactory; 58 import org.apache.derby.iapi.services.uuid.UUIDFactory; 59 import org.apache.derby.iapi.util.ByteArray; 60 61 import org.apache.derby.iapi.error.StandardException; 62 63 import org.apache.derby.iapi.services.monitor.Monitor; 64 65 import org.apache.derby.iapi.services.context.Context; 66 import org.apache.derby.iapi.services.context.ContextService; 67 import org.apache.derby.iapi.services.context.ContextManager; 68 import org.apache.derby.iapi.services.sanity.SanityManager; 69 70 import org.apache.derby.iapi.services.loader.GeneratedClass; 71 72 import java.sql.Timestamp ; 73 import java.sql.SQLWarning ; 74 75 public class GenericStatement 76 implements Statement { 77 78 private final SchemaDescriptor compilationSchema; 80 private final String statementText; 81 private final boolean isForReadOnly; 82 private int prepareIsolationLevel; 83 private GenericPreparedStatement preparedStmt; 84 85 91 92 public GenericStatement(SchemaDescriptor compilationSchema, String statementText, boolean isForReadOnly) 93 { 94 this.compilationSchema = compilationSchema; 95 this.statementText = statementText; 96 this.isForReadOnly = isForReadOnly; 97 } 98 99 102 103 104 105 public PreparedStatement prepare(LanguageConnectionContext lcc) throws StandardException 106 { 107 111 return prepMinion(lcc, true, (Object []) null, (SchemaDescriptor) null, false); 112 } 113 public PreparedStatement prepare(LanguageConnectionContext lcc, boolean forMetaData) throws StandardException 114 { 115 119 return prepMinion(lcc, true, (Object []) null, (SchemaDescriptor) null, forMetaData); 120 } 121 122 private PreparedStatement prepMinion(LanguageConnectionContext lcc, boolean cacheMe, Object [] paramDefaults, 123 SchemaDescriptor spsSchema, boolean internalSQL) 124 throws StandardException 125 { 126 127 long beginTime = 0; 128 long parseTime = 0; 129 long bindTime = 0; 130 long optimizeTime = 0; 131 long generateTime = 0; 132 Timestamp beginTimestamp = null; 133 Timestamp endTimestamp = null; 134 StatementContext statementContext = null; 135 136 if (preparedStmt != null) { 140 if (preparedStmt.upToDate()) 141 return preparedStmt; 142 } 143 144 if (lcc.getOptimizerTrace()) 146 lcc.setOptimizerTraceOutput(getSource() + "\n"); 147 148 beginTime = getCurrentTimeMillis(lcc); 149 152 if (beginTime != 0) 153 { 154 beginTimestamp = new Timestamp (beginTime); 155 } 156 157 160 prepareIsolationLevel = lcc.getPrepareIsolationLevel(); 161 162 179 boolean foundInCache = false; 180 if (preparedStmt == null) 181 { 182 if (cacheMe) 183 preparedStmt = (GenericPreparedStatement)((GenericLanguageConnectionContext)lcc).lookupStatement(this); 184 185 if (preparedStmt == null) 186 { 187 preparedStmt = new GenericPreparedStatement(this); 188 } 189 else 190 { 191 foundInCache = true; 192 } 193 } 194 195 synchronized (preparedStmt) 202 { 203 204 for (;;) { 205 206 if (foundInCache) { 207 if (preparedStmt.referencesSessionSchema()) { 208 foundInCache = false; 211 preparedStmt = new GenericPreparedStatement(this); 212 break; 213 } 214 } 215 216 if (preparedStmt.upToDate()) { 218 return preparedStmt; 219 } 220 221 if (!preparedStmt.compilingStatement) { 222 break; 223 } 224 225 try { 226 preparedStmt.wait(); 227 } catch (InterruptedException ie) { 228 throw StandardException.interrupt(ie); 229 } 230 } 231 232 preparedStmt.compilingStatement = true; 233 preparedStmt.setActivationClass(null); 234 } 235 236 try { 237 238 HeaderPrintWriter istream = lcc.getLogStatementText() ? Monitor.getStream() : null; 239 240 248 if (!preparedStmt.isStorable() || lcc.getStatementDepth() == 0) 249 { 250 statementContext = lcc.pushStatementContext(true, isForReadOnly, getSource(), 253 null, false, 0L); 254 } 255 256 257 258 264 CompilerContext cc = lcc.pushCompilerContext(compilationSchema); 265 266 if (prepareIsolationLevel != 267 ExecutionContext.UNSPECIFIED_ISOLATION_LEVEL) 268 { 269 cc.setScanIsolationLevel(prepareIsolationLevel); 270 } 271 272 273 277 if (internalSQL || 278 (spsSchema != null) && (spsSchema.isSystemSchema()) && 279 (spsSchema.equals(compilationSchema))) { 280 cc.setReliability(CompilerContext.INTERNAL_SQL_LEGAL); 281 } 282 283 try 284 { 285 if (istream != null) 287 { 288 String xactId = lcc.getTransactionExecute().getActiveStateTxIdString(); 289 istream.printlnWithHeader(LanguageConnectionContext.xidStr + 290 xactId + 291 "), " + 292 LanguageConnectionContext.lccStr + 293 lcc.getInstanceNumber() + 294 "), " + 295 LanguageConnectionContext.dbnameStr + 296 lcc.getDbname() + 297 "), " + 298 LanguageConnectionContext.drdaStr + 299 lcc.getDrdaID() + 300 "), Begin compiling prepared statement: " + 301 getSource() + 302 " :End prepared statement"); 303 } 304 305 Parser p = cc.getParser(); 306 307 cc.setCurrentDependent(preparedStmt); 308 309 QueryTreeNode qt = p.parseStatement(statementText, paramDefaults); 312 313 parseTime = getCurrentTimeMillis(lcc); 314 315 if (SanityManager.DEBUG) 316 { 317 if (SanityManager.DEBUG_ON("DumpParseTree")) 318 { 319 qt.treePrint(); 320 } 321 322 if (SanityManager.DEBUG_ON("StopAfterParsing")) 323 { 324 throw StandardException.newException(SQLState.LANG_STOP_AFTER_PARSING); 325 } 326 } 327 328 333 334 DataDictionary dataDictionary = lcc.getDataDictionary(); 335 336 int ddMode = dataDictionary == null ? 0 : dataDictionary.startReading(lcc); 337 338 try 339 { 340 lcc.beginNestedTransaction(true); 344 345 qt = qt.bind(); 346 bindTime = getCurrentTimeMillis(lcc); 347 348 if (SanityManager.DEBUG) 349 { 350 if (SanityManager.DEBUG_ON("DumpBindTree")) 351 { 352 qt.treePrint(); 353 } 354 355 if (SanityManager.DEBUG_ON("StopAfterBinding")) { 356 throw StandardException.newException(SQLState.LANG_STOP_AFTER_BINDING); 357 } 358 } 359 360 if (preparedStmt.referencesSessionSchema(qt)) { 392 if (foundInCache) 393 ((GenericLanguageConnectionContext)lcc).removeStatement(this); 394 } 395 396 qt = qt.optimize(); 397 398 optimizeTime = getCurrentTimeMillis(lcc); 399 400 if (istream != null) 402 { 403 String xactId = lcc.getTransactionExecute().getActiveStateTxIdString(); 404 istream.printlnWithHeader(LanguageConnectionContext.xidStr + 405 xactId + 406 "), " + 407 LanguageConnectionContext.lccStr + 408 lcc.getInstanceNumber() + 409 "), " + 410 LanguageConnectionContext.dbnameStr + 411 lcc.getDbname() + 412 "), " + 413 LanguageConnectionContext.drdaStr + 414 lcc.getDrdaID() + 415 "), End compiling prepared statement: " + 416 getSource() + 417 " :End prepared statement"); 418 } 419 } 420 421 catch (StandardException se) 422 { 423 lcc.commitNestedTransaction(); 424 425 if (istream != null) 427 { 428 String xactId = lcc.getTransactionExecute().getActiveStateTxIdString(); 429 istream.printlnWithHeader(LanguageConnectionContext.xidStr + 430 xactId + 431 "), " + 432 LanguageConnectionContext.lccStr + 433 lcc.getInstanceNumber() + 434 "), " + 435 LanguageConnectionContext.dbnameStr + 436 lcc.getDbname() + 437 "), " + 438 LanguageConnectionContext.drdaStr + 439 lcc.getDrdaID() + 440 "), Error compiling prepared statement: " + 441 getSource() + 442 " :End prepared statement"); 443 } 444 throw se; 445 } 446 447 finally 448 { 449 450 if (dataDictionary != null) 451 dataDictionary.doneReading(ddMode, lcc); 452 } 453 454 463 try { 465 if (SanityManager.DEBUG) 466 { 467 if (SanityManager.DEBUG_ON("DumpOptimizedTree")) 468 { 469 qt.treePrint(); 470 } 471 472 if (SanityManager.DEBUG_ON("StopAfterOptimizing")) 473 { 474 throw StandardException.newException(SQLState.LANG_STOP_AFTER_OPTIMIZING); 475 } 476 } 477 478 GeneratedClass ac = qt.generate(preparedStmt.getByteCodeSaver()); 479 480 generateTime = getCurrentTimeMillis(lcc); 481 484 if (generateTime != 0) 485 { 486 endTimestamp = new Timestamp (generateTime); 487 } 488 489 if (SanityManager.DEBUG) 490 { 491 if (SanityManager.DEBUG_ON("StopAfterGenerating")) 492 { 493 throw StandardException.newException(SQLState.LANG_STOP_AFTER_GENERATING); 494 } 495 } 496 497 505 preparedStmt.setConstantAction( qt.makeConstantAction() ); 506 preparedStmt.setSavedObjects( cc.getSavedObjects() ); 507 preparedStmt.setRequiredPermissionsList(cc.getRequiredPermissionsList()); 508 preparedStmt.setActivationClass(ac); 509 preparedStmt.setNeedsSavepoint(qt.needsSavepoint()); 510 preparedStmt.setCursorInfo((CursorInfo)cc.getCursorInfo()); 511 preparedStmt.setIsAtomic(qt.isAtomic()); 512 preparedStmt.setExecuteStatementNameAndSchema( 513 qt.executeStatementName(), 514 qt.executeSchemaName() 515 ); 516 preparedStmt.setSPSName(qt.getSPSName()); 517 preparedStmt.completeCompile(qt); 518 preparedStmt.setCompileTimeWarnings(cc.getWarnings()); 519 } 520 catch (StandardException e) { 522 lcc.commitNestedTransaction(); 523 throw e; 524 } 525 526 if (lcc.getRunTimeStatisticsMode()) 527 { 528 preparedStmt.setCompileTimeMillis( 529 parseTime - beginTime, bindTime - parseTime, optimizeTime - bindTime, generateTime - optimizeTime, getElapsedTimeMillis(beginTime), 534 beginTimestamp, 535 endTimestamp); 536 } 537 538 } 539 finally { 541 lcc.popCompilerContext( cc ); 542 } 543 } 544 catch (StandardException se) 545 { 546 if (foundInCache) 547 ((GenericLanguageConnectionContext)lcc).removeStatement(this); 548 throw se; 549 } 550 finally 551 { 552 synchronized (preparedStmt) { 553 preparedStmt.compilingStatement = false; 554 preparedStmt.notifyAll(); 555 } 556 } 557 558 lcc.commitNestedTransaction(); 559 560 if (statementContext != null) 561 lcc.popStatementContext(statementContext, null); 562 563 return preparedStmt; 564 } 565 566 578 public PreparedStatement prepareStorable( 579 LanguageConnectionContext lcc, 580 PreparedStatement ps, 581 Object [] paramDefaults, 582 SchemaDescriptor spsSchema, 583 boolean internalSQL) 584 throws StandardException 585 { 586 if (ps == null) 587 ps = new GenericStorablePreparedStatement(this); 588 else 589 ((GenericPreparedStatement) ps).statement = this; 590 591 this.preparedStmt = (GenericPreparedStatement) ps; 592 return prepMinion(lcc, false, paramDefaults, spsSchema, internalSQL); 593 } 594 595 public String getSource() { 596 return statementText; 597 } 598 599 public String getCompilationSchema() { 600 return compilationSchema.getDescriptorName(); 601 } 602 603 private static long getCurrentTimeMillis(LanguageConnectionContext lcc) 604 { 605 if (lcc.getStatisticsTiming()) 606 { 607 return System.currentTimeMillis(); 608 } 609 else 610 { 611 return 0; 612 } 613 } 614 615 private static long getElapsedTimeMillis(long beginTime) 616 { 617 if (beginTime != 0) 618 { 619 return System.currentTimeMillis() - beginTime; 620 } 621 else 622 { 623 return 0; 624 } 625 } 626 627 630 631 public boolean equals(Object other) { 632 633 if (other instanceof GenericStatement) { 634 635 GenericStatement os = (GenericStatement) other; 636 637 return statementText.equals(os.statementText) && isForReadOnly==os.isForReadOnly 638 && compilationSchema.equals(os.compilationSchema) && 639 (prepareIsolationLevel == os.prepareIsolationLevel); 640 } 641 642 return false; 643 } 644 645 public int hashCode() { 646 647 return statementText.hashCode(); 648 } 649 } 650 | Popular Tags |