summaryrefslogtreecommitdiff
path: root/test/insert_list.ml
blob: e23f13b4fcb7030477beeddc1d8b00f3c9c03894 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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))