Skip to content

Commit

Permalink
Remove ppx.lwt usage for cross-compatibility between lwt 2 and 3.
Browse files Browse the repository at this point in the history
This may be reverted when Eliom can be linked with lwt 3.
  • Loading branch information
paurkedal committed Oct 26, 2017
1 parent 5ae66ec commit 33f33b3
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 112 deletions.
1 change: 0 additions & 1 deletion bin/jbuild
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
(executable
((name query)
(modules (query))
(preprocess (pps (lwt.ppx)))
(libraries (caqti caqti-lwt))))

(install
Expand Down
35 changes: 21 additions & 14 deletions bin/query.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

open Caqti_describe
open Caqti_lwt
open Lwt.Infix
module Cal = CalendarLib

let field_separator = ref ","
Expand Down Expand Up @@ -45,30 +46,36 @@ end

let main do_describe uri qs =
let q = Caqti_query.prepare_any qs in
let%lwt connection = Caqti_lwt.connect uri in
Caqti_lwt.connect uri >>= fun connection ->
let module C = (val connection) in
let module U = Connection_utils (C) in
let describe =
(match C.describe with
| None -> failwith "The database does not support describe."
| Some describe -> describe) in
let%lwt qd = describe q in
describe q >>= fun qd ->

let n = Array.length qd.querydesc_fields in

let print_description () =
let rec loop i =
if i = n then Lwt.return_unit else
let name, tdesc = qd.querydesc_fields.(i) in
Lwt_io.printf "%s : %s\n" name (string_of_typedesc tdesc) >>= fun () ->
loop (i + 1) in
loop 0 in

let print_tuple r =
assert (n > 0);
Lwt_io.print (U.show_field qd 0 r) >>
for%lwt i = 1 to n - 1 do
Lwt_io.print !field_separator >>
Lwt_io.print (U.show_field qd i r)
done >>
let rec loop i =
Lwt_io.print (U.show_field qd i r) >>= fun () ->
if i = n then Lwt.return_unit else
Lwt_io.print !field_separator >>= fun () ->
loop (i + 1) in
loop 0 >>= fun () ->
Lwt_io.print "\n" in
if do_describe then
for%lwt i = 0 to n - 1 do
let name, tdesc = qd.querydesc_fields.(i) in
Lwt_io.printf "%s : %s\n" name (string_of_typedesc tdesc)
done
else
C.iter_s q print_tuple [||]

if do_describe then print_description () else C.iter_s q print_tuple [||]

let () =
let arg_desc = ref false in
Expand Down
10 changes: 7 additions & 3 deletions lib-lwt/caqti_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,13 @@ include Caqti_connect.Make (struct
if choices = [] then
Lwt.fail_invalid_arg "Caqti_lwt.Unix.poll: No operation specified."
else
let%lwt timed_out =
try%lwt Lwt.choose choices >|= fun _ -> false
with Lwt_unix.Timeout -> Lwt.return_true in
begin
Lwt.catch
(fun () -> Lwt.choose choices >|= fun _ -> false)
(function
| Lwt_unix.Timeout -> Lwt.return_true
| exn -> Lwt.fail exn)
end >>= fun timed_out ->
Lwt.return (Lwt_unix.readable fd, Lwt_unix.writable fd, timed_out)
end

Expand Down
1 change: 0 additions & 1 deletion lib-lwt/jbuild
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
((name caqti_lwt)
(public_name caqti-lwt)
(wrapped false)
(flags (-ppx ppx_lwt))
(modules
(Caqti_lwt
Caqti_lwt_sql_io))
Expand Down
28 changes: 14 additions & 14 deletions tests/bikereg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,25 @@ let iter_s_stolen (module Db : Caqti_lwt.CONNECTION) f =

let test db =
(* Examples of statement execution: Create and populate the register. *)
create_bikereg db >>
reg_bike db "BIKE-0000" "Arthur Dent" >>
reg_bike db "BIKE-0001" "Ford Perfect" >>
reg_bike db "BIKE-0002" "Zaphod Beeblebrox" >>
reg_bike db "BIKE-0003" "Trillian" >>
reg_bike db "BIKE-0004" "Marvin" >>
report_stolen db "BIKE-0000" >>
report_stolen db "BIKE-0004" >>
create_bikereg db >>= fun () ->
reg_bike db "BIKE-0000" "Arthur Dent" >>= fun () ->
reg_bike db "BIKE-0001" "Ford Perfect" >>= fun () ->
reg_bike db "BIKE-0002" "Zaphod Beeblebrox" >>= fun () ->
reg_bike db "BIKE-0003" "Trillian" >>= fun () ->
reg_bike db "BIKE-0004" "Marvin" >>= fun () ->
report_stolen db "BIKE-0000" >>= fun () ->
report_stolen db "BIKE-0004" >>= fun () ->

(* Examples of single-row queries. *)
let show_owner frameno =
match%lwt find_bike_owner frameno db with
| Some owner -> Lwt_io.printf "%s is owned by %s.\n" frameno owner
| None -> Lwt_io.printf "%s is not registered.\n" frameno in
show_owner "BIKE-0003" >>
show_owner "BIKE-0042" >>
find_bike_owner frameno db >>= function
| Some owner -> Lwt_io.printf "%s is owned by %s.\n" frameno owner
| None -> Lwt_io.printf "%s is not registered.\n" frameno in
show_owner "BIKE-0003" >>= fun () ->
show_owner "BIKE-0042" >>= fun () ->

(* An example multi-row query. *)
Lwt_io.printf "Stolen:" >>
Lwt_io.printf "Stolen:" >>= fun () ->
iter_s_stolen db
(fun ~frameno ~owner ?stolen () ->
let stolen = match stolen with Some x -> x | None -> assert false in
Expand Down
4 changes: 0 additions & 4 deletions tests/jbuild
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
(executable
((name test_pool_lwt)
(modules (Test_pool_lwt))
(preprocess (pps (lwt.ppx)))
(libraries (caqti_lwt testkit))))

; Tests Using DBs
Expand All @@ -35,7 +34,6 @@
(executable
((name bikereg)
(modules (Bikereg))
(preprocess (pps (lwt.ppx)))
(libraries (caqti caqti_lwt testkit))))

(alias
Expand All @@ -56,7 +54,6 @@
(executable
((name test_parallel_lwt)
(modules (Test_parallel_lwt))
(preprocess (pps (lwt.ppx)))
(libraries (caqti caqti_lwt testkit))))

(alias
Expand All @@ -67,7 +64,6 @@
(executable
((name test_sql_lwt)
(modules (Test_sql_lwt))
(preprocess (pps (lwt.ppx)))
(libraries (caqti caqti_lwt testkit))))

; Fake META Files
Expand Down
10 changes: 5 additions & 5 deletions tests/test_parallel_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ let do_query =
Caqti_lwt.Pool.use @@ fun (module C : Caqti_lwt.CONNECTION) ->
(match Random.int 4 with
| 0 ->
C.exec insert_q C.Param.[|int (random_int ()); int (random_int ())|] >>
C.exec insert_q C.Param.[|int (random_int ()); int (random_int ())|] >>= fun () ->
Lwt.return 0
| 1 ->
C.exec delete_q C.Param.[|int (random_int ())|] >>
C.exec delete_q C.Param.[|int (random_int ())|] >>= fun () ->
Lwt.return 0
| 2 ->
C.fold select_1_q C.Tuple.(fun t -> (+) (int 0 t))
Expand Down Expand Up @@ -84,10 +84,10 @@ let () =
let pool = Caqti_lwt.connect_pool ~max_size uri in
Caqti_lwt.Pool.use
(fun (module C : Caqti_lwt.CONNECTION) ->
C.exec drop_q [||] >>
C.exec drop_q [||] >>= fun () ->
C.exec create_q [||])
pool >>
(test2 pool !n_r >|= ignore) >>
pool >>= fun () ->
(test2 pool !n_r >|= ignore) >>= fun () ->
Caqti_lwt.Pool.use
(fun (module C : Caqti_lwt.CONNECTION) ->
C.exec drop_q [||])
Expand Down
16 changes: 8 additions & 8 deletions tests/test_pool_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ let test n =

let () =
Lwt_main.run begin
test 0 >>
test 1 >>
test 3 >>
test 8 >>
test 12 >>
test 16 >>
test 64 >>
test 128 >>
test 0 >>= fun () ->
test 1 >>= fun () ->
test 3 >>= fun () ->
test 8 >>= fun () ->
test 12 >>= fun () ->
test 16 >>= fun () ->
test 64 >>= fun () ->
test 128 >>= fun () ->
test 1024
end
Loading

0 comments on commit 33f33b3

Please sign in to comment.