Skip to content

Commit 4ac21fa

Browse files
committed
Add set_single_row_mode and extend result_status as needed.
1 parent 3cae7b6 commit 4ac21fa

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

examples/async.ml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,17 @@ let main () =
5353
for i = 0 to r#ntuples - 1 do
5454
Printf.printf "%s %s %s\n"
5555
(r#getvalue i 0) (r#getvalue i 1) (r#getvalue i 2)
56-
done
56+
done;
57+
c#send_query_prepared "test_sel";
58+
for i = 0 to 1 do
59+
match fetch_result c with
60+
| None -> assert false
61+
| Some r ->
62+
assert (r#status = Single_tuple);
63+
Printf.printf "%s %s %s\n"
64+
(r#getvalue i 0) (r#getvalue i 1) (r#getvalue i 2)
65+
done;
66+
assert (fetch_result c = None)
5767

5868
let _ =
5969
try main () with

examples/prompt.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ let print_res conn res =
3939
| Bad_response -> printf "Bad response: %s\n" res#error; conn#reset
4040
| Nonfatal_error -> printf "Non fatal error: %s\n" res#error
4141
| Fatal_error -> printf "Fatal error: %s\n" res#error
42+
| Copy_both -> printf "Copy in/out, not handled!\n"; exit 1
43+
| Single_tuple -> printf "Single tuple, not handled!\n"; exit 1
4244

4345
let rec dump_res conn =
4446
match conn#get_result with

lib/postgresql.ml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ type result_status =
260260
| Bad_response
261261
| Nonfatal_error
262262
| Fatal_error
263+
| Copy_both
264+
| Single_tuple
263265

264266
external result_status : result_status -> string = "PQresStatus_stub"
265267

@@ -406,6 +408,7 @@ module Stub = struct
406408
external send_describe_portal : connection -> string -> int
407409
= "PQsendDescribePortal_stub"
408410

411+
external set_single_row_mode : connection -> int = "PQsetSingleRowMode_stub"
409412
external get_result : connection -> result = "PQgetResult_stub"
410413
external consume_input : connection -> int = "PQconsumeInput_stub" "noalloc"
411414
external is_busy : connection -> bool = "PQisBusy_stub" "noalloc"
@@ -811,13 +814,15 @@ object (self)
811814

812815
method send_describe_prepared stm_name =
813816
wrap_conn (fun conn ->
814-
if Stub.send_describe_prepared conn stm_name <> 1 then
815-
signal_error conn)
817+
if Stub.send_describe_prepared conn stm_name <> 1 then signal_error conn)
816818

817819
method send_describe_portal portal_name =
818820
wrap_conn (fun conn ->
819-
if Stub.send_describe_portal conn portal_name <> 1 then
820-
signal_error conn)
821+
if Stub.send_describe_portal conn portal_name <> 1 then signal_error conn)
822+
823+
method set_single_row_mode =
824+
wrap_conn (fun conn ->
825+
if Stub.set_single_row_mode conn <> 1 then signal_error conn)
821826

822827
method get_result =
823828
let res = wrap_conn Stub.get_result in

lib/postgresql.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ type result_status =
117117
| Bad_response (** The server's response was not understood *)
118118
| Nonfatal_error
119119
| Fatal_error
120+
| Copy_both
121+
| Single_tuple (** One tuple of a result set ({!set_single_row_mode}) *)
120122

121123
(** Result of getline *)
122124
type getline_result =
@@ -631,6 +633,10 @@ object
631633
632634
@raise Error if there is a connection error. *)
633635

636+
method set_single_row_mode : unit
637+
(** [#set_single_row_mode] called right after {!send_query} or a sibling
638+
function causes the returned rows to be split into individual results. *)
639+
634640
method get_result : result option
635641
(** [get_result] @return [Some result] of an asynchronous query if
636642
available or [None].

lib/postgresql_stubs.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
|| ( PG_OCAML_MAJOR_VERSION >= 8 && PG_OCAML_MINOR_VERSION >= 2)
3737
# define PG_OCAML_8_2
3838
#endif
39+
#if PG_OCAML_MAJOR_VERSION > 9 \
40+
|| ( PG_OCAML_MAJOR_VERSION >= 9 && PG_OCAML_MINOR_VERSION >= 2)
41+
# define PG_OCAML_9_2
42+
#endif
3943

4044
#include <string.h>
4145
#include <ctype.h>
@@ -817,6 +821,17 @@ PQsendDescribePortal_stub(value __unused v_conn, value __unused v_portal_name)
817821
}
818822
#endif
819823

824+
#ifdef PG_OCAML_9_2
825+
noalloc_conn_info(PQsetSingleRowMode, Val_int)
826+
#else
827+
CAMLprim value
828+
PQsetSingleRowMode_stub(value __unused conn)
829+
{
830+
caml_failwith("Postgresql.set_single_row_mode: not supported");
831+
return Val_unit;
832+
}
833+
#endif
834+
820835
CAMLprim value PQgetResult_stub(value v_conn)
821836
{
822837
CAMLparam1(v_conn);

0 commit comments

Comments
 (0)