Lazarus

Programming => Databases => Topic started by: egsuh on April 01, 2017, 06:11:45 pm

Title: TBufDataSet.Clear
Post by: egsuh on April 01, 2017, 06:11:45 pm
The TBufDataSet.Clear seems the equivalent of EmptyDataSet or EmptyTable of Delphi, i.e. remove all records leaving the dataset structure. But this seems to remove field definitions as well. Or, it does nothing.

When I tested,

   BufDataSet1.Open;
   BufDataSet1.Clear;
   BufDataSet1.Open;

There are no change in data. The DBGrid is the same.

But if I do:

   BufDataSet1.Close;
   BufDataSet1.Clear;
   BufDataSet1.Open;

Then the last command raise an error. No dataset exist or something.

Can anyone explain why to me?
Title: Re: TBufDataSet.Clear
Post by: HatForCat on April 01, 2017, 06:35:44 pm
Can't help with the explanation but I have written a generic procedure that I use to Empty a table.

I too come from Win/Delphi!! :)

Code: Pascal  [Select][+][-]
  1. procedure TableEmpty(const aQry : TSQLQuery; const aTrans : TSQLTransaction);
  2. begin
  3.   if aQry.RecordCount > 0 then
  4.   begin
  5.     aQry.SQL.Clear;
  6.     aQry.SQL.Text:='DELETE FROM '+aQry.Name;
  7.     aQry.ExecSQL;
  8.   end;
  9.   aQry.ApplyUpdates;
  10.   aTrans.Commit;
  11. end;
  12.  
Title: Re: TBufDataSet.Clear
Post by: wp on April 01, 2017, 06:41:29 pm
This is what BufDataset.Clear does:
Code: Pascal  [Select][+][-]
  1. procedure TCustomBufDataset.Clear;
  2. begin
  3.   Close;
  4.   FieldDefs.Clear;
  5.   Fields.Clear;
  6. end;

I don't think that HatForCat's code works for TBufDataset because it does not understand SQL. Instead iterate through all records and delete them (untested...):
Code: Pascal  [Select][+][-]
  1. procedure ClearAllRecords(ADataset: TDataset);
  2. begin
  3.   ADataset.DisableControls;
  4.   try
  5.     ADataset.First;
  6.     while not ADataset.EoF do
  7.       ADataset.Delete;
  8.   finally
  9.     ADataset.EnableControls;
  10.   end;
  11. end;
Title: Re: TBufDataSet.Clear
Post by: Thaddy on April 01, 2017, 06:49:23 pm
Calling clear on a TBufDataset deletes its content from memory and resets *everything*.
You probably forgot to save to file or save (commit) to an underlying dataset.
The reason is that the Field Defs are cleared too.
The fastest way  I know in your case is to copy the fielddefs to a temp TFielddefs, call clear and re-assign the fielddefs.
Something like:
Code: Pascal  [Select][+][-]
  1.   // your code first
  2.    OldFieldDefs := TFielddefs.Create(nil);
  3.    OldFieldDefs.Assign(BufDb.FieldDefs);
  4.    BufDb.Clear;
  5.    BufDb.Close;
  6.    BufDb.FieldDefs.Assign(OldFieldDefs);
  7.    BufDb.CreateDataset;
  8.    BufDb.Open;
  9.    OldFieldDefs.Free;
Title: Re: TBufDataSet.Clear
Post by: Thaddy on April 01, 2017, 10:18:59 pm
I did some tests. wp's code is faster than my code, so use that.
Title: Re: TBufDataSet.Clear
Post by: egsuh on April 04, 2017, 11:23:08 pm
Thank all of you for your comments. I may use wp's method. Just interested as  TBufDataSet provides ClearFields method as well.
Title: Re: TBufDataSet.Clear
Post by: Anspach on July 17, 2017, 08:05:01 am
Thank all of you for your comments. I may use wp's method. Just interested as  TBufDataSet provides ClearFields method as well.

Thanks for the code wp, worked great!
TinyPortal © 2005-2018