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
|
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
(** 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 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 columns =
("id", "\"id\"") :: List.concat_map list.Model.DataList.fields ~f:column
in
let open Caqti.Templater in
direct T.(unit -->* string)
(Common.json_select_sql list.Model.DataList.id columns)
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)
(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. *)
let provider_for (list : Model.list) (row : Int64.t) : string -> Value.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 ->
Value.single (Text, Some (field_name ^ "-" ^ Int64.to_string row))
| Model.Typ.Integer -> Value.single (Integer, Some row)
| Model.Typ.MonoReference reference ->
Value.single (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
Value.single (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 = Common.read_stdin () in
let+ configuration = Configuration.Read_conf.read_string_conf stdin in
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))
|