From 172a02b80d8dbdcfe09129d7fa76015bb5ec4d79 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Fri, 20 Nov 2026 18:57:38 +0100 Subject: Refactoring --- lib/query/insert.ml | 58 ++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 30 deletions(-) (limited to 'lib/query/insert.ml') diff --git a/lib/query/insert.ml b/lib/query/insert.ml index 01b2dae..703d5a9 100644 --- a/lib/query/insert.ml +++ b/lib/query/insert.ml @@ -64,37 +64,35 @@ module MultiReferencesInsert = struct and target = Identifier.quote Identifier.target in match content_type with | Model.Typ.MultiReference _target -> ( - let result = provider (Model.Identifier.to_string name) in - match result with - | Value.Data.Any d -> ( - match d with - | Value.Data.None -> acc - | Value.Data.V (field_type', values) -> ( - match Model.Typ.eq_ocaml content_type field_type' with - | None -> raise Encoder.TypeMismatch - | Some Equal -> - let q = - Croquette_common.Format.format_string (fun format -> - Format.fprintf format - "INSERT INTO %s (%s, %s) VALUES (?, ?)" - (Identifier.quote (Identifier.dot table name)) - parent target) - in - let request : request = - Caqti.Templater.(dynamic T.(t2 int64 int64 -->. unit) q) - in - let k = - (* Successively insert all the values in the dedicated table *) - let* res = - Seq.fold_left (accumulate db request parent_id) k values - in - let* () = Caqti_lwt.or_fail res in - let* res = Db.deallocate request in - let* () = Caqti_lwt.or_fail res in - Lwt.return res - in + let (Value.Data.Any d) = provider (Model.Identifier.to_string name) in + match d with + | Value.Data.None -> acc + | Value.Data.V (field_type', values) -> ( + match Model.Typ.eq_ocaml content_type field_type' with + | None -> raise Encoder.TypeMismatch + | Some Equal -> + let q = + Croquette_common.Format.format_string (fun format -> + Format.fprintf format + "INSERT INTO %s (%s, %s) VALUES (?, ?)" + (Identifier.quote (Identifier.dot table name)) + parent target) + in + let request : request = + Caqti.Templater.(dynamic T.(t2 int64 int64 -->. unit) q) + in + let k = + (* Successively insert all the values in the dedicated table *) + let* res = + Seq.fold_left (accumulate db request parent_id) k values + in + let* () = Caqti_lwt.or_fail res in + let* res = Db.deallocate request in + let* () = Caqti_lwt.or_fail res in + Lwt.return res + in - { acc with k }))) + { acc with k })) | _ -> raise Encoder.TypeMismatch end -- cgit v1.2.3