summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2026-04-04 09:13:20 +0200
committerSébastien Dailly <sebastien@dailly.me>2026-04-04 09:13:20 +0200
commitae4814880d7a2ad9c96e8f4001edf5032b9a7fb2 (patch)
treef56f5858a8ea36a6a5fecd10479997e714aa9855
parent4af7d2f6ff8b38365c97a89c295710f5ed3266f5 (diff)
small refactoring
-rw-r--r--lib/query/encoder.ml85
-rw-r--r--lib/query/insert.ml80
-rw-r--r--lib/query/insert.mli3
-rw-r--r--lib/query/intf.ml3
-rw-r--r--lib/query/tests/insert.t2
5 files changed, 97 insertions, 76 deletions
diff --git a/lib/query/encoder.ml b/lib/query/encoder.ml
index 2f86eab..c9314cb 100644
--- a/lib/query/encoder.ml
+++ b/lib/query/encoder.ml
@@ -5,34 +5,40 @@
open StdLabels
open Intf
+exception TypeMismatch
+(** Exception raised when the given type does not match the value *)
+
module Map (T : Encoder) = struct
- let rec f : type a. a tuple -> a T.t =
- fun t ->
+ let rec f : type a. T.env -> a tuple -> a T.t =
+ fun env t ->
match t with
| T0 -> T.unit
- | T1 t1 -> T.f t1
- | T2 (t1, t2) -> T.t2 (T.f t1) (T.f t2)
- | T3 (t1, t2, t3) -> T.t3 (T.f t1) (T.f t2) (T.f t3)
- | T4 (t1, t2, t3, t4) -> T.t4 (T.f t1) (T.f t2) (T.f t3) (T.f t4)
+ | T1 t1 -> T.f env t1
+ | T2 (t1, t2) -> T.t2 (T.f env t1) (T.f env t2)
+ | T3 (t1, t2, t3) -> T.t3 (T.f env t1) (T.f env t2) (T.f env t3)
+ | T4 (t1, t2, t3, t4) ->
+ T.t4 (T.f env t1) (T.f env t2) (T.f env t3) (T.f env t4)
| T5 (t1, t2, t3, t4, t5) ->
- T.t5 (T.f t1) (T.f t2) (T.f t3) (T.f t4) (T.f t5)
+ T.t5 (T.f env t1) (T.f env t2) (T.f env t3) (T.f env t4) (T.f env t5)
| T6 (t1, t2, t3, t4, t5, t6) ->
- T.t6 (T.f t1) (T.f t2) (T.f t3) (T.f t4) (T.f t5) (T.f t6)
+ T.t6 (T.f env t1) (T.f env t2) (T.f env t3) (T.f env t4) (T.f env t5)
+ (T.f env t6)
| T7 (t1, t2, t3, t4, t5, t6, t7) ->
- T.t7 (T.f t1) (T.f t2) (T.f t3) (T.f t4) (T.f t5) (T.f t6) (T.f t7)
+ T.t7 (T.f env t1) (T.f env t2) (T.f env t3) (T.f env t4) (T.f env t5)
+ (T.f env t6) (T.f env t7)
| T8 (t1, t2, t3, t4, t5, t6, t7, t8) ->
- T.t8 (T.f t1) (T.f t2) (T.f t3) (T.f t4) (T.f t5) (T.f t6) (T.f t7)
- (T.f t8)
+ T.t8 (T.f env t1) (T.f env t2) (T.f env t3) (T.f env t4) (T.f env t5)
+ (T.f env t6) (T.f env t7) (T.f env t8)
| T9 (t1, t2, t3, t4, t5, t6, t7, t8, t9) ->
- T.t9 (T.f t1) (T.f t2) (T.f t3) (T.f t4) (T.f t5) (T.f t6) (T.f t7)
- (T.f t8) (T.f t9)
+ T.t9 (T.f env t1) (T.f env t2) (T.f env t3) (T.f env t4) (T.f env t5)
+ (T.f env t6) (T.f env t7) (T.f env t8) (T.f env t9)
| T10 (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) ->
- T.t10 (T.f t1) (T.f t2) (T.f t3) (T.f t4) (T.f t5) (T.f t6) (T.f t7)
- (T.f t8) (T.f t9) (T.f t10)
+ T.t10 (T.f env t1) (T.f env t2) (T.f env t3) (T.f env t4) (T.f env t5)
+ (T.f env t6) (T.f env t7) (T.f env t8) (T.f env t9) (T.f env t10)
| T_N (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t_n) ->
- let tl = f t_n in
- T.t_n (T.f t1) (T.f t2) (T.f t3) (T.f t4) (T.f t5) (T.f t6) (T.f t7)
- (T.f t8) (T.f t9) (T.f t10) tl
+ let tl = f env t_n in
+ T.t_n (T.f env t1) (T.f env t2) (T.f env t3) (T.f env t4) (T.f env t5)
+ (T.f env t6) (T.f env t7) (T.f env t8) (T.f env t9) (T.f env t10) tl
end
module Fold (T : sig
@@ -200,9 +206,12 @@ let rec list_to_tup : f:('a -> t_type) -> 'a list -> t =
module CaqtiRequest = Map (struct
include Caqti_type
+ type env = unit
+
let t_n = Caqti_type.t11
- let f : type a. a data -> a Caqti_type.t = function
+ let f : type a. unit -> a data -> a Caqti_type.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
@@ -243,10 +252,12 @@ module ConcatParam = struct
String.concat ~sep [ a; b; c; d; e; f; g; h; i; j; k ]
end
-module EncodeInsert = Map (struct
+module EncodeParameter = Map (struct
type 'a t = string
+ type env = unit
- let f : type a. a data -> a t = function
+ let f : type a. unit -> a data -> a t =
+ fun () -> function
| { content_type = Text; _ } -> "?"
| { content_type = Integer; _ } -> "?"
| { content_type = MonoReference _; _ } -> "?"
@@ -258,8 +269,10 @@ end)
module InsertTableName = Map (struct
type 'a t = string
+ type env = unit
- let f : type a. a data -> a t = function
+ let f : type a. unit -> a data -> a t =
+ fun () -> function
| { name = column; content_type = Text; _ } -> column
| { name = column; content_type = Integer; _ } -> column
| { name = column; content_type = MultiReference _; _ } -> column
@@ -267,3 +280,31 @@ module InsertTableName = Map (struct
include ConcatParam
end)
+
+(** Extract the value for caqti from the type [Intf.data] and a function mapping
+ each key to the value.
+
+ This module only care about values which can fit in a single column, and
+ will raise an error if a sequence of values is provided. *)
+module DataToCaqtiValues = Map (struct
+ include DefaultEncoder
+
+ type 'a t = 'a
+ type env = string -> Model.Reference.t Model.Typ.value
+
+ let f : type a. env -> a Intf.data -> a t =
+ fun provider Intf.{ name; content_type } ->
+ let result = provider name in
+ (* Check the value given by the provider and ensure it’s match the
+ expected type *)
+ match result with
+ | Model.Typ.Seq _ -> raise TypeMismatch
+ | Model.Typ.None ->
+ (* The provider does not have any value for this property, use the
+ default value*)
+ Model.Typ.default_value content_type
+ | Model.Typ.V (field_type', value) -> (
+ match Model.Typ.eq content_type field_type' with
+ | Some Equal -> value
+ | None -> raise TypeMismatch)
+end)
diff --git a/lib/query/insert.ml b/lib/query/insert.ml
index b6aee0f..1ee1549 100644
--- a/lib/query/insert.ml
+++ b/lib/query/insert.ml
@@ -1,8 +1,5 @@
(** Generate the insert queries for the different database *)
-exception TypeMismatch
-(** Exception raised when the given type does not match the value *)
-
(** Transform the list of fields from the model into a tuple with the type of
each field associated with *)
let collect_fields :
@@ -24,6 +21,15 @@ let collect_fields :
let (Model.Field.T content_type) = Model.Field.type_ field in
TT { name; content_type })
+let format_string : (Format.formatter -> unit) -> string =
+ fun f ->
+ let buff = Buffer.create 16 in
+ let format = Format.formatter_of_buffer buff in
+ let () = f format in
+
+ Format.pp_print_flush format ();
+ Buffer.contents buff
+
module MultiReferencesInsert = struct
type 'a t = {
db : Caqti_lwt.connection;
@@ -68,24 +74,22 @@ module MultiReferencesInsert = struct
let result = provider name in
match result with
| Model.Typ.None -> acc
- | Model.Typ.V _ -> raise TypeMismatch
+ | Model.Typ.V _ -> raise Encoder.TypeMismatch
| Model.Typ.Seq (field_type', values) -> (
match Model.Typ.eq content_type field_type' with
- | None -> raise TypeMismatch
+ | None -> raise Encoder.TypeMismatch
| Some Equal ->
- let buff = Buffer.create 16 in
- let format = Format.formatter_of_buffer buff in
- let () =
- Format.fprintf format
- "INSERT INTO \"%s.%s\" (\"parent\", \"target\") VALUES (?, \
- ?)"
- (Model.Content.id content) name
+ let q =
+ format_string (fun format ->
+ Format.fprintf format
+ "INSERT INTO \"%s.%s\" (\"parent\", \"target\") VALUES \
+ (?, ?)"
+ (Model.Content.id content) name)
in
- Format.pp_print_flush format ();
let request : request =
Caqti_request.Infix.(
Caqti_type.(t2 int64 int64 ->. Caqti_type.unit))
- (Buffer.contents buff)
+ q
in
let k =
(* Successively insert all the values in the dedicated table *)
@@ -97,7 +101,7 @@ module MultiReferencesInsert = struct
in
{ acc with k }))
- | _ -> raise TypeMismatch
+ | _ -> raise Encoder.TypeMismatch
end
module Fold = Encoder.Fold (MultiReferencesInsert)
@@ -111,27 +115,6 @@ let insert_returning :
int64 Lwt.t =
fun db content provider ->
let module Db = (val db) in
- let module CaqtiValues = Encoder.Map (struct
- include Encoder.DefaultEncoder
-
- type 'a t = 'a
-
- let f : type a. a Intf.data -> a t =
- fun Intf.{ name; content_type } ->
- let result = provider name in
- (* Check the value given by the provider and ensure it’s match
- the expected type *)
- match result with
- | Model.Typ.Seq _ -> raise TypeMismatch
- | Model.Typ.None ->
- (* The provider does not have any value for this property, use the
- default value*)
- Model.Typ.default_value content_type
- | Model.Typ.V (field_type', value) -> (
- match Model.Typ.eq content_type field_type' with
- | Some Equal -> value
- | None -> raise TypeMismatch)
- end) in
let init_fields =
match Model.Content.parent content with
| None -> []
@@ -148,27 +131,26 @@ let insert_returning :
~init_fields
in
let insert_template =
- match t with Intf.T0 -> "" | _ -> "(" ^ Encoder.EncodeInsert.f t ^ ")"
+ match t with
+ | Intf.T0 -> ""
+ | _ -> "(" ^ (Encoder.EncodeParameter.f ()) t ^ ")"
and columns =
match t with
| Intf.T0 -> "DEFAULT"
- | _ -> "(" ^ Encoder.InsertTableName.f t ^ ")"
+ | _ -> "(" ^ (Encoder.InsertTableName.f ()) t ^ ")"
in
- let buff = Buffer.create 16 in
- let format = Format.formatter_of_buffer buff in
- let () =
- Format.fprintf format "INSERT INTO \"%s\" %s VALUES %s RETURNING ID"
- (Model.Content.id content) columns insert_template
+ let q =
+ format_string (fun format ->
+ Format.fprintf format "INSERT INTO \"%s\" %s VALUES %s RETURNING ID"
+ (Model.Content.id content) columns insert_template)
in
- Format.pp_print_flush format ();
- let types_param = Encoder.CaqtiRequest.f t in
+ let types_param = (Encoder.CaqtiRequest.f ()) t in
let request =
- Caqti_request.Infix.((types_param ->! Caqti_type.int64) ~oneshot:true)
- (Buffer.contents buff)
+ Caqti_request.Infix.((types_param ->! Caqti_type.int64) ~oneshot:true) q
in
let ( let* ) = Lwt.bind in
- let values = CaqtiValues.f t in
+ let values = (Encoder.DataToCaqtiValues.f provider) t in
(* The insert request is returning the ID. That’s why we call it with the
fuction [find] even if it is not a real find in the database. *)
@@ -206,7 +188,7 @@ let insert :
| None, Some _ ->
(* Raise an error right now if the parent of the subcontent is not
given *)
- raise TypeMismatch
+ raise Encoder.TypeMismatch
| Some v, Some _ -> (
(* Otherwise update the provider to give a value for the parent *)
function
diff --git a/lib/query/insert.mli b/lib/query/insert.mli
index 63e16a6..f19ff11 100644
--- a/lib/query/insert.mli
+++ b/lib/query/insert.mli
@@ -1,8 +1,5 @@
(** Low level insertion in the database *)
-exception TypeMismatch
-(** Exception raised when the given type does not match the value *)
-
val insert :
Caqti_lwt.connection ->
Model.Reference.t Model.Content.t ->
diff --git a/lib/query/intf.ml b/lib/query/intf.ml
index 10e78b0..4c09eef 100644
--- a/lib/query/intf.ml
+++ b/lib/query/intf.ml
@@ -75,8 +75,9 @@ type t = T : _ tuple -> t [@@unboxed]
module type Encoder = sig
type 'a t
+ type env
- val f : 'a data -> 'a t
+ val f : env -> 'a data -> 'a t
val unit : unit t
val t2 : 'a t -> 'b t -> ('a * 'b) t
val t3 : 'a t -> 'b t -> 'c t -> ('a * 'b * 'c) t
diff --git a/lib/query/tests/insert.t b/lib/query/tests/insert.t
index af74436..578dae4 100644
--- a/lib/query/tests/insert.t
+++ b/lib/query/tests/insert.t
@@ -46,7 +46,7 @@ This error is expected to be checked and reported.
> ]
> }
> EOF
- Fatal error: exception Query__Insert.TypeMismatch
+ Fatal error: exception Query__Encoder.TypeMismatch
[2]
$ tests -insert <<EOF