delphifirebirdfiredacfibplus

How to extract stored procedure DDL using FireDAC


I am in the process of changing from FIBPlus to FireDAC, and most functionality I need is in FireDAC, I'm just battling to find the FIBPlus equivalent of TpFIBDBSchemaExtract and TpFIBScripter for extracting the stored procedures as DDL.

Does FireDAC have a way of extracting the stored procedure DDL from a database?

For example, would look like:

SET TERM ^ ;

CREATE PROCEDURE MY_PROC RETURNS (aParam INTEGER) AS
BEGIN
  aParam = 10;
END^

Solution

  • FireDAC does not support (unified) fetching DDL definitions of stored procedures (at this time). So, you'll need to fetch that DDL from the RDB$PROCEDURES table, RDB$PROCEDURE_SOURCE column by yourself. For example (not ideally designed as a connection object helper though):

    uses
      FireDAC.Stan.Util;
    
    type
      TFDConnectionHelper = class helper for TFDConnection
      public
        function GetStoredProcCode(const AName: string): string;
      end;
    
    implementation
    
    { TFDConnectionHelper }
    
    function TFDConnectionHelper.GetStoredProcCode(const AName: string): string;
    var
      Table: TFDDatSTable;
      Command: IFDPhysCommand;
    begin
      CheckActive;
      if RDBMSKind <> TFDRDBMSKinds.Firebird then
        raise ENotSupportedException.Create('This feature is supported only for Firebird');
    
      Result := '';
      ConnectionIntf.CreateCommand(Command);
    
      Command.CommandText := 'SELECT RDB$PROCEDURE_SOURCE FROM RDB$PROCEDURES WHERE RDB$PROCEDURE_NAME = :Name';
      Command.Params[0].DataType := ftString;
      Command.Params[0].Size := 31;
      Command.Params[0].AsString := UpperCase(AName);
    
      Table := TFDDatSTable.Create;
      try
        Command.Define(Table);
        Command.Open;
        Command.Fetch(Table);
    
        if Table.Rows.Count > 0 then
          Result := Table.Rows[0].GetData(0);
      finally
        FDFree(Table);
      end;
    end;
    

    Usage then (when you're connected to Firebird DBMS):

    procedure TForm1.Button1Click(Sender: TObject);
    var
      S: string;
    begin
      S := FDConnection1.GetStoredProcCode('MyProcedure');
      ...
    end;