diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-11-20 18:47:26 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-11-20 18:47:26 +0100 |
| commit | fa4970bf1b74f3fa0b2f63cbd461d98c251dc0f0 (patch) | |
| tree | 577a41415e67510f08f43fc48c794c36a10f6e80 /lib/query/insert.ml | |
| parent | b05e2d2be7be28ee6c577671002797cf9abaf177 (diff) | |
Updated the model for representing ocaml types in the application type
Diffstat (limited to 'lib/query/insert.ml')
| -rw-r--r-- | lib/query/insert.ml | 65 |
1 files changed, 34 insertions, 31 deletions
diff --git a/lib/query/insert.ml b/lib/query/insert.ml index 7b7619e..01b2dae 100644 --- a/lib/query/insert.ml +++ b/lib/query/insert.ml @@ -55,7 +55,7 @@ module MultiReferencesInsert = struct acc (** The function is called for each feature declared as multi reference. *) - let f : type any. 'a t -> any Intf.data -> 'a t = + let f : type any b. 'a t -> (any, b) Intf.data -> 'a t = fun ({ db; parent_id; provider; k; table } as acc) Intf.{ name; content_type } -> let module Db = (val db) in @@ -69,33 +69,32 @@ module MultiReferencesInsert = struct | Value.Data.Any d -> ( match d with | Value.Data.None -> acc - | Value.Data.V _ -> raise Encoder.TypeMismatch - | Value.Data.Seq (field_type', values) -> ( - match Model.Typ.eq 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 + | 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 @@ -203,7 +202,11 @@ let insert : raise Encoder.TypeMismatch | Some v, Some _ -> ( (* Otherwise update the provider to give a value for the parent *) - let parent_id = Value.id v in + let parent_id = + match Value.get v Value.id_ref with + | Some parent_id -> parent_id + | None -> raise Encoder.TypeMismatch + in function | "parent" -> Value.single (Model.Typ.Integer, Some parent_id) | other -> provider other) @@ -227,13 +230,13 @@ let insert_list : fun db list provider -> let module Db = (val db) in match Db.dialect with - | Caqti.Template.Dialect.(Sqlite _ | Pgsql _) -> ( + | Caqti.Template.Dialect.(Sqlite _ | Pgsql _) -> let ( let* ) = Lwt.bind in let* id = insert_row db ~table:list.Model.DataList.id list.Model.DataList.fields provider in - Lwt.return (Value.v list id)) + Lwt.return (Value.v list id) | _ -> raise Not_found let insert_references : @@ -242,7 +245,7 @@ let insert_references : Int64.t -> Model.Identifier.t -> Value.value -> - Int64.t Intf.data -> + (Int64.t, _) Intf.data -> unit Lwt.t = fun db content content_id field_name value data -> let provider : string -> Value.value = function |
