diff options
Diffstat (limited to 'lib/query/insert.ml')
| -rw-r--r-- | lib/query/insert.ml | 125 |
1 files changed, 99 insertions, 26 deletions
diff --git a/lib/query/insert.ml b/lib/query/insert.ml index 39c32d0..b6aee0f 100644 --- a/lib/query/insert.ml +++ b/lib/query/insert.ml @@ -21,8 +21,86 @@ let collect_fields : Encoder.list_to_tup fields ~f:(fun field -> let name = Model.Field.name field in - let (Model.Field.T content) = Model.Field.type_ field in - TT { name; content }) + let (Model.Field.T content_type) = Model.Field.type_ field in + TT { name; content_type }) + +module MultiReferencesInsert = struct + type 'a t = { + db : Caqti_lwt.connection; + parent_id : int64; + content : Model.Reference.t Model.Content.t; + provider : string -> Model.Reference.t Model.Typ.value; + k : (unit, Caqti_error.t) result Lwt.t; + } + + type request = (int64 * int64, unit, [ `Zero ]) Caqti_request.t + (** Helper for the request insert signature *) + + let ( let* ) = Lwt.bind + + let accumulate : + Caqti_lwt.connection -> + request -> + int64 -> + (unit, [> Caqti_error.t ]) result Lwt.t -> + int64 -> + (unit, [> Caqti_error.t ]) result Lwt.t = + fun db request parent k value -> + let module Db = (val db) in + let acc = + let* res = k in + match res with + | Error _ as err -> Lwt.return err + | Ok () -> + (* We have the type for each of the value, so we can create + a request directly. *) + Db.exec request (parent, value) + in + acc + + (** 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) + Intf.{ name; content_type } -> + let module Db = (val db) in + match content_type with + | Model.Typ.MultiReference _target -> ( + let result = provider name in + match result with + | Model.Typ.None -> acc + | Model.Typ.V _ -> raise TypeMismatch + | Model.Typ.Seq (field_type', values) -> ( + match Model.Typ.eq content_type field_type' with + | None -> raise TypeMismatch + | Some Equal -> + let buff = Buffer.create 16 in + let format = Format.formatter_of_buffer buff in + let () = + Format.fprintf format + "INSERT INTO \"%s.%s\" (\"parent\", \"target\") VALUES (?, \ + ?)" + (Model.Content.id content) name + in + Format.pp_print_flush format (); + let request : request = + Caqti_request.Infix.( + Caqti_type.(t2 int64 int64 ->. Caqti_type.unit)) + (Buffer.contents buff) + 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* _ = Db.deallocate request in + Lwt.return res + in + + { acc with k })) + | _ -> raise TypeMismatch +end + +module Fold = Encoder.Fold (MultiReferencesInsert) (** Insert a row. This syntax use RETURNING ID to get the value we just inserted *) @@ -39,18 +117,19 @@ let insert_returning : type 'a t = 'a let f : type a. a Intf.data -> a t = - fun Intf.{ name; content } -> + fun Intf.{ name; content_type } -> let result = provider name in (* Check the value given by the provider and ensure it’s match the expected type *) - match (content, result) with - | _, None -> + match result with + | Model.Typ.Seq _ -> raise TypeMismatch + | Model.Typ.None -> (* The provider does not have any value for this property, use the - default value*) - Model.Typ.default_value content - | _, V (field_type', value) -> ( - match Model.Typ.eq content field_type' with - | Some Equal -> Some value + default value*) + Model.Typ.default_value content_type + | Model.Typ.V (field_type', value) -> ( + match Model.Typ.eq content_type field_type' with + | Some Equal -> value | None -> raise TypeMismatch) end) in let init_fields = @@ -94,7 +173,7 @@ 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* id = Caqti_lwt.or_fail id_result in + let* content_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 *) @@ -104,20 +183,14 @@ let insert_returning : match ty with Model.Typ.MultiReference _ -> true | _ -> false) in - let module Fold = Encoder.Fold (struct - type 'a t = 'a Lwt.t - - (** The function is called for each feature declared as multi reference. *) - let f : 'a Lwt.t -> 'b Intf.data -> 'a Lwt.t = - fun acc Intf.{ name; content } -> - ignore content; - let values = provider name in - ignore values; - let* acc = acc in - Lwt.return acc - end) in - let* () = Fold.f Lwt.return_unit multi_valuated_fields in - Lwt.return id + let acc = + MultiReferencesInsert. + { db; provider; k = Lwt.return_ok (); parent_id = content_id; content } + in + let ret = Fold.f acc multi_valuated_fields in + let* k = ret.k in + let* () = Caqti_lwt.or_fail k in + Lwt.return content_id (** Insert an entry for a specific content *) let insert : @@ -137,7 +210,7 @@ let insert : | Some v, Some _ -> ( (* Otherwise update the provider to give a value for the parent *) function - | "parent" -> Model.Typ.(V (Integer, v)) + | "parent" -> Model.Typ.(V (Integer, Some v)) | other -> provider other) in let module Db = (val db) in |
