blob: 39c32d0a6661cdcbe256e861436346ad1d55799f (
plain)
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
|
(** Generate the insert queries for the different database *)
exception TypeMismatch
(** Exception raised when the given type does not match the value *)
(** Transform the list of fields from the model into a tuple with the type of
each field associated with *)
let collect_fields :
?init_fields:Model.Reference.t Model.Field.t list ->
?filter:('a Model.Field.t -> bool) ->
'a Model.Content.t ->
Intf.t =
fun ?(init_fields = []) ?filter content ->
let field_seq =
match filter with
| None -> Model.Content.fields content
| Some f -> Model.Content.fields content |> Seq.filter f
in
let fields = init_fields @ List.of_seq field_seq in
Encoder.list_to_tup fields ~f:(fun field ->
let name = Model.Field.name field in
let (Model.Field.T content) = Model.Field.type_ field in
TT { name; content })
(** Insert a row. This syntax use RETURNING ID to get the value we just inserted
*)
let insert_returning :
Caqti_lwt.connection ->
Model.Reference.t Model.Content.t ->
(string -> Model.Reference.t Model.Typ.value) ->
int64 Lwt.t =
fun db content provider ->
let module Db = (val db) in
let module CaqtiValues = Encoder.Map (struct
include Encoder.DefaultEncoder
type 'a t = 'a
let f : type a. a Intf.data -> a t =
fun Intf.{ name; content } ->
let result = provider name in
(* Check the value given by the provider and ensure it’s match
the expected type *)
match (content, result) with
| _, None ->
(* The provider does not have any value for this property, use the
default value*)
Model.Typ.default_value content
| _, V (field_type', value) -> (
match Model.Typ.eq content field_type' with
| Some Equal -> Some value
| None -> raise TypeMismatch)
end) in
let init_fields =
match Model.Content.parent content with
| None -> []
| Some _ -> Model.Field.[ create (T Integer) [] ~required:true "parent" ]
in
(* Remove the mulivaluated content fields from the request because they are
not inserted in the table directly *)
let (T t) =
collect_fields content
~filter:(fun field ->
let (T ty) = Model.Field.type_ field in
match ty with Model.Typ.MultiReference _ -> false | _ -> true)
~init_fields
in
let insert_template =
match t with Intf.T0 -> "" | _ -> "(" ^ Encoder.EncodeInsert.f t ^ ")"
and columns =
match t with
| Intf.T0 -> "DEFAULT"
| _ -> "(" ^ Encoder.InsertTableName.f t ^ ")"
in
let buff = Buffer.create 16 in
let format = Format.formatter_of_buffer buff in
let () =
Format.fprintf format "INSERT INTO \"%s\" %s VALUES %s RETURNING ID"
(Model.Content.id content) columns insert_template
in
Format.pp_print_flush format ();
let types_param = Encoder.CaqtiRequest.f t in
let request =
Caqti_request.Infix.((types_param ->! Caqti_type.int64) ~oneshot:true)
(Buffer.contents buff)
in
let ( let* ) = Lwt.bind in
let values = CaqtiValues.f t in
(* The insert request is returning the ID. That’s why we call it with the
fuction [find] even if it is not a real find in the database. *)
let* id_result = Db.find request values in
let* id = Caqti_lwt.or_fail id_result in
(* The value is created in the db, reuse the ID in order to insert the
multi-values fields in their own tables *)
let (T multi_valuated_fields) =
collect_fields content ~filter:(fun field ->
let (T ty) = Model.Field.type_ field in
match ty with Model.Typ.MultiReference _ -> true | _ -> false)
in
let module Fold = Encoder.Fold (struct
type 'a t = 'a Lwt.t
(** The function is called for each feature declared as multi reference. *)
let f : 'a Lwt.t -> 'b Intf.data -> 'a Lwt.t =
fun acc Intf.{ name; content } ->
ignore content;
let values = provider name in
ignore values;
let* acc = acc in
Lwt.return acc
end) in
let* () = Fold.f Lwt.return_unit multi_valuated_fields in
Lwt.return id
(** Insert an entry for a specific content *)
let insert :
Caqti_lwt.connection ->
Model.Reference.t Model.Content.t ->
?parent:int64 ->
(string -> Model.Reference.t Model.Typ.value) ->
int64 Lwt.t =
fun db content ?parent provider ->
let provider =
match (parent, Model.Content.parent content) with
| _, None -> provider
| None, Some _ ->
(* Raise an error right now if the parent of the subcontent is not
given *)
raise TypeMismatch
| Some v, Some _ -> (
(* Otherwise update the provider to give a value for the parent *)
function
| "parent" -> Model.Typ.(V (Integer, v))
| other -> provider other)
in
let module Db = (val db) in
match Caqti_driver_info.dialect_tag Db.driver_info with
| `Sqlite | `Pgsql -> insert_returning db content provider
| _ -> raise Not_found
|