summaryrefslogtreecommitdiff
path: root/test/insert.ml
diff options
context:
space:
mode:
Diffstat (limited to 'test/insert.ml')
-rw-r--r--test/insert.ml443
1 files changed, 214 insertions, 229 deletions
diff --git a/test/insert.ml b/test/insert.ml
index ee2c927..217fc83 100644
--- a/test/insert.ml
+++ b/test/insert.ml
@@ -18,6 +18,13 @@ type content = Model.content
type value = Model.value
type properties = (string * value) list
+(* A subcontent to insert, with one entry (properties) per row *)
+type content_to_insert = content * properties list
+
+(* One entry of the "values" list: the properties of a row, and the entries
+ of each subcontent (identified by a key equal to the subcontent name) *)
+type entry = properties * content_to_insert list
+
(** [insert_and_get_result db content entry] will insert the value in the
database and return the informations for the content just inserted.
@@ -41,250 +48,228 @@ let insert_and_get_result :
let* json = Caqti_lwt.or_fail json_result in
Lwt.return (content_id, id, json)
-(** Read the values to insert in the configuration *)
-module Make (S : Decoders.Decode.S) = struct
- let ( let* ) = S.( let* )
- let ( let+ ) = S.( let+ )
-
- type content_to_insert = content * properties list
-
- let ( let@@ ) = Lwt.bind
+(** Insert one row and collect the result line, together with the lines of its
+ multivalued references. Return the accumulator and the id of the new row. *)
+let insert_one :
+ Caqti_lwt.connection ->
+ ?parent:int64 ->
+ content ->
+ properties ->
+ (string * Int64.t * string) list ->
+ ((string * Int64.t * string) list * Int64.t) Lwt.t =
+ fun db ?parent content entry acc ->
+ let ( let* ) = Lwt.bind in
+ let* content_id, id, json = insert_and_get_result ?parent db content entry in
+ let* ext = Common.collect_references db content id in
+ Lwt.return (List.append ext ((content_id, id, json) :: acc), id)
- (** Load the values associated with the configuration. *)
- class loader (db : Caqti_lwt.connection) (conf : Configuration.Ast.setting) =
- object (self)
- method insert_and_return_references :
- content -> properties list Lwt.t -> int64 list Lwt.t =
- fun content properties ->
- let ( let* ) = Lwt.bind in
- let* properties = properties in
- Lwt_list.map_s
- (fun line ->
- let* _, id, _ = insert_and_get_result db content line in
- Lwt.return id)
- properties
+module S = Configuration.Json5.Decode
- method value : content -> string -> (string * value) Lwt.t S.decoder =
- fun content key ->
- ignore content;
- S.one_of
- [
- ( "numeric",
- S.map
- (fun v ->
- Lwt.return
- (key, Model.Typ.(V (Integer, Some (Int64.of_int v)))))
- S.int );
- ( "text",
- S.map
- (fun v -> Lwt.return (key, Model.Typ.(V (Text, Some v))))
- S.string );
- ( "reference",
- match Model.Content.find_target content key with
- | None -> S.fail "Not a feature"
- | Some (Model.Reference.List _ as t) ->
- (* A list is referenced by the id of one of its rows *)
- let reference =
- Model.Typ.MonoReference { t; constraints = None }
- in
- S.map
- (fun v ->
- let value = Some (Int64.of_int v) in
- Lwt.return (key, Model.Typ.V (reference, value)))
- S.int
- | Some (Model.Reference.Content target_ref as t) ->
- let target = Model.Reference.get target_ref in
- let* properties = self#properties target in
- let sequence =
- let@@ properties = properties in
+let ( let* ) = Lwt.bind
- let@@ _, id, _ =
- insert_and_get_result db target properties
- in
- let reference =
- Model.Typ.MonoReference { t; constraints = None }
- in
- Lwt.return (key, Model.Typ.V (reference, Some id))
- in
- S.succeed sequence );
- ( "references",
- match Model.Content.find_target content key with
- | None -> S.fail "Not a feature"
- | Some (Model.Reference.List _ as t) ->
- let multireference =
- Model.Typ.MultiReference { t; constraints = None }
- in
- S.map
- (fun ids ->
- Lwt.return
- (key, Model.Typ.Seq (multireference, List.to_seq ids)))
- (S.map (List.map ~f:Int64.of_int) (S.list S.int))
- | Some (Model.Reference.Content target_ref as t) ->
- let target = Model.Reference.get target_ref in
- let* properties = self#values target in
- let sequence =
- let@@ ids =
- self#insert_and_return_references target properties
- in
- let reference =
- Model.Typ.MultiReference { t; constraints = None }
- in
- Lwt.return
- (key, Model.Typ.Seq (reference, List.to_seq ids))
- in
+(* bind in the decoder monad *)
+let ( let** ) = S.( let* )
- S.succeed sequence );
- ( "other",
- (* If the value is a dictionnary list, consider it as external references *)
- S.succeed (Lwt.return (key, Model.Typ.None)) );
- ]
- (** The values are encoded in a very simple way, depending of the nature
- of the field in the json, we create a property in for text or numeric
- *)
+(* Force a list of Lwt.t sequentially: the sqlite connection does not support
+ concurrent usage, and some of these Lwt.t perform inserts *)
+let seq_lwt : 'a Lwt.t list -> 'a list Lwt.t =
+ fun l -> Lwt_list.map_s (fun x -> x) l
- method properties : content -> properties Lwt.t S.decoder =
- fun content ->
- S.map Lwt.all (S.key_value_pairs_seq (self#value content))
+let is_field : content -> string -> bool =
+ fun content key ->
+ Seq.exists
+ (fun field ->
+ String.equal key Model.(Identifier.to_string (Field.name field)))
+ (Model.Content.fields content)
- method values : content -> properties list Lwt.t S.decoder =
- fun content -> S.map Lwt.all (S.list (self#properties content))
+(** Decode the value of a field: the json nature of the value tells the type
+ (numeric, text), or it is a reference (an integer for a list target, an
+ object holding the properties of a new row of the target content) *)
+let rec value :
+ Caqti_lwt.connection ->
+ content ->
+ string ->
+ (string * value) Lwt.t S.decoder =
+ fun db content key ->
+ S.one_of
+ [
+ ( "numeric",
+ S.map
+ (fun v ->
+ Lwt.return (key, Model.Typ.(V (Integer, Some (Int64.of_int v)))))
+ S.int );
+ ( "text",
+ S.map (fun v -> Lwt.return (key, Model.Typ.(V (Text, Some v)))) S.string
+ );
+ ("reference", reference_value db content key false);
+ ("references", reference_value db content key true);
+ ("other", S.succeed (Lwt.return (key, Model.Typ.None)));
+ ]
- method load_content : content_to_insert list Lwt.t S.decoder =
- let* properties =
- S.key_value_pairs_seq (fun key ->
- S.one_of
- [
- ( "content",
- fun v ->
- match
- (Configuration.Ast.get_content conf)
- (Model.Identifier.of_string key)
- with
- | None -> Error (Decoders.Error.make "no content")
- | Some subcontent ->
- let values = self#values subcontent v in
- Result.map
- (fun v ->
- let content_to_insert =
- let@@ v = v in
- Lwt.return (subcontent, v)
- in
- Some content_to_insert)
- values );
- ("other", fun _ -> Ok None);
- ])
- in
- let properties = List.filter_map ~f:(fun v -> v) properties in
- S.succeed (Lwt.all properties)
- (** Read the content and for each key, check if there is an existing
- subcontent with the same name, If this subcontent exists, parse all
- the subcontents with all the values and return them. *)
+(** Decode a reference field: single value or list of values, depending on
+ [multivaluated].
- method root_content :
- string ->
- (content * (properties * content_to_insert list) list) Lwt.t S.decoder
- =
- fun name ->
- (* In this syntax, the content is supposed to follow the definition
- and will never be None *)
- let content =
- Configuration.Ast.get_content conf (Model.Identifier.of_string name)
- |> Option.get
- in
- let* properties =
- S.field_opt_or ~default:(Lwt.return []) "values"
- (self#values content)
- in
+ A list target is referenced by the id of one of its rows (an integer). A
+ content target is referenced by an object holding the properties of a new
+ row, inserted in its own table — the inserts are chained in a single Lwt
+ promise, because the sqlite connection does not support concurrent usage. *)
+and reference_value :
+ Caqti_lwt.connection ->
+ content ->
+ string ->
+ bool ->
+ (string * value) Lwt.t S.decoder =
+ fun db content key multivaluated ->
+ match Model.Content.find_target content key with
+ | None -> S.fail "Not a feature"
+ | Some t -> (
+ let reference = Model.Typ.{ t; constraints = None } in
+ match t with
+ | Model.Reference.List _ ->
+ if multivaluated then
+ S.map
+ (fun ids ->
+ Lwt.return
+ ( key,
+ Model.Typ.Seq
+ ( Model.Typ.MultiReference reference,
+ List.to_seq (List.map ~f:Int64.of_int ids) ) ))
+ (S.list S.int)
+ else
+ S.map
+ (fun v ->
+ Lwt.return
+ ( key,
+ Model.Typ.V
+ (Model.Typ.MonoReference reference, Some (Int64.of_int v))
+ ))
+ S.int
+ | Model.Reference.Content target_ref ->
+ let target = Model.Reference.get target_ref in
+ if multivaluated then
+ let** entries = S.list (properties db target) in
+ S.succeed
+ (let* entries = seq_lwt entries in
+ let* ids =
+ Lwt_list.map_s
+ (fun props ->
+ let* _, id, _ = insert_and_get_result db target props in
+ Lwt.return id)
+ entries
+ in
+ Lwt.return
+ ( key,
+ Model.Typ.Seq
+ (Model.Typ.MultiReference reference, List.to_seq ids) ))
+ else
+ let** props = properties db target in
+ S.succeed
+ (let* props = props in
+ let* _, id, _ = insert_and_get_result db target props in
+ Lwt.return
+ (key, Model.Typ.V (Model.Typ.MonoReference reference, Some id)))
+ )
- (* Look for subcontents in the given values. The subcontent is
- identfied by having a key name equal to the subcontent
- identifier. *)
- let* subcontents : content_to_insert list list Lwt.t =
- S.map Lwt.all
- (S.field_opt_or ~default:[] "values" (S.list self#load_content))
- in
- let result =
- let@@ properties = properties in
- let@@ subcontents = subcontents in
+and properties : Caqti_lwt.connection -> content -> properties Lwt.t S.decoder =
+ fun db content -> S.map seq_lwt (S.key_value_pairs_seq (value db content))
- let to_insert = List.combine properties subcontents in
- Lwt.return (content, to_insert)
- in
- S.succeed result
+(** One entry of the "values" list of a content: each key is decoded once, as a
+ field value or as the entries of a subcontent *)
+and entry :
+ Caqti_lwt.connection ->
+ Configuration.Ast.setting ->
+ content ->
+ entry Lwt.t S.decoder =
+ fun db conf content ->
+ let key_value key =
+ if is_field content key then S.map (fun v -> `Prop v) (value db content key)
+ else
+ match
+ Configuration.Ast.get_content conf (Model.Identifier.of_string key)
+ with
+ | Some subcontent -> S.map (fun v -> `Sub v) (sub_entries db subcontent)
+ | None -> S.map (fun v -> `Prop v) (value db content key)
+ in
+ S.map
+ (fun pairs ->
+ let props =
+ List.filter_map pairs ~f:(function `Prop v -> Some v | `Sub _ -> None)
+ in
+ let subs =
+ List.filter_map pairs ~f:(function `Sub v -> Some v | `Prop _ -> None)
+ in
+ let* properties = seq_lwt props in
+ let* subs = seq_lwt subs in
+ Lwt.return (properties, subs))
+ (S.key_value_pairs_seq key_value)
- method content :
- (content * (properties * content_to_insert list) list) Lwt.t S.decoder
- =
- let* name = S.field "id" S.string in
- let* properties = self#root_content name in
- S.succeed properties
+(** The entries of a subcontent: a list of plain properties, one per row *)
+and sub_entries :
+ Caqti_lwt.connection -> content -> content_to_insert Lwt.t S.decoder =
+ fun db content ->
+ S.map
+ (fun entries ->
+ let* entries = seq_lwt entries in
+ Lwt.return (content, entries))
+ (S.list (properties db content))
- method configuration :
- (content * (properties * content_to_insert list) list) list Lwt.t
- S.decoder =
- let* content = S.field "content" (S.list self#content) in
- let conf = Lwt.all content in
- S.succeed conf
- end
-end
+(** Decode one content of the data document: its id and its "values" entries *)
+and content_entry :
+ Caqti_lwt.connection ->
+ Configuration.Ast.setting ->
+ (content * entry list) Lwt.t S.decoder =
+ fun db conf ->
+ let** name = S.field "id" S.string in
+ let content =
+ Configuration.Ast.get_content conf (Model.Identifier.of_string name)
+ |> Option.get
+ in
+ S.map
+ (fun entries ->
+ let* entries = seq_lwt entries in
+ Lwt.return (content, entries))
+ (S.field_opt_or ~default:[] "values" (S.list (entry db conf content)))
-module D = Make (Configuration.Json5.Decode)
+(** Decode the whole data document *)
+and data_document :
+ Caqti_lwt.connection ->
+ Configuration.Ast.setting ->
+ (content * entry list) list Lwt.t S.decoder =
+ fun db conf ->
+ S.map seq_lwt (S.field "content" (S.list (content_entry db conf)))
let exec : string list -> t =
fun _ ->
- let stdin = In_channel.input_all stdin in
+ (* The same document holds both the configuration and the data to insert:
+ the configuration reader ignores the "values" keys, the data reader
+ ignores the "section" keys *)
+ let stdin = Common.read_stdin () in
let ( let** ) = Result.bind in
let** configuration = Configuration.Read_conf.read_string_conf stdin in
- Lwt_main.run
- (let ( let* ) = Lwt.bind in
- let* db = Common.connect configuration in
- let module Db = (val db) in
- let inserts =
- Configuration.Json5.Decode.decode_string
- (new D.loader db configuration)#configuration stdin
- in
- match inserts with
- | Error err -> Lwt.return_error err
- | Ok inserts ->
- let* inserts = inserts in
- let* insertions =
- Lwt_list.fold_left_s
- (fun acc (content, entries) ->
- Lwt_list.fold_left_s
- (fun acc (entry, subcontents) ->
- (* Insert the main content *)
- let* content_id, id, json =
- insert_and_get_result db content entry
- in
- let acc = (content_id, id, json) :: acc in
-
- (* Also query the multivaluated references *)
- let* ext = Common.collect_references db content id in
- let acc = List.append ext acc in
- (* Insert the subcontent associated *)
- Lwt_list.fold_left_s
- (fun acc (subcontent, entries) ->
- let* acc =
- Lwt_list.fold_left_s
- (fun acc entry ->
- let* subcontent_id, id, json =
- insert_and_get_result ~parent:id db subcontent
- entry
- in
- let acc = (subcontent_id, id, json) :: acc in
-
- (* Also query the multivaluated references *)
- let* ext =
- Common.collect_references db subcontent id
- in
- let acc = List.append ext acc in
- Lwt.return acc)
- acc entries
- in
-
- Lwt.return acc)
- acc subcontents)
- acc entries)
- [] inserts
- in
- Lwt.return_ok (List.rev insertions))
+ Common.with_db configuration (fun db ->
+ match S.decode_string (data_document db configuration) stdin with
+ | Error err -> Lwt.return_error err
+ | Ok inserts ->
+ let* inserts = inserts in
+ let* insertions =
+ Lwt_list.fold_left_s
+ (fun acc (content, entries) ->
+ Lwt_list.fold_left_s
+ (fun acc (entry, subcontents) ->
+ (* Insert the main content *)
+ let* acc, id = insert_one db content entry acc in
+ (* Insert the subcontents associated *)
+ Lwt_list.fold_left_s
+ (fun acc (subcontent, subentries) ->
+ Lwt_list.fold_left_s
+ (fun acc entry ->
+ let* acc, _ =
+ insert_one db ~parent:id subcontent entry acc
+ in
+ Lwt.return acc)
+ acc subentries)
+ acc subcontents)
+ acc entries)
+ [] inserts
+ in
+ Lwt.return_ok (List.rev insertions))