我开发了一个使用 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 中执行程序时,它会在“消息”选项卡中显示通知。
有没有办法在我的客户端应用程序中捕获引发的通知?

最佳答案

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

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

关于PostgreSQL:通过 ZeosLib/Lazarus 从客户端连接捕获 RAISE NOTICE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33526809/

10-10 16:56