Developing Custom Commands (FireDAC)

From RAD Studio
Jump to: navigation, search

Go Up to Executing SQL Scripts (FireDAC)

Contents

Description

The set of the script control execution commands can be extended by custom commands. You can find the sources of the existing commands in the FireDAC.Comp.ScriptCommands unit and use them as a template for your own commands. The command class must be registered at the commands registry. For example, the SPOOL command code can be used:

type
  TFDSpoolScriptCommand = class(TFDScriptCommand)
  private
    FMode: Integer;
    FFileName: String;
    FAppend: Boolean;
  public
    class function Help(): String; override;
    class procedure Keywords(AKwds: TStrings); override;
    function Parse(const AKwd: String): Boolean; override;
    procedure Execute(); override;
    // -1 - off, 0 - show, 1 - file
    property Mode: Integer read FMode;
    property Append: Boolean read FAppend;
    property FileName: String read FFileName;
  end;

{-------------------------------------------------------------------------------}
class function TFDSpoolScriptCommand.Help(): String;
begin
  Result := '(SPOol | OUTput) [OFF|[APPend] <spool name>] - turns spooling off, print it status'#13#10 +
            '                                               or redirect it to the name';
end;

{-------------------------------------------------------------------------------}
class procedure TFDSpoolScriptCommand.Keywords(AKwds: TStrings);
begin
  AKwds.Add('SPOol');
  AKwds.Add('OUTput');
end;

{-------------------------------------------------------------------------------}
function TFDSpoolScriptCommand.Parse(const AKwd: String): Boolean;
var
  iLastBmk: Integer;
  ucS: string;
begin
  iLastBmk := Parser.GetBookmark;
  ucS := Parser.GetUCWord;
  if ucS = 'OFF' then
    FMode := -1
  else if ucS = '' then
    FMode := 0
  else begin
    FMode := 1;
    FAppend := FDKeyWordMatch(ucS, 'APPEND', 3);
    if not FAppend then
      Parser.SetBookmark(iLastBmk);
    FFileName := Parser.GetLine();
    if FFileName = '' then
      FMode := -1;
  end;
  Result := True;
end;

{-------------------------------------------------------------------------------}
procedure TFDSpoolScriptCommand.Execute();
begin
  case FMode of
  -1:
    begin
      Engine.ScriptOptions.SpoolOutput := smNone;
      EngineIntf.UpdateSpool;
    end;
   0:
    if Engine.ScriptOptions.SpoolOutput <> smNone then
      EngineIntf.ConPut('currently spooling to ' + Engine.ScriptOptions.SpoolFileName, soInfo)
    else
      EngineIntf.ConPut('not spooling currently', soInfo);
   1:
    begin
      if FAppend then
        Engine.ScriptOptions.SpoolOutput := smOnAppend
      else
        Engine.ScriptOptions.SpoolOutput := smOnReset;
      Engine.ScriptOptions.SpoolFileName := EngineIntf.ExpandString(FileName);
      EngineIntf.UpdateSpool;
    end;
  end;
end;

initialization
  FDScriptCommandRegistry().AddCommand(TFDSpoolScriptCommand);