杂记
 


Datasnap主从表更新

作者:  2012-12-10 00:00:00     分类:Delphi     标签:

procedure TServerMethods1.dspMastersBeforeApplyUpdates(Sender: TObject;
  var OwnerData: OleVariant);
var
  SessionID, InsertID: Integer;
begin
  { Add a new element related to the current session. }
  SessionID:= TDSSessionManager.GetThreadSession.Id;
  InsertID:= 0; // Have no value yet.
  InsertIDs.Add(SessionID, InsertID);
end;
 
procedure TServerMethods1.dspMastersBeforeUpdateRecord(Sender: TObject;
  SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind;
  var Applied: Boolean);
var
  SessionID, InsertID: Integer;
begin
  { Set the MasterID with the saved InsertID before details update. }
  if (UpdateKind = ukInsert) and (SourceDS = sdsDetails)
      and (DeltaDS.FieldByName('MasterID').AsInteger < 0) then
  begin
    SessionID:= TDSSessionManager.GetThreadSession.Id;
    InsertID:= InsertIDs.Items[SessionID]; // Get the id from saved dictionary.
    DeltaDS.FieldByName('MasterID').NewValue:= InsertIDs.Items[SessionID];
  end;
end;
 
procedure TServerMethods1.dspMastersAfterUpdateRecord(Sender: TObject;
  SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind);
var
  SessionID, InsertID: Integer;
begin
  { Return the inserted identity to client. }
  if UpdateKind = ukInsert then
  begin
    SessionID:= TDSSessionManager.GetThreadSession.Id;
    InsertID:= GetIdentity; // Get the inserted identity value.
 
    if SourceDS = sdsMasters then
    begin
      { Save the inserted identity of masters for details. }
      if InsertIDs.Items[SessionID] = 0 then
        InsertIDs.Items[SessionID]:= InsertID;
 
      DeltaDS.FieldByName('ID').ReadOnly:= False;
      DeltaDS.FieldByName('ID').NewValue:= InsertID;
    end
    else if SourceDS = sdsDetails then
    begin
      DeltaDS.FieldByName('ID').ReadOnly:= False;
      DeltaDS.FieldByName('ID').NewValue:= InsertID;
    end;
  end;
end;
 
procedure TServerMethods1.dspMastersAfterApplyUpdates(Sender: TObject;
  var OwnerData: OleVariant);
var
  SessionID, InsertID: Integer;
begin
  { Remove the saved dictionary element after applyupdates. }
  SessionID:= TDSSessionManager.GetThreadSession.Id;
  InsertID:= InsertIDs.Items[SessionID];
  InsertIDs.Remove(TDSSessionManager.GetThreadSession.Id);
end;
 
procedure TServerMethods1.dspMastersUpdateError(Sender: TObject;
  DataSet: TCustomClientDataSet; E: EUpdateError; UpdateKind: TUpdateKind;
  var Response: TResolverResponse);
begin
  AddLog('Master-ERROR:' + E.Message);
end;
 
procedure TServerMethods1.DSServerModuleCreate(Sender: TObject);
begin
  InsertIDs:= TDictionary.Create();
end;
 
procedure TServerMethods1.DSServerModuleDestroy(Sender: TObject);
begin
  InsertIDs.Free;
end;
 
{ Get the current inserted identity value. }
function TServerMethods1.GetIdentity: Integer;
begin
  Result:= 0;
  with sdsTemp do
  begin
    Close;
    CommandText:= 'SELECT @@IDENTITY AS FID';
    Open;
    if not IsEmpty then
      Result:= Fields[0].AsInteger;
    Close;
  end;
end;
更多
阅读(1000)     评论(0)