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
|
(** Generate the update queries for a content *)
type provider = string -> Model.Reference.t Model.Typ.value
module UpdateTemplate = Encoder.Map (struct
type 'a t = string
type env = unit
let f : type a. unit -> a Intf.data -> a t =
fun () -> function
| { name = column; content_type = Text; _ } -> column ^ "=?"
| { name = column; content_type = Integer; _ } -> column ^ "=?"
| { name = column; content_type = MonoReference _; _ } -> column ^ "=?"
| { name = _; content_type = MultiReference _; _ } -> raise Not_found
include Encoder.ConcatParam
end)
let update_fields :
Caqti_lwt.connection ->
'a Model.Content.t ->
(Intf.t_type * (string * Model.Reference.t Model.Typ.value)) list ->
int64 ->
unit Lwt.t =
fun db content fields id ->
let module Db = (val db) in
let fields_list, value_list = List.split fields in
(* Update all the fields stored in the content table *)
let (T query_fields) =
Encoder.list_to_tup fields_list ~f:(fun field -> field)
in
let format_param = (UpdateTemplate.f ()) query_fields in
let types_param =
Caqti.Templater.T.(t2 (Encoder.CaqtiRequest.f () query_fields) int64)
in
let q =
Croquette_common.Format.format_string (fun format ->
Format.fprintf format "UPDATE \"%s\" SET %s WHERE ID = ?"
(Model.Content.id content) format_param)
in
let request = Caqti.Templater.(direct T.(types_param -->. unit) q) in
let query_param =
Encoder.DataToCaqtiValues.f
(fun name -> List.assoc name value_list)
query_fields
in
let ( let* ) = Lwt.bind in
let* result = Db.exec request (query_param, id) in
Caqti_lwt.or_fail result
(** Encode a sequence of references into a type usable in Caqti request *)
module ReferenceListEncoder = Encoder.Map (struct
include Encoder.DefaultEncoder
type 'a t = 'a
type env = (int64, Model.Reference.t) Model.Typ.t * Int64.t Seq.t ref
let f : type a. env -> a Intf.data -> a t =
fun (t, env) d ->
match !env () with
| Seq.Nil -> raise Not_found
| Cons (value, tl) -> (
env := tl;
match Model.Typ.eq t d.Intf.content_type with
| Some Type.Equal -> value
| None -> raise Encoder.TypeMismatch)
end)
module ReferencesSet = Set.Make (Int64)
let update_mulivaluated_field :
Caqti_lwt.connection ->
Model.Reference.t Model.Content.t ->
(string * (int64, Model.Reference.t) Model.Typ.t * int64 Seq.t) list ->
int64 ->
unit Lwt.t =
fun db content fields id ->
let module Db = (val db) in
let ( let* ) = Lwt.bind in
List.fold_left
(fun acc (field_name, t, values) ->
let* () = acc in
let values = ReferencesSet.of_seq values in
let lvalues = ReferencesSet.to_list values in
let data_type = Intf.{ name = field_name; content_type = t } in
(* First delete all the values not belonging to the new set *)
let (T existing_references_t) =
Encoder.list_to_tup lvalues ~f:(fun ref ->
ignore ref;
Intf.(TT data_type))
in
let q_delete =
Croquette_common.Format.format_string (fun f ->
Format.fprintf f
"DELETE FROM \"%s.%s\" where parent=? and target not in (%s)"
(Model.Content.id content) field_name
(Encoder.EncodeParameter.f () existing_references_t))
in
let caqti_param = Encoder.CaqtiRequest.f () existing_references_t in
let request_delete =
Caqti.Templater.(direct T.(t2 int64 caqti_param -->. unit) q_delete)
in
let seq = ref (ReferencesSet.to_seq values) in
let* result =
Db.exec request_delete
(id, ReferenceListEncoder.f (t, seq) existing_references_t)
in
let* () = Caqti_lwt.or_fail result in
(* Then collect all the existing remaining values *)
let q_select =
Croquette_common.Format.format_string (fun f ->
Format.fprintf f "SELECT id FROM \"%s.%s\" WHERE parent=?"
(Model.Content.id content) field_name)
in
let request_select =
Caqti.Templater.(direct T.(int64 -->* int64) q_select)
in
let* request_result = Db.collect_list request_select id in
let* remaining' = Caqti_lwt.or_fail request_result in
(* Finaly insert the missing values.
At this step, we can just use the code from the insert module. *)
let remaining = ReferencesSet.(diff values (of_list remaining')) in
let v = Model.Typ.Seq (t, ReferencesSet.to_seq remaining) in
Insert.insert_references db content id field_name v data_type)
Lwt.return_unit fields
let q :
Caqti_lwt.connection ->
Int64.t ->
Model.Reference.t Model.Content.t ->
(string -> Model.Reference.t Model.Typ.value) ->
unit Lwt.t =
fun db id content provider ->
let module Db = (val db) in
(* Get all the single fields to update in the content *)
(* There is a difference between
- Typ.None
- Typ.V None
The former will be ignored in the update, and the value will remains as is
The later will set the content to NULL in the database. *)
let fields, references =
Seq.fold_left
(fun ((fields, references) as acc) field ->
let name = Model.Field.name field in
let (Model.Field.T content_type) = Model.Field.type_ field in
let tt = Intf.TT { name; content_type } in
let value = provider name in
match value with
| Model.Typ.None -> acc
| Model.Typ.V _ as value -> ((tt, (name, value)) :: fields, references)
| Model.Typ.Seq (t, seq) -> (fields, (name, t, seq) :: references))
([], [])
(Model.Content.fields content)
in
let ( let* ) = Lwt.bind in
let* () =
match fields with
| [] -> Lwt.return_unit
| _ -> update_fields db content fields id
in
let* () =
match references with
| [] -> Lwt.return_unit
| _ -> update_mulivaluated_field db content references id
in
Lwt.return_unit
|