db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r827760 - in /db/derby/code/trunk/java/engine/org/apache/derby: iapi/sql/compile/ASTVisitor.java iapi/sql/conn/LanguageConnectionContext.java impl/sql/GenericStatement.java impl/sql/conn/GenericLanguageConnectionContext.java
Date Tue, 20 Oct 2009 18:36:54 GMT
Author: rhillegas
Date: Tue Oct 20 18:36:54 2009
New Revision: 827760

URL: http://svn.apache.org/viewvc?rev=827760&view=rev
Log:
DERBY-4415: Add ability to plug customized AST printers into the compiler.

Added:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/compile/ASTVisitor.java   (with
props)
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/conn/LanguageConnectionContext.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/GenericStatement.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/conn/GenericLanguageConnectionContext.java

Added: db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/compile/ASTVisitor.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/compile/ASTVisitor.java?rev=827760&view=auto
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/compile/ASTVisitor.java (added)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/compile/ASTVisitor.java Tue
Oct 20 18:36:54 2009
@@ -0,0 +1,87 @@
+/*
+
+   Derby - Class org.apache.derby.iapi.sql.compile.ASTVisitor
+
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to you 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.iapi.sql.compile;
+
+import org.apache.derby.iapi.error.StandardException;
+
+/**
+ * <p>
+ * A Visitor which handles nodes in Derby's abstract syntax trees. In addition
+ * to this contract, it is expected that an ASTVisitor will have a 0-arg
+ * constructor. You use an ASTVisitor like this:
+ * </p>
+ *
+ * <blockquote><pre>
+ * // initialize your visitor
+ * MyASTVisitor myVisitor = new MyASTVisitor();
+ * myVisitor.initializeVisitor();
+ * languageConnectionContext.setASTVisitor( myVisitor );
+ *
+ * // then run your queries.
+ * ...
+ *
+ * // when you're done inspecting query trees, release resources and
+ * // remove your visitor
+ * languageConnectionContext.setASTVisitor( null );
+ * myVisitor.teardownVisitor();
+ * </pre></blockquote>
+ *
+ */
+public interface ASTVisitor extends Visitor
+{
+    // Compilation phases for tree handling
+
+    public static final int AFTER_PARSE = 0;
+    public static final int AFTER_BIND = 1;
+    public static final int AFTER_OPTIMIZE = 2;
+
+    /**
+     * Initialize the Visitor before processing any trees. User-written code
+     * calls this method before poking the Visitor into the
+     * LanguageConnectionContext. For example, an
+     * implementation of this method might open a trace file.
+     */
+    public void initializeVisitor() throws StandardException;
+
+    /**
+     * Final call to the Visitor. User-written code calls this method when it is
+     * done inspecting query trees. For instance, an implementation of this method
+     * might release resources, closing files it has opened.
+     */
+    public void teardownVisitor() throws StandardException;
+
+    /**
+     * The compiler calls this method just before walking a query tree.
+     *
+     * @param statementText Text used to create the tree.
+     * @param phase of compilation (AFTER_PARSE, AFTER_BIND, or AFTER_OPTIMIZE).
+     */
+    public void begin( String statementText, int phase ) throws StandardException;
+    
+    /**
+     * The compiler calls this method when it's done walking a tree.
+     *
+     * @param phase of compilation (AFTER_PARSE, AFTER_BIND, or AFTER_OPTIMIZE).
+     */
+    public void end( int phase ) throws StandardException;
+    
+}	

Propchange: db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/compile/ASTVisitor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/conn/LanguageConnectionContext.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/conn/LanguageConnectionContext.java?rev=827760&r1=827759&r2=827760&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/conn/LanguageConnectionContext.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/sql/conn/LanguageConnectionContext.java
Tue Oct 20 18:36:54 2009
@@ -32,6 +32,7 @@
 import org.apache.derby.iapi.sql.compile.OptimizerFactory;
 import org.apache.derby.iapi.types.DataValueFactory;
 
+import org.apache.derby.iapi.sql.compile.ASTVisitor;
 import org.apache.derby.iapi.sql.depend.Provider;
 import org.apache.derby.iapi.sql.execute.ConstantAction;
 import org.apache.derby.iapi.sql.execute.CursorActivation;
@@ -1241,4 +1242,20 @@
     public String getXplainSchema();
     public void setXplainStatement(Object key, Object stmt);
     public Object getXplainStatement(Object key);
