SQL Parser .NET version SQL Parser Java version
   

Installation
   Just copy all dcu files(or dll files for .NET version) to a directory. And make this directory in the search path of your project options.(Project->Options->Directories/Conditionals->Search path).

How general sql parser works:
  • 1. Read sql text, and it will be tokenlized by lex parser into a list of tokens. OnTokenlizerToken event is fired when a lex token is found. user can modify the source token in the event as they needed. After read all input sql, a source token list is generated which is the input of the yacc parser. AfterTokenlizer event is also fired before the source token list parsed by yacc parser.
  • 2. Yacc parser read source token in SourceTokenList,based on the BNF of different database dialects, the parser will create a raw parse tree if no syntax error was found. During the parsing process,OnParserToken event is fired when a token is read. If there is a syntax error after reading a source token, then OnParserTokenError event is fired , you can create your own event handler to recover syntax error dynamically in this event.
  • 3.Translate the raw parse tree into a formal parse tree, top level node of the formal parse tree is type of sql statement such as TSelectSqlStatement, TInsertSqlStatement, TDeleteSqlStatement, TUpdateSqlStatement, TCreateTableSqlStatement etc. which in turn includes other sub tree nodes(All available parse tree nodes). During the translation, parser will iterate the whole raw parse tree, and OnTableToken event will be fired if a source token stands for a table is found, OnFieldToken event will be fired if a source token stands for a field is found.
  • 4. How to use this formal parse tree dependeds on your requirement and imagination.
    Getting information such as tables and fields in a sql statement is very easy, they are ready for use along with other db ojects in sql objects.
    Visitors descends from TLzVisitorAbs can be used to iterate the parse tree. TLzParseTree2XMLVisitor is a simple visitor which tranlate the parse tree to XML output. This class is a good reference before you begin to write your own visitor to handle the parse tree.
    Check the Visitor pattern used in General SQL Parser for more information on how to use visitor to handle the parse tree.

    Followings are some typical usages of parse tree.



Pretty print function
   Generate pretty sql from parse tree. since pretty sql is built from parse tree, the input sql must be parsed first before print out pretty sql, and there should be no syntax errors in sql text. If there are syntax errors, no parse tree will be built, so can't generate pretty sql by gsqlparser.

Generate sql dynamically
   Change objects in parse tree such as field, table and then regenereate sql on the fly.
The other way to generate sql is creating sql objects and then build parse tree manually, such as create a TSelectSqlStatement, TField, TTable object, and then assign TField and TTable objects to TSelectSqlStatement , thus a parse tree will be created with TSelectSqlStatement as root node. From this parse tree, you can generate sql text.

Sql translate between different database dialects
   Sql translate between different database dialects is really a challenge work , based on parse tree generated by gsqlparser, it is possible to do this job. But frankly speaking, the parse tree is just a place to start, it is your job to achieve this.



COPYRIGHT (C) 2001-2011 sqlparser.com , privacy policy  ALL RIGHT RESERVED