Recent

Author Topic: TBufDataset.First issue.  (Read 1878 times)

TRon

  • Hero Member
  • *****
  • Posts: 2515

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: TBufDataset.First issue.
« Reply #16 on: January 25, 2023, 11:04:36 am »
Result
Quote
RecCount=0
RecCount=1
BOF=FALSE - EOF=FALSE
recNo=1 - s=ABCDEFG
BOF=TRUE - EOF=FALSE
recNo=1 - s=ABCDEFG
BOF=FALSE - EOF=TRUE
recNo=1 - s=ABCDEFG
BOF=TRUE - EOF=FALSE
recNo=1 - s=ABCDEFG
Still doesn't explain the BOF/EOF confusion
What confusion?
They are correct in this example. (or am I missing something?)

BOF and EOF are not set until they are actually encountered.
With a POST they are not set. Only with Last, First, Next and Prior (and only in the direction you where going).
For a RecordCount = 1 i would expect BOF and EOF to be True.
Or at least EOF to be True from the get-go, since OP used Append
« Last Edit: January 25, 2023, 11:06:16 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: TBufDataset.First issue.
« Reply #17 on: January 25, 2023, 11:11:07 am »
For a RecordCount = 1 i would expect BOF and EOF to be True.
No. BOF and EOF are only set on movement of the cursor.
Quote
1. The cursor is on the last record, and the TDataset.Next method is called.
https://www.freepascal.org/docs-html/fcl/db/tdataset.eof.html

So, although you are on the last record after Append with one record, Next isn't called, so EOF is false.

Although the documentation page of BOF isn't that clear, the same as for EOF goes for BOF.

Only exception is when there is an empty dataset. Then both BOF and EOF are true.

