Applyupdates on REST with Firedac

Asked

Viewed 834 times

3

I’m doing a project that uses a REST server with Firedac.

I use a Generic function to give my select but when I try to give my ApplyUpdates it does not give any error but the data is not reflected in the DB.

Man Applyupdates:

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; 

I believe the problem is here but there is the vague possibility that the mistake is in mine Binding (that I will be running in time).

Thank you very much.

1 answer

2

When invoking the Applyupdates method in an Fdquery we should remember that exceptions will not be generated for the application, however Firedac records the error in an internal data logging structure and continues to process updates until the number of errors is equal to or higher than Amaxerrors.

After calling the Applyupdates method, we should include rtHasErrors in Filterchanges to filter the records that contain errors. We then navigate through the data set and read the Rowerror property that will contain an exception object associated with the current record. As the example below shows:

var
  oErr: EFDException;
begin
if FDQuery1.ApplyUpdate > 0 then begin
  FDQuery1.FilterChanges := [rtModified, rtInserted, rtDeleted, rtHasErrors];
  try
    FDQuery1.First;
    while not FDQuery1.Eof do begin
      oErr := FDQuery1.RowError;
      if oErr <> nil then begin
        raise Exception.Create(oErr.Message);
      end;
      FDQuery1.Next;
    end;
  finally
    FDQuery1.FilterChanges := [rtUnmodified, rtModified, rtInserted];
  end;
end;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.