From d1047791e9e25bc5a30ffc62f31c3d898843f042 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Thu, 19 Nov 2026 11:57:17 +0100 Subject: Insert list data --- lib/query/insert.ml | 99 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 65 insertions(+), 34 deletions(-) (limited to 'lib/query/insert.ml') diff --git a/lib/query/insert.ml b/lib/query/insert.ml index ab0dc2b..40887e8 100644 --- a/lib/query/insert.ml +++ b/lib/query/insert.ml @@ -5,16 +5,14 @@ let collect_fields : ?init_fields:Model.field list -> ?filter:(Model.field -> bool) -> - Model.content -> + Model.field list -> Intf.t = - fun ?(init_fields = []) ?filter content -> - let field_seq = - match filter with - | None -> Model.Content.fields content - | Some f -> Model.Content.fields content |> Seq.filter f + fun ?(init_fields = []) ?filter fields -> + let fields = + match filter with None -> fields | Some f -> List.filter f fields in - let fields = init_fields @ List.of_seq field_seq in + let fields = init_fields @ fields in Encoder.list_to_tup fields ~f:(fun field -> let name = Model.Field.name field in @@ -25,7 +23,8 @@ module MultiReferencesInsert = struct type 'a t = { db : Caqti_lwt.connection; parent_id : int64; - content : Model.content; + table : Model.Identifier.t; + (** The table holding the row: a content table, or a list *) provider : string -> Model.value; k : (unit, Caqti.Error.t) result Lwt.t; } @@ -57,7 +56,7 @@ module MultiReferencesInsert = struct (** The function is called for each feature declared as multi reference. *) let f : type any. 'a t -> any Intf.data -> 'a t = - fun ({ db; parent_id; provider; k; content } as acc) + fun ({ db; parent_id; provider; k; table } as acc) Intf.{ name; content_type } -> let module Db = (val db) in let module Identifier = Model.Identifier in @@ -77,8 +76,7 @@ module MultiReferencesInsert = struct Croquette_common.Format.format_string (fun format -> Format.fprintf format "INSERT INTO %s (%s, %s) VALUES (?, ?)" - (Identifier.quote - (Identifier.dot (Model.Content.id content) name)) + (Identifier.quote (Identifier.dot table name)) parent target) in let request : request = @@ -101,31 +99,26 @@ end module Fold = Encoder.Fold (MultiReferencesInsert) -(** Insert a row. This syntax use RETURNING ID to get the value we just inserted -*) -let insert_returning : +(** Insert a row in the given table, then insert the multivalued fields in their + dedicated tables. This syntax uses RETURNING ID to get the value we just + inserted *) +let insert_row : Caqti_lwt.connection -> - Model.content -> + table:Model.Identifier.t -> + ?init_fields:Model.field list -> + Model.field list -> (string -> Model.value) -> int64 Lwt.t = - fun db content provider -> + fun db ~table ?(init_fields = []) fields provider -> let module Db = (val db) in - let init_fields = - match Model.Content.parent content with - | None -> [] - | Some _ -> - Model.Field. - [ create (T Integer) [] ~required:true Model.Identifier.parent ] - in - - (* Remove the mulivaluated content fields from the request because they are + (* Remove the mulivaluated fields from the request because they are not inserted in the table directly *) let (T t) = - collect_fields content + collect_fields ~init_fields ~filter:(fun field -> let (T ty) = Model.Field.type_ field in match ty with Model.Typ.MultiReference _ -> false | _ -> true) - ~init_fields + fields in let insert_template = match t with @@ -141,8 +134,7 @@ let insert_returning : let q = Croquette_common.Format.format_string (fun format -> Format.fprintf format "INSERT INTO %s %s VALUES %s RETURNING ID" - (Identifier.quote (Model.Content.id content)) - columns insert_template) + (Identifier.quote table) columns insert_template) in let types_param = (Encoder.CaqtiRequest.f ()) t in let request = Caqti.Templater.(direct T.(types_param -->! int64) q) in @@ -152,24 +144,41 @@ let insert_returning : (* 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. *) let* id_result = Db.find request values in - let* content_id = Caqti_lwt.or_fail id_result in + let* row_id = Caqti_lwt.or_fail id_result in (* The value is created in the db, reuse the ID in order to insert the multi-values fields in their own tables *) let (T multi_valuated_fields) = - collect_fields content ~filter:(fun field -> + collect_fields fields ~filter:(fun field -> let (T ty) = Model.Field.type_ field in match ty with Model.Typ.MultiReference _ -> true | _ -> false) in let acc = MultiReferencesInsert. - { db; provider; k = Lwt.return_ok (); parent_id = content_id; content } + { db; provider; k = Lwt.return_ok (); parent_id = row_id; table } in let MultiReferencesInsert.{ k; _ } = Fold.f acc multi_valuated_fields in let* res = k in let* () = Caqti_lwt.or_fail res in - Lwt.return content_id + Lwt.return row_id + +let insert_returning : + Caqti_lwt.connection -> + Model.content -> + (string -> Model.value) -> + int64 Lwt.t = + fun db content provider -> + let init_fields = + match Model.Content.parent content with + | None -> [] + | Some _ -> + Model.Field. + [ create (T Integer) [] ~required:true Model.Identifier.parent ] + in + insert_row db ~table:(Model.Content.id content) ~init_fields + (List.of_seq (Model.Content.fields content)) + provider (** Insert an entry for a specific content *) let insert : @@ -198,6 +207,22 @@ let insert : insert_returning db content provider | _ -> raise Not_found +(** Insert a row in the dedicated table of a list, and return its id. + + Like [insert], the [provider] is called with the name of each field of the + list to get its value. Multivalued fields are inserted in their own + dedicated table, with the parent pointing to the list row. *) +let insert_list : + Caqti_lwt.connection -> Model.list -> (string -> Model.value) -> int64 Lwt.t + = + fun db list provider -> + let module Db = (val db) in + match Db.dialect with + | Caqti.Template.Dialect.(Sqlite _ | Pgsql _) -> + insert_row db ~table:list.Model.DataList.id list.Model.DataList.fields + provider + | _ -> raise Not_found + let insert_references : Caqti_lwt.connection -> Model.content -> @@ -213,7 +238,13 @@ let insert_references : in let acc = MultiReferencesInsert. - { db; provider; k = Lwt.return_ok (); parent_id = content_id; content } + { + db; + provider; + k = Lwt.return_ok (); + parent_id = content_id; + table = Model.Content.id content; + } in let ( let* ) = Lwt.bind in let MultiReferencesInsert.{ k; _ } = MultiReferencesInsert.f acc data in -- cgit v1.2.3