Well, after reading the unit postgres3, and asking a couple of questions in the PostgreSql IRC channel I've got this solution:
First, libpq has a function for capturing RAISE NOTICE: PQsetNoticeReceiver, already declared in the postgres3 unit, so, we just need to use it...but, as it needs access to the connection handler, and the handler is not accessible by TPQConnection we need to inherit it by just declaring a class inherited from TPQConnection.
TMyPQConnection = class(TPQConnection)
end;
Then, in both, the .lfm and .pas we must use TMyPQConnection instead of TPQConnection:
This is the .lfm:
object DataModule1: TDataModule1
OnCreate = DataModuleCreate
OldCreateOrder = False
Height = 159
HorizontalOffset = 1975
VerticalOffset = 257
Width = 281
object PQConnection1: TMyPQConnection
Connected = False
LoginPrompt = False
KeepConnection = False
Options = []
left = 48
top = 24
end
object SQLTransaction1: TSQLTransaction
Active = False
Options = []
left = 168
top = 24
end
end
Then, use it this way:
unit dm;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, pqconnection,
postgres3, // PQsetNoticeReceiver lives here!
sqldb, inifiles;
type
// we inherit TPQConnection to be able to access GetHandle's protected method.
TMyPQConnection = class(TPQConnection)
end;
{ TDataModule1 }
TDataModule1 = class(TDataModule)
PQConnection1: TMyPQConnection; // <-- don't use TPQConnection, use TMyPQConnection!.
SQLTransaction1: TSQLTransaction;
procedure DataModuleCreate(Sender: TObject);
private
end;
var
DataModule1: TDataModule1;
implementation
{$R *.lfm}
{ TDataModule1 }
procedure MyNoticeReceiver(arg:pointer; res:PPGresult);cdecl;
const PG_DIAG_MESSAGE_PRIMARY = 77; // 77 is ascii code for 'M', please see https://docs.huihoo.com/doxygen/postgresql/postgres__ext_8h_source.html
begin
writeln('----------->YEA<-------');
writeln(PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY));
end;
procedure TDataModule1.DataModuleCreate(Sender: TObject);
begin
PQConnection1.HostName:= '127.0.0.1';
PQConnection1.DatabaseName:= 'dicomscp';
PQConnection1.UserName:= 'postgres';
PQConnection1.Password:= 'postgres';
PQConnection1.Transaction:= SQLTransaction1;
PQConnection1.Connected:= True;
// now we set the notice receiver callback
PQsetNoticeReceiver(PPGConn(PQConnection1.GetHandle), @MyNoticeReceiver, nil);
end;
end.
And that's it.