Skip to content

Commit 3ddf9c2

Browse files
committed
An #set_notice_processing as an alternative to #set_notice_processor.
1 parent 5ed13a6 commit 3ddf9c2

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

examples/async.ml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ let test (c : connection) =
4747
(id SERIAL PRIMARY KEY, a INTEGER NOT NULL, b TEXT NOT NULL)";
4848
assert ((fetch_single_result c)#status = Command_ok);
4949

50+
(* Create another table which will trigger a notice. *)
51+
c#send_query "\
52+
CREATE TEMPORARY TABLE postgresql_ocaml_async_2 \
53+
(id INTEGER PRIMARY KEY \
54+
REFERENCES postgresql_ocaml_async ON DELETE CASCADE)";
55+
assert ((fetch_single_result c)#status = Command_ok);
56+
5057
(* Populate using a prepared statement. *)
5158
c#send_prepare "test_ins"
5259
"INSERT INTO postgresql_ocaml_async (a, b) VALUES ($1, $2)";
@@ -93,7 +100,11 @@ let test (c : connection) =
93100
| Some r ->
94101
assert (r#status = Tuples_ok)
95102
done;
96-
assert (fetch_result c = None)
103+
assert (fetch_result c = None);
104+
105+
(* Drop the main table. *)
106+
c#send_query "DROP TABLE postgresql_ocaml_async CASCADE";
107+
assert ((fetch_single_result c)#status = Command_ok)
97108

98109
let main () =
99110
(* Async connect and test. *)
@@ -109,6 +120,7 @@ let main () =
109120
finish_conn (Obj.magic c#socket) (fun () -> c#reset_poll) Polling_writing;
110121
if c#status = Bad then failwith_f "Reset connection bad: %s" c#error_message;
111122
assert (c#status = Ok);
123+
c#set_notice_processing `Quiet;
112124
test c
113125

114126
let _ =

src/postgresql.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,9 @@ module Stub = struct
566566
external set_notice_processor :
567567
connection -> (string -> unit) -> unit = "PQsetNoticeProcessor_stub"
568568

569+
external set_notice_processor_num :
570+
connection -> int -> unit = "PQsetNoticeProcessor_num"
571+
569572

570573
(* Large objects *)
571574

@@ -882,6 +885,10 @@ object (self)
882885
method set_notice_processor f =
883886
wrap_conn (fun conn -> Stub.set_notice_processor conn f)
884887

888+
method set_notice_processing (h : [`Stderr | `Quiet]) =
889+
let i = match h with `Stderr -> 0 | `Quiet -> 1 in
890+
wrap_conn (fun conn -> Stub.set_notice_processor_num conn i)
891+
885892

886893
(* Accessors *)
887894

src/postgresql.mli

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,18 @@ object
586586
(** [#set_notice_processor] controls reporting of notice and warning
587587
messages generated by a connection.
588588
589+
{e Warning:} This function is unsafe in combination with a number of libpq
590+
entry points, and should not be used for now. As a workaround,
591+
{!#set_notice_processing} can be used to silence notices, if this is more
592+
appropriate than the default behaviour of printing them to standard error.
593+
594+
@raise Error if there is a connection error.
595+
*)
596+
597+
method set_notice_processing : [`Stderr | `Quiet] -> unit
598+
(** [#set_notice_processing] controls reporting of notice and warning messages
599+
generated by a connection by providing predefined callbacks.
600+
589601
@raise Error if there is a connection error.
590602
*)
591603

src/postgresql_stubs.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,31 @@ CAMLprim value PQsetNoticeProcessor_stub(value v_conn, value v_cb)
14201420
return Val_unit;
14211421
}
14221422

1423+
static void np_quiet(void __unused *arg, const char __unused *message)
1424+
{
1425+
}
1426+
1427+
static void np_stderr(void __unused *arg, const char *message)
1428+
{
1429+
fprintf(stderr, "%s", message);
1430+
}
1431+
1432+
CAMLprim value PQsetNoticeProcessor_num(value v_conn, value v_cb_num)
1433+
{
1434+
np_decr_refcount(get_conn_cb(v_conn));
1435+
set_conn_cb(v_conn, NULL);
1436+
switch (Int_val(v_cb_num)) {
1437+
case 0:
1438+
PQsetNoticeProcessor(get_conn(v_conn), np_stderr, NULL);
1439+
break;
1440+
case 1:
1441+
PQsetNoticeProcessor(get_conn(v_conn), np_quiet, NULL);
1442+
break;
1443+
default:
1444+
break;
1445+
}
1446+
return Val_unit;
1447+
}
14231448

14241449
/* Large objects */
14251450

0 commit comments

Comments
 (0)