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 | |
| parent | fa4970bf1b74f3fa0b2f63cbd461d98c251dc0f0 (diff) | |
Refactoring
Diffstat (limited to 'lib/query')
| -rw-r--r-- | lib/query/insert.ml | 58 | ||||
| -rw-r--r-- | lib/query/update.ml | 51 |
2 files changed, 54 insertions, 55 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 diff --git a/lib/query/update.ml b/lib/query/update.ml index ec1510b..5b53cb7 100644 --- a/lib/query/update.ml +++ b/lib/query/update.ml @@ -78,7 +78,8 @@ module ReferencesSet = Set.Make (Int64) let update_mulivaluated_field : Caqti_lwt.connection -> Model.content -> - (Model.Identifier.t * Model.target Model.Typ.multireference * int64 Seq.t) list -> + (Model.Identifier.t * Model.target Model.Typ.multireference * int64 Seq.t) + list -> int64 -> unit Lwt.t = fun db content fields id -> @@ -165,33 +166,33 @@ let q : Seq.fold_left (fun ((fields, references) as acc : (Intf.t_type * (string * Value.value)) list - * (Model.Identifier.t * Model.target Model.Typ.multireference * int64 Seq.t) list) - field -> + * (Model.Identifier.t + * Model.target Model.Typ.multireference + * int64 Seq.t) + list) field -> let name = Model.Field.name field in let (Model.Field.T content_type) = Model.Field.type_ field in let tt = Intf.TT { name; content_type } in - let value = provider (Model.Identifier.to_string name) in - match value with - | Value.Data.Any d -> ( - match content_type with - | Model.Typ.MultiReference _ -> ( - match d with - | Value.Data.None -> acc - | Value.Data.V (t, seq) -> ( - match - ( Model.Typ.eq content_type t, - Model.Typ.eq_ocaml content_type t ) - with - | Some Type.Equal, Some Type.Equal -> - (fields, (name, t, seq) :: references) - | _ -> raise Encoder.TypeMismatch)) - | _ -> ( - match d with - | Value.Data.None -> acc - | Value.Data.V _ as dv -> - ( (tt, (Model.Identifier.to_string name, Value.Data.any dv)) - :: fields, - references )))) + let (Value.Data.Any d) = provider (Model.Identifier.to_string name) in + match content_type with + | Model.Typ.MultiReference _ -> ( + match d with + | Value.Data.None -> acc + | Value.Data.V (t, seq) -> ( + match + ( Model.Typ.eq content_type t, + Model.Typ.eq_ocaml content_type t ) + with + | Some Type.Equal, Some Type.Equal -> + (fields, (name, t, seq) :: references) + | _ -> raise Encoder.TypeMismatch)) + | _ -> ( + match d with + | Value.Data.None -> acc + | Value.Data.V _ as dv -> + ( (tt, (Model.Identifier.to_string name, Value.Data.any dv)) + :: fields, + references ))) ([], []) (Model.Content.fields content) in |
