Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is TParams.ParamRef used for?

Unit db.pas contains implementation of TParam class, which represents a parameter in database query.

While testing queries with lots of params I noticed that function TParam.ParamRef: TParam takes a lot of time, since it calls ParamByName which does an unindexed search of params.

The implementation is simple:

function TParam.ParamRef: TParam;
begin
  if not Assigned(FParamRef) then
    if Assigned(Collection) and (Name <> '') then
      FParamRef := TParams(Collection).ParamByName(Name) else
      FParamRef := Self;
  Result := FParamRef;
end;

It can return self or ParamRef, so the idea is to allow some sort of redirection. But it does a slow ParamByName, is called a lot and I don't understand the purpose. In fact, if I modify it to just returns self everything seems to work correctly.

The only use I see for it would be to have several params of same name all redirect to same instance. If that's the case, surely the performance penalty of ParamByName overweights the benefit of this feature.

ParamRef is undocumented and private so only relevant within the db.pas unit. Also, there is no significant discussion about it online.

Has anyone encountered the same problem ?

like image 662
Daniel Maurić Avatar asked Apr 09 '18 08:04

Daniel Maurić


1 Answers

The only use I see for it would be to have several params of same name all redirect to same instance.

Far as I know that is correct. In Delphi the same parameter can be used multiple times but the underlying database calls usually consider them separate parameters so you end up with multiple instances of the same parameter.

SELECT * FROM SomeTable WHERE FirstName = :NAME or LastName = :NAME

enter image description here

With both parameters redirected to the same instance modifying either sets the other as well.

like image 170
Brian Avatar answered Nov 02 '22 07:11

Brian