open StdLabels let name = __FILE__ let description = "Insert data and check the result" let speclist () = [] type t = ((string * Int64.t * string) list, Configuration.Read_conf.error) result let pp : Format.formatter -> t -> unit = Format.pp_print_result ~error:Configuration.Read_conf.pp_error ~ok: (Format.pp_print_list ~pp_sep:Format.pp_print_cut (fun formatter (content_id, id, content) -> Format.fprintf formatter "@[%s:%Ld@;%s@]" content_id id content)) type content = Model.content type value = Value.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. [content] is the type definition for the element we want to insert [entry] contains all the values to insert in the database. *) let insert_and_get_result : ?parent:Model.content Value.t -> Caqti_lwt.connection -> content -> properties -> (string * int64 * string) Lwt.t = fun ?parent db content entry -> let ( let* ) = Lwt.bind in let* inserted = Query.insert ?parent db content (fun v -> match List.assoc_opt v entry with Some v -> v | None -> Value.none) in let id = Value.id inserted in let content_id = Model.Identifier.to_string (Model.Content.id content) in let module Db = (val db) in let* json_result = Db.find (Common.content_as_json content) id in let* json = Caqti_lwt.or_fail json_result in Lwt.return (content_id, id, json) (** Insert one row and collect the result line, together with the lines of its multivalued references. Return the accumulator and the inserted value. *) let insert_one : Caqti_lwt.connection -> ?parent:Model.content Value.t -> content -> properties -> (string * Int64.t * string) list -> ((string * Int64.t * string) list * Model.content Value.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), Value.v content id) module S = Configuration.Json5.Decode let ( let* ) = Lwt.bind (* bind in the decoder monad *) let ( let** ) = S.( let* ) (* 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 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) (** 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, Value.single (Model.Typ.Integer, Some (Int64.of_int v)))) S.int ); ( "text", S.map (fun v -> Lwt.return (key, Value.single (Model.Typ.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, Value.none))); ] (** Decode a reference field: single value or list of values, depending on [multivaluated]. 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, Value.multi ( 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, Value.single (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, Value.multi (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, Value.single (Model.Typ.MonoReference reference, Some id))) ) 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)) (** 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) (** 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)) (** 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))) (** 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 _ -> (* 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 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, parent = 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 subcontent entry acc in Lwt.return acc) acc subentries) acc subcontents) acc entries) [] inserts in Lwt.return_ok (List.rev insertions))