summaryrefslogtreecommitdiff
path: root/test/insert_list.ml
blob: 88e17dbba1ee59988a3dbea3c875c1eec15f4440 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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

(** Insert two rows in the given list, with a generated provider *)
let insert_rows db (list : Model.list) =
  let fields = list.Model.DataList.fields in
  let* _id1 =
    Query.insert_list db list (Common.provider_for_fields fields 1L)
  in
  let* _id2 =
    Query.insert_list db list (Common.provider_for_fields fields 2L)
  in
  Lwt.return_unit

let exec : string list -> t =
 fun _args ->
  Common.exec_with_conf (fun configuration db ->
      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. The
         reference columns are followed by a [<field>_table] literal naming the
         targeted table, so that the output is self-describing. *)
      let* rows =
        Lwt_list.map_s
          (fun list ->
            let table = Model.Identifier.to_string list.Model.DataList.id in
            let columns =
              ("id", Model.Identifier.quote Model.Identifier.id)
              :: Common.field_columns ~annotate_references:true
                   list.Model.DataList.fields
            in
            let* json = Common.collect_json db list.Model.DataList.id columns 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 columns =
              [
                ("parent", Model.Identifier.quote Model.Identifier.parent);
                ("target", Model.Identifier.quote Model.Identifier.target);
                ( "target_table",
                  Croquette_common.Sql.quote_literal
                    (Model.Identifier.to_string target) );
              ]
            in
            let* json = Common.collect_json db table columns in
            Lwt.return ((name ^ ":") :: json))
          external_tables
      in
      Lwt.return (List.concat rows @ List.concat ext_rows))