JavaXT

SQL Parser

The javaxt.sql.Parser class is used to parse SQL Select statments and return the constituent parts.

Instantiating the Parser

   String sqlString = 
   "SELECT Customers.FirstName, Customers.LastName, SUM(Sales.SaleAmount) AS SalesPerCustomer " +
   "FROM Customers LEFT JOIN Sales ON Customers.CustomerID = Sales.CustomerID " +
   "GROUP BY Customers.FirstName, Customers.LastName;";

   javaxt.sql.Parser sqlParser = new javaxt.sql.Parser(sqlString);

Print "Select" Statements

        System.out.println();
        System.out.println("-----------------------------------------");
        System.out.println("SELECT Statements");
        System.out.println("-----------------------------------------");
        SelectStatement[] selectStatements = sqlParser.getSelectStatements();
        for (int i=0; i<selectStatements.length; i++){
            SelectStatement statement = selectStatements[i];
            System.out.println(statement);
        }

Print Selected Columns

Print a list of columns found in the select clause. Note that aliases defined in the "AS" statements are ignored.

        System.out.println();
        System.out.println("-----------------------------------------");
        System.out.println("Selected Columns");
        System.out.println("-----------------------------------------");
        String[] selectColumns = sqlParser.getSelectedColumns();
        for (int i=0; i<selectColumns.length; i++){
            String columnName = selectColumns[i];
            System.out.println(columnName);
        }

Print Selected Tables

        System.out.println();
        System.out.println("-----------------------------------------");
        System.out.println("Selected Tables");
        System.out.println("-----------------------------------------");
        String[] selectTables = sqlParser.getTables();
        for (int i=0; i<selectTables.length; i++){
            String tableName = selectTables[i];
            System.out.println(tableName);
        }

Print "Where" Statements

        WhereStatement[] whereStatements = sqlParser.getWhereStatements();
        if (whereStatements.length>0){
            System.out.println();
            System.out.println("-----------------------------------------");
            System.out.println("WHERE Statements");
            System.out.println("-----------------------------------------");

            for (int i=0; i<whereStatements.length; i++){
                WhereStatement statement = whereStatements[i];
                System.out.println(statement.toString() + " \"" + statement.getOperator() + "\"");
            }
        }

Print "Order By" Statements

        OrderByStatement[] orderByStatements = sqlParser.getOrderByStatements();
        if (orderByStatements.length>0){
            System.out.println();
            System.out.println("-----------------------------------------");
            System.out.println("ORDER BY Statements");
            System.out.println("-----------------------------------------");

            for (int i=0; i<orderByStatements.length; i++){
                OrderByStatement statement = orderByStatements[i];
                System.out.println(statement);
            }
        }

Print "Group By" Statements

        GroupByStatement[] groupByStatements = sqlParser.getGroupByStatements();
        if (groupByStatements.length>0){
            System.out.println();
            System.out.println("-----------------------------------------");
            System.out.println("GROUP BY Statements");
            System.out.println("-----------------------------------------");

            for (int i=0; i<groupByStatements.length; i++){
                GroupByStatement statement = groupByStatements[i];
                System.out.println(statement);
            }
        }