summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/common.ml70
-rw-r--r--test/insert.ml443
-rw-r--r--test/insert_list.ml160
-rw-r--r--test/path.ml2
-rw-r--r--test/schema.ml7
5 files changed, 335 insertions, 347 deletions
diff --git a/test/common.ml b/test/common.ml
index 6223846..2a1e070 100644
--- a/test/common.ml
+++ b/test/common.ml
@@ -16,6 +16,39 @@ let connect : Configuration.Ast.setting -> Caqti_lwt.connection Lwt.t =
let* () = Query.create db conf in
Lwt.return db
+(** Read the whole standard input (a configuration or a data document) *)
+let read_stdin : unit -> string = fun () -> In_channel.input_all stdin
+
+(** Run [f] with a fresh in-memory database created from the configuration *)
+let with_db :
+ Configuration.Ast.setting -> (Caqti_lwt.connection -> 'a Lwt.t) -> 'a =
+ fun conf f ->
+ Lwt_main.run
+ (let* db = connect conf in
+ f db)
+
+(** Name of the table targeted by a reference (a content or a list) *)
+let target_table : Model.target -> string = function
+ | Model.Reference.List list ->
+ Model.Identifier.to_string list.Model.DataList.id
+ | Model.Reference.Content ref ->
+ Model.Identifier.to_string (Model.Content.id (Model.Reference.get ref))
+
+(** Build a [SELECT json_object(...)] query returning one json object per row
+ of [table]. [columns] are (json key, SQL expression) pairs. *)
+let json_select_sql :
+ ?where:string -> Model.Identifier.t -> (string * string) list -> string =
+ fun ?where table columns ->
+ let expr =
+ String.concat ", "
+ (ListLabels.map columns ~f:(fun (key, e) ->
+ Croquette_common.Sql.quote_literal key ^ ", " ^ e))
+ in
+ let q =
+ "SELECT json_object(" ^ expr ^ ") from " ^ Model.Identifier.quote table
+ in
+ match where with None -> q | Some w -> q ^ " where " ^ w
+
(** Load the given content and return the values as a json *)
let content_as_json :
Model.content -> (int64, string, [ `One ]) Caqti.Template.Request.t =
@@ -26,36 +59,27 @@ let content_as_json :
|> Seq.filter (fun t ->
let (Model.Field.T ty) = Model.Field.type_ t in
match ty with Model.Typ.MultiReference _ -> false | _ -> true)
+ |> List.of_seq
in
- let buff = Buffer.create 64 in
- let () =
- Seq.iter
- (fun field ->
+ let columns =
+ ListLabels.map fields ~f:(fun field ->
let name = Model.Field.name field in
- Buffer.add_string buff
- ( Croquette_common.Sql.quote_literal (Model.Identifier.to_string name)
- ^ "," ^ Model.Identifier.quote name ^ "," ))
- fields
+ (Model.Identifier.to_string name, Model.Identifier.quote name))
in
-
- let () =
+ let columns =
match Model.Content.parent content with
- | None ->
- let length = Buffer.length buff in
- if length > 1 then Buffer.truncate buff (length - 1);
- ()
+ | None -> columns
| Some _ ->
- Buffer.add_string buff
- ( Croquette_common.Sql.quote_literal "parent" ^ ","
- ^ Model.Identifier.quote (Model.Identifier.parent) )
+ columns
+ @ [ ("parent", Model.Identifier.quote Model.Identifier.parent) ]
+ in
+ let q =
+ json_select_sql
+ ~where:(Model.Identifier.quote Model.Identifier.id ^ "=?")
+ (Model.Content.id content) columns
in
-
- let q = Buffer.contents buff in
let open Caqti.Templater in
- direct T.(int64 -->! string)
- ("SELECT json_object(" ^ q ^ ") from "
- ^ Model.Identifier.quote (Model.Content.id content)
- ^ " where " ^ Model.Identifier.quote (Model.Identifier.id) ^ "=?")
+ direct T.(int64 -->! string) q
let query_values (table : Model.content)
(feature : Model.Identifier.t) =
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))
diff --git a/test/insert_list.ml b/test/insert_list.ml
index e23f13b..9d4018f 100644
--- a/test/insert_list.ml
+++ b/test/insert_list.ml
@@ -15,52 +15,32 @@ let pp : Format.formatter -> t -> unit =
let ( let* ) = Lwt.bind
let ( let+ ) opt cont = Result.map cont opt
-(** Name of the table targeted by a reference *)
-let target_table : Model.target -> string = function
- | Model.Reference.List list ->
- Model.Identifier.to_string list.Model.DataList.id
- | Model.Reference.Content ref ->
- Model.Identifier.to_string (Model.Content.id (Model.Reference.get ref))
-
(** Build a request returning each row of the list table as a json object.
Reference columns are followed by a [<field>_table] literal naming the
targeted table, so that the output is self-describing. *)
let list_as_json : Model.list -> (unit, string, _) Caqti.Template.Request.t =
fun list ->
- let buff = Buffer.create 64 in
- let () = Buffer.add_string buff "json_object('id', \"id\"" in
- let () =
- List.iter
- ~f:(fun field ->
- let name = Model.Field.name field in
- let (Model.Field.T ty) = Model.Field.type_ field in
- let literal =
- Croquette_common.Sql.quote_literal (Model.Identifier.to_string name)
- in
- match ty with
- | Model.Typ.MultiReference _ -> () (* in a dedicated table *)
- | Model.Typ.MonoReference reference ->
- Buffer.add_string buff
- (", " ^ literal ^ ", "
- ^ Model.Identifier.quote name
- ^ ", "
- ^ Croquette_common.Sql.quote_literal
- (Model.Identifier.to_string name ^ "_table")
- ^ ", "
- ^ Croquette_common.Sql.quote_literal
- (target_table reference.Model.Typ.t))
- | _ ->
- Buffer.add_string buff
- (", " ^ literal ^ ", " ^ Model.Identifier.quote name))
- list.Model.DataList.fields
+ let column field =
+ let name = Model.Field.name field in
+ let (Model.Field.T ty) = Model.Field.type_ field in
+ let key = Model.Identifier.to_string name in
+ match ty with
+ | Model.Typ.MultiReference _ -> [] (* in a dedicated table *)
+ | Model.Typ.MonoReference reference ->
+ [
+ (key, Model.Identifier.quote name);
+ ( key ^ "_table",
+ Croquette_common.Sql.quote_literal
+ (Common.target_table reference.Model.Typ.t) );
+ ]
+ | _ -> [ (key, Model.Identifier.quote name) ]
in
- let () = Buffer.add_string buff ")" in
- let q =
- "SELECT " ^ Buffer.contents buff ^ " FROM "
- ^ Model.Identifier.quote list.Model.DataList.id
+ let columns =
+ ("id", "\"id\"") :: List.concat_map list.Model.DataList.fields ~f:column
in
let open Caqti.Templater in
- direct T.(unit -->* string) q
+ direct T.(unit -->* string)
+ (Common.json_select_sql list.Model.DataList.id columns)
let external_table_as_json :
Model.Identifier.t ->
@@ -68,14 +48,15 @@ let external_table_as_json :
(unit, string, _) Caqti.Template.Request.t =
fun table target_table ->
let open Caqti.Templater in
- direct
- T.(unit -->* string)
- ("SELECT json_object('parent', \"parent\", 'target', \"target\", \
- 'target_table', "
- ^ Croquette_common.Sql.quote_literal
- (Model.Identifier.to_string target_table)
- ^ ") FROM "
- ^ Model.Identifier.quote table)
+ direct T.(unit -->* string)
+ (Common.json_select_sql table
+ [
+ ("parent", "\"parent\"");
+ ("target", "\"target\"");
+ ( "target_table",
+ Croquette_common.Sql.quote_literal
+ (Model.Identifier.to_string target_table) );
+ ])
(** A provider giving a value for each field of the list, depending on its type.
The references point to the row of id 1 of their target list. *)
@@ -105,48 +86,47 @@ let insert_rows db (list : Model.list) =
let exec : string list -> t =
fun _args ->
- let stdin = In_channel.input_all stdin in
+ let stdin = Common.read_stdin () in
let+ configuration = Configuration.Read_conf.read_string_conf stdin in
- Lwt_main.run
- (let* db = Common.connect configuration in
- let module Db = (val db) in
- let lists = Configuration.Ast.lists configuration in
- (* Insert two rows per list, in identifier order *)
- let* () = Lwt_list.iter_s (insert_rows db) lists in
- (* Display the list tables, each under a header naming the table *)
- let* rows =
- Lwt_list.map_s
- (fun list ->
- let table = Model.Identifier.to_string list.Model.DataList.id in
- let* res = Db.collect_list (list_as_json list) () in
- let* json = Caqti_lwt.or_fail res in
- Lwt.return ((table ^ ":") :: json))
- lists
- in
- (* Display the dedicated tables of the multivalued fields, with the table
- targeted by the reference *)
- let external_tables =
- List.concat_map lists ~f:(fun list ->
- List.filter_map list.Model.DataList.fields ~f:(fun field ->
- let (Model.Field.T ty) = Model.Field.type_ field in
- match ty with
- | Model.Typ.MultiReference reference ->
- Some
- ( Model.Identifier.dot list.Model.DataList.id
- (Model.Field.name field),
- Model.Identifier.of_string
- (target_table reference.Model.Typ.t) )
- | _ -> None))
- in
- let* ext_rows =
- Lwt_list.map_s
- (fun (table, target) ->
- let name = Model.Identifier.to_string table in
- let* res =
- Db.collect_list (external_table_as_json table target) ()
- in
- let* json = Caqti_lwt.or_fail res in
- Lwt.return ((name ^ ":") :: json))
- external_tables
- in
- Lwt.return (List.concat rows @ List.concat ext_rows))
+ Common.with_db configuration (fun db ->
+ let module Db = (val db) in
+ let lists = Configuration.Ast.lists configuration in
+ (* Insert two rows per list, in identifier order *)
+ let* () = Lwt_list.iter_s (insert_rows db) lists in
+ (* Display the list tables, each under a header naming the table *)
+ let* rows =
+ Lwt_list.map_s
+ (fun list ->
+ let table = Model.Identifier.to_string list.Model.DataList.id in
+ let* res = Db.collect_list (list_as_json list) () in
+ let* json = Caqti_lwt.or_fail res in
+ Lwt.return ((table ^ ":") :: json))
+ lists
+ in
+ (* Display the dedicated tables of the multivalued fields, with the table
+ targeted by the reference *)
+ let external_tables =
+ List.concat_map lists ~f:(fun list ->
+ List.filter_map list.Model.DataList.fields ~f:(fun field ->
+ let (Model.Field.T ty) = Model.Field.type_ field in
+ match ty with
+ | Model.Typ.MultiReference reference ->
+ Some
+ ( Model.Identifier.dot list.Model.DataList.id
+ (Model.Field.name field),
+ Model.Identifier.of_string
+ (Common.target_table reference.Model.Typ.t) )
+ | _ -> None))
+ in
+ let* ext_rows =
+ Lwt_list.map_s
+ (fun (table, target) ->
+ let name = Model.Identifier.to_string table in
+ let* res =
+ Db.collect_list (external_table_as_json table target) ()
+ in
+ let* json = Caqti_lwt.or_fail res in
+ Lwt.return ((name ^ ":") :: json))
+ external_tables
+ in
+ Lwt.return (List.concat rows @ List.concat ext_rows))
diff --git a/test/path.ml b/test/path.ml
index 465606e..73daf4a 100644
--- a/test/path.ml
+++ b/test/path.ml
@@ -39,7 +39,7 @@ let describe_field : Model.field -> string =
let exec : string list -> t =
fun args ->
- let stdin = In_channel.input_all stdin in
+ let stdin = Common.read_stdin () in
let+ configuration = Configuration.Read_conf.read_string_conf stdin in
(* Anonymous arguments arrive reversed: [content; path] *)
match List.rev args with
diff --git a/test/schema.ml b/test/schema.ml
index 3b52812..5fcd406 100644
--- a/test/schema.ml
+++ b/test/schema.ml
@@ -25,8 +25,7 @@ let get_schema : Caqti_lwt.connection -> string -> string Lwt.t =
let exec : string list -> t =
fun tables ->
- let stdin = In_channel.input_all stdin in
+ let stdin = Common.read_stdin () in
let+ configuration = Configuration.Read_conf.read_string_conf stdin in
- Lwt_main.run
- (let* db = Common.connect configuration in
- Lwt_list.map_s (get_schema db) (List.rev tables))
+ Common.with_db configuration (fun db ->
+ Lwt_list.map_s (get_schema db) (List.rev tables))