使用Firedac在REST上应用更新 [英] ApplyUpdates on REST with Firedac

查看:226
本文介绍了使用Firedac在REST上应用更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目使用带有FireDac的REST服务器.

My project uses a REST server with FireDac.

我有一个通用函数,可以进行所有选择,但是当我尝试执行ApplyUpdates时却什么也没做.没有消息,没有崩溃,它只是继续运行,并且数据不会反映到数据库中.

I have a generic function that makes all my Selects but when I try to ApplyUpdates if does nothings. No message, no crash, it just keeps going and the data is not reflected to the database.

我的代码:

function TServerMethods.ApplyUpdates(banco, tabela : String; const DeltaList: TFDJSONDeltas; var Mensagem : String) : Boolean; 
var 
  LApply : IFDJSONDeltasApplyUpdates; 
  Query : TFDQuery; 
begin 
  mensagem := ''; 
  result := false; 
  try 
    try 
      LApply := TFDJSONDeltasApplyUpdates.Create(DeltaList); 
      Query := CriaQuery(banco,Tabela); 
      Query.Open(); 
      LApply.ApplyUpdates(banco + '.' + tabela, Query.Command); 
      if LApply.Errors.Count > 0 then 
        raise Exception.Create(LApply.Errors.Strings.ToString); 
      result := true; 
    except 
      on E:Exception do 
      begin 
        mensagem := 'Ocorreu um Erro na atualização: ' + #13#10 + E.Message; 
      end; 
    end; 
  finally 

  end; 

end; 

GetDeltas函数(生成DeltaList的函数):

The GetDeltas function (the function that generates the DeltaList):

function GetDeltas(Banco, Tabela : String; MemTable : TFDMemTable) : TFDJSONDeltas;
begin
  if MemTable.State in [dsInsert, dsEdit] then
    MemTable.Post;
  result := TFDJSONDeltas.Create;
  TFDJSONDeltasWriter.ListAdd(result, MemTable);
end;

我的"CriaQuery"功能:

My "CriaQuery" Function:

function TServerMethods.CriaQuery(Database : String; Tabela : String = '') : TFDQuery;
var
  FieldName : Boolean;
  i : Integer;
begin
  result := TFDQuery.Create(self);
  result.Connection := Connection;
  result.FetchOptions.AutoFetchAll := afAll;
  result.name := 'Qry' + Database + tabela;
  result.SQL.Clear;
  FieldName := false;
  if Trim(Tabela) <> '' then
  begin
    result.SQL := MontaSQL(database + '.' + tabela);
    result.SQL.Add(' and 1 = 0');
    result.Open();
    QryCampos.First;
    result.IndexFieldNames := result.Fields[1].FieldName;
    for i := 0 to result.Fields.Count-1 do
    begin
      if (UPPERCASE(Copy(result.Fields[i].FieldName, Length(result.Fields[i].FieldName)-1,2)) = 'ID') and
         (not FieldName) then
      begin
        result.Fields[i].ProviderFlags := [pfInUpdate, pfInWhere, pfInKey];
        FieldName := true;
      end
      else
        result.Fields[i].ProviderFlags := [pfInUpdate];
    end;
    result.Close;
    result.SQL.Delete(result.SQL.Count-1);
  end;
end;

生成组件绑定的函数:

procedure LinkaComponente(Campo : TField; Dono : TFmxObject; Classe : String);
var
  BindSource : TBindSourceDB;
  BindingList : TBindingsList;
  Link : TLinkControlToField;
