Prepared SQL

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
MamProblem
Posts: 13
Joined: Mon 09 May 2011 11:11

Prepared SQL

Post by MamProblem » Thu 12 Jan 2012 10:32

Hi!

I use RadStudio XE C++, UniDAC 4.1.4, PostgreSQL/SQLite/MySQL.

Is there any possibility to get complete sql using params?

Code: Select all

SELECT * FROM t1 WHERE p1 = :param1 AND p2 = :param2;
Query->ParamByName("param1")->AsString = "test1";
Query->ParamByName("param2")->AsInteger = 1024;

I need to get complete SQL (for logging and debugging):

Code: Select all

SELECT * FROM t1 WHERE p1 = 'test1' AND p2 = 1024;
Thanks in advance :)

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Post by AlexP » Thu 12 Jan 2012 12:29

Hello,

For debugging, you can set the Debug property to True. The window with the SQL text and the used parameters list with their values will appear before your SQL execution. For logging, you can use TUniSQLMonitor, and in the onSQL event save the Text parameter of the method, in which SQL and its parameters are.

MamProblem
Posts: 13
Joined: Mon 09 May 2011 11:11

Post by MamProblem » Thu 12 Jan 2012 13:25

Actually I thought that it would get 'pure' SQL, but that should be OK for me.


Thanx a lot !

:)

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Post by AlexP » Thu 12 Jan 2012 14:31

Hello,

To retrieve "clean" SQL, you should handle parameters in the cycle yourself and change their names in the SQL with values, for example:

Code: Select all

function GetFinalSQL(DataSet: TCustomDADataSet): string;
var
  i: integer;
begin
  result := '';
  if not Assigned(DataSet) then exit;
  Result:= DataSet.FinalSQL;
  for i := 0 to DataSet.ParamCount -1 do
    if DataSet.Params.IsNull then
      Result:= StringReplace(Result,':'+DataSet.Params.Name, 'Null', [rfReplaceAll])
    else
      Result:= StringReplace(Result,':'+DataSet.Params.Name, DataSet.Params.Value, [rfReplaceAll])
end;

Post Reply