summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2026-11-18 18:21:13 +0100
committerSébastien Dailly <sebastien@dailly.me>2026-11-18 18:21:13 +0100
commit7087a0c2c84e796e474784cbd28d4a4e574cf2c0 (patch)
tree2cf3d4be465830726668f344378dc1771f57168c
parent7b5baa29818c9fe6e33d50c8458aa2824274158e (diff)
Updated to latest Caqti library
-rw-r--r--lib/query/create.ml21
-rw-r--r--lib/query/encoder.ml15
-rw-r--r--lib/query/insert.ml21
-rw-r--r--lib/query/update.ml14
-rw-r--r--test/common.ml16
-rw-r--r--test/schema.ml2
6 files changed, 40 insertions, 49 deletions
diff --git a/lib/query/create.ml b/lib/query/create.ml
index ec1fb21..d34339b 100644
--- a/lib/query/create.ml
+++ b/lib/query/create.ml
@@ -79,9 +79,8 @@ let create_external_reference_table :
Format.pp_print_flush format ();
let request_str = Buffer.contents buff in
let request =
- Caqti_request.Infix.(
- (Caqti_type.unit ->. Caqti_type.unit) ~oneshot:true)
- request_str
+ let open Caqti.Templater in
+ direct T.(unit -->. unit) request_str
in
let module Db = (val db) in
let* result = Db.exec request () in
@@ -175,8 +174,8 @@ let rec create_table :
Format.pp_print_flush format ();
let request_str = Buffer.contents buff in
let request =
- Caqti_request.Infix.((Caqti_type.unit ->. Caqti_type.unit) ~oneshot:true)
- request_str
+ let open Caqti.Templater in
+ direct T.(unit -->. unit) request_str
in
let module Db = (val db) in
let* result = Db.exec request () in
@@ -202,10 +201,8 @@ let add_foreign_statement : Caqti_lwt.connection -> foreign list -> unit Lwt.t =
"ALTER TABLE ? ADD CONSTRAINT FOREIGN KEY(?) REFERENCES ?(?)"
in
let request =
- Caqti_request.Infix.(
- (Caqti_type.(t4 string string string string) ->. Caqti_type.unit)
- ~oneshot:true)
- request_str
+ let open Caqti.Templater in
+ direct T.(t4 string string string string -->. unit) request_str
in
let module Db = (val db) in
let* () =
@@ -238,8 +235,8 @@ let create : Caqti_lwt.connection -> Configuration.Ast.setting -> unit Lwt.t =
| None -> create_table db acc content)
([], []) contents
in
- match Caqti_driver_info.dialect_tag Db.driver_info with
- | `Sqlite ->
+ match Db.dialect with
+ | Caqti.Template.Dialect.Sqlite _ ->
(* There is no way to add a foreign key to an existing table in sqlite,
so we just stop the table creation here.
@@ -248,4 +245,4 @@ let create : Caqti_lwt.connection -> Configuration.Ast.setting -> unit Lwt.t =
*)
ignore foreign;
Lwt.return_unit
- | `Pgsql | `Mysql | _ -> add_foreign_statement db foreign
+ | _ -> add_foreign_statement db foreign
diff --git a/lib/query/encoder.ml b/lib/query/encoder.ml
index ae34c3e..6897ec7 100644
--- a/lib/query/encoder.ml
+++ b/lib/query/encoder.ml
@@ -204,19 +204,18 @@ let rec list_to_tup : f:('a -> t_type) -> 'a list -> t =
T (T_N (v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, tl))
module CaqtiRequest = Map (struct
- include Caqti_type
+ include Caqti.Template.Row_type
type env = unit
- let t_n = Caqti_type.t11
+ let t_n = t11
- let f : type a. unit -> a data -> a Caqti_type.t =
+ let f : type a. unit -> a data -> a t =
fun () -> function
- | { content_type = Text; _ } -> Caqti_type.option @@ Caqti_type.string
- | { content_type = Integer; _ } -> Caqti_type.option @@ Caqti_type.int64
- | { content_type = MultiReference _; _ } -> Caqti_type.int64
- | { content_type = MonoReference _; _ } ->
- Caqti_type.option @@ Caqti_type.int64
+ | { content_type = Text; _ } -> option string
+ | { content_type = Integer; _ } -> option int64
+ | { content_type = MultiReference _; _ } -> int64
+ | { content_type = MonoReference _; _ } -> option int64
end)
module DefaultEncoder = struct
diff --git a/lib/query/insert.ml b/lib/query/insert.ml
index ecb0efb..c124f0a 100644
--- a/lib/query/insert.ml
+++ b/lib/query/insert.ml
@@ -27,10 +27,10 @@ module MultiReferencesInsert = struct
parent_id : int64;
content : Model.Reference.t Model.Content.t;
provider : string -> Model.Reference.t Model.Typ.value;
- k : (unit, Caqti_error.t) result Lwt.t;
+ k : (unit, Caqti.Error.t) result Lwt.t;
}
- type request = (int64 * int64, unit, [ `Zero ]) Caqti_request.t
+ type request = (int64 * int64, unit, [ `Zero ]) Caqti.Template.Request.t
(** Helper for the request insert signature *)
let ( let* ) = Lwt.bind
@@ -39,9 +39,9 @@ module MultiReferencesInsert = struct
Caqti_lwt.connection ->
request ->
int64 ->
- (unit, [> Caqti_error.t ]) result Lwt.t ->
+ (unit, [> Caqti.Error.t ]) result Lwt.t ->
int64 ->
- (unit, [> Caqti_error.t ]) result Lwt.t =
+ (unit, [> Caqti.Error.t ]) result Lwt.t =
fun db request parent k value ->
let module Db = (val db) in
let acc =
@@ -78,9 +78,7 @@ module MultiReferencesInsert = struct
(Model.Content.id content) name)
in
let request : request =
- Caqti_request.Infix.(
- Caqti_type.(t2 int64 int64 ->. Caqti_type.unit))
- q
+ Caqti.Templater.(dynamic T.(t2 int64 int64 -->. unit) q)
in
let k =
(* Successively insert all the values in the dedicated table *)
@@ -139,9 +137,7 @@ let insert_returning :
(Model.Content.id content) columns insert_template)
in
let types_param = (Encoder.CaqtiRequest.f ()) t in
- let request =
- Caqti_request.Infix.((types_param ->! Caqti_type.int64) ~oneshot:true) q
- in
+ let request = Caqti.Templater.(direct T.(types_param -->! int64) q) in
let ( let* ) = Lwt.bind in
let values = (Encoder.DataToCaqtiValues.f provider) t in
@@ -189,8 +185,9 @@ let insert :
| other -> provider other)
in
let module Db = (val db) in
- match Caqti_driver_info.dialect_tag Db.driver_info with
- | `Sqlite | `Pgsql -> insert_returning db content provider
+ match Db.dialect with
+ | Caqti.Template.Dialect.(Sqlite _ | Pgsql _) ->
+ insert_returning db content provider
| _ -> raise Not_found
let insert_references :
diff --git a/lib/query/update.ml b/lib/query/update.ml
index 4b8adfb..6d20999 100644
--- a/lib/query/update.ml
+++ b/lib/query/update.ml
@@ -31,16 +31,14 @@ let update_fields :
in
let format_param = (UpdateTemplate.f ()) query_fields in
let types_param =
- Caqti_type.(t2 ((Encoder.CaqtiRequest.f ()) query_fields) int64)
+ Caqti.Templater.T.(t2 (Encoder.CaqtiRequest.f () query_fields) int64)
in
let q =
Croquette_common.Format.format_string (fun format ->
Format.fprintf format "UPDATE \"%s\" SET %s WHERE ID = ?"
(Model.Content.id content) format_param)
in
- let request =
- Caqti_request.Infix.((types_param ->. Caqti_type.unit) ~oneshot:true) q
- in
+ let request = Caqti.Templater.(direct T.(types_param -->. unit) q) in
let query_param =
Encoder.DataToCaqtiValues.f
(fun name -> List.assoc name value_list)
@@ -104,9 +102,7 @@ let update_mulivaluated_field :
in
let caqti_param = Encoder.CaqtiRequest.f () existing_references_t in
let request_delete =
- Caqti_request.Infix.(
- (Caqti_type.(t2 int64 caqti_param) ->. Caqti_type.unit) ~oneshot:true)
- q_delete
+ Caqti.Templater.(direct T.(t2 int64 caqti_param -->. unit) q_delete)
in
let seq = ref (ReferencesSet.to_seq values) in
let* result =
@@ -122,9 +118,7 @@ let update_mulivaluated_field :
(Model.Content.id content) field_name)
in
let request_select =
- Caqti_request.Infix.(
- (Caqti_type.int64 ->* Caqti_type.int64) ~oneshot:true)
- q_select
+ Caqti.Templater.(direct T.(int64 -->* int64) q_select)
in
let* request_result = Db.collect_list request_select id in
let* remaining' = Caqti_lwt.or_fail request_result in
diff --git a/test/common.ml b/test/common.ml
index f06d431..0ef5cb3 100644
--- a/test/common.ml
+++ b/test/common.ml
@@ -6,7 +6,11 @@ let ( let* ) = Lwt.bind
configuration *)
let connect : Configuration.Ast.setting -> Caqti_lwt.connection Lwt.t =
fun conf ->
- let* conn = Caqti_lwt_unix.connect (Uri.of_string "sqlite3::memory:") in
+ (* Pin the driver tweaks below 1.8 so that the sqlite3 driver does not turn
+ on PRAGMA foreign_keys: the tests insert references to rows that are not
+ present in the referenced table. *)
+ let config = Caqti.Connect.Config.(set tweaks_version (1, 7) default) in
+ let* conn = Caqti_lwt_unix.connect ~config (Uri.of_string "sqlite3::memory:") in
let* db = Caqti_lwt.or_fail conn in
let module Db = (val db) in
let* () = Query.create db conf in
@@ -14,7 +18,7 @@ let connect : Configuration.Ast.setting -> Caqti_lwt.connection Lwt.t =
(** Load the given content and return the values as a json *)
let content_as_json :
- 'a Model.Content.t -> (int64, string, [ `One ]) Caqti_request.t =
+ 'a Model.Content.t -> (int64, string, [ `One ]) Caqti.Template.Request.t =
fun content ->
let fields =
Model.Content.fields content
@@ -46,15 +50,15 @@ let content_as_json :
in
let q = Buffer.contents buff in
- Caqti_request.Infix.(Caqti_type.(int64) ->! Caqti_type.string)
- ~oneshot:true
+ let open Caqti.Templater in
+ direct T.(int64 -->! string)
("SELECT json_object(" ^ q ^ ") from " ^ Model.Content.id content
^ " where id=?")
let query_values (table : Model.Reference.t Model.Content.t) (feature : string)
=
- Caqti_request.Infix.(Caqti_type.(int64) ->* Caqti_type.string)
- ~oneshot:true
+ let open Caqti.Templater in
+ direct T.(int64 -->* string)
("SELECT json_object('" ^ feature ^ "', target) from \""
^ Model.Content.id table ^ "." ^ feature ^ "\" where parent=?")
diff --git a/test/schema.ml b/test/schema.ml
index b063270..3b52812 100644
--- a/test/schema.ml
+++ b/test/schema.ml
@@ -14,7 +14,7 @@ let ( let* ) = Lwt.bind
let ( let+ ) opt cont = Result.map cont opt
let schema_query =
- Caqti_request.Infix.(Caqti_type.(string) ->! Caqti_type.string)
+ Caqti.Templater.static Caqti.Templater.T.(string -->! string)
"SELECT sql from sqlite_schema where name=?"
let get_schema : Caqti_lwt.connection -> string -> string Lwt.t =