summaryrefslogtreecommitdiff
path: root/test/insert_list.ml
diff options
context:
space:
mode:
Diffstat (limited to 'test/insert_list.ml')
-rw-r--r--test/insert_list.ml160
1 files changed, 70 insertions, 90 deletions
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))