How the General SQL Parser works
The General SQL Parser consists of complete library packages available for the following development environments:
The General SQL Parser supports a multitude of databases, including SQL Server, Oracle, DB2, MySQL, Teradata, Sybase, Informix, Netezza, PostgreSQL, Hadoop Hive and Access databases.
Not only do we provide the libraries. We also offer a wide range of free demos that you can copy and adapt to solve your specific problems.
Below is the high-level architecture of the General SQL Parser.
Figure1: Architecture of General SQL Parser.
The General SQL Parser reads the input SQL text of the query. Lexer (lex parser) tokenizes the input SQL into a list of tokens. Separator turns the source tokens into a list of SQL statements. Each separate SQL statement includes a fragment of source tokens, which is the input to the Yacc Parser.
The Yacc Parser reads the source tokens of each SQL statement. If no syntax error is found, the Parser creates a raw parse tree , based on the BNF of different dialects of databases. Otherwise, the syntax error is detected. A syntax error in one SQL statement doesn’t affect others.
Parser then translates the raw query parse tree into a formal parse tree. The top-level node of the formal parse tree is a specific type of SQL statement, such as TSelectSqlStatement , TInsertSqlStatement , TDeleteSqlStatement , TUpdateSqlStatement , or TCreateTableSqlStatement , which, in turn, includes other sub-parse-tree nodes.
Figure 2: Data Flow in General SQL Parser .
The formal parse tree has a variety of uses, such as the following:
- Obtaining information, such as tables and fields, in a SQL statement is easy. These items are ready for use, along with other database objects in SQL objects.
- Parse tree visitors that descend from TLzVisitorAbs can be used to iterate the parse tree . TLzParseTree2XMLVisitor is a simple visitor that translates the parse tree to XML output. This class is a good reference before you write your own visitor to handle the parse tree.
Many other uses depend only on your requirement and imagination.