summaryrefslogtreecommitdiff
path: root/lib/query/create.ml
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2026-11-18 20:24:52 +0100
committerSébastien Dailly <sebastien@dailly.me>2026-11-18 21:57:48 +0100
commit62123e577ee5d4066d53f8ad7e5ac3d2eb190430 (patch)
treef569686630681a9eceb2685f69b55fe5a2bac17c /lib/query/create.ml
parent7087a0c2c84e796e474784cbd28d4a4e574cf2c0 (diff)
Securized the queries in the database with a dedicated type for the Identifiers
Diffstat (limited to 'lib/query/create.ml')
-rw-r--r--lib/query/create.ml89
1 files changed, 50 insertions, 39 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 ->