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
|
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))
type content = Model.content
type value = Model.value
type properties = (string * value) list
(* A subcontent to insert, with one entry (properties) per row *)
type content_to_insert = content * properties list
(* One entry of the "values" list: the properties of a row, and the entries
of each subcontent (identified by a key equal to the subcontent name) *)
type entry = properties * content_to_insert 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.Identifier.to_string (Model.Content.id content) in
let module Db = (val db) in
let* json_result = Db.find (Common.content_as_json content) id in
let* json = Caqti_lwt.or_fail json_result in
Lwt.return (content_id, id, json)
(** Insert one row and collect the result line, together with the lines of its
multivalued references. Return the accumulator and the id of the new row. *)
let insert_one :
Caqti_lwt.connection ->
?parent:int64 ->
content ->
properties ->
(string * Int64.t * string) list ->
((string * Int64.t * string) list * Int64.t) Lwt.t =
fun db ?parent content entry acc ->
let ( let* ) = Lwt.bind in
let* content_id, id, json = insert_and_get_result ?parent db content entry in
let* ext = Common.collect_references db content id in
Lwt.return (List.append ext ((content_id, id, json) :: acc), id)
module S = Configuration.Json5.Decode
let ( let* ) = Lwt.bind
(* bind in the decoder monad *)
let ( let** ) = S.( let* )
(* Force a list of Lwt.t sequentially: the sqlite connection does not support
concurrent usage, and some of these Lwt.t perform inserts *)
let seq_lwt : 'a Lwt.t list -> 'a list Lwt.t =
fun l -> Lwt_list.map_s (fun x -> x) l
let is_field : content -> string -> bool =
fun content key ->
Seq.exists
(fun field ->
String.equal key Model.(Identifier.to_string (Field.name field)))
(Model.Content.fields content)
(** Decode the value of a field: the json nature of the value tells the type
(numeric, text), or it is a reference (an integer for a list target, an
object holding the properties of a new row of the target content) *)
let rec value :
Caqti_lwt.connection ->
content ->
string ->
(string * value) Lwt.t S.decoder =
fun db content key ->
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", reference_value db content key false);
("references", reference_value db content key true);
("other", S.succeed (Lwt.return (key, Model.Typ.None)));
]
(** Decode a reference field: single value or list of values, depending on
[multivaluated].
A list target is referenced by the id of one of its rows (an integer). A
content target is referenced by an object holding the properties of a new
row, inserted in its own table — the inserts are chained in a single Lwt
promise, because the sqlite connection does not support concurrent usage. *)
and reference_value :
Caqti_lwt.connection ->
content ->
string ->
bool ->
(string * value) Lwt.t S.decoder =
fun db content key multivaluated ->
match Model.Content.find_target content key with
| None -> S.fail "Not a feature"
| Some t -> (
let reference = Model.Typ.{ t; constraints = None } in
match t with
| Model.Reference.List _ ->
if multivaluated then
S.map
(fun ids ->
Lwt.return
( key,
Model.Typ.Seq
( Model.Typ.MultiReference reference,
List.to_seq (List.map ~f:Int64.of_int ids) ) ))
(S.list S.int)
else
S.map
(fun v ->
Lwt.return
( key,
Model.Typ.V
(Model.Typ.MonoReference reference, Some (Int64.of_int v))
))
S.int
| Model.Reference.Content target_ref ->
let target = Model.Reference.get target_ref in
if multivaluated then
let** entries = S.list (properties db target) in
S.succeed
(let* entries = seq_lwt entries in
let* ids =
Lwt_list.map_s
(fun props ->
let* _, id, _ = insert_and_get_result db target props in
Lwt.return id)
entries
in
Lwt.return
( key,
Model.Typ.Seq
(Model.Typ.MultiReference reference, List.to_seq ids) ))
else
let** props = properties db target in
S.succeed
(let* props = props in
let* _, id, _ = insert_and_get_result db target props in
Lwt.return
(key, Model.Typ.V (Model.Typ.MonoReference reference, Some id)))
)
and properties : Caqti_lwt.connection -> content -> properties Lwt.t S.decoder =
fun db content -> S.map seq_lwt (S.key_value_pairs_seq (value db content))
(** One entry of the "values" list of a content: each key is decoded once, as a
field value or as the entries of a subcontent *)
and entry :
Caqti_lwt.connection ->
Configuration.Ast.setting ->
content ->
entry Lwt.t S.decoder =
fun db conf content ->
let key_value key =
if is_field content key then S.map (fun v -> `Prop v) (value db content key)
else
match
Configuration.Ast.get_content conf (Model.Identifier.of_string key)
with
| Some subcontent -> S.map (fun v -> `Sub v) (sub_entries db subcontent)
| None -> S.map (fun v -> `Prop v) (value db content key)
in
S.map
(fun pairs ->
let props =
List.filter_map pairs ~f:(function `Prop v -> Some v | `Sub _ -> None)
in
let subs =
List.filter_map pairs ~f:(function `Sub v -> Some v | `Prop _ -> None)
in
let* properties = seq_lwt props in
let* subs = seq_lwt subs in
Lwt.return (properties, subs))
(S.key_value_pairs_seq key_value)
(** The entries of a subcontent: a list of plain properties, one per row *)
and sub_entries :
Caqti_lwt.connection -> content -> content_to_insert Lwt.t S.decoder =
fun db content ->
S.map
(fun entries ->
let* entries = seq_lwt entries in
Lwt.return (content, entries))
(S.list (properties db content))
(** Decode one content of the data document: its id and its "values" entries *)
and content_entry :
Caqti_lwt.connection ->
Configuration.Ast.setting ->
(content * entry list) Lwt.t S.decoder =
fun db conf ->
let** name = S.field "id" S.string in
let content =
Configuration.Ast.get_content conf (Model.Identifier.of_string name)
|> Option.get
in
S.map
(fun entries ->
let* entries = seq_lwt entries in
Lwt.return (content, entries))
(S.field_opt_or ~default:[] "values" (S.list (entry db conf content)))
(** Decode the whole data document *)
and data_document :
Caqti_lwt.connection ->
Configuration.Ast.setting ->
(content * entry list) list Lwt.t S.decoder =
fun db conf ->
S.map seq_lwt (S.field "content" (S.list (content_entry db conf)))
let exec : string list -> t =
fun _ ->
(* The same document holds both the configuration and the data to insert:
the configuration reader ignores the "values" keys, the data reader
ignores the "section" keys *)
let stdin = Common.read_stdin () in
let ( let** ) = Result.bind in
let** configuration = Configuration.Read_conf.read_string_conf stdin in
Common.with_db configuration (fun db ->
match S.decode_string (data_document db configuration) stdin 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* acc, id = insert_one db content entry acc in
(* Insert the subcontents associated *)
Lwt_list.fold_left_s
(fun acc (subcontent, subentries) ->
Lwt_list.fold_left_s
(fun acc entry ->
let* acc, _ =
insert_one db ~parent:id subcontent entry acc
in
Lwt.return acc)
acc subentries)
acc subcontents)
acc entries)
[] inserts
in
Lwt.return_ok (List.rev insertions))
|