views:

435

answers:

3

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).

A: 

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

Artem Barger
Thank you. I will rephrase the question, so.
Fabricio Araujo
Unfortunately I don't remember answer to this question, since I left Delphi programing a long ago. My intuition says me, it should be valid until DataSet is open, but I could think of implementation where it could be useful even after open/closing it. I also tend to agree with answer bellow, since it definitely could be dependent on vendor.
Artem Barger
+3  A: 

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.

skamradt
+2  A: 

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.

Jeroen Pluimers
After some experimentations, even BookmarkValid proves itself unreliable. It happens when you have a filter activated on the dataset - it returns true even if the record does not match the filter condition. The end result is the throwing of a exception.
Fabricio Araujo