Search code examples
delphicursorclonefiredac

FireDac Memtable post to Database


I am new to FireDac and trying to get my head around the components. I have used ClientDataSets before so, i guess i am expecting similar funcitonality. I have a FDQuery in a DataModule and would like to create views of this data at run time. The way i would have done this previously is to use ClientDataSets and the CloneCursor function. This would mean that i can work on an 'independent' dataset but still save changes to the source.

However, with FireDac i cannot seem to do this. I tried to use CloneCursor on another FDQuery component but got a connection error (as it does not clone these properties). I partially got it working with a FDMemTable where i am able to clone and post changes to the dataset but in order to save to the DB i must call 'ApplyUpdates' on the source FDQuery.

Am i missing something which is stopping me saving the data from the FDMemTable straight to the DB much like a ClientDataSet. I have tried applyupdates on the FDMemTable but no joy.

Thank you for your time


Solution

  • I have been working on this issue, and I what I have found is that changes to an FDMemTable that is cloned from an FDQuery are only written back to the underlying database if 1) the FDQuery being cloned is in cached updates mode, and 2) you call ApplyUpdates on the original FDQuery. This is consistent with the comment that you posted to your own question.

    If the FDQuery is not in cached updates mode, and you call an FDMemTable's CloneCursor method, cloning the FDQuery, the in-memory data is synchronized between the FDQuery and the FDMemTable, but only the changes made by the original FDQuery are written to the underlying database. Edits performed by the FDMemTable are visible to the FDQuery, but the FDQuery will not write them to the underlying table.

    If you really want any data posted to be immediately written to the underlying database, you can add an AfterPost, AfterDelete, and AfterInsert event handler to each participating dataset, and call ApplyUpdates on the original FDQuery from those event handlers (of course, you could create just a single event handler and use it for each dataset's AfterPost, AfterDelete, and AfterInsert events).