begin
  if Dono is TForm then
  begin
    BindSource := TBindSourceDB.Create(Dono);
  end
  else
  begin
    BindSource := TBindSourceDB.Create(Dono.Owner);
  end;
  BindingList := TBindingsList.Create(BindSource.Owner);
  Link := TLinkControlToField.Create(BindSource.Owner);
  BindSource.DataSet := Campo.DataSet;

  if Classe = 'TCheckBox' then
  begin
    Link.Control := Dono.FindComponent(Campo.FieldName);
    Link.CustomFormat := 'ToStr(%s) <> "N"';
    Link.CustomParse  := 'IfThen(%s,"S","N")';
  end
  else if Classe = 'TFrameF2' then
  begin
    Link.Control := (Dono.FindComponent('Frame' + Campo.FieldName) as TFrameF2).edtFK;
  end
  else
    Link.Control := Dono.FindComponent(Campo.FieldName);
  Link.DataSource := BindSource;
  Link.FieldName := Campo.FieldName;
  Link.Active := true;
end;

当我调用applyUpdates函数时:

the moment I call the applyUpdates function:

procedure TDMPadrao.DMApplyUpdates;
var
  Deltas : TFDJSONDeltas;
  Mensagem : String;
begin
  //repetir esses comando para todas as MemTables do DM na sua ordem de dependencia
  //       tabelas pai antes de tabelas filhas...
  try
    Deltas := GetDeltas(banco, tabela, FDMemTable);
  except
    on E:Exception do
    begin
      FDMemTable.Edit;
      MostraMensagemBasica('Ocorreu um Erro na atualização:' + #13#10 + E.Message);
      abort;
    end;
  end;
  if not DMClient.ServerMethodsClient.ApplyUpdates(banco, tabela, Deltas, Mensagem) then
  begin
    FDMemTable.Edit;
    MostraMensagemBasica(Mensagem);
    abort;
  end;
end;

我在阅读时一切正常.我只有在调用ApplyUpdates函数时遇到问题

Everything works fine when I'm reading. I Only get a problem when I call the ApplyUpdates function

谢谢.

推荐答案

我遇到了类似的问题,并且解决了在ApplyUpdates之前将表名传递给Query.UpdateOptions.UpdateTableName的问题.

I had similar problem and I got it resolved passing the table name to Query.UpdateOptions.UpdateTableName before ApplyUpdates.

  • 您是否在"CriaQuery"中执行此操作?
  • 您的Delphi版本是什么?

这是我的工作代码,我已经在Delphi XE7和XE7 Update 1中对其进行了测试:

Here is my working code, I have tested it in Delphi XE7 e XE7 Update 1:

procedure TDBDM.ApplyDeltas(const ADeltaList: TFDJSONDeltas; const TableName: string);
var
  JSONDeltas: IFDJSONDeltasApplyUpdates;
  Query: TFDQuery;
begin
  JSONDeltas := TFDJSONDeltasApplyUpdates.Create(ADeltaList);
  Query := CreateQuery(TableName);
  try
    Query.UpdateOptions.UpdateTableName := TableName;
    JSONDeltas.ApplyUpdates(0, Query.Command);

    if JSONDeltas.Errors.Count > 0 then
    begin
      raise Exception.Create(JSONDeltas.Errors.Strings.Text);
    end;
  finally
    Query.Free;
  end;
end;

注释

  • 与您的代码不同,不会调用Query.Open.​​
  • TFDMemTable.CachedUpdates 必须为 True
  • different from your code, Query.Open is not called.
  • TFDMemTable.CachedUpdates must be True

添加了客户端代码以应用更新

我在TFDMemTable.AfterPost事件中调用此方法.

I call this method in TFDMemTable.AfterPost event.

    const
      CustomerTableName = 'CUSTOMER';

    procedure TCustomersDataModuleClient.ApplyUpdates;
    var
      Deltas: TFDJSONDeltas;
    begin
      Deltas := TFDJSONDeltas.Create;
      TFDJSONDeltasWriter.ListAdd(Deltas, CustomerTableName, CustomersMemTable);
      RestClientModule.CustomersMethodsClient.ApplyUpdates(Deltas);
      CustomersMemTable.CommitUpdates;
    end;

这篇关于使用Firedac在REST上应用更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