Hi,
I have to parse SQL statements (MySql or PostgreSql) for params:
Input: "select * from PERSON where NAME = :PNAME and CITY = :PCITY"
Output: "select * from PERSON where NAME = ? and CITY = ?" and array ("PNAME", "PCITY")
I found the code in TCRCommand.ParseSQL for this. But how to use it without any connection? Can I just create a TCRCommand object and call ParseSQL?
Or what is the correct way to use this code?
Christian
Parse SQL with UniDAC
Re: Parse SQL with UniDAC
To solve the issue, you can try to use next code:
Code: Select all
procedure TMyForm.ParseSQL(const SQL: string; out SQLText: string; out ParamsText: string);
var
Params: TDAParams;
i: integer;
UniSQL: TUniSQL;
begin
SQLText := '';
ParamsText := '';
UniSQL := TUniSQL.Create(nil);
try
Params := TDAParams.Create(UniSQL);
try
SQLText := TDBAccessUtils.ParseSQL(UniSQL, SQL, Params);
for i := 0 to Params.Count - 1 do
if i = 0 then
ParamsText := AnsiQuotedStr(Params[i].Name, '"')
else
ParamsText := ParamsText + ', ' + AnsiQuotedStr(Params[i].Name, '"');
finally
Params.Free;
end;
finally
UniSQL.Free;
end;
end;