我创建了使用firedac访问PostgreSQL数据库的VCL组件。这工作得很好,除了查询跟踪,我不能去上班。
在组件的构造中,创建连接对象,设置PARAMS等。在uses子句,我添加FireDAC.Phys.PG和FireDAC.Moni.Base,FireDAC.Moni.FlatFile。
继英巴卡迪诺文档时,TFDMoniFlatFileClientLink是TFDConnection之前创建的,并且跟踪被设置为true两个FDTracer和FDConnection.ConnectionIntf。
unit My.Database;
interface
uses
System.SysUtils, System.Classes, FireDAC.Comp.UI, FireDAC.Comp.Client, FireDAC.Phys.PG,
FireDAC.Moni.Base, FireDAC.Moni.FlatFile;
type
TMyDatabase = class(TComponent)
private
{ Private declarations }
FDTracer: TFDMoniFlatFileClientLink;
FDConnection: TFDConnection;
FConnectionString: string;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function IsConnected: Boolean;
property ConnectionString: stringread FConnectionString write FConnectionString;
function OpenConnection: Boolean;
function GetAsDataset(ASQL: string): TFDQuery;
end;
implementation
uses
FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.Stan.Def, FireDAC.Stan.Async,
FireDAC.VCLUI.Login, FireDAC.VCLUI.Wait;
constructor TMyDatabase.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDTracer := TFDMoniFlatFileClientLink.Create(Self);
FDConnection := TFDConnection.Create(Self);
FDTracer.tracing := True;
with FDConnection do
begin
DriverName := 'PG';
LoginPrompt := False;
//params for PostgreSQL
with Params do
begin
Add('Port=5432');
Add('LoginTimeout=2');
Add('Pooled=False');
Add('OidAsBlob=No');
Add('UnknownFormat=BYTEA');
Add('ExtendedMetadata=True');
end;
end;
FDConnection.ConnectionIntf.Tracing := True;
end;
function TMyDatabase.IsConnected: Boolean;
begin
Result := FDConnection.Connected;
end;
function TMyDatabase.OpenConnection: Boolean;
begin
with FDConnection do
try
ConnectionString := FConnectionString;
if not Connected then
begin
Open;
end;
finally
Result := Connected;
end;
end;
function TMyDatabase.GetAsDataset(ASQL: string): TFDQuery;
begin
Result := TFDQuery.Create(Self);
with Result do
begin
Connection := FDConnection;
end;
Result.Open(ASQL);
end;
当我连接到数据库,并执行经GetAsDataSet查询,跟踪文件仅记录FireDac连接的启动/停止,而不是查询的执行。如果我把TFDConnection和TFDMoniFlatFileClientLink组件的形式或数据模块上,如预期的查询执行跟踪。
谁知道如何跟踪与从组件动态创建一个连接对象的查询的执行?
最好的问候,罗纳德
我试图重现你的问题在我的一个简单的测试平台FireDAC,加入了TFDMoniFlatFileClientLink
给它,并得到了类似的结果,即跟踪文件包含的条目说,它已经开始和停止,但没有在两者之间。
看着http://docwiki.embarcadero.com/CodeExamples/Tokyo/en/FireDAC.MoniLayerClients_Sample我似乎缺少一个小而重要的一步,即我FDConnection的MonitorBy
参数设置为“简单文件”,以匹配TFDMoniFlatFileClientLink
于是,刚刚开放在我的代码连接前,我在下面的第一行
FDConnection1.Params.MonitorBy := mbFlatFile;
FDConnection1.Connected := True;
和跟踪,然后运行得很好。
部分微量提取物
--- new start of FireDAC Trace ---
4361928750001 19:48:07.092 -=#!!! FireDAC Tracer started !!!#=-
4361928750001 19:48:07.094 TFDPhysMSSQLConnection TFDPhysMSSQLConnecti . CreateConnection [ConnectionDef=""]
4361928750001 19:48:07.094 TFDPhysMSSQLConnection TFDPhysMSSQLConnecti . CreateTransaction [ConnectionDef=""]
4361928750001 19:48:07.094 TFDPhysMSSQLConnection FDConnection1 >> Open [ConnectionDef=""]
4361928750001 19:48:07.094 TFDConnectionDef $0100C390 >> Definition [Name=""]
4361928750001 19:48:07.094 TFDConnectionDef $0100C390 . Server=MAT430\ss2014
4361928750001 19:48:07.094 TFDConnectionDef $0100C390 . User_Name=sa
4361928750001 19:48:07.094 TFDConnectionDef $0100C390 . Password=*****
4361928750001 19:48:07.094 TFDConnectionDef $0100C390 . Database=MAtest
4361928750001 19:48:07.094 TFDConnectionDef $0100C390 . OSAuthent=No
4361928750001 19:48:07.094 TFDConnectionDef $0100C390 . DriverID=MSSQL
4361928750001 19:48:07.094 TFDConnectionDef $0100C390 . MonitorBy=FlatFile
4361928750001 19:48:07.094 TFDConnectionDef $0100C390 << Definition [Name=""]
4361928750001 19:48:07.094 TFDPhysMSSQLConnection FDConnection1 >> FireDAC info
4361928750001 19:48:07.094 TFDPhysMSSQLConnection FDConnection1 . Tool=RAD Studio 10.2
4361928750001 19:48:07.094 TFDPhysMSSQLConnection FDConnection1 . FireDAC=16.0.0 (Build 88974)
4361928750001 19:48:07.094 TFDPhysMSSQLConnection FDConnection1 . Platform=Windows 32 bit
4361928750001 19:48:07.094 TFDPhysMSSQLConnection FDConnection1 . Defines=FireDAC_NOLOCALE_META;FireDAC_MONITOR
4361928750001 19:48:07.094 TFDPhysMSSQLConnection FDConnection1 << FireDAC info
4361931710001 19:48:07.390 TFDPhysMSSQLConnection FDConnection1 . SQLDriverConnect [szConnStr="DRIVER=SQL Server Native Client 11.0;UID=sa;PWD=*******;Server=MAT430\ss2014;Database=MAtest;Trusted_Connection=No;MARS_Connection=yes"]
4361931710001 19:48:07.392 TFDPhysMSSQLDriver MSSQL . ENTER SQLDriverConnectW
HDBC 0x01044B18
HWND 0x00000000
WCHAR * 0x61942440 [ -3] "******\ 0"
SWORD -3
WCHAR * 0x61942440
SWORD -3
SWORD * 0x00000000
UWORD 0 <SQL_DRIVER_NOPROMPT>
4361935310001 19:48:07.756 TFDPhysMSSQLDriver MSSQL . EXIT SQLDriverConnectW with return code 1 (SQL_SUCCESS_WITH_INFO)
HDBC 0x01044B18
HWND 0x00000000
WCHAR * 0x61942440 [ -3] "******\ 0"
SWORD -3
WCHAR * 0x61942440 <Invalid buffer length!> [-3]
SWORD -3
SWORD * 0x00000000
UWORD 0 <SQL_DRIVER_NOPROMPT>
DIAG [01000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Changed database context to 'MAtest'. (5701)
DIAG [01000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Changed language setting to us_english. (5703)
4361935310001 19:48:07.758 TFDPhysMSSQLDriver MSSQL . ENTER SQLGetDiagRecW
SQLSMALLINT 2 <SQL_HANDLE_DBC>
SQLHANDLE 0x01044B18
SQLSMALLINT 1
SQLWCHAR * 0x00FCB5CC
SQLINTEGER * 0x0019F840
SQLWCHAR * 0x0019D83A
SQLSMALLINT 4096
SQLSMALLINT * 0x0019F846
其次是厨房水槽的剩余部分。