Page 1 of 1

Prepared SQL

Posted: Thu 12 Jan 2012 10:32
by MamProblem
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 :)

Posted: Thu 12 Jan 2012 12:29
by AlexP
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.

Posted: Thu 12 Jan 2012 13:25
by MamProblem
Actually I thought that it would get 'pure' SQL, but that should be OK for me.


Thanx a lot !

:)

Posted: Thu 12 Jan 2012 14:31
by AlexP
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;