SMACS

Package smacs.sql

[1] SQL-to-AST parser, AST-to-SQL generator, Tree Walker/Visitor.

See:
          Description

Interface Summary
Node  
SqlScriptConstants  
SqlScriptTreeConstants  
SqlScriptVisitor  
SqlTreeConstants  
 

Class Summary
SimpleCharStream An implementation of interface CharStream, where the stream is assumed to contain only ASCII characters (without unicode processing).
SimpleNode  
SqlParserNode Wrapping the SQL parser with a file-loader.
SqlScript  
SqlScript.ValueNode  
SqlScriptTokenManager  
SqlTree  
SqlTreeTokenManager  
SqlWalker for each Token[Name] call an "each[Name]" method of this class.
SqlWalkerXml SqlWalker that outputs an XML text representation.
SqlWalkerXmlWriter create XML output text with help of SqlWalker.
SqlWriter Formatting a ParsingTree into SQL strings.
Token Describes the input token stream.
 

Exception Summary
ParseException This exception is thrown when parse errors are encountered.
 

Error Summary
TokenMgrError  
 

Package smacs.sql Description

[1] SQL-to-AST parser, AST-to-SQL generator, Tree Walker/Visitor.

This is a base part of the project.

We check out some variants of sql parsers, while there are some different variants, they are always somehow based on the javacc parser provided by Oracle to promote the psql (turing)language.

That original javacc implementation is missing a lot however, we even have to implement things like CREATE TABLE to be recognized from an *.sql snippet. So, actually this implementation is always under change to match our needs.

Apart from the input syntax, most stuff out there is not giving us an Abstract Syntax Tree that can be turned back into a proper SQL script. Since the late 90ies, we always use ASTs. Therefore, the task was to explore tools that give us a proper tree view of the input language.

The first choice is of course JJTree provided in the same java package of JavaCC. That is one dependency less in contrast to more sophisticated AST builders which need to be installed separately. However, JavaCC does not give su an ASTtoSQL, the generated Visitor is wrong, and it can not generate on itself as a subpackage.

So I had a look at JTB, short for "Java Tree Builder"), which is very close to JJTree/JavaCC allowing us to take the same input syntax description for a start. It does give us a proper ASTtoSQL, proper subpackage build, and a Visitor class. It does not give us a generic Node/Visitor, so we can not add extra Node-types to that tree which renders the Visitor useless. And the ASTtoSQL happens to be not the same as the input, esp. killing any proper indentation, so we need to writer our own ASTtoSQL anyway. (Experiment source is the SqlTree.jtb file).

So, we are back with a JJTree-based implementation of the SQL syntax, in `SqlScript.jjt`. For that one, we add a proper Visitor scheme in SqlWalker that also allows to walk node-types added later. And we provide SqlWriter which is an ASTtoSQL generator with proper indentation, however it is unfinished and still needs to be worked on. Later I discovered that JJTree does not register leaf literals on its own (i.e. Strings or Names in the SQL input), so that's what SqlValue is about, storing the value.

The rest of the classes are either JavaCC generated (e.g. Node, SimpleNode, Token*, *Visitor), or wrappers around the three main classes. We check validity of the SqlWalker with some implementations showing the AST in paraXML output syntax, the ScriptNode allows you to just give it a filename as an argument (and abstracting whether we used a JJTree or JTB base parser).

Remember to check out `make check` for possible usages, about all classes have a main() entry point to check the intended basic functionality of the given class. The check-* targets in the makefile will show a possible call scheme, generally just picking up a test sql script file.


SMACS