Vlada
Vlada

Reputation: 667

How to apply updates of Delphi TClientDataset in specified order?

I need apply updates (ClientDataSet.ApplyUpdates) to be applied in specified order: - Delete as first - Modify as second - Insert as third

They are applied in order in which they were done.

Upvotes: 0

Views: 3355

Answers (1)

Vladimir Ulchenko
Vladimir Ulchenko

Reputation: 463

see KTDataComponents to get the idea how this can be accomplished

besides that I sometimes form required update order by switching to specific index in OnUpdateData event handler on provider side. there are couple of tricks that have to be utilized in order not to break delta handling but it is quite doable

procedure TLogResolver.LogUpdateRecord(Tree: TUpdateTree);
var
  I: Integer;
  CurVal: Variant;
  fld: TField;
  SaveRecNo: Integer;
  OldIndexName: WideString;
  OldIndexFieldNames: WideString;
begin
  if(TClientDataSet(Tree.Delta).IndexName<>'')or(TClientDataSet(Tree.Delta).IndexFieldNames<>'') then begin
    OldIndexName:=TClientDataSet(Tree.Delta).IndexName;
    OldIndexFieldNames:=TClientDataSet(Tree.Delta).IndexFieldNames;
    SaveRecNo:=Tree.Delta.RecNo;
    Tree.Delta.Edit;                       // vavan
    for I := 0 to Tree.Delta.FieldCount - 1 do begin
      fld:=Tree.Delta.Fields[I];
      { Blobs, Bytes and VarBytes are not included in result packet }
      if {(fld.IsBlob) or} (fld.DataType in [ftBytes, ftVarBytes]) then // vavan allowed blobs
        continue;
      CurVal := fld.NewValue;
      if not VarIsClear(CurVal) then begin
        fld.Value:=CurVal;  { TODO -ovavan -cSIC! : edit delta }
      end;
    end;
    Tree.Delta.Post; // vavan
    TClientDataSet(Tree.Delta).IndexName:=''; { TODO -ovavan -cSIC! : reset delta index to get correct original recno in order not to break reconcilation mechanism }
  end;
  try
    Tree.ErrorDS.IndexFieldNames:='ERROR_RECORDNO';
//    Tree.InitErrorPacket(nil, rrApply);
    TreeInitErrorPacket(Tree,nil, rrApply);
    for I := 0 to TVPacketDataSet(Tree.Delta).NewValueFields.Count - 1 do
    begin
      fld:=TVPacketDataSet(Tree.Delta).NewValueFields[i];
      { Blobs, Bytes and VarBytes are not included in result packet }
      if {(Tree.Delta.Fields[I].IsBlob) or} // vavan allowed blobs
         (fld.DataType in [ftBytes, ftVarBytes]) then
        continue;
      CurVal := fld.NewValue;
      if not VarIsClear(CurVal) then begin { TODO 2 -ovavan -ccheck! : get rid of this check since we only process modified fields? }
        Tree.ErrorDS.Fields[6+fld.Index].Value := CurVal; { TODO -ovavan -ccheck : ErrorDS fieldd structure is identical to delta but with extra 6 leading fields }
      end;
    end;
    Tree.ErrorDS.Post;
  finally
    if(OldIndexName<>'')then begin
      TClientDataSet(Tree.Delta).IndexName:=OldIndexName;
      Tree.Delta.RecNo:=SaveRecNo;
    end else
    if(OldIndexFieldNames<>'')then begin
      TClientDataSet(Tree.Delta).IndexFieldNames:=OldIndexFieldNames;
      Tree.Delta.RecNo:=SaveRecNo;
    end;
  end;
end;

Upvotes: 0

Related Questions