0

Was trying to migrate app to FireDAC usage on Delphi 10.2 and stuck only with this function: Source on github

   procedure TMainForm.ExecuteScript(script: string; memo: TMemo);
var
  Log: TStringList;
  FN: string;
begin
  ShowHourGlassCursor;
  ZSQLProcessor.Script.Text := script;
  try
    ZSQLProcessor.Connection.StartTransaction;
    ZSQLProcessor.Execute;
    ZSQLProcessor.Connection.Commit;
  except
    on E:Exception do
    begin
      ZSQLProcessor.Connection.Rollback;
      memo.Text := E.Message;
      Exit;
    end;
  end;

Stuck with line and was not able to get along, any help would be good:

ZSQLProcessor.Script.Text := script;

reference: ZSqlProcessor.pas

Converted function without last part looks so:

procedure TMainForm.ExecuteScript(script: string; memo: TMemo);
var
  Log: TStringList;
  FN: string;
begin
  ShowHourGlassCursor;
  //ZSQLProcessor.Script.Text := script;
  try
    MyTrinityConnection.StartTransaction;
    FDScript1.ValidateAll;
    FDScript1.ExecuteAll;
    MyTrinityConnection.Commit;
  except
    on E:Exception do
    begin
      MyTrinityConnection.Rollback;
      memo.Text := E.Message;
      Exit;
    end;
  end;
Goaul
  • 943
  • 11
  • 13
  • Btw. there's more to improve here. FireDAC has its own waiting cursors, but better consider asynchronous mode or worker thread for this task (anyway, that cursor seem to never change back). The `try..finally` block should be in pseudocode like `Transaction.Start; try DoWork; Transaction.Commit; except Transaction.Rollback end;`. You can handle more detailed exceptions and you don't need to `Exit` from the method in this case. – Victoria Aug 20 '18 at 11:23

1 Answers1

0

To assign single SQL script to the TFDScript object you can simply Add one TFDSQLScript item to the TFDSQLScripts collection and set its SQL string list collection text to the SQL command of your choice, for example:

FDScript1.SQLScripts.Add.SQL.Text := script; { ← this adds one TFDSQLScript item to the script collection and to this just added item assigns a SQL script }

Of course, this assumes the TFDSQLScripts collection is clear, if not call Clear before.

Victoria
  • 7,822
  • 2
  • 21
  • 44
  • You're welcome! Just keep in mind clearing that collection if you're about to call that method multiple times. – Victoria Aug 20 '18 at 14:49