📄 querytreenode.java
字号:
/* Derby - Class org.apache.derby.impl.sql.compile.QueryTreeNode Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derby.impl.sql.compile;import org.apache.derby.iapi.sql.execute.ExecutionFactory;import org.apache.derby.impl.sql.compile.ActivationClassBuilder;import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;import org.apache.derby.impl.sql.execute.GenericConstantActionFactory;import org.apache.derby.impl.sql.execute.GenericExecutionFactory;import org.apache.derby.iapi.util.ByteArray;import org.apache.derby.iapi.services.loader.ClassFactory;import org.apache.derby.iapi.services.loader.ClassInspector;import org.apache.derby.iapi.services.loader.GeneratedClass;import org.apache.derby.iapi.services.context.ContextManager;import org.apache.derby.iapi.services.compiler.MethodBuilder;import org.apache.derby.iapi.services.monitor.Monitor;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.services.io.StoredFormatIds;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.sql.compile.CompilerContext;import org.apache.derby.iapi.sql.compile.NodeFactory;import org.apache.derby.iapi.sql.compile.Parser;import org.apache.derby.iapi.sql.compile.Visitable;import org.apache.derby.iapi.sql.compile.Visitor;import org.apache.derby.iapi.sql.compile.C_NodeTypes;import org.apache.derby.iapi.sql.compile.NodeFactory;import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;import org.apache.derby.iapi.sql.conn.LanguageConnectionFactory;import org.apache.derby.iapi.sql.dictionary.DataDictionary;import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext;import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;import org.apache.derby.iapi.sql.dictionary.AliasDescriptor;import org.apache.derby.iapi.sql.dictionary.TableDescriptor;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.sql.execute.ConstantAction;import org.apache.derby.iapi.types.DataTypeDescriptor;import org.apache.derby.iapi.types.TypeId;import org.apache.derby.iapi.types.DataValueDescriptor;import org.apache.derby.iapi.sql.compile.TypeCompiler;import org.apache.derby.iapi.sql.ResultDescription;import org.apache.derby.iapi.sql.StatementType;import org.apache.derby.iapi.sql.Activation;import org.apache.derby.iapi.services.classfile.VMOpcode;import org.apache.derby.iapi.sql.depend.DependencyManager;import org.apache.derby.iapi.sql.dictionary.AliasDescriptor;import org.apache.derby.catalog.AliasInfo;import org.apache.derby.catalog.types.SynonymAliasInfo;import java.util.Properties;import java.util.Vector;import java.sql.Types;import org.apache.derby.iapi.reference.ClassName;/** * QueryTreeNode is the root class for all query tree nodes. All * query tree nodes inherit from QueryTreeNode except for those that extend * QueryTreeNodeVector. * * @author Jeff Lichtman */public abstract class QueryTreeNode implements Visitable{ public static final int AUTOINCREMENT_START_INDEX = 0; public static final int AUTOINCREMENT_INC_INDEX = 1; public static final int AUTOINCREMENT_IS_AUTOINCREMENT_INDEX = 2; int beginOffset; // offset into SQL input of the substring // which this query node encodes. int endOffset; private int nodeType; private ContextManager cm; private LanguageConnectionContext lcc; private GenericConstantActionFactory constantActionFactory; /** * Set the ContextManager for this node. * * @param cm The ContextManager. * * @return Nothing. */ public void setContextManager(ContextManager cm) { this.cm = cm; if (SanityManager.DEBUG) { SanityManager.ASSERT(cm != null, "cm not expected to be null"); } } /** * Get the current ContextManager. * * @return The current ContextManager. */ public final ContextManager getContextManager() { if (SanityManager.DEBUG) { if (cm == null) SanityManager.THROWASSERT("Null context manager in QueryTreeNode of type :" + this.getClass()); } return cm; } /** * Gets the NodeFactory for this database. * * @return the node factory for this database. * */ public final NodeFactory getNodeFactory() { return getLanguageConnectionContext().getLanguageConnectionFactory(). getNodeFactory(); } /** * Gets the constant action factory for this database. * * @return the constant action factory. */ public final GenericConstantActionFactory getGenericConstantActionFactory() { if ( constantActionFactory == null ) { GenericExecutionFactory execFactory = (GenericExecutionFactory) getExecutionFactory(); constantActionFactory = execFactory.getConstantActionFactory(); } return constantActionFactory; } public final ExecutionFactory getExecutionFactory() { ExecutionFactory ef = getLanguageConnectionContext().getLanguageConnectionFactory().getExecutionFactory(); return ef; } /** Get the ClassFactory to use with this database. */ protected final ClassFactory getClassFactory() { return getLanguageConnectionContext().getLanguageConnectionFactory(). getClassFactory(); } /** * Gets the LanguageConnectionContext for this connection. * * @return the lcc for this connection * */ protected final LanguageConnectionContext getLanguageConnectionContext() { if (lcc == null) { lcc = (LanguageConnectionContext) getContextManager(). getContext(LanguageConnectionContext.CONTEXT_ID); } return lcc; } /** * Get the name of the SPS that is used * to execute this statement. Only relevant * for an ExecSPSNode -- otherwise, returns null. * * @return the name of the underlying sps */ public String getSPSName() { return null; } /** * Gets the beginning offset of the SQL substring which this * query node represents. * * @return The beginning offset of the SQL substring. * */ public int getBeginOffset() { return beginOffset; } /** * Sets the beginning offset of the SQL substring which this * query node represents. * * @param The beginning offset of the SQL substring. * */ public void setBeginOffset( int beginOffset ) { this.beginOffset = beginOffset; } /** * Gets the ending offset of the SQL substring which this * query node represents. * * @return The ending offset of the SQL substring. * */ public int getEndOffset() { return endOffset; } /** * Sets the ending offset of the SQL substring which this * query node represents. * * @param The ending offset of the SQL substring. * */ public void setEndOffset( int endOffset ) { this.endOffset = endOffset; } /** * Return header information for debug printing of this query * tree node. * * @return Header information for debug printing of this query * tree node. */ protected String nodeHeader() { if (SanityManager.DEBUG) { return "\n" + this.getClass().getName() + '@' + Integer.toHexString(hashCode()) + "\n"; } else { return ""; } } /** * Format a node that has been converted to a String for printing * as part of a tree. This method indents the String to the given * depth by inserting tabs at the beginning of the string, and also * after every newline. * * @param nodeString The node formatted as a String * @param depth The depth to indent the given node * * @return The node String reformatted with tab indentation */ public static String formatNodeString(String nodeString, int depth) { if (SanityManager.DEBUG) { StringBuffer nodeStringBuffer = new StringBuffer(nodeString); int pos; char c; char[] indent = new char[depth]; /* ** Form an array of tab characters for indentation. */ while (depth > 0) { indent[depth - 1] = '\t'; depth--; } /* Indent the beginning of the string */ nodeStringBuffer.insert(0, indent); /* ** Look for newline characters, except for the last character. ** We don't want to indent after the last newline. */ for (pos = 0; pos < nodeStringBuffer.length() - 1; pos++) { c = nodeStringBuffer.charAt(pos); if (c == '\n') { /* Indent again after each newline */ nodeStringBuffer.insert(pos + 1, indent); } } return nodeStringBuffer.toString(); } else { return ""; } } /** * Print this tree for debugging purposes. This recurses through * all the sub-nodes and prints them indented by their depth in * the tree. * * @return Nothing */ public void treePrint() { if (SanityManager.DEBUG) { debugPrint(nodeHeader()); debugPrint(formatNodeString(this.toString(), 0)); printSubNodes(0); debugFlush(); } } /** * Print this tree for debugging purposes. This recurses through * all the sub-nodes and prints them indented by their depth in * the tree, starting with the given indentation. * * @param depth The depth of this node in the tree, thus, * the amount to indent it when printing it. * * @return Nothing */ public void treePrint(int depth) { if (SanityManager.DEBUG) { debugPrint(formatNodeString(nodeHeader(), depth)); debugPrint(formatNodeString(this.toString(), depth)); printSubNodes(depth); } } /** * Print a String for debugging * * @param outputString The String to print * * @return Nothing */ public static void debugPrint(String outputString) { if (SanityManager.DEBUG) { SanityManager.GET_DEBUG_STREAM().print(outputString); } } /** * Flush the debug stream out * * @return Nothing */ protected static void debugFlush() { if (SanityManager.DEBUG) { SanityManager.GET_DEBUG_STREAM().flush(); } } /** * Print the sub-nodes of this node. * * Each sub-class of QueryTreeNode is expected to provide its own * printSubNodes() method. In each case, it calls super.printSubNodes(), * passing along its depth, to get the sub-nodes of the super-class. * Then it prints its own sub-nodes by calling treePrint() on each * of its members that is a type of QueryTreeNode. In each case where * it calls treePrint(), it should pass "depth + 1" to indicate that * the sub-node should be indented one more level when printing. * Also, it should call printLabel() to print the name of each sub-node * before calling treePrint() on the sub-node, so that the reader of * the printed tree can tell what the sub-node is. * * This printSubNodes() exists in here merely to act as a backstop. * In other words, the calls to printSubNodes() move up the type * hierarchy, and in this node the calls stop. * * I would have liked to put the call to super.printSubNodes() in * this super-class, but Java resolves "super" statically, so it * wouldn't get to the right super-class. * * @param depth The depth to indent the sub-nodes * * @return Nothing */ public void printSubNodes(int depth) { } /** * Format this node as a string * * Each sub-class of QueryTreeNode should implement its own toString() * method. In each case, toString() should format the class members * that are not sub-types of QueryTreeNode (printSubNodes() takes care * of following the references to sub-nodes, and toString() takes care * of all members that are not sub-nodes). Newlines should be used * liberally - one good way to do this is to have a newline at the * end of each formatted member. It's also a good idea to put the * name of each member in front of the formatted value. For example, * the code might look like: * * "memberName: " + memberName + "\n" + ... * * @return This node formatted as a String */ public String toString() { return ""; } /** * Print the given label at the given indentation depth. * * @param depth The depth of indentation to use when printing * the label * @param label The String to print * * @return Nothing */ public void printLabel(int depth, String label) { if (SanityManager.DEBUG) { debugPrint(formatNodeString(label, depth)); } } /** * Perform the binding operation on a query tree. Binding consists of * permissions checking, view resolution, datatype resolution, and * creation of a dependency list (for determining whether a tree or * plan is still up to date). * * This bind() method does nothing. Each node type that can appear * at the top of a tree can override this method with its own bind() * method that does "something". * * @return The bound query tree * * @exception StandardException Thrown on error */ public QueryTreeNode bind() throws StandardException { return this; } /** * Return true if the node references SESSION schema tables (temporary or permanent) * * @return true if references SESSION schema tables, else false * * @exception StandardException Thrown on error */ public boolean referencesSessionSchema() throws StandardException { return false; } /** * Checks if the passed schema descriptor is for SESSION schema * * @return true if the passed schema descriptor is for SESSION schema * * @exception StandardException Thrown on error */ final boolean isSessionSchema(SchemaDescriptor sd) { return isSessionSchema(sd.getSchemaName()); } /** * Checks if the passed schema name is for SESSION schema * * @return true if the passed schema name is for SESSION schema * * @exception StandardException Thrown on error */ final boolean isSessionSchema(String schemaName) { return SchemaDescriptor.STD_DECLARED_GLOBAL_TEMPORARY_TABLES_SCHEMA_NAME.equals(schemaName); } /** * Get the optimizer's estimate of the number of rows returned or affected * for an optimized QueryTree. * * For non-optimizable statements (for example, CREATE TABLE), * return 0. For optimizable statements, this method will be * over-ridden in the statement's root node (DMLStatementNode * in all cases we know about so far). * * @return 0L */ public long getRowEstimate() { return 0L; } /** * Generates an optimized QueryTree from a bound QueryTree. Actually, * it annotates the tree in place rather than generating a new tree, * but this interface allows the root node of the optmized QueryTree * to be different from the root node of the bound QueryTree. * * For non-optimizable statements (for example, CREATE TABLE), * return the bound tree without doing anything. For optimizable * statements, this method will be over-ridden in the statement's * root node (DMLStatementNode in all cases we know about so far). * * Throws an exception if the tree is not bound, or if the binding
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -