diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-11-20 18:57:38 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-11-20 18:57:38 +0100 |
| commit | 172a02b80d8dbdcfe09129d7fa76015bb5ec4d79 (patch) | |
| tree | cb8613d53ce8051e39937ff05f8dc9b0fe80320e /lib/query/insert.ml | |
| parent | fa4970bf1b74f3fa0b2f63cbd461d98c251dc0f0 (diff) | |
Refactoring
Diffstat (limited to 'lib/query/insert.ml')
| -rw-r--r-- | lib/query/insert.ml | 58 |
1 files changed, 28 insertions, 30 deletions
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 |
