From ce13c93cf14cb71894481b0303a7058a72a6f4ce Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Sat, 21 Nov 2026 11:23:41 +0100 Subject: Load values from the database --- test/insert.ml | 286 ++++++--------------------------------------------------- 1 file changed, 27 insertions(+), 259 deletions(-) (limited to 'test/insert.ml') diff --git a/test/insert.ml b/test/insert.ml index 1b12596..9133832 100644 --- a/test/insert.ml +++ b/test/insert.ml @@ -1,7 +1,9 @@ -open StdLabels - let name = __FILE__ -let description = "Insert data and check the result" + +let description = + "Insert two rows of the given content with a generated provider, and display \ + the inserted rows. The configuration is read on the standard input" + let speclist () = [] type t = @@ -14,267 +16,33 @@ let pp : Format.formatter -> t -> unit = (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 -> +(** Insert one row of [content], and return its json representation together + with one line per value of each multivalued reference field *) +let insert_one : Caqti_lwt.connection -> - content -> - properties -> - (string * int64 * string) Lwt.t = - fun ?parent db content entry -> + Model.content -> + Int64.t -> + (string * Int64.t * string) list Lwt.t = + fun db content row -> 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 = - match Value.get inserted Value.id_ref with - | Some id -> id - | None -> assert false - in + let* id = Query.insert db content (Common.provider_for content row) 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.single - ( 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.single - (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 + Lwt.return ((content_id, id, json) :: List.rev ext) + +let exec : string list -> t = function + | [ content_id ] -> + Common.exec_with_conf (fun configuration db -> + let content = + Option.get + (Configuration.Ast.get_content configuration + (Model.Identifier.of_string content_id)) in - Lwt.return_ok (List.rev insertions)) + let ( let* ) = Lwt.bind in + let* lines1 = insert_one db content 1L in + let* lines2 = insert_one db content 2L in + Lwt.return (lines1 @ lines2)) + | _ -> failwith "Invalid arguments" -- cgit v1.2.3