diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/insert_list.ml | 152 | ||||
| -rw-r--r-- | test/tests.ml | 1 |
2 files changed, 153 insertions, 0 deletions
diff --git a/test/insert_list.ml b/test/insert_list.ml new file mode 100644 index 0000000..e23f13b --- /dev/null +++ b/test/insert_list.ml @@ -0,0 +1,152 @@ +open StdLabels + +let name = __FILE__ +let description = "Insert rows in the lists and display their tables" +let speclist () = [] + +type 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_newline + Format.pp_print_string) + +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 + in + let () = Buffer.add_string buff ")" in + let q = + "SELECT " ^ Buffer.contents buff ^ " FROM " + ^ Model.Identifier.quote list.Model.DataList.id + in + let open Caqti.Templater in + direct T.(unit -->* string) q + +let external_table_as_json : + Model.Identifier.t -> + Model.Identifier.t -> + (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) + +(** 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. *) +let provider_for (list : Model.list) (row : Int64.t) : string -> Model.value = + fun field_name -> + let field = + List.find list.Model.DataList.fields ~f:(fun field -> + String.equal field_name + (Model.Identifier.to_string (Model.Field.name field))) + in + let (Model.Field.T ty) = Model.Field.type_ field in + match ty with + | Model.Typ.Text -> + Model.Typ.V (Text, Some (field_name ^ "-" ^ Int64.to_string row)) + | Model.Typ.Integer -> Model.Typ.V (Integer, Some row) + | Model.Typ.MonoReference reference -> + Model.Typ.V (MonoReference reference, Some 1L) + | Model.Typ.MultiReference reference -> + (* Several values per row, to highlight the multivalued data *) + let values = if Int64.equal row 1L then [ 1L; 2L ] else [ 2L ] in + Model.Typ.Seq (MultiReference reference, List.to_seq values) + +let insert_rows db (list : Model.list) = + let* _id1 = Query.insert_list db list (provider_for list 1L) in + let* _id2 = Query.insert_list db list (provider_for list 2L) in + Lwt.return_unit + +let exec : string list -> t = + fun _args -> + let stdin = In_channel.input_all 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)) diff --git a/test/tests.ml b/test/tests.ml index 0f0fdc5..15fb51e 100644 --- a/test/tests.ml +++ b/test/tests.ml @@ -34,6 +34,7 @@ let () = register (module Hash); register (module Configuration_); register (module Insert); + register (module Insert_list); register (module Update); register (module Identifier); ] |