Also note:
Quote
In all other cases, EOF is False. Note: when the cursor is on the last-but-one record, and Next is called (moving the cursor to the last record), EOF will not yet be True.
So, EOF will only trigger if you are trying to go past the last record. Not when you reach that last record.
(That's why you do a while EOF loop with Next on the end, otherwise this would go wrong)
« Last Edit: January 25, 2023, 11:13:44 am by rvk »

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: TBufDataset.First issue.
« Reply #18 on: January 25, 2023, 11:15:42 am »
Ok, I came in from another end: https://www.freepascal.org/docs-html/fcl/db/tfielddef.fieldno.html
I agree that it is "poorly documented". I just know this because I once was involved in a bug fix related to ZEOS components which changed their RecNo to begin at 0, and this caused some trouble (https://sourceforge.net/p/zeoslib/tickets/539/).
« Last Edit: January 25, 2023, 11:17:28 am by wp »

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: TBufDataset.First issue.
« Reply #19 on: January 25, 2023, 11:32:31 am »
For a RecordCount = 1 i would expect BOF and EOF to be True.
No. BOF and EOF are only set on movement of the cursor.
Quote
1. The cursor is on the last record, and the TDataset.Next method is called.
https://www.freepascal.org/docs-html/fcl/db/tdataset.eof.html

So, although you are on the last record after Append with one record, Next isn't called, so EOF is false.

Although the documentation page of BOF isn't that clear, the same as for EOF goes for BOF.

Only exception is when there is an empty dataset. Then both BOF and EOF are true.

Also note:
Quote
In all other cases, EOF is False. Note: when the cursor is on the last-but-one record, and Next is called (moving the cursor to the last record), EOF will not yet be True.
So, EOF will only trigger if you are trying to go past the last record. Not when you reach that last record.
(That's why you do a while EOF loop with Next on the end, otherwise this would go wrong)

Ah... OK.
I've just read something on Microsofts website, and it is consistent
BOF/EOF will only be set to True if you try to MOVE BEYOND the First/Last record
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

TRon

  • Hero Member
  • *****
  • Posts: 2515
Re: TBufDataset.First issue.
« Reply #20 on: January 25, 2023, 11:53:54 am »
BOF/EOF will only be set to True if you try to MOVE BEYOND the First/Last record
Ah, yes !!. That was the conclusion from the conversation I remembered as well. Thx for the refresh guys/galls !

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: TBufDataset.First issue.
« Reply #21 on: January 25, 2023, 02:08:55 pm »
An old example from 2016 written by me and already on this forum:
Code: Pascal  [Select][+][-]
  1. {$apptype console}{$mode objfpc}
  2. uses
  3.   db,BufDataset;
  4. var
  5.   BufDb:TBufDataset;
  6. begin
  7.  BufDb:=TBufDataset.Create(nil);
  8.  try
  9.    BufDb.FieldDefs.Add('NAME',ftString,20);
  10.    BufDb.FieldDefs.Add('NUM',ftinteger);
  11.    BufDb.FieldDefs.Add('NUM2',ftInteger);
  12.    BufDb.CreateDataSet;
  13.    BufDb.Open;
  14.    BufDb.Append;
  15.    BufDb.FieldByName('NAME').Value:='Free';
  16.    BufDb.Post;
  17.    BufDb.Append;
  18.    BufDb.FieldByName('NAME').Value:='Pascal';
  19.    BufDb.Post;
  20.    BufDb.SaveToFile('BufDb.txt');
  21.  finally
  22.    BufDb.Close;
  23.    BufDb.Free;
  24.  end;
  25.  // now, open..
  26.  BufDb := TBufDataset.Create(nil);
  27.  try
  28.    BufDb.LoadFromFile('BufDb.txt');
  29.    BufDb.Open;
  30.    BufDb.First;
  31.    Writeln(BufDb.FieldByName('NAME').Value);
  32.  finally
  33.    BufDb.Close;
  34.    BufDb.Free;
  35.  end;
  36.  
  37. end.

It seems to me that just the call to open is the culprit.

https://forum.lazarus.freepascal.org/index.php/topic,33508.msg217317.html#msg217317


« Last Edit: January 25, 2023, 02:15:02 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: TBufDataset.First issue.
« Reply #22 on: January 25, 2023, 02:30:46 pm »
An old example from 2016 written by me and already on this forum:
Code: Pascal  [Select][+][-]
  1. {$apptype console}{$mode objfpc}
  2. uses
  3.   db,BufDataset;
  4. var
  5.   BufDb:TBufDataset;
  6. begin
  7.  BufDb:=TBufDataset.Create(nil);
  8.  try
  9.    BufDb.FieldDefs.Add('NAME',ftString,20);
  10.    BufDb.FieldDefs.Add('NUM',ftinteger);
  11.    BufDb.FieldDefs.Add('NUM2',ftInteger);
  12.    BufDb.CreateDataSet;
  13.    BufDb.Open;
  14.    BufDb.Append;
  15.    BufDb.FieldByName('NAME').Value:='Free';
  16.    BufDb.Post;
  17.    BufDb.Append;
  18.    BufDb.FieldByName('NAME').Value:='Pascal';
  19.    BufDb.Post;
  20.    BufDb.SaveToFile('BufDb.txt');
  21.  finally
  22.    BufDb.Close;
  23.    BufDb.Free;
  24.  end;
  25.  // now, open..
  26.  BufDb := TBufDataset.Create(nil);
  27.  try
  28.    BufDb.LoadFromFile('BufDb.txt');
  29.    BufDb.Open;
  30.    BufDb.First;
  31.    Writeln(BufDb.FieldByName('NAME').Value);
  32.  finally
  33.    BufDb.Close;
  34.    BufDb.Free;
  35.  end;
  36.  
  37. end.

It seems to me that just the call to open is the culprit.

https://forum.lazarus.freepascal.org/index.php/topic,33508.msg217317.html#msg217317
No, it was the FieldNo in the (overloaded) Add-Function. OP (and myself) missed that FieldNo is 1-based. OP used for that sample FieldNo=0
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: TBufDataset.First issue.
« Reply #23 on: January 25, 2023, 08:00:35 pm »
I wouldn't call it no!, because if you want to read the first entry, first will do nicely. Hence that old example.
It is cleaner and much easier than the bull dung above.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

egsuh

  • Hero Member
  • *****
  • Posts: 1292
Re: TBufDataset.First issue.
« Reply #24 on: January 26, 2023, 03:09:37 am »
In Zvony’s example, calling RecNo might have moved cursor beyond last record.

 

TinyPortal © 2005-2018