Page 1 of 1

Bag or not with Params TZquery

Posted: 05.06.2015, 08:33
by tvvwild
I'm use only 1 TZquery component for many queries. Database PostgreSql
like

Code: Select all

select * from public.p_test1(:id, :b, :c)
:id is integer param
:b, :c other params no matter what types

then other query

Code: Select all

select * from public.p_test2(:id, :d, :e)
:id is integer[] param (array)
:d, :e other params no matter what types

and on TZquery.Open have error Could not convert variant of type (String) into type (Boolean)

all code

Code: Select all

zqrQuery.Close;
zqrQuery.Sql.Text := 'select * from public.p_test1(:id, :b, :c)';
zqrQuery.ParamByName('id').Value := VarToInt(...);
...
zqrQuery.Open;
...
zqrQuery.Close;
zqrQuery.Sql.Text := 'select * from public.p_test2(:id, :d, :e)';
zqrQuery.ParamByName('id').Value := VarToIntArray(...);
...
zqrQuery.Open; // error;
function VarToInt():variant; return null or integer like 10
function VarToIntArray():variant; return null or string like '{10,20,30}'


quick analysis of errors found as for me wrong code

Code: Select all

procedure TZAbstractRODataset.UpdateSQLStrings(Sender: TObject);
var
  I: Integer;
  OldParams: TParams;
begin
  FieldDefs.Clear;
  if Active then
    Close
  else Statement := nil;

  OldParams := TParams.Create; //save old params
  OldParams.Assign(FParams);
  FParams.Clear;

  try
    for I := 0 to FSQL.ParamCount - 1 do
      FParams.CreateParam(ftUnknown, FSQL.ParamNames[I], ptUnknown); //add new params with datatype ftUnknown
    FParams.AssignValues(OldParams); // restore from old query param params with same name (datatype and value)
  finally
    OldParams.Free;
  end;
end;
fix in my program

Code: Select all

zqrQuery.Close;
zqrQuery.Params.Clear;
is not good, must remember