Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How long have the Lookup and Locate methods of TDataset been around?

Tags:

dataset

delphi

I'm working on modernizing and fixing bugs in the codebase of a Delphi 4-era program written by someone else. A lot of the code is kinda scary by modern standards, and I can't help but wonder if some of the things I'm seeing are there because the original author didn't know about certain standard library features, or if they weren't available.

One of the more obnoxious "patterns" I see all over the app looks like this:

table := TClientDataset.Create;
with table do
begin
  CloneCursor(dmDatabase.OriginalTable, false, true);
  filtered := true;
  active := true;
  first;
  while not EOF do
  begin
    if fieldByName('whatever').AsString = 'some criteria' then break;
    next;
  end;
  if EOF then exit;
  //do something based on the current row of the dataset
  table.free;
end;

Almost every one of these groups could be replaced by a one-line call to either Lookup or Locate on the original dataset, with no need for an intermediary CDS at all. That makes me wonder, were these methods available back in the D4 days? When were Lookup and Locate first added?

like image 822
Mason Wheeler Avatar asked Jul 27 '11 19:07

Mason Wheeler


2 Answers

Lookup and Locate were introduced in Delphi 2. It looks like the original author simply didn't take advantage of them.

like image 166
Bruce McGee Avatar answered Sep 28 '22 04:09

Bruce McGee


Seems the Original programmer wanted to make sure that the row pointer is not changed at all. Doing Locate (or Lookup) would change the row pointer, provoking all kinds of data events (Datasource.OnDataChange, Dataset.AfterScroll and so on).

Doing the search with TClientDataset.CloneCursor, none of the these events ger triggered on the dmDatabase.OriginalTable and there's no need to reload the data from database.

Seems to me that is the intention. TClientDataset was presented on D3. And cloned cursors are a kind of advanced feature - and need the dmDatabase.OriginalTable to be a CDS too.

like image 25
Fabricio Araujo Avatar answered Sep 28 '22 03:09

Fabricio Araujo