+
+    /**
+     * Set a Visitor which walks the AST at various stages. This is useful
+     * for poking user-written inspectors into the parse, bind, and optimize phases.
+     *
+     * @param visitor The Visitor which should walk the tree. Could be null.
+     */
+    public void setASTVisitor( ASTVisitor visitor );
+
+    /**
+     * Get the Visitor which should walk the AST.
+     *
+     * @return The Visitor for that phase. Could be null.
+     */
+    public ASTVisitor getASTVisitor( );
+    
 }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/GenericStatement.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/GenericStatement.java?rev=827760&r1=827759&r2=827760&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/GenericStatement.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/GenericStatement.java Tue Oct
20 18:36:54 2009
@@ -33,6 +33,8 @@
 import org.apache.derby.iapi.sql.Statement;
 import org.apache.derby.iapi.sql.compile.CompilerContext;
 import org.apache.derby.iapi.sql.compile.Parser;
+import org.apache.derby.iapi.sql.compile.Visitable;
+import org.apache.derby.iapi.sql.compile.ASTVisitor;
 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
 import org.apache.derby.iapi.sql.conn.StatementContext;
 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
@@ -281,6 +283,9 @@
 
 				parseTime = getCurrentTimeMillis(lcc);
 
+                // Call user-written tree-printer if it exists
+                walkAST( lcc, qt, ASTVisitor.AFTER_PARSE);
+
 				if (SanityManager.DEBUG) 
 				{
 					if (SanityManager.DEBUG_ON("DumpParseTree")) 
@@ -319,6 +324,9 @@
 					qt.bindStatement();
 					bindTime = getCurrentTimeMillis(lcc);
 
+                    // Call user-written tree-printer if it exists
+                    walkAST( lcc, qt, ASTVisitor.AFTER_BIND);
+
 					if (SanityManager.DEBUG) 
 					{
 						if (SanityManager.DEBUG_ON("DumpBindTree")) 
@@ -374,6 +382,9 @@
 
 					optimizeTime = getCurrentTimeMillis(lcc);
 
+                    // Call user-written tree-printer if it exists
+                    walkAST( lcc, qt, ASTVisitor.AFTER_OPTIMIZE);
+
 					// Statement logging if lcc.getLogStatementText() is true
 					if (istream != null)
 					{
@@ -543,6 +554,18 @@
 		return preparedStmt;
 	}
 
+    /** Walk the AST, using a (user-supplied) Visitor */
+    private void walkAST( LanguageConnectionContext lcc, Visitable queryTree, int phase )
throws StandardException
+    {
+        ASTVisitor visitor = lcc.getASTVisitor();
+        if ( visitor != null )
+        {
+            visitor.begin( statementText, phase );
+            queryTree.accept( visitor );
+            visitor.end( phase );
+        }
+    }
+
 	/**
 	 * Generates an execution plan given a set of named parameters.
 	 * Does so for a storable prepared statement.

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/conn/GenericLanguageConnectionContext.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/conn/GenericLanguageConnectionContext.java?rev=827760&r1=827759&r2=827760&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/conn/GenericLanguageConnectionContext.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/conn/GenericLanguageConnectionContext.java
Tue Oct 20 18:36:54 2009
@@ -41,6 +41,7 @@
 import org.apache.derby.iapi.error.StandardException;
 import org.apache.derby.iapi.sql.compile.CompilerContext;
 import org.apache.derby.iapi.sql.compile.OptimizerFactory;
+import org.apache.derby.iapi.sql.compile.ASTVisitor;
 import org.apache.derby.iapi.sql.conn.Authorizer;
 import org.apache.derby.iapi.error.ExceptionSeverity;
 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
@@ -290,6 +291,9 @@
 	// cache of ai being handled in memory (bulk insert + alter table).
 	private HashMap autoincrementCacheHashtable;
 
+    // User-written inspector to print out query tree
+    private ASTVisitor astWalker;
+    
 	/*
 	   constructor
 	*/
@@ -3537,4 +3541,14 @@
 	{
 		return xplain_statements.get(key);
 	}
+    
+    public void setASTVisitor( ASTVisitor visitor )
+    {
+        astWalker = visitor;
+    }
+    public ASTVisitor getASTVisitor( )
+    {
+        return astWalker;
+    }
+
 }



Mime
View raw message