【问题标题】:PostgreSQL: Capture RAISE NOTICE from a client connection via ZeosLib/LazarusPostgreSQL:通过 ZeosLib/Lazarus 从客户端连接捕获 RAISE NOTICE
【发布时间】:2016-02-05 05:45:34
【问题描述】:

我开发了一个使用 PostgreSQL 8.4 RDBMS 的客户端应用程序。

我的应用程序是用 Lazarus 和 ZeosLib 7.2 编写的,用于数据库访问。

我使用了很多存储过程,并且在特定点我使用 raise notice 来获取过程状态的信息,Es:

RAISE NOTICE 'Step 1: Import Items from CSV file';
....
....
RAISE NOTICE 'Step 2: Check Items data';

当我在 PgAdmin3 中执行程序时,它会在“消息”选项卡中显示通知。 有没有办法在我的客户端应用程序中捕获引发的通知?

【问题讨论】:

  • TZIBEventAlerter 可能是接收这些通知的组件。在我的博客article 中查看我关于 Firebird 事件的相关示例
  • FPC 包含 Postgres 的 API 标头:fpc/packages/postgres/src/postgres3.ppfpc/packages/postgres/src/postgres3dyn.pp 在这里您可以找到函数 PQsetNoticeReceiver 所以您只需要从 Zeos 连接中获取 PGconn 参数(尝试启动在ZPlainPostgreSqlDriver单元中的function TZPostgreSQLBaseDriver.ConnectDatabase方法)PS:我正在使用FPC和Zeos的最新主干

标签: postgresql delphi lazarus notice zeos


【解决方案1】:

好的,虽然我对这个话题也很感兴趣,但这里有一些经过快速调查后创建的工作示例:

uses
    Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
    ZConnection, ZDbcPostgreSql;

type

    { TForm1 }

    TForm1 = class(TForm)
        Button1: TButton;
        Memo1: TMemo;
        pgConn: TZConnection;
        procedure Button1Click(Sender: TObject);
        procedure pgConnAfterConnect(Sender: TObject);
    private
        { private declarations }
    public
        { public declarations }
    end;

var
    Form1: TForm1;

implementation

{$R *.lfm}

procedure PGNotifyProcessor(arg: Pointer; message: PAnsiChar); cdecl;
begin
    Form1.Memo1.Lines.Add(message);
end;

{ TForm1 }

procedure TForm1.pgConnAfterConnect(Sender: TObject);
var
    pg: IZPostgreSQLConnection;
    args: Pointer;
begin
    pg := pgConn.DbcConnection as IZPostgreSQLConnection;
    pg.GetPlainDriver.SetNoticeProcessor(pg.GetConnectionHandle, @PGNotifyProcessor, args);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    pgConn.ExecuteDirect('select foo(''bar'')');
end;

end. 

它对我有用。

我猜这个例子不准确并且包含一些问题。例如,在从外部源调用的过程中使用 LCL 调用。但我希望这已经足够开始了。

测试环境为:FPC 2.6.4、Lazarus 1.5、Postgres 9.3、Linux Mint

【讨论】:

  • 效果很好!当然,对于需要较长时间的函数,要让消息实时存储过程必须在外部线程中启动......谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 2018-04-30
  • 2018-04-23
  • 2020-11-06
  • 2018-01-06
相关资源
最近更新 更多