Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How long does a TDataset bookmark remain valid?

I have code like below in a project I'm working.

procedure TForm.EditBtnClick(Sender:TObject);
begin
  // Mark is form variable. It's private
  Mark = cdsMain.GetBookmark;
  // blabalbal
  .
  .   
  .
end;

procedure TForm.OkBtnClick(Sender:TObject);
var  
  mistakes: Integer;
begin
  //Validation stuff and transaction control
  //removed to not clutter the code
  If cdsMain.ChangeCount <> 0 then 
    mistakes := cdsMain.AppyUpdates(-1); 
  cdsMain.Refresh;
  try
    cdsMain.GotoBookmark(Mark);
    // Yes, I know I would have to call FreeBookmark
    // but I'm just reproducing 
  except
    cdsMain.First;
  end;
end;

Personally, I do not use bookmarks much — except to reposition a dataset where I only moved the cursor position (to create a listing, fill a string list, etc). If I Refresh, update (especially when a filter can make the record invisible), refetch (Close/Open) or any operation that modifies the data in the dataset, I don't use bookmarks. I prefer to Locate on the primary key (using a TClientDataset, of course) or requery modifying the parameters.

Until when is a bookmark valid? Until a Refresh? Until a Close/Open is done to refetch data? Where does the safe zone end?

Consider in the answer I'm using TClientDataset with a TSQLQuery (DbExpress).

like image 307
Fabricio Araujo Avatar asked May 11 '09 19:05

Fabricio Araujo


3 Answers

Like both c0rwin and skamradt already mention: the bookmark behaviour depends on the TDataSet descendant you use.

In general, bookmarks become invalid during:

  1. close/open
  2. refresh (on datasets that support it)
  3. data changes (sometimes only deletions)

I know 1. and 2. can invalidate your bookmarks in TClientDataSets. I am almost sure that for TClientDataSets it does not matter which underlying provider is used (TSQLQuery, TIBQuery, etc).

The only way to make sure what works and what not is testing it. Which means you are totally right in not using them: bookmarks have an intrinsic chance of being unreliable.

To be on the safe side, always call BookmarkValid before going to a bookmark.

like image 87
Jeroen Wiert Pluimers Avatar answered Nov 10 '22 13:11

Jeroen Wiert Pluimers


TDataSet implements virtual bookmark methods. While these methods ensure that any dataset object derived from TDataSet returns a value if a bookmark method is called, the return values are merely defaults that do not keep track of the current location. Descendants of TDataSet, such as TBDEDataSet, reimplement the bookmark methods to return meaningful values as described in the following list:

  • BookmarkValid, for determining if a specified bookmark is in use.
  • CompareBookmarks, to test two bookmarks to see if they are the same.
  • GetBookmark, to allocate a bookmark for your current position in the dataset.
  • GotoBookmark, to return to a bookmark previously created by GetBookmark
  • FreeBookmark, to free a bookmark previously allocated by GetBookmark.

Get it from here

like image 26
Artem Barger Avatar answered Nov 10 '22 13:11

Artem Barger


Personally I rarely ever use bookmarks. I instead use the id of the record I am viewing and perform a locate on it once the refresh is complete. If I need to iterate over all of the records in the set, I do that using a clone of the tClientDataset (which gets its own cursor).

It is my understanding is that the implementation of the bookmark is up to the vendor of the tDataset descendant and can vary between implementations. In my very simple dataset (tBinData), I implemented bookmarks as the physical record number so it would persist between refreshes as long as the record was not deleted. I can not speak this true for all implementations.

like image 28
skamradt Avatar answered Nov 10 '22 13:11

skamradt