Search code examples
delphidbexpresstclientdataset

How can I detect if ApplyUpdates will Insert or Update data?


In the AfterPost event handler for a ClientDataSet, I need the information if the ApplyUpdates function for the current record will do an update or an insert.

The AfterPost event will be executed for new and updated records, and I do not want to declare a new Flag variable to indicate if a 'update' or ' insert' operation is in progress.

Example code:

procedure TdmMain.QryTestAfterPost(DataSet: TDataSet);
begin
  if IsInserting(QryTest) then
     // ShowMessage('Inserting')...
  else
     // ShowMessage('Updating');

  QryTest.ApplyUpdates(-1); 
end;

The application will write a log in the AfterPost method, after ApplyUpdate has completed. So this method is the place which is closest to the action, I would prefer a solution which completely can be inserted in this event handler.

How could I implement the IsInserting function, using information in the ClientDataSet instance QryTest?

Edit: I will try ClientDataSet.UpdateStatus which is explained here.


Solution

  • ApplyUpdates doesn't give you that information - since it can be Inserting, updating and deleting.

    ApplyUpdates apply the change information stored on Delta array. That change information can, for example, contain any number of changes of different types (insertions, deletions and updatings) and all these will be applied on the same call.

    On TDatasetProvider you have the BeforeUpdateRecord event (or something like that, sleep does funny things on memory :-) ). That event is called before each record of Delta is applied to the underlying database/dataset and therefore the place to get such information... But Showmessage will stop the apply process.

    EDIT: Now I remembered there's another option: you can assign Delta to another clientdataset Data property and read the dataset UpdateStatus for that record. Of course, you need to do this before doing applyupdates...

    var
      cdsAux: TClientDataset;
    begin
      .
      . 
      <creation of cdsAux>
      cdsAUx.Data := cdsUpdated.Delta;
      cdsAux.First;
      case cdsAux.UpdateStatus of
        usModified:
          ShowMessage('Modified');
        usInserted:
          ShowMessage('Inserted');
        usDeleted:
          ShowMessage('Deleted'); // For this to work you have to modify  
                                  // TClientDataset.StatusFilter  
      end;
      <cleanup code>
    end;