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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
open StdLabels
let name = __FILE__
let description = "Insert data and check the result"
let speclist = []
type t =
((string * Int64.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_cut
(fun formatter (content_id, id, content) ->
Format.fprintf formatter "@[<v 2>%s:%Ld@;%s@]" content_id id content))
let query_columns : 'a Model.Content.t -> string =
fun content ->
let fields =
Model.Content.fields content
(* Remove the multivaluated fields from the query *)
|> Seq.filter (fun t ->
let (Model.Field.T ty) = Model.Field.type_ t in
match ty with Model.Typ.MultiReference _ -> false | _ -> true)
in
let buff = Buffer.create 64 in
let () =
Seq.iter
(fun field ->
Buffer.add_char buff '\'';
Buffer.add_string buff (Model.Field.name field);
Buffer.add_string buff "',";
Buffer.add_string buff (Model.Field.name field);
Buffer.add_string buff ",")
fields
in
let () =
match Model.Content.parent content with
| None ->
let length = Buffer.length buff in
if length > 1 then Buffer.truncate buff (length - 1);
()
| Some _ -> Buffer.add_string buff "'parent',parent"
in
Buffer.contents buff
type content = Model.Reference.t Model.Content.t
let query (table : content) =
Caqti_request.Infix.(Caqti_type.(int64) ->! Caqti_type.string)
~oneshot:true
("SELECT json_object(" ^ query_columns table ^ ") from "
^ Model.Content.id table ^ " where id=?")
let query_values (table : content) (feature : string) =
Caqti_request.Infix.(Caqti_type.(int64) ->* Caqti_type.string)
~oneshot:true
("SELECT json_object('" ^ feature ^ "', target) from \""
^ Model.Content.id table ^ "." ^ feature ^ "\" where parent=?")
type value = Model.Reference.t Model.Typ.value
type properties = (string * value) list
(** [insert_and_get_result db content entry] will insert the value in the
database and return the informations for the content just inserted.
[content] is the type definition for the element we want to insert [entry]
contains all the values to insert in the database. *)
let insert_and_get_result :
?parent:int64 ->
Caqti_lwt.connection ->
content ->
properties ->
(string * int64 * string) Lwt.t =
fun ?parent db content entry ->
let ( let* ) = Lwt.bind in
let* id =
Query.insert ?parent db content (fun v ->
match List.assoc_opt v entry with Some v -> v | None -> Model.Typ.None)
in
let content_id = Model.Content.id content in
let module Db = (val db) in
let* json_result = Db.find (query content) id in
let* json = Caqti_lwt.or_fail json_result in
Lwt.return (content_id, id, json)
(** Read the values to insert in the configuration *)
module Make (S : Decoders.Decode.S) = struct
let ( let* ) = S.( let* )
let ( let+ ) = S.( let+ )
type content_to_insert = content * properties list
let ( let@@ ) = Lwt.bind
(** Load the values associated with the configuration. *)
class loader (db : Caqti_lwt.connection) (conf : Configuration.Ast.setting) =
object (self)
method insert_and_return_references :
content -> properties list Lwt.t -> int64 list Lwt.t =
fun content properties ->
let ( let* ) = Lwt.bind in
let* properties = properties in
Lwt_list.map_s
(fun line ->
let* _, id, _ = insert_and_get_result db content line in
Lwt.return id)
properties
method value : content -> string -> (string * value) Lwt.t S.decoder =
fun content key ->
ignore content;
S.one_of
[
( "numeric",
S.map
(fun v ->
Lwt.return
(key, Model.Typ.(V (Integer, Some (Int64.of_int v)))))
S.int );
( "text",
S.map
(fun v -> Lwt.return (key, Model.Typ.(V (Text, Some v))))
S.string );
( "reference",
match Model.Content.find_target content key with
| None -> S.fail "Not a feature"
| Some target_ref ->
let target = Model.Reference.get target_ref in
let* properties = self#properties target in
let sequence =
let@@ properties = properties in
let@@ _, id, _ =
insert_and_get_result db target properties
in
let reference =
Model.Typ.{ t = target_ref; constraints = None }
in
Lwt.return
( key,
Model.Typ.(
V (Model.Typ.MonoReference reference, Some id)) )
in
S.succeed sequence );
( "references",
match Model.Content.find_target content key with
| None -> S.fail "Not a feature"
| Some target_ref ->
let target = Model.Reference.get target_ref in
let* properties = self#values target in
let sequence =
let@@ ids =
self#insert_and_return_references target properties
in
let reference =
Model.Typ.{ t = target_ref; constraints = None }
in
Lwt.return
( key,
Model.Typ.(
Seq
( Model.Typ.MultiReference reference,
List.to_seq ids )) )
in
S.succeed sequence );
( "other",
(* If the value is a dictionnary list, consider it as external references *)
S.succeed (Lwt.return (key, Model.Typ.None)) );
]
(** The values are encoded in a very simple way, depending of the nature
of the field in the json, we create a property in for text or numeric
*)
method properties : content -> properties Lwt.t S.decoder =
fun content ->
S.map Lwt.all (S.key_value_pairs_seq (self#value content))
method values : content -> properties list Lwt.t S.decoder =
fun content -> S.map Lwt.all (S.list (self#properties content))
method load_content : content_to_insert list Lwt.t S.decoder =
let* properties =
S.key_value_pairs_seq (fun key ->
S.one_of
[
( "content",
fun v ->
match (Configuration.Ast.get_content conf) key with
| None -> Error (Decoders.Error.make "no content")
| Some subcontent ->
let values = self#values subcontent v in
Result.map
(fun v ->
let content_to_insert =
let@@ v = v in
Lwt.return (subcontent, v)
in
Some content_to_insert)
values );
("other", fun _ -> Ok None);
])
in
let properties = List.filter_map ~f:(fun v -> v) properties in
S.succeed (Lwt.all properties)
(** Read the content and for each key, check if there is an existing
subcontent with the same name, If this subcontent exists, parse all
the subcontents with all the values and return them. *)
method root_content :
string ->
(content * (properties * content_to_insert list) list) Lwt.t S.decoder
=
fun name ->
(* In this syntax, the content is supposed to follow the definition
and will never be None *)
let content = Configuration.Ast.get_content conf name |> Option.get in
let* properties =
S.field_opt_or ~default:(Lwt.return []) "values"
(self#values content)
in
(* Look for subcontents in the given values. The subcontent is
identfied by having a key name equal to the subcontent
identifier. *)
let* subcontents : content_to_insert list list Lwt.t =
S.map Lwt.all
(S.field_opt_or ~default:[] "values" (S.list self#load_content))
in
let result =
let@@ properties = properties in
let@@ subcontents = subcontents in
let to_insert = List.combine properties subcontents in
Lwt.return (content, to_insert)
in
S.succeed result
method content :
(content * (properties * content_to_insert list) list) Lwt.t S.decoder
=
let* name = S.field "id" S.string in
let* properties = self#root_content name in
S.succeed properties
method configuration :
(content * (properties * content_to_insert list) list) list Lwt.t
S.decoder =
let* content = S.field "content" (S.list self#content) in
let conf = Lwt.all content in
S.succeed conf
end
end
module D = Make (Configuration.Json5.Decode)
(** 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 -> content -> int64 -> 'a list Lwt.t =
fun db content id ->
let module Db = (val db) in
let ( let* ) = Lwt.bind in
let content_id = 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.Typ.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_result =
Db.collect_list (query_values content field_name) id
in
let* json = Caqti_lwt.or_fail json_result in
let line =
List.fold_left ~init:acc json ~f:(fun acc json ->
(content_id, id, json) :: acc)
in
Lwt.return line)
[] fields
in
results
let exec : string list -> t =
fun _ ->
let stdin = In_channel.input_all stdin in
let ( let** ) = Result.bind in
let** configuration = Configuration.Read_conf.read_string_conf stdin in
Lwt_main.run
(let ( let* ) = Lwt.bind in
let* db = Common.connect configuration in
let module Db = (val db) in
let inserts =
Configuration.Json5.Decode.decode_string
(new D.loader db configuration)#configuration stdin
in
match inserts with
| Error err -> Lwt.return_error err
| Ok inserts ->
let* inserts = inserts in
let* insertions =
Lwt_list.fold_left_s
(fun acc (content, entries) ->
Lwt_list.fold_left_s
(fun acc (entry, subcontents) ->
(* Insert the main content *)
let* content_id, id, json =
insert_and_get_result db content entry
in
let acc = (content_id, id, json) :: acc in
(* Also query the multivaluated references *)
let* ext = collect_references db content id in
let acc = List.append ext acc in
(* Insert the subcontent associated *)
Lwt_list.fold_left_s
(fun acc (subcontent, entries) ->
let* acc =
Lwt_list.fold_left_s
(fun acc entry ->
let* subcontent_id, id, json =
insert_and_get_result ~parent:id db subcontent
entry
in
let acc = (subcontent_id, id, json) :: acc in
(* Also query the multivaluated references *)
let* ext = collect_references db subcontent id in
let acc = List.append ext acc in
Lwt.return acc)
acc entries
in
Lwt.return acc)
acc subcontents)
acc entries)
[] inserts
in
Lwt.return_ok (List.rev insertions))
|