From ae4814880d7a2ad9c96e8f4001edf5032b9a7fb2 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Sat, 4 Apr 2026 09:13:20 +0200 Subject: small refactoring --- lib/query/insert.ml | 80 +++++++++++++++++++++-------------------------------- 1 file changed, 31 insertions(+), 49 deletions(-) (limited to 'lib/query/insert.ml') 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 -- cgit v1.2.3