Skip to content

Commit 768f230

Browse files
committed
Improved notification API + fixed recent notification GC bug
1 parent 7d0c5bf commit 768f230

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

examples/prompt.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ let rec dump_res conn =
4949

5050
let rec dump_notification conn =
5151
match conn#notifies with
52-
| Some (msg, pid, extra) ->
53-
printf "Notication from backend %i: [%s] [%s]\n" pid msg extra;
52+
| Some { Notification.name; pid; extra } ->
53+
printf "Notication from backend %i: [%s] [%s]\n" pid name extra;
5454
flush stdout;
5555
dump_notification conn
5656
| None -> ()

lib/postgresql.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ let string_of_error = function
325325

326326
exception Error of error
327327

328+
module Notification = struct
329+
type t = { name : string; pid : int; extra : string }
330+
end (* Notification *)
328331

329332
module Stub = struct
330333
(* Database Connection Functions *)
@@ -452,7 +455,7 @@ module Stub = struct
452455

453456
(* Asynchronous Notification *)
454457

455-
external notifies : connection -> (string * int * string) option = "PQnotifies_stub"
458+
external notifies : connection -> Notification.t option = "PQnotifies_stub"
456459

457460

458461
(* Functions Associated with the COPY Command *)

lib/postgresql.mli

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,15 @@ type conninfo_option =
414414
cio_dispsize : int; (** Field size in characters for dialog *)
415415
}
416416

417+
(** Type of asynchronous notifications *)
418+
module Notification : sig
419+
type t = {
420+
name : string; (** name the of relation containing data *)
421+
pid : int; (** the process id of the backend *)
422+
extra : string; (** payload data (empty if not provided) *)
423+
}
424+
end (* Notification *)
425+
417426
external conndefaults : unit -> conninfo_option array = "PQconndefaults_stub"
418427
(** [conndefaults ()] @return array of all records of type [conninfo_option] *)
419428

@@ -468,11 +477,8 @@ object
468477

469478
(** Asynchronous Notification *)
470479

471-
method notifies : (string * int * string) option
472-
(** [#notifies] @return [Some (name, pid, extra)] if available ([None]
473-
otherwise), where [name] is the name the of relation containing
474-
data, [pid] the process id of the backend, and [extra] is any payload
475-
data associated with the notification (empty it not provided).
480+
method notifies : Notification.t option
481+
(** [#notifies] @return [Some notification] if available ([None] otherwise).
476482
477483
@raise Error if there is a connection error.
478484
*)

lib/postgresql_stubs.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,20 +1005,24 @@ CAMLprim value PQunescapeBytea_stub(value v_from)
10051005
CAMLprim value PQnotifies_stub(value v_conn)
10061006
{
10071007
CAMLparam1(v_conn);
1008-
CAMLlocal1(v_str);
1009-
CAMLlocal1(v_extra);
1010-
PGnotify *noti = PQnotifies(get_conn(v_conn));
1011-
1012-
if (noti) {
1013-
value v_pair;
1014-
v_str = make_string(noti->relname);
1015-
v_pair = caml_alloc_small(3, 0);
1016-
v_extra = make_string(noti->extra);
1017-
Field(v_pair, 0) = v_str;
1018-
Field(v_pair, 1) = Val_int(noti->be_pid);
1019-
Field(v_pair, 2) = v_extra;
1020-
PQfreemem(noti);
1021-
CAMLreturn(make_some(v_pair));
1008+
CAMLlocal2(v_str, v_extra);
1009+
PGnotify *notif = PQnotifies(get_conn(v_conn));
1010+
1011+
if (notif) {
1012+
value v_notif;
1013+
v_str = make_string(notif->relname);
1014+
v_extra =
1015+
#if PG_OCAML_MAJOR_VERSION >= 9
1016+
make_string(notif->extra);
1017+
#else
1018+
v_empty_string;
1019+
#endif
1020+
v_notif = caml_alloc_small(3, 0);
1021+
Field(v_notif, 0) = v_str;
1022+
Field(v_notif, 1) = Val_int(notif->be_pid);
1023+
Field(v_notif, 2) = v_extra;
1024+
PQfreemem(notif);
1025+
CAMLreturn(make_some(v_notif));
10221026
}
10231027
else CAMLreturn(v_None);
10241028
}

0 commit comments

Comments
 (0)