Table of Contents

Class TGSqlParser

Namespace
gudusoft.gsqlparser
Assembly
gudusoft.gsqlparser.dll

This is the class where you start to use this SQL parser library.

create a sql parser by specify a database vendor, then set sql script or a file including sql script need to be processed, then call one of the following methods to achieve what you need:

  • parse(), check syntax of input SQL, do a in-depth analysis of input SQL such as creating relationship between table and columns. SQL query parse tree nodes are available after calling parse method and return without any syntax errors.

    Syntax error in one SQL statement in SQL scripts separated by statement delimiter such as ; doesn't affects others, but failed to parse a SQL statement insides a stored procedure will leads all SQL statements inside this stored procedure un-parsed which means no parse tree nodes will be available for this stored procedure.

    If syntax error detected, please check ErrorCount and Errormessage. SyntaxErrors include a list of syntax error object.

  • checkSyntax(), same as parse()
  • tokenizeSqltext(), Turn input sql into a list of tokens which is the basic lexis element of SQL syntax.Token is categorized as keyword, identifier, number, operator, whitespace and other types. Fetch source tokens via sourcetokenlist
    Parse tree node is not available
  • getrawsqlstatements(), Separate SQL statement in a script file without doing syntax checking, a list of sql statements which is sub class of TCustomSqlStatement is avaiable after calling this method. Use statement type ESqlStatementType to check type of SQL statement, Use String to get text representation of each SQL statement, no further detailed information about SQL statement was available. You may fetch SQL statements via sqlstatements

Sample code used to format SQL script.

int ret = sqlparser.parse();
 if (ret == 0){
     GFmtOpt option = GFmtOptFactory.newInstance();
     String result = FormatterFactory.pp(sqlparser, option);
     System.out.println(result);
 }else{
     System.out.println(sqlparser.getErrormessage());
 }

Some use cases after paring SQL script: are:

  • Table/column impact analysis
  • SQL rewriting
  • SQL translate between different databases
  • SQL migration analysis
  • Help to anti SQL injection
  • More use cases

Typically, SQL parse tree nodes generated by this SQL Parser were closely related to SQL elements defined in database vendor's SQL reference book. here is a brief summary of some most used SQL elements and corresponding classes defined in this SQL parser.

Some major SQL statements:

Stored procedure

[CLSCompliant(true)]
public class TGSqlParser
Inheritance
TGSqlParser
Inherited Members

Constructors

TGSqlParser(EDbVendor)

public TGSqlParser(EDbVendor pdbvendor)

Parameters

pdbvendor EDbVendor

The SQL dialect to be parsed

Properties

DbVendor

SQL dialect handled by this parser

public virtual EDbVendor DbVendor { get; }

Property Value

EDbVendor

EnablePartialParsing

if this property is true, parser will try to parse statements inside Sybase procedure body after AS keyword.

public bool EnablePartialParsing { get; set; }

Property Value

bool

ErrorCount

Syntax errors of SQL script.

public virtual int ErrorCount { get; }

Property Value

int

Errormessage

Detailed syntax error messages.

public virtual string Errormessage { get; }

Property Value

string

Flexer

lexer used by SQL parser

public TCustomLexer Flexer { get; }

Property Value

TCustomLexer

MetaDatabase

Provide meta information about table/column inside database to help this parser determine relationship of table/column in sql query which is been parsed. Please check demo: demos\gettablecolumns for more detailed information.

public virtual IMetaDatabase MetaDatabase { get; set; }

Property Value

IMetaDatabase

SqlInputStream

used by SQL parser internally

public virtual Stream SqlInputStream { get; set; }

Property Value

Stream

SyntaxErrors

List of parse error, element of this list is TSyntaxError

public virtual List<TSyntaxError> SyntaxErrors { get; }

Property Value

List<TSyntaxError>

error list

TokenHandle

add your own processing code while tokenize input SQL query.

public ITokenHandle TokenHandle { set; }

Property Value

ITokenHandle

sourcetokenlist

Source token list of input sql text.

public virtual TSourceTokenList sourcetokenlist { get; set; }

Property Value

TSourceTokenList

sqlfilename

The full path of sql file to be parsed.

public virtual string sqlfilename { get; set; }

Property Value

string

sqlstatements

statements avaiable after parse sql query.

public virtual TStatementList sqlstatements { get; set; }

Property Value

TStatementList

sqltext

sql query to be parsed. This value will be ignored if sqlfilename is specified.

public virtual string sqltext { get; set; }

Property Value

string

Methods

checkSyntax()

check syntax of input sql, it works the same as parse().

public virtual int checkSyntax()

Returns

int

a value 0 indicates no syntax error found, otherwise using ErrorCount and Errormessage to get detailed error information.

getrawsqlstatements()

Separate SQL statements of a script into sqlstatements without parsing each statement, no parse tree is generated.

public int getrawsqlstatements()

Returns

int

0 if get SQL statements successfully

parse()

Parse input SQL script.

a value 0 indicates no syntax error found, otherwise using ErrorCount and Errormessage to get detailed error information.

public virtual int parse()

Returns

int

parseConstant(string)

Turn a constant in string into an instace of TConstant

public TConstant parseConstant(string newConstant)

Parameters

newConstant string

string representation of constant literal

Returns

TConstant

an instance of TConstant

parseExpression(string)

Turn an expression in string into an instace of TExpression

public virtual TExpression parseExpression(string expr)

Parameters

expr string

expression in string

Returns

TExpression

expression in TExpression

parseExpression(EDbVendor, string)

public static TExpression parseExpression(EDbVendor dbVendor, string expr)

Parameters

dbVendor EDbVendor
expr string

Returns

TExpression

parseFunctionCall(string)

Turn a function in string into an instace of TFunctionCall

public virtual TFunctionCall parseFunctionCall(string newFunction)

Parameters

newFunction string

function in string

Returns

TFunctionCall

function in TFunctionCall

parseFunctionCall(EDbVendor, string)

public static TFunctionCall parseFunctionCall(EDbVendor dbVendor, string newFunction)

Parameters

dbVendor EDbVendor
newFunction string

Returns

TFunctionCall

parseObjectName(string)

Turn a objectname in string into an instace of TObjectName

public virtual TObjectName parseObjectName(string newObjectName)

Parameters

newObjectName string

Objectname in string

Returns

TObjectName

objectname represented by TObjectName

parseObjectName(EDbVendor, string)

public static TObjectName parseObjectName(EDbVendor dbVendor, string newObjectName)

Parameters

dbVendor EDbVendor
newObjectName string

Returns

TObjectName

parseSubquery(string)

public virtual TSelectSqlStatement parseSubquery(string subquery)

Parameters

subquery string

Returns

TSelectSqlStatement

parseSubquery(EDbVendor, string)

public static TSelectSqlStatement parseSubquery(EDbVendor dbVendor, string subquery)

Parameters

dbVendor EDbVendor
subquery string

Returns

TSelectSqlStatement

tokenizeSqltext()

converting input sql text into a sequence of tokens: sourcetokenlist.

public void tokenizeSqltext()