Skip to content

Commit f6d38b9

Browse files
committed
Merge branch 'set_single_row_mode'
2 parents 0941a94 + 4ac21fa commit f6d38b9

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
@@ -78,7 +78,17 @@ let test (c : connection) =
7878
for i = 0 to r#ntuples - 1 do
7979
Printf.printf "%s %s %s\n"
8080
(r#getvalue i 0) (r#getvalue i 1) (r#getvalue i 2)
81-
done
81+
done;
82+
c#send_query_prepared "test_sel";
83+
for i = 0 to 1 do
84+
match fetch_result c with
85+
| None -> assert false
86+
| Some r ->
87+
assert (r#status = Single_tuple);
88+
Printf.printf "%s %s %s\n"
89+
(r#getvalue i 0) (r#getvalue i 1) (r#getvalue i 2)
90+
done;
91+
assert (fetch_result c = None)
8292

8393
let main () =
8494
(* Async connect and test. *)

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
@@ -273,6 +273,8 @@ type result_status =
273273
| Bad_response
274274
| Nonfatal_error
275275
| Fatal_error
276+
| Copy_both
277+
| Single_tuple
276278

277279
external result_status : result_status -> string = "PQresStatus_stub"
278280

@@ -428,6 +430,7 @@ module Stub = struct
428430
external send_describe_portal : connection -> string -> int
429431
= "PQsendDescribePortal_stub"
430432

433+
external set_single_row_mode : connection -> int = "PQsetSingleRowMode_stub"
431434
external get_result : connection -> result = "PQgetResult_stub"
432435
external consume_input : connection -> int = "PQconsumeInput_stub" "noalloc"
433436
external is_busy : connection -> bool = "PQisBusy_stub" "noalloc"
@@ -833,13 +836,15 @@ object (self)
833836

834837
method send_describe_prepared stm_name =
835838
wrap_conn (fun conn ->
836-
if Stub.send_describe_prepared conn stm_name <> 1 then
837-
signal_error conn)
839+
if Stub.send_describe_prepared conn stm_name <> 1 then signal_error conn)
838840

839841
method send_describe_portal portal_name =
840842
wrap_conn (fun conn ->
841-
if Stub.send_describe_portal conn portal_name <> 1 then
842-
signal_error conn)
843+
if Stub.send_describe_portal conn portal_name <> 1 then signal_error conn)
844+
845+
method set_single_row_mode =
846+
wrap_conn (fun conn ->
847+
if Stub.set_single_row_mode conn <> 1 then signal_error conn)
843848

844849
method get_result =
845850
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 =
@@ -651,6 +653,10 @@ object
651653
652654
@raise Error if there is a connection error. *)
653655

656+
method set_single_row_mode : unit
657+
(** [#set_single_row_mode] called right after {!send_query} or a sibling
658+
function causes the returned rows to be split into individual results. *)
659+
654660
method get_result : result option
655661
(** [get_result] @return [Some result] of an asynchronous query if
656662
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>
@@ -823,6 +827,17 @@ PQsendDescribePortal_stub(value __unused v_conn, value __unused v_portal_name)
823827
}
824828
#endif
825829

830+
#ifdef PG_OCAML_9_2
831+
noalloc_conn_info(PQsetSingleRowMode, Val_int)
832+
#else
833+
CAMLprim value
834+
PQsetSingleRowMode_stub(value __unused conn)
835+
{
836+
caml_failwith("Postgresql.set_single_row_mode: not supported");
837+
return Val_unit;
838+
}
839+
#endif
840+
826841
CAMLprim value PQgetResult_stub(value v_conn)
827842
{
828843
CAMLparam1(v_conn);

0 commit comments

Comments
 (0)