diff options
Diffstat (limited to 'lib/query/insert.ml')
| -rw-r--r-- | lib/query/insert.ml | 63 |
1 files changed, 38 insertions, 25 deletions
diff --git a/lib/query/insert.ml b/lib/query/insert.ml index 40887e8..7b7619e 100644 --- a/lib/query/insert.ml +++ b/lib/query/insert.ml @@ -25,7 +25,7 @@ module MultiReferencesInsert = struct parent_id : int64; table : Model.Identifier.t; (** The table holding the row: a content table, or a list *) - provider : string -> Model.value; + provider : string -> Value.value; k : (unit, Caqti.Error.t) result Lwt.t; } @@ -66,10 +66,12 @@ module MultiReferencesInsert = struct | Model.Typ.MultiReference _target -> ( let result = provider (Model.Identifier.to_string name) in match result with - | Model.Typ.None -> acc - | Model.Typ.V _ -> raise Encoder.TypeMismatch - | Model.Typ.Seq (field_type', values) -> ( - match Model.Typ.eq content_type field_type' with + | 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 = @@ -93,7 +95,7 @@ module MultiReferencesInsert = struct Lwt.return res in - { acc with k })) + { acc with k }))) | _ -> raise Encoder.TypeMismatch end @@ -107,7 +109,7 @@ let insert_row : table:Model.Identifier.t -> ?init_fields:Model.field list -> Model.field list -> - (string -> Model.value) -> + (string -> Value.value) -> int64 Lwt.t = fun db ~table ?(init_fields = []) fields provider -> let module Db = (val db) in @@ -166,8 +168,8 @@ let insert_row : let insert_returning : Caqti_lwt.connection -> Model.content -> - (string -> Model.value) -> - int64 Lwt.t = + (string -> Value.value) -> + Model.content Value.t Lwt.t = fun db content provider -> let init_fields = match Model.Content.parent content with @@ -176,17 +178,21 @@ let insert_returning : 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 + let ( let* ) = Lwt.bind in + let* id = + insert_row db ~table:(Model.Content.id content) ~init_fields + (List.of_seq (Model.Content.fields content)) + provider + in + Lwt.return (Value.v content id) (** Insert an entry for a specific content *) let insert : Caqti_lwt.connection -> Model.content -> - ?parent:int64 -> - (string -> Model.value) -> - int64 Lwt.t = + ?parent:Model.content Value.t -> + (string -> Value.value) -> + Model.content Value.t Lwt.t = fun db content ?parent provider -> let provider = match (parent, Model.Content.parent content) with @@ -197,8 +203,9 @@ 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 function - | "parent" -> Model.Typ.(V (Integer, Some v)) + | "parent" -> Value.single (Model.Typ.Integer, Some parent_id) | other -> provider other) in let module Db = (val db) in @@ -207,20 +214,26 @@ let insert : insert_returning db content provider | _ -> raise Not_found -(** Insert a row in the dedicated table of a list, and return its id. +(** Insert a row in the dedicated table of a list, and return it. 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 - = + Caqti_lwt.connection -> + Model.list -> + (string -> Value.value) -> + Model.list Value.t 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 + | 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)) | _ -> raise Not_found let insert_references : @@ -228,13 +241,13 @@ let insert_references : Model.content -> Int64.t -> Model.Identifier.t -> - Model.value -> + Value.value -> Int64.t Intf.data -> unit Lwt.t = fun db content content_id field_name value data -> - let provider : string -> Model.value = function + let provider : string -> Value.value = function | n when String.equal n (Model.Identifier.to_string field_name) -> value - | _ -> Model.Typ.None + | _ -> Value.none in let acc = MultiReferencesInsert. |
