TFDMemTable Questions

From RAD Studio
Jump to: navigation, search

Go Up to FAQ (FireDAC)

This topic contains a list of questions and answers related to TFDMemTable.

Q: With TClientDataSet you can add, delete, change records and then process TClientDataSet.Delta. Is that possible with TFDMemTable?

A: TFDMemTable has similar functionality, but it follows the Cached Updates model. When CachedUpdates property value is True, MemTable will record changes in the updates log.

To post updates to a database, you have to use ApplyUpdates. To merge the change log, you have to call CommitUpdates.

You can find the description of these and other methods in the FireDAC help files and examples in the FireDAC\Samples\Comp Layer\TFDMemTable folder.

Q: I use CloneCursor a lot with client datasets. How can I do that with TFDQuery?

A: Use the following code:

FDMemTable1.CloneCursor(FDQuery1, False, False);

Q: How can I create and populate an FDMemTable?

A: 1) Create the in-memory dataset.

with FDMemTable1.FieldDefs do begin
  with AddFieldDef do begin
    Name := 'f1';
    DataType := ftInteger;
  end;
  with AddFieldDef do begin
    Name := 'f2';
    DataType := ftString;
    Size := 50;
  end;
end;

with FDMemTable1 do begin
  Open;
  Append;
  Fields[0].AsInteger := ...;
  Fields[1].AsString := ...;
  Post;
end;

2) Append data to it, browse/edit it.

Q: What is the fastest way to load a TFDMemTable from another dataset (including structure)?

A: The most simple way to copy the structure and data from a TDataSet to a TFDMemTable is to use the CopyDataSet method:

FDMemTable1.CopyDataSet(DataSet1, [coStructure, coRestart, coAppend]);

Q: How can I copy all records from FDQuery into FDMemTable and edit them in FDMemTable?

A: You can do something like that:

// UniDirectiona must be false if no
FDQuery1.FetchOptions.Undirectional := False;

FDQuery1.Open;
FDQuery1.FetchAll;
FDMemTable1.Data := FDQuery1.Data;
FDMemTable1.First;

while not FDMemTable1.Eof do begin
  FDMemTable1.Edit;
  .......
  FDMemTable1.Post;
  FDMemTable1.Next;
end;

Q: I cannot add a persistent field of type ftGUID. I am getting an error about field size, but the field size control is disabled. What is wrong?

A: This is a known issue in DB.pas unit: http://qc.embarcadero.com/wc/qcmain.aspx?d=8008. The workaround is to create a GUID field at run time.

Q: Is there a general rule as to when it is advantageous to use a memory table?

A: If you are only using FireDAC, then FDMemTable should be used only when you have memory-only data. And also in some special cases; for example, you can load CSV file into FDMemTable, then use LocalSQL to query this CSV data.

For other tasks, you can just use TFDQuery / TFDStoredProc, because they have almost the same client-side capabilities as TFDMemTable.

Q: How can I optimize the performance of TFDMemTable on massive editions (insert, edit, delete)?

A: Set the following properties LogChanges, FetchOptions, ResourceOptions, and UpdateOptions.

  FDMemTable1.LogChanges := False;
  FDMemTable1.FetchOptions.RecsMax := 300000;  //Sample value
  FDMemTable1.ResourceOptions.SilentMode := True;
  FDMemTable1.UpdateOptions.LockMode := lmNone;
  FDMemTable1.UpdateOptions.LockPoint := lpDeferred;
  FDMemTable1.UpdateOptions.FetchGeneratorsPoint := gpImmediate;

Then surround massive edition operations into BeginBatch / EndBatch method calls:

  FDMemTable1.BeginBatch;
  try
    for i := 1 to 1000 do begin
      FDMemTable1.Append;
      // ...
      FDMemTable1.Post;
    end;
  finally
    FDMemTable1.EndBatch;
  end;