DATASNAP数据提交之FIREDAC的TFDJSONDeltas
FIREDAC的TFDJSONDeltas相当于CLIENTDATASET的DELTA,是作为CLIENTDATASET.DELTA的替代者出现的。
TFDJSONDeltas是多个数据集DELTA的集合,一次性可以提交多个数据集修改后的数据到数据库。
服务端代码:
function TServerMethods1.SaveData2(const accountNo, tableName: string; delta: TFDJSONDeltas): Boolean;
var
d: TfrmDB;
LApply: IFDJSONDeltasApplyUpdates;
begin
Result := False;
if (accountNo = '') or (tableName = '') or (delta = nil) then
Exit;
d := GetDBPool(accountNo).Lock;
if not Assigned(d) then
Exit;
try
try
SetTraceOn(d);
d.qryOpen.Close;
d.qryOpen.sql.Clear;
d.qryOpen.sql.Text := 'select * from ' + tableName + ' where 1=2';
d.qryOpen.Open;
LApply := TFDJSONDeltasApplyUpdates.Create(delta);
LApply.ApplyUpdates(0, d.qryOpen.Command);
Result := LApply.Errors.Count = 0;
except
on e: Exception do
begin
Result := False;
Log.WriteLog('TServerMethods1.SaveData2 ' + e.Message);
end;
end;
finally
d.qryOpen.Close;
GetDBPool(accountNo).Unlock(d);
SetTraceOff(d);
end;
end;
客户端代码:
procedure TForm1.btnSave2Click(Sender: TObject);
var
LDeltas: TFDJSONDeltas;
begin
if FDMemTable1.State in dsEditModes then // 向内存表提交修改
FDMemTable1.Post
else
Exit;
LDeltas := TFDJSONDeltas.Create;
TFDJSONDeltasWriter.ListAdd(LDeltas, '1', FDMemTable1);
if methods.SaveData2('0', 't1', LDeltas) then
Self.Caption := 'save ok'
else
Self.Caption := 'save fail';
end;