FireDAC ResultConnectionDef and information about Server and Port

馋奶兔 提交于 2020-01-16 18:41:27

问题


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;

回答1:


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.



来源:https://stackoverflow.com/questions/51933905/firedac-resultconnectiondef-and-information-about-server-and-port

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!