diff options
Diffstat (limited to 'lib/query/insert.ml')
| -rw-r--r-- | lib/query/insert.ml | 48 |
1 files changed, 22 insertions, 26 deletions
diff --git a/lib/query/insert.ml b/lib/query/insert.ml index 703d5a9..26597d8 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 -> Value.value; + provider : string -> Model.target Model.Data.any; k : (unit, Caqti.Error.t) result Lwt.t; } @@ -64,10 +64,10 @@ module MultiReferencesInsert = struct and target = Identifier.quote Identifier.target in match content_type with | Model.Typ.MultiReference _target -> ( - let (Value.Data.Any d) = provider (Model.Identifier.to_string name) in + let (Model.Data.Any d) = provider (Model.Identifier.to_string name) in match d with - | Value.Data.None -> acc - | Value.Data.V (field_type', values) -> ( + | Model.Data.None -> acc + | Model.Data.V (field_type', values) -> ( match Model.Typ.eq_ocaml content_type field_type' with | None -> raise Encoder.TypeMismatch | Some Equal -> @@ -106,7 +106,7 @@ let insert_row : table:Model.Identifier.t -> ?init_fields:Model.field list -> Model.field list -> - (string -> Value.value) -> + (string -> Model.target Model.Data.any) -> int64 Lwt.t = fun db ~table ?(init_fields = []) fields provider -> let module Db = (val db) in @@ -165,8 +165,8 @@ let insert_row : let insert_returning : Caqti_lwt.connection -> Model.content -> - (string -> Value.value) -> - Model.content Value.t Lwt.t = + (string -> Model.target Model.Data.any) -> + int64 Lwt.t = fun db content provider -> let init_fields = match Model.Content.parent content with @@ -181,15 +181,15 @@ let insert_returning : (List.of_seq (Model.Content.fields content)) provider in - Lwt.return (Value.v content id) + Lwt.return id -(** Insert an entry for a specific content *) +(** Insert an entry for a specific content, and return the id of the new row *) let insert : Caqti_lwt.connection -> Model.content -> - ?parent:Model.content Value.t -> - (string -> Value.value) -> - Model.content Value.t Lwt.t = + ?parent:Int64.t -> + (string -> Model.target Model.Data.any) -> + int64 Lwt.t = fun db content ?parent provider -> let provider = match (parent, Model.Content.parent content) with @@ -198,15 +198,11 @@ let insert : (* Raise an error right now if the parent of the subcontent is not given *) raise Encoder.TypeMismatch - | Some v, Some _ -> ( + | Some parent_id, Some _ -> ( (* Otherwise update the provider to give a value for the parent *) - 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) + | "parent" -> + Model.Data.any (Model.Data.v (Model.Typ.Integer, Some parent_id)) | other -> provider other) in let module Db = (val db) in @@ -215,7 +211,7 @@ let insert : insert_returning db content provider | _ -> raise Not_found -(** Insert a row in the dedicated table of a list, and return it. +(** 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 @@ -223,8 +219,8 @@ let insert : let insert_list : Caqti_lwt.connection -> Model.list -> - (string -> Value.value) -> - Model.list Value.t Lwt.t = + (string -> Model.target Model.Data.any) -> + int64 Lwt.t = fun db list provider -> let module Db = (val db) in match Db.dialect with @@ -234,7 +230,7 @@ let insert_list : insert_row db ~table:list.Model.DataList.id list.Model.DataList.fields provider in - Lwt.return (Value.v list id) + Lwt.return id | _ -> raise Not_found let insert_references : @@ -242,13 +238,13 @@ let insert_references : Model.content -> Int64.t -> Model.Identifier.t -> - Value.value -> + Model.target Model.Data.any -> (Int64.t, _) Intf.data -> unit Lwt.t = fun db content content_id field_name value data -> - let provider : string -> Value.value = function + let provider : string -> Model.target Model.Data.any = function | n when String.equal n (Model.Identifier.to_string field_name) -> value - | _ -> Value.none + | _ -> Model.Data.any Model.Data.none in let acc = MultiReferencesInsert. |
