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
|
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))
(** Read the values to insert in the configuration *)
module Make (S : Decoders.Decode.S) = struct
let ( let* ) = S.( let* )
let ( let+ ) = S.( let+ )
type value = Model.Reference.t Model.Typ.value
(** 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 *)
let value : value S.decoder =
S.one_of
[
( "numeric",
S.map (fun v -> Model.Typ.(V (Integer, Int64.of_int v))) S.int );
("text", S.map (fun v -> Model.Typ.(V (Text, v))) S.string);
("other", S.map (fun _ -> Model.Typ.None) S.value);
]
type content = Model.Reference.t Model.Content.t
type properties = (string * value) list
type content_to_insert = content * properties list
let rec properties :
Configuration.Ast.setting ->
string ->
(content * (properties * content_to_insert list) list) S.decoder =
fun conf name ->
(* We are loading the same configuration template, the content is supposed
to exists by definition and will never be None *)
let content = Configuration.Ast.get_content conf name |> Option.get in
let* properties =
S.field_opt_or ~default:[] "values" (S.list (S.key_value_pairs value))
in
let* subcontents =
S.field_opt_or ~default:[] "values" (S.list (subcontents conf))
in
let to_insert = List.combine properties subcontents in
S.succeed (content, to_insert)
and subcontents :
Configuration.Ast.setting -> content_to_insert list S.decoder =
fun conf ->
let* properties =
S.key_value_pairs_seq (fun key ->
S.one_of
[
( "subcontent",
fun v ->
match Configuration.Ast.get_content conf key with
| None -> Error (Decoders.Error.make "no content")
| Some subcontent ->
let res = S.list (S.key_value_pairs value) v in
let v = Result.map (fun v -> Some (subcontent, v)) res in
v );
("other", fun _ -> Ok None);
])
in
let properties = List.filter_map ~f:(fun v -> v) properties in
S.succeed properties
let content :
Configuration.Ast.setting ->
(content * (properties * content_to_insert list) list) S.decoder =
fun conf ->
let* name = S.field "id" S.string in
properties conf name
let configuration :
Configuration.Ast.setting ->
(content * (properties * content_to_insert list) list) list S.decoder =
fun conf -> S.field "content" (S.list (content conf))
end
module D = Make (Configuration.Json5.Decode)
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
let query table =
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 insert_and_get_result ?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)
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
let* inserts =
Configuration.Json5.Decode.decode_string
(D.configuration configuration)
stdin
in
Lwt_main.run
(let ( let* ) = Lwt.bind in
let* db = Common.connect configuration in
let module Db = (val db) in
let* insertions =
Lwt_list.fold_left_s
(fun acc (content, entries) ->
Lwt_list.fold_left_s
(fun acc (entry, subcontents) ->
let* content_id, id, json =
insert_and_get_result db content entry
in
let acc = (content_id, id, json) :: 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
Lwt.return ((subcontent_id, id, json) :: acc))
acc entries
in
Lwt.return acc)
acc subcontents)
acc entries)
[] inserts
in
Lwt.return_ok (List.rev insertions))
|