ApplyUpdates on REST with Firedac

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.

My 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 a vague possibility that the error is in my binding (which it generates at runtime).

Thank you very much.

    
asked by anonymous 01.07.2015 / 15:35

1 answer

2

When we invoke the ApplyUpdates method in an FDQuery we must remember that exceptions will not be generated for the application, but FireDac logs the error in an internal data record structure and continues to process updates until the number of errors is equal to or higher than AMaxErrors.

After calling the ApplyUpdates method, we must include rtHasErrors in FilterChanges to filter the records containing errors. We then navigate through the dataset 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;
    
15.09.2015 / 20:58