summaryrefslogtreecommitdiff
path: root/lib/query
diff options
context:
space:
mode:
Diffstat (limited to 'lib/query')
-rw-r--r--lib/query/create.ml89
-rw-r--r--lib/query/encoder.ml13
-rw-r--r--lib/query/insert.ml26
-rw-r--r--lib/query/insert.mli2
-rw-r--r--lib/query/intf.ml2
-rw-r--r--lib/query/tests/create.t9
-rw-r--r--lib/query/update.ml42
7 files changed, 111 insertions, 72 deletions
diff --git a/lib/query/create.ml b/lib/query/create.ml
index d34339b..05aec0f 100644
--- a/lib/query/create.ml
+++ b/lib/query/create.ml
@@ -17,14 +17,15 @@ let get_field_query : _ Model.Field.t -> string option =
in
Option.map
(fun t ->
- String.concat ~sep:" " [ "\"" ^ Model.Field.name field ^ "\""; t ])
+ String.concat ~sep:" "
+ [ Model.Identifier.quote (Model.Field.name field); t ])
ty
type foreign = {
- src_table : string;
- src_column : string;
- dst_table : string;
- dst_column : string;
+ src_table : Model.Identifier.t;
+ src_column : Model.Identifier.t;
+ dst_table : Model.Identifier.t;
+ dst_column : Model.Identifier.t;
}
(** Foreign key between a column referencing a content and the referenced value
*)
@@ -35,10 +36,15 @@ ADD FOREIGN KEY (best_a_id)
REFERENCES answer(a_id);
*)
-let get_foreign_statement : string -> _ Model.Content.t -> string =
+let get_foreign_statement : Model.Identifier.t -> _ Model.Content.t -> string =
fun property content ->
- "FOREIGN KEY(\"" ^ property ^ "\") REFERENCES " ^ Model.Content.id content
- ^ "(\"id\")"
+ "FOREIGN KEY("
+ ^ Model.Identifier.quote property
+ ^ ") REFERENCES "
+ ^ Model.Identifier.quote (Model.Content.id content)
+ ^ "("
+ ^ Model.Identifier.quote Model.Identifier.id
+ ^ ")"
(** Create the dedicated table required for multivaluated fields *)
let create_external_reference_table :
@@ -62,13 +68,15 @@ let create_external_reference_table :
"\"id\" INTEGER PRIMARY KEY";
"\"parent\" INTEGER NOT NULL";
"\"target\" INTEGER NOT NULL";
- get_foreign_statement "parent" content;
- get_foreign_statement "target" target;
+ get_foreign_statement Model.Identifier.parent content;
+ get_foreign_statement Model.Identifier.target target;
]
in
let () =
- Format.fprintf format "CREATE TABLE \"%s.%s\"@; @[@[<v>( %a@] )@]"
- (Model.Content.id content) (Model.Field.name field)
+ Format.fprintf format "CREATE TABLE %s@; @[@[<v>( %a@] )@]"
+ (Model.Identifier.quote
+ (Model.Identifier.dot (Model.Content.id content)
+ (Model.Field.name field)))
(Format.pp_print_list
~pp_sep:(fun f () ->
Format.pp_print_break f 0 0;
@@ -94,6 +102,7 @@ let rec create_table :
_ Model.Content.t ->
(foreign list * Model.Reference.t Model.Content.t list) Lwt.t =
fun db (alter_queries, created) content ->
+ let module Identifier = Model.Identifier in
(* The foreign keys are added at the end of the query, and need to be
defined first *)
let alter_queries =
@@ -102,9 +111,9 @@ let rec create_table :
| Some p ->
{
src_table = Model.Content.id content;
- src_column = "parent";
+ src_column = Model.Identifier.parent;
dst_table = Model.Content.id p;
- dst_column = "id";
+ dst_column = Model.Identifier.id;
}
:: alter_queries
in
@@ -115,7 +124,7 @@ let rec create_table :
the foreign key constraint *)
let init_foreign =
match Model.Content.parent content with
- | Some parent -> [ get_foreign_statement "parent" parent ]
+ | Some parent -> [ get_foreign_statement Identifier.parent parent ]
| None -> []
in
let foreign, alter_queries =
@@ -140,7 +149,7 @@ let rec create_table :
src_table = Model.Content.id content;
src_column = column;
dst_table = Model.Content.id dst;
- dst_column = "id";
+ dst_column = Model.Identifier.id;
}
:: alter_queries
in
@@ -162,8 +171,8 @@ let rec create_table :
let buff = Buffer.create 128 in
let format = Format.formatter_of_buffer buff in
let () =
- Format.fprintf format "CREATE TABLE \"%s\"@; @[@[<v>( %a@] )@]"
- (Model.Content.id content)
+ Format.fprintf format "CREATE TABLE %s@; @[@[<v>( %a@] )@]"
+ (Identifier.quote (Model.Content.id content))
(Format.pp_print_list
~pp_sep:(fun f () ->
Format.pp_print_break f 0 0;
@@ -197,28 +206,30 @@ let rec create_table :
(** Modify the table and add the foreign key in case of cyclic references *)
let add_foreign_statement : Caqti_lwt.connection -> foreign list -> unit Lwt.t =
fun db foreigns ->
- let request_str =
- "ALTER TABLE ? ADD CONSTRAINT FOREIGN KEY(?) REFERENCES ?(?)"
- in
- let request =
- let open Caqti.Templater in
- direct T.(t4 string string string string -->. unit) request_str
- in
let module Db = (val db) in
- let* () =
- Lwt_list.iter_s
- (fun foreign ->
- let* result =
- Db.exec request
- ( foreign.src_table,
- foreign.src_column,
- foreign.dst_table,
- foreign.dst_column )
- in
- Caqti_lwt.or_fail result)
- foreigns
- in
- Lwt.return_unit
+ let module Identifier = Model.Identifier in
+ (* Identifiers cannot be passed as query parameters, so the statement is
+ built with validated and quoted identifiers for each constraint. *)
+ Lwt_list.iter_s
+ (fun foreign ->
+ let request_str =
+ "ALTER TABLE "
+ ^ Identifier.quote foreign.src_table
+ ^ " ADD CONSTRAINT FOREIGN KEY("
+ ^ Identifier.quote foreign.src_column
+ ^ ") REFERENCES "
+ ^ Identifier.quote foreign.dst_table
+ ^ "("
+ ^ Identifier.quote foreign.dst_column
+ ^ ")"
+ in
+ let request =
+ let open Caqti.Templater in
+ direct T.(unit -->. unit) request_str
+ in
+ let* result = Db.exec request () in
+ Caqti_lwt.or_fail result)
+ foreigns
let create : Caqti_lwt.connection -> Configuration.Ast.setting -> unit Lwt.t =
fun db conf ->
diff --git a/lib/query/encoder.ml b/lib/query/encoder.ml
index 6897ec7..5ee6727 100644
--- a/lib/query/encoder.ml
+++ b/lib/query/encoder.ml
@@ -271,10 +271,13 @@ module InsertTableName = Map (struct
let f : type a. unit -> a data -> a t =
fun () -> function
- | { name = column; content_type = Text; _ } -> column
- | { name = column; content_type = Integer; _ } -> column
- | { name = column; content_type = MultiReference _; _ } -> 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 = MultiReference _; _ } ->
+ Model.Identifier.quote column
+ | { name = column; content_type = MonoReference _; _ } ->
+ Model.Identifier.quote column
include ConcatParam
end)
@@ -292,7 +295,7 @@ module DataToCaqtiValues = Map (struct
let f : type a. env -> a Intf.data -> a t =
fun provider Intf.{ name; content_type } ->
- let result = provider name in
+ let result = provider (Model.Identifier.to_string name) in
(* Check the value given by the provider and ensure it’s match the
expected type *)
match result with
diff --git a/lib/query/insert.ml b/lib/query/insert.ml
index c124f0a..6e95631 100644
--- a/lib/query/insert.ml
+++ b/lib/query/insert.ml
@@ -60,9 +60,12 @@ module MultiReferencesInsert = struct
fun ({ db; parent_id; provider; k; content } as acc)
Intf.{ name; content_type } ->
let module Db = (val db) in
+ let module Identifier = Model.Identifier in
+ let parent = Identifier.quote Identifier.parent
+ and target = Identifier.quote Identifier.target in
match content_type with
| Model.Typ.MultiReference _target -> (
- let result = provider name in
+ let result = provider (Model.Identifier.to_string name) in
match result with
| Model.Typ.None -> acc
| Model.Typ.V _ -> raise Encoder.TypeMismatch
@@ -73,9 +76,10 @@ module MultiReferencesInsert = struct
let q =
Croquette_common.Format.format_string (fun format ->
Format.fprintf format
- "INSERT INTO \"%s.%s\" (\"parent\", \"target\") VALUES \
- (?, ?)"
- (Model.Content.id content) name)
+ "INSERT INTO %s (%s, %s) VALUES (?, ?)"
+ (Identifier.quote
+ (Identifier.dot (Model.Content.id content) name))
+ parent target)
in
let request : request =
Caqti.Templater.(dynamic T.(t2 int64 int64 -->. unit) q)
@@ -109,7 +113,9 @@ let insert_returning :
let init_fields =
match Model.Content.parent content with
| None -> []
- | Some _ -> Model.Field.[ create (T Integer) [] ~required:true "parent" ]
+ | Some _ ->
+ Model.Field.
+ [ create (T Integer) [] ~required:true Model.Identifier.parent ]
in
(* Remove the mulivaluated content fields from the request because they are
@@ -131,10 +137,12 @@ let insert_returning :
| _ -> "(" ^ (Encoder.InsertTableName.f ()) t ^ ")"
in
+ let module Identifier = Model.Identifier in
let q =
Croquette_common.Format.format_string (fun format ->
- Format.fprintf format "INSERT INTO \"%s\" %s VALUES %s RETURNING ID"
- (Model.Content.id content) columns insert_template)
+ Format.fprintf format "INSERT INTO %s %s VALUES %s RETURNING ID"
+ (Identifier.quote (Model.Content.id content))
+ columns insert_template)
in
let types_param = (Encoder.CaqtiRequest.f ()) t in
let request = Caqti.Templater.(direct T.(types_param -->! int64) q) in
@@ -194,13 +202,13 @@ let insert_references :
Caqti_lwt.connection ->
Model.Reference.t Model.Content.t ->
Int64.t ->
- string ->
+ Model.Identifier.t ->
Model.Reference.t Model.Typ.value ->
Int64.t Intf.data ->
unit Lwt.t =
fun db content content_id field_name value data ->
let provider : string -> Model.Reference.t Model.Typ.value = function
- | n when String.equal n field_name -> value
+ | n when String.equal n (Model.Identifier.to_string field_name) -> value
| _ -> Model.Typ.None
in
let acc =
diff --git a/lib/query/insert.mli b/lib/query/insert.mli
index 798c37b..3f39bd5 100644
--- a/lib/query/insert.mli
+++ b/lib/query/insert.mli
@@ -12,7 +12,7 @@ val insert_references :
Caqti_lwt.connection ->
Model.Reference.t Model.Content.t ->
Int64.t ->
- string ->
+ Model.Identifier.t ->
Model.Reference.t Model.Typ.value ->
Int64.t Intf.data ->
unit Lwt.t
diff --git a/lib/query/intf.ml b/lib/query/intf.ml
index 4c09eef..8c7ead5 100644
--- a/lib/query/intf.ml
+++ b/lib/query/intf.ml
@@ -1,5 +1,5 @@
type 'a data = {
- name : string;
+ name : Model.Identifier.t;
content_type : ('a, Model.Reference.t) Model.Typ.t;
}
(** The data is the name of the field and the type associated with *)
diff --git a/lib/query/tests/create.t b/lib/query/tests/create.t
index 6ba48c3..38a4630 100644
--- a/lib/query/tests/create.t
+++ b/lib/query/tests/create.t
@@ -69,7 +69,7 @@ A subcontent is linked to the parent:
CREATE TABLE "details"
( "parent" INTEGER NOT NULL
, "id" INTEGER PRIMARY KEY
- , FOREIGN KEY("parent") REFERENCES entity("id") )
+ , FOREIGN KEY("parent") REFERENCES "entity"("id") )
A reference field is declared as a foreign key to the linked element
@@ -109,7 +109,7 @@ A reference field is declared as a foreign key to the linked element
( "id" INTEGER PRIMARY KEY
, "user_type" INTEGER
, "name" VARCHAR
- , FOREIGN KEY("user_type") REFERENCES user_type_list("id") )
+ , FOREIGN KEY("user_type") REFERENCES "user_type_list"("id") )
When the field is multivaluated, there is no column for this property in the
table but a dedicated table is created:
@@ -146,5 +146,6 @@ table but a dedicated table is created:
( "id" INTEGER PRIMARY KEY
, "parent" INTEGER NOT NULL
, "target" INTEGER NOT NULL
- , FOREIGN KEY("parent") REFERENCES entity("id")
- , FOREIGN KEY("target") REFERENCES user_type_list("id") )
+ , FOREIGN KEY("parent") REFERENCES "entity"("id")
+ , FOREIGN KEY("target") REFERENCES "user_type_list"("id") )
+
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)