I am looking for a good SQL parser in Delphi (2010) for SQL Server. I need such thing to parse an query and extract: select list, where and order by clauses. It does not matter if it is a commercial or an open-source solution. It can also be a DLL (written in any language, of course) but I prefer a VCL component over a DLL.
Rafael Delphi comes with a sql parser located in the DBCommon
unit
Check these functions
function NextSQLToken(var p: PAnsiChar; out Token: AnsiString; CurSection: TSQLToken): TSQLToken; overload;
function NextSQLToken(var p: PWideChar; out Token: WideString; CurSection: TSQLToken): TSQLToken; overload;
function NextSQLToken(var p: PChar; out Token: String; CurSection: TSQLToken): TSQLToken; overload;
function GetIndexForOrderBy(const SQL: WideString; DataSet: TDataSet): TIndexDef;
function GetTableNameFromSQL(const SQL: WideString): WideString;
function GetTableNameFromQuery(const SQL: Widestring): Widestring;
function AddParamSQLForDetail(Params: TParams; SQL: WideString; Native: Boolean; QuoteChar: WideString = ''): WideString;
function IsMultiTableQuery(const SQL: WideString): Boolean;
function SQLRequiresParams(const SQL: WideString): Boolean;
function NextSQLTokenEx(var p: PWideChar; out Token: UnicodeString; CurSection: TSQLToken; IdOption: IDENTIFIEROption): TSQLToken; overload;
function NextSQLTokenEx(var p: PWideChar; out Token: WideString; CurSection: TSQLToken; IdOption: IDENTIFIEROption): TSQLToken; overload;
function NextSQLTokenEx(var p: PAnsiChar; out Token: AnsiString; CurSection: TSQLToken; IdOption: IDENTIFIEROption): TSQLToken; overload;
function GetTableNameFromSQLEx(const SQL: WideString; IdOption: IDENTIFIEROption): WideString;
This is a very simple sample to show how parse a sql sentence and get all the elements.
uses
TypInfo,
DbCommon,
SysUtils;
const
StrSql ='Select Field1, Field2, 54 field3, Field4 from Mytable1 Order by Field1,Field5';
procedure ParseSql(Const Sql : string);
var
SQLToken : TSQLToken;
CurSection : TSQLToken;
Start : PWideChar;
Token : WideString;
IdOption : IDENTIFIEROption;
begin
IdOption :=idMixCase;
Start :=PWideChar(StrSql);
CurSection := stUnknown;
repeat
SQLToken := NextSQLTokenEx(Start, Token, CurSection, IdOption);
if SQLToken<>stEnd then
Writeln(Format('Type %s Token %s', [GetEnumName(TypeInfo(TSQLToken), integer(SQLToken)),Token]));
CurSection := SQLToken;
until SQLToken in [stEnd];
end;
begin
try
ParseSql(StrSql);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
this will return
Type stSelect Token Select
Type stFieldName Token Field1
Type stFieldName Token Field2
Type stNumber Token 54
Type stFieldName Token field3
Type stFieldName Token Field4
Type stFrom Token from
Type stTableName Token Mytable1
Type stOrderBy Token Order by
Type stFieldName Token Field1
Type stFieldName Token Field5
check out: http://www.sqlparser.com/sql-parser-vcl.php
This is a very powerful sql parser and all in VCL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With