📄 genericstatement.java
字号:
int ddMode = dataDictionary == null ? 0 : dataDictionary.startReading(lcc); try { // start a nested transaction -- all locks acquired by bind // and optimize will be released when we end the nested // transaction. lcc.beginNestedTransaction(true); qt = qt.bind(); bindTime = getCurrentTimeMillis(lcc); if (SanityManager.DEBUG) { if (SanityManager.DEBUG_ON("DumpBindTree")) { qt.treePrint(); } if (SanityManager.DEBUG_ON("StopAfterBinding")) { throw StandardException.newException(SQLState.LANG_STOP_AFTER_BINDING); } } //Derby424 - In order to avoid caching select statements referencing // any SESSION schema objects (including statements referencing views // in SESSION schema), we need to do the SESSION schema object check // here. //a specific eg for statement referencing a view in SESSION schema //CREATE TABLE t28A (c28 int) //INSERT INTO t28A VALUES (280),(281) //CREATE VIEW SESSION.t28v1 as select * from t28A //SELECT * from SESSION.t28v1 should show contents of view and we // should not cache this statement because a user can later define // a global temporary table with the same name as the view name. //Following demonstrates that //DECLARE GLOBAL TEMPORARY TABLE SESSION.t28v1(c21 int, c22 int) not // logged //INSERT INTO SESSION.t28v1 VALUES (280,1),(281,2) //SELECT * from SESSION.t28v1 should show contents of global temporary //table and not the view. Since this select statement was not cached // earlier, it will be compiled again and will go to global temporary // table to fetch data. This plan will not be cached either because // select statement is using SESSION schema object. // //Following if statement makes sure that if the statement is // referencing SESSION schema objects, then we do not want to cache it. // We will remove the entry that was made into the cache for //this statement at the beginning of the compile phase. //The reason we do this check here rather than later in the compile // phase is because for a view, later on, we loose the information that // it was referencing SESSION schema because the reference //view gets replaced with the actual view definition. Right after // binding, we still have the information on the view and that is why // we do the check here. if (preparedStmt.referencesSessionSchema(qt)) { if (foundInCache) ((GenericLanguageConnectionContext)lcc).removeStatement(this); } qt = qt.optimize(); optimizeTime = getCurrentTimeMillis(lcc); // Statement logging if lcc.getLogStatementText() is true if (istream != null) { String xactId = lcc.getTransactionExecute().getActiveStateTxIdString(); istream.printlnWithHeader(LanguageConnectionContext.xidStr + xactId + "), " + LanguageConnectionContext.lccStr + lcc.getInstanceNumber() + "), " + LanguageConnectionContext.dbnameStr + lcc.getDbname() + "), " + LanguageConnectionContext.drdaStr + lcc.getDrdaID() + "), End compiling prepared statement: " + getSource() + " :End prepared statement"); } } catch (StandardException se) { lcc.commitNestedTransaction(); if (foundInCache) ((GenericLanguageConnectionContext)lcc).removeStatement(this); // Statement logging if lcc.getLogStatementText() is true if (istream != null) { String xactId = lcc.getTransactionExecute().getActiveStateTxIdString(); istream.printlnWithHeader(LanguageConnectionContext.xidStr + xactId + "), " + LanguageConnectionContext.lccStr + lcc.getInstanceNumber() + "), " + LanguageConnectionContext.dbnameStr + lcc.getDbname() + "), " + LanguageConnectionContext.drdaStr + lcc.getDrdaID() + "), Error compiling prepared statement: " + getSource() + " :End prepared statement"); } throw se; } finally { /* Tell the data dictionary that we are done reading */ if (dataDictionary != null) dataDictionary.doneReading(ddMode, lcc); } /* we need to move the commit of nested sub-transaction * after we mark PS valid, during compilation, we might need * to get some lock to synchronize with another thread's DDL * execution, in particular, the compilation of insert/update/ * delete vs. create index/constraint (see Beetle 3976). We * can't release such lock until after we mark the PS valid. * Otherwise we would just erase the DDL's invalidation when * we mark it valid. */ try // put in try block, commit sub-transaction if bad { if (SanityManager.DEBUG) { if (SanityManager.DEBUG_ON("DumpOptimizedTree")) { qt.treePrint(); } if (SanityManager.DEBUG_ON("StopAfterOptimizing")) { throw StandardException.newException(SQLState.LANG_STOP_AFTER_OPTIMIZING); } } GeneratedClass ac = qt.generate(preparedStmt.getByteCodeSaver()); generateTime = getCurrentTimeMillis(lcc); /* endTimestamp only meaningful if generateTime is meaningful. * generateTime is meaningful if STATISTICS TIMING is ON. */ if (generateTime != 0) { endTimestamp = new Timestamp(generateTime); } if (SanityManager.DEBUG) { if (SanityManager.DEBUG_ON("StopAfterGenerating")) { throw StandardException.newException(SQLState.LANG_STOP_AFTER_GENERATING); } } /* copy over the compile-time created objects to the prepared statement. This always happens at the end of a compile, so there is no need to erase the previous entries on a re-compile -- this erases as it replaces. Set the activation class in case it came from a StorablePreparedStatement */ preparedStmt.setConstantAction( qt.makeConstantAction() ); preparedStmt.setSavedObjects( cc.getSavedObjects() ); preparedStmt.setActivationClass(ac); preparedStmt.setParams(cc.getParams()); preparedStmt.setNeedsSavepoint(qt.needsSavepoint()); preparedStmt.setCursorInfo((CursorInfo)cc.getCursorInfo()); preparedStmt.setIsAtomic(qt.isAtomic()); preparedStmt.setExecuteStatementNameAndSchema( qt.executeStatementName(), qt.executeSchemaName() ); preparedStmt.setSPSName(qt.getSPSName()); preparedStmt.completeCompile(qt); preparedStmt.setCompileTimeWarnings(cc.getWarnings()); } catch (StandardException e) // hold it, throw it { lcc.commitNestedTransaction(); if (foundInCache) ((GenericLanguageConnectionContext)lcc).removeStatement(this); throw e; } if (lcc.getRunTimeStatisticsMode()) { preparedStmt.setCompileTimeMillis( parseTime - beginTime, //parse time bindTime - parseTime, //bind time optimizeTime - bindTime, //optimize time generateTime - optimizeTime, //generate time getElapsedTimeMillis(beginTime), beginTimestamp, endTimestamp); } } finally // for block introduced by pushCompilerContext() { lcc.popCompilerContext( cc ); } } finally { synchronized (preparedStmt) { preparedStmt.compilingStatement = false; preparedStmt.notifyAll(); } } lcc.commitNestedTransaction(); if (statementContext != null) lcc.popStatementContext(statementContext, null); return preparedStmt; } /** * Generates an execution plan given a set of named parameters. * Does so for a storable prepared statement. * * @param compilationSchema the schema to compile against * @param paramDefaults Parameter defaults * * @return A PreparedStatement that allows execution of the execution * plan. * @exception StandardException Thrown if this is an * execution-only version of the module (the prepare() method * relies on compilation). */ public PreparedStatement prepareStorable( LanguageConnectionContext lcc, PreparedStatement ps, Object[] paramDefaults, SchemaDescriptor spsSchema, boolean internalSQL) throws StandardException { if (ps == null) ps = new GenericStorablePreparedStatement(this); else ((GenericPreparedStatement) ps).statement = this; this.preparedStmt = (GenericPreparedStatement) ps; return prepMinion(lcc, false, paramDefaults, spsSchema, internalSQL); } public String getSource() { return statementText; } public boolean getUnicode() { return true; } public String getCompilationSchema() { return compilationSchema.getDescriptorName(); } private static long getCurrentTimeMillis(LanguageConnectionContext lcc) { if (lcc.getStatisticsTiming()) { return System.currentTimeMillis(); } else { return 0; } } private static long getElapsedTimeMillis(long beginTime) { if (beginTime != 0) { return System.currentTimeMillis() - beginTime; } else { return 0; } } /* ** Identity */ public boolean equals(Object other) { if (other instanceof GenericStatement) { GenericStatement os = (GenericStatement) other; return statementText.equals(os.statementText) && compilationSchema.equals(os.compilationSchema) && (prepareIsolationLevel == os.prepareIsolationLevel); } return false; } public int hashCode() { return statementText.hashCode(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -