summaryrefslogtreecommitdiff
path: root/lib/query/update.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/query/update.ml')
-rw-r--r--lib/query/update.ml42
1 files changed, 29 insertions, 13 deletions
diff --git a/lib/query/update.ml b/lib/query/update.ml
index 6d20999..d828b6b 100644
--- a/lib/query/update.ml
+++ b/lib/query/update.ml
@@ -8,9 +8,12 @@ module UpdateTemplate = Encoder.Map (struct
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 = column; content_type = Text; _ } ->
+ Model.Identifier.quote column ^ "=?"
+ | { name = column; content_type = Integer; _ } ->
+ Model.Identifier.quote column ^ "=?"
+ | { name = column; content_type = MonoReference _; _ } ->
+ Model.Identifier.quote column ^ "=?"
| { name = _; content_type = MultiReference _; _ } -> raise Not_found
include Encoder.ConcatParam
@@ -33,10 +36,13 @@ let update_fields :
let types_param =
Caqti.Templater.T.(t2 (Encoder.CaqtiRequest.f () query_fields) int64)
in
+ let module Identifier = Model.Identifier in
+ let id_key = Identifier.quote Identifier.id 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)
+ Format.fprintf format "UPDATE %s SET %s WHERE %s = ?"
+ (Identifier.quote (Model.Content.id content))
+ format_param id_key)
in
let request = Caqti.Templater.(direct T.(types_param -->. unit) q) in
let query_param =
@@ -72,7 +78,8 @@ 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 ->
+ (Model.Identifier.t * (int64, Model.Reference.t) Model.Typ.t * int64 Seq.t)
+ list ->
int64 ->
unit Lwt.t =
fun db content fields id ->
@@ -93,11 +100,16 @@ let update_mulivaluated_field :
ignore ref;
Intf.(TT data_type))
in
+ let module Identifier = Model.Identifier in
+ let id_key = Identifier.quote Identifier.id
+ and parent = Identifier.quote Identifier.parent
+ and target = Identifier.quote Identifier.target 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
+ Format.fprintf f "DELETE FROM %s where %s=? and %s not in (%s)"
+ (Identifier.quote
+ (Identifier.dot (Model.Content.id content) field_name))
+ parent target
(Encoder.EncodeParameter.f () existing_references_t))
in
let caqti_param = Encoder.CaqtiRequest.f () existing_references_t in
@@ -114,8 +126,10 @@ let update_mulivaluated_field :
(* 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)
+ Format.fprintf f "SELECT %s FROM %s WHERE %s=?" id_key
+ (Identifier.quote
+ (Identifier.dot (Model.Content.id content) field_name))
+ parent)
in
let request_select =
Caqti.Templater.(direct T.(int64 -->* int64) q_select)
@@ -154,10 +168,12 @@ let q :
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
+ let value = provider (Model.Identifier.to_string name) in
match value with
| Model.Typ.None -> acc
- | Model.Typ.V _ as value -> ((tt, (name, value)) :: fields, references)
+ | Model.Typ.V _ as value ->
+ ( (tt, (Model.Identifier.to_string name, value)) :: fields,
+ references )
| Model.Typ.Seq (t, seq) -> (fields, (name, t, seq) :: references))
([], [])
(Model.Content.fields content)