如何按指定顺序应用 Delphi TClientDataset的更新?

zu0ti5jz  于 2023-01-08  发布在  其他
关注(0)|答案(1)|浏览(158)

我需要按指定顺序应用更新(ClientDataSet.ApplyUpdates):- 删除为第一个-修改为第二个-插入为第三个
它们是按照完成的顺序应用的。

chy5wohz

chy5wohz1#

请参阅KTDataComponents以了解如何完成此操作
除此之外,我有时通过切换到提供程序端OnUpdateData事件处理程序中的特定索引来形成所需的更新顺序。为了不破坏增量处理,必须使用几个技巧,但这是相当可行的

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;

相关问题