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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
(** Common fuctions shared between the tests *)
let ( let* ) = Lwt.bind
(** Connect to the database in memory and create a schema matching the given
configuration *)
let connect : Configuration.Ast.setting -> Caqti_lwt.connection Lwt.t =
fun conf ->
(* Pin the driver tweaks below 1.8 so that the sqlite3 driver does not turn
on PRAGMA foreign_keys: the tests insert references to rows that are not
present in the referenced table. *)
let config = Caqti.Connect.Config.(set tweaks_version (1, 7) default) in
let* conn =
Caqti_lwt_unix.connect ~config (Uri.of_string "sqlite3::memory:")
in
let* db = Caqti_lwt.or_fail conn in
let module Db = (val db) in
let* () = Query.create db conf in
Lwt.return db
(** Read the whole standard input (a configuration or a data document) *)
let read_stdin : unit -> string = fun () -> In_channel.input_all stdin
(** Run [f] with a fresh in-memory database created from the configuration *)
let with_db :
Configuration.Ast.setting -> (Caqti_lwt.connection -> 'a Lwt.t) -> 'a =
fun conf f ->
Lwt_main.run
(let* db = connect conf in
f db)
(** Name of the table targeted by a reference (a content or a list) *)
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 [SELECT json_object(...)] query returning one json object per row of
[table]. [columns] are (json key, SQL expression) pairs. *)
let json_select_sql :
?where:string -> Model.Identifier.t -> (string * string) list -> string =
fun ?where table columns ->
let expr =
String.concat ", "
(ListLabels.map columns ~f:(fun (key, e) ->
Croquette_common.Sql.quote_literal key ^ ", " ^ e))
in
let q =
"SELECT json_object(" ^ expr ^ ") from " ^ Model.Identifier.quote table
in
match where with None -> q | Some w -> q ^ " where " ^ w
(** Build the [(json key, SQL expression)] columns selecting the single-column
fields of a table: one quoted column per field, skipping multivalued
references (they live in dedicated tables). With [~annotate_references], a
monovalued reference column is followed by a [[<field>_table]] literal
naming the targeted table, so the output is self-describing. *)
let field_columns :
?annotate_references:bool -> Model.field list -> (string * string) list =
fun ?(annotate_references = false) fields ->
List.concat_map
(fun 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.Types.MultiReference _ -> []
| Model.Types.MonoReference reference when annotate_references ->
[
(key, Model.Identifier.quote name);
( key ^ "_table",
Croquette_common.Sql.quote_literal
(target_table reference.Model.Types.t) );
]
| _ -> [ (key, Model.Identifier.quote name) ])
fields
(** Run a [json_select_sql] query over [table] and collect one json string per
row. With [~where] and [~id], the query is restricted to the rows where the
[where] column equals [id]. *)
let collect_json :
Caqti_lwt.connection ->
?where:Model.Identifier.t ->
?id:Int64.t ->
Model.Identifier.t ->
(string * string) list ->
string list Lwt.t =
fun db ?where ?id table columns ->
let module Db = (val db) in
let open Caqti.Templater in
let ( let* ) = Lwt.bind in
match (where, id) with
| Some col, Some id ->
let request =
direct
T.(int64 -->* string)
(json_select_sql
~where:(Model.Identifier.quote col ^ "=?")
table columns)
in
let* res = Db.collect_list request id in
Caqti_lwt.or_fail res
| None, None ->
let request =
direct T.(unit -->* string) (json_select_sql table columns)
in
let* res = Db.collect_list request () in
Caqti_lwt.or_fail res
| _ -> invalid_arg "collect_json: [where] and [id] must be given together"
(** Request returning one row of a content table as a json object *)
let content_as_json :
Model.content -> (int64, string, [ `One ]) Caqti.Template.Request.t =
fun content ->
let columns =
field_columns (List.of_seq (Model.Content.fields content))
@
match Model.Content.parent content with
| None -> []
| Some _ -> [ ("parent", Model.Identifier.quote Model.Identifier.parent) ]
in
let open Caqti.Templater in
direct
T.(int64 -->! string)
(json_select_sql
~where:(Model.Identifier.quote Model.Identifier.id ^ "=?")
(Model.Content.id content) columns)
(** Query all the multireferences tables for the given content and the given id.
Collect all the values and return them in the same json format as the same
content. *)
let collect_references :
Caqti_lwt.connection ->
Model.content ->
int64 ->
(string * int64 * string) list Lwt.t =
fun db content id ->
let ( let* ) = Lwt.bind in
let content_id = Model.Identifier.to_string (Model.Content.id content) in
(* Only keep the multivaluated fields *)
let fields =
Seq.filter
(fun field ->
let (T ty) = Model.Field.type_ field in
match ty with Model.Types.MultiReference _ -> true | _ -> false)
(Model.Content.fields content)
|> List.of_seq
in
let results =
Lwt_list.fold_left_s
(fun acc field ->
let field_name = Model.Field.name field in
let* json =
collect_json db ~where:Model.Identifier.parent ~id
(Model.Identifier.dot (Model.Content.id content) field_name)
[
( Model.Identifier.to_string field_name,
Model.Identifier.quote Model.Identifier.target );
]
in
let line =
ListLabels.fold_left ~init:acc json ~f:(fun acc json ->
(content_id, id, json) :: acc)
in
Lwt.return line)
[] fields
in
results
(** A provider giving a deterministic value for each field of a row, depending
on the field type and the row number. References point to the row of id 1 of
their target. *)
let provider_for_fields : Model.field list -> Int64.t -> string -> Value.value =
fun fields row field_name ->
match
List.find_opt
(fun field ->
String.equal field_name
(Model.Identifier.to_string (Model.Field.name field)))
fields
with
| None -> Value.none
| Some field -> (
let (Model.Field.T ty) = Model.Field.type_ field in
match ty with
| Model.Types.Text ->
Value.single
(Model.Types.Text, Some (field_name ^ "-" ^ Int64.to_string row))
| Model.Types.Integer -> Value.single (Model.Types.Integer, Some row)
| Model.Types.MonoReference reference ->
Value.single (Model.Types.MonoReference reference, Some 1L)
| Model.Types.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 (Model.Types.MultiReference reference, List.to_seq values)
)
(** Same, for the fields of a content *)
let provider_for : Model.content -> Int64.t -> string -> Value.value =
fun content row field_name ->
provider_for_fields
(List.of_seq (Model.Content.fields content))
row field_name
(** Render a [(named, data)] pair as a single line, whatever the field type *)
let show_field :
'db 'ocaml.
('db, 'ocaml, Model.target) Model.Types.t Model.Types.named ->
'ocaml ->
string =
fun named data ->
let name = Model.Identifier.to_string (Model.Types.name named) in
let show : type db ocaml.
(db, ocaml, Model.target) Model.Types.t -> ocaml -> string =
fun typ data ->
match typ with
| Model.Types.Text -> (
match data with
| Some s -> name ^ " = \"" ^ s ^ "\""
| None -> name ^ " = NULL")
| Model.Types.Integer -> (
match data with
| Some i -> name ^ " = " ^ Int64.to_string i
| None -> name ^ " = NULL")
| Model.Types.MonoReference _ -> (
match data with
| Some i -> name ^ " -> " ^ Int64.to_string i
| None -> name ^ " = NULL")
| Model.Types.MultiReference _ ->
let targets = List.of_seq data |> List.sort compare in
name ^ " -> ["
^ String.concat "," (List.map Int64.to_string targets)
^ "]"
in
show (Model.Types.typ named) data
(** Read a configuration from the standard input and run [f] with a fresh
in-memory database created from it *)
let exec_with_conf :
(Configuration.Ast.setting -> Caqti_lwt.connection -> 'a Lwt.t) ->
('a, Configuration.Read_conf.error) result =
fun f ->
let ( let+ ) opt cont = Result.map cont opt in
let+ configuration =
Configuration.Read_conf.read_string_conf (read_stdin ())
in
with_db configuration (fun db -> f configuration db)
|