/* Derby - Class ASTParser 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. */ import java.io.*; import java.sql.*; import org.apache.derby.iapi.reference.ContextId; import org.apache.derby.iapi.services.context.ContextManager; import org.apache.derby.iapi.services.sanity.SanityManager; import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; import org.apache.derby.impl.jdbc.EmbedConnection; import org.apache.derby.impl.sql.compile.QueryTreeNode; /** * Little program to run the Derby parser on a text string and to print * the resulting abstract syntax tree produced by the parser. */ public class ASTParser { /////////////////////////////////////////////////////////////////////////////////// // // CONSTANTS // /////////////////////////////////////////////////////////////////////////////////// public static final String DRIVER_NAME = "org.apache.derby.jdbc.EmbeddedDriver"; public static final String CONNECTION_URL = "jdbc:derby:memory:dummy;create=true"; public static final String DERBY_DEBUG_SETTING = "derby.debug.true"; public static final String STOP_AFTER_PARSING = "StopAfterParsing"; public static final String STOPPED_AFTER_PARSING = "42Z55"; public static final String SHUTDOWN_URL = "jdbc:derby:;shutdown=true"; /////////////////////////////////////////////////////////////////////////////////// // // STATE // /////////////////////////////////////////////////////////////////////////////////// private PrintStream _printStream = System.out; /////////////////////////////////////////////////////////////////////////////////// // // ENTRY POINT // /////////////////////////////////////////////////////////////////////////////////// /** * Entry point. Takes one argument: the string to be parsed. Along * the way, this program will create a dummy Derby database if it * doesn't already exist. */ public static void main( String[] args ) throws Exception { ASTParser astParser = new ASTParser(); astParser.execute( args ); } private void execute( String[] args ) throws Exception { System.setProperty( DERBY_DEBUG_SETTING, STOP_AFTER_PARSING ); Class.forName( DRIVER_NAME ); String text = args[ 0 ]; Connection conn = DriverManager.getConnection( CONNECTION_URL ); PreparedStatement ps = null; println( "Parsing:\n" + text ); try { ps = conn.prepareStatement( text ); } catch (SQLException se) { String sqlState = se.getSQLState(); if ( !STOPPED_AFTER_PARSING.equals( sqlState ) ) { throw se; } } ContextManager contextManager = ((EmbedConnection) conn).getContextManager(); LanguageConnectionContext lcc = (LanguageConnectionContext) contextManager.getContext( ContextId.LANG_CONNECTION); QueryTreeNode queryTree = (QueryTreeNode) lcc.getLastQueryTree(); SanityManager.SET_DEBUG_STREAM( new PrintWriter( _printStream ) ); queryTree.treePrint(); try { DriverManager.getConnection( SHUTDOWN_URL ); } catch (Exception e) {} } public void println( String text ) { _printStream.println( text ); } }