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.
- SQL identifier: TObjectName
- SQL literal: TConstant
- SQL datatype: TTypeName
- SQL function: TFunctionCall
- SQL constraint: TConstraint
- SQL expression/condition: TExpression
- SQL select list item: TResultColumn
- More: gudusoft.gsqlparser.nodes
Some major SQL statements:
- Select: TSelectSqlStatement
- Delete: TDeleteSqlStatement
- Insert: TInsertSqlStatement
- Update: TUpdateSqlStatement
- Create table: TCreateTableSqlStatement
- More: gudusoft.gsqlparser.stmt
Stored procedure
- Create function: TDb2CreateFunction, TMssqlCreateProcedure, TMySQLCreateFunction, TPlsqlCreateFunction
- Create procedure: TDb2CreateProcedure, TMssqlCreateProcedure, TMySQLCreateProcedure, TPlsqlCreateProcedure
- Create trigger: TDb2CreateTrigger, TMssqlCreateTrigger, TMySQLCreateTrigger, TPlsqlCreateTrigger
- Create package: TPlsqlCreatePackage
[CLSCompliant(true)]
public class TGSqlParser
- Inheritance
-
TGSqlParser
- Inherited Members
Constructors
TGSqlParser(EDbVendor)
public TGSqlParser(EDbVendor pdbvendor)
Parameters
pdbvendorEDbVendorThe SQL dialect to be parsed
Properties
DbVendor
SQL dialect handled by this parser
public virtual EDbVendor DbVendor { get; }
Property Value
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
ErrorCount
Syntax errors of SQL script.
public virtual int ErrorCount { get; }
Property Value
Errormessage
Detailed syntax error messages.
public virtual string Errormessage { get; }
Property Value
Flexer
lexer used by SQL parser
public TCustomLexer Flexer { get; }
Property Value
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
SqlInputStream
used by SQL parser internally
public virtual Stream SqlInputStream { get; set; }
Property Value
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
sourcetokenlist
Source token list of input sql text.
public virtual TSourceTokenList sourcetokenlist { get; set; }
Property Value
sqlfilename
The full path of sql file to be parsed.
public virtual string sqlfilename { get; set; }
Property Value
sqlstatements
statements avaiable after parse sql query.
public virtual TStatementList sqlstatements { get; set; }
Property Value
sqltext
sql query to be parsed. This value will be ignored if sqlfilename is specified.
public virtual string sqltext { get; set; }
Property Value
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.
public virtual int parse()
Returns
parseConstant(string)
Turn a constant in string into an instace of TConstant
public TConstant parseConstant(string newConstant)
Parameters
newConstantstringstring representation of constant literal
Returns
parseExpression(string)
Turn an expression in string into an instace of TExpression
public virtual TExpression parseExpression(string expr)
Parameters
exprstringexpression in string
Returns
- TExpression
expression in TExpression
parseExpression(EDbVendor, string)
public static TExpression parseExpression(EDbVendor dbVendor, string expr)
Parameters
Returns
parseFunctionCall(string)
Turn a function in string into an instace of TFunctionCall
public virtual TFunctionCall parseFunctionCall(string newFunction)
Parameters
newFunctionstringfunction in string
Returns
- TFunctionCall
function in TFunctionCall
parseFunctionCall(EDbVendor, string)
public static TFunctionCall parseFunctionCall(EDbVendor dbVendor, string newFunction)
Parameters
Returns
parseObjectName(string)
Turn a objectname in string into an instace of TObjectName
public virtual TObjectName parseObjectName(string newObjectName)
Parameters
newObjectNamestringObjectname in string
Returns
- TObjectName
objectname represented by TObjectName
parseObjectName(EDbVendor, string)
public static TObjectName parseObjectName(EDbVendor dbVendor, string newObjectName)
Parameters
Returns
parseSubquery(string)
public virtual TSelectSqlStatement parseSubquery(string subquery)
Parameters
subquerystring
Returns
parseSubquery(EDbVendor, string)
public static TSelectSqlStatement parseSubquery(EDbVendor dbVendor, string subquery)
Parameters
Returns
tokenizeSqltext()
converting input sql text into a sequence of tokens: sourcetokenlist.
public void tokenizeSqltext()