Search code examples
delphifiredac

FireDAC ResultConnectionDef and information about Server and Port


DelphiXE 10.2.2

Was checking here old http://codeverge.com topic where back then was working, but now with

ResultConnectionDef for getting information about established connection (Server and Port).

ZeosLib (ZeosDBO) code:

procedure TMainForm.UpdateCaption;
begin
  Caption := Format('Truice %s - Connection: %s:%d / %s', [VERSION_EXE, MyTrinityConnection.HostName, MyTrinityConnection.Port, GetDBVersion]);
  Application.Title := Caption;
end;

With FireDac:

uses FireDAC.Phys.MySQLDef, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.MySQL, FireDAC.VCLUI.Wait, FireDAC.Comp.UI, FireDAC.Comp.Client, FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, FireDAC.Comp.DataSet, FireDAC.Comp.Script, FireDAC.Comp.ScriptCommands, FireDAC.Stan.Util; 

procedure TMainForm.UpdateCaption;
begin
  Caption := Format('Truice %s - Connection: %s:%d / %s', [VERSION_EXE, MyTrinityConnection.ResultConnectionDef.Server, MyTrinityConnection.ResultConnectionDef.Port, GetDBVersion]);
  Application.Title := Caption;
end;

Result:

'IFDStanConnectionDef' does not contain a member named 'Server'
'IFDStanConnectionDef' does not contain a member named 'Port'

Questions:

  1. Are there any changes to FireDAC where this part was changed?
  2. What is best method to gather Server and Port for active connection?



Final code after Victorias solution, looks so:

procedure TMainForm.UpdateCaption;
var
  Server: string;
  Port: Integer;
begin
  Server := TFDPhysMySQLConnectionDefParams(MyTrinityConnection.ResultConnectionDef.Params).Server;
  Port := TFDPhysMySQLConnectionDefParams(MyTrinityConnection.ResultConnectionDef.Params).Port;
  Caption := Format('Truice %s - Connection: %s:%d / %s', [VERSION_EXE, Server, Port, GetDBVersion]);

  Application.Title := Caption;
end;

Solution

  • 1. Was FireDAC changed in this part?

    Yes, it has been changed. The old variant of the underlying interface (IADStanConnectionDef) was having properties like Server and Port. The new version of this interface (IFDStanConnectionDef) publishes the Params property which points to specific DBMS parameter collection implementation.

    The reason for the change might have been variety of the supported DBMS (some of which has no remote connection).

    2. How to get server and port of the active MySQL connection?

    There are at least two ways to get server and port of the active MySQL connection I'm aware of. The first is using the mentioned ResultConnectionDef and its Params property (FDConnection1 object in both examples is assumed to be connected to MySQL server at the moment), e.g.:

    uses
      FireDAC.Phys.MySQLDef;
    
    var
      Port: Integer;
      Server: string;
    begin
      Port := TFDPhysMySQLConnectionDefParams(FDConnection1.ResultConnectionDef.Params).Port;
      Server := TFDPhysMySQLConnectionDefParams(FDConnection1.ResultConnectionDef.Params).Server;
      ...
    end;
    

    Another option for MySQL is getting these information from TMySQLSession session object, e.g.:

    uses
      FireDAC.Phys.MySQLWrapper;
    
    var
      Port: Cardinal;
      Server: string;
    begin
      Port := TMySQLSession(FDConnection1.ConnectionIntf.CliObj).Port;
      Server := TMySQLSession(FDConnection1.ConnectionIntf.CliObj).Host;
      ...
    end;
    

    Both above ways equal (at this time) because internally the TMySQLSession receives host and port from the connection definition parameters (which means what you set is what you get just like ZEOS is doing with their properties).

    If you wanted to have host and port information as reported by MySQL, you could fetch the values of the hostname and port system variables.