Parse SQL with UniDAC

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
chkaufmann
Posts: 78
Joined: Sat 01 Jul 2006 11:42

Parse SQL with UniDAC

Post by chkaufmann » Thu 05 Sep 2019 21:03

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

ViktorV
Devart Team
Posts: 2876
Joined: Wed 30 Jul 2014 07:16

Re: Parse SQL with UniDAC

Post by ViktorV » Fri 27 Dec 2019 11:23

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;

Post Reply