summaryrefslogtreecommitdiff
path: root/test/common.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 /test/common.ml
parent7087a0c2c84e796e474784cbd28d4a4e574cf2c0 (diff)
Securized the queries in the database with a dedicated type for the Identifiers
Diffstat (limited to 'test/common.ml')
-rw-r--r--test/common.ml33
1 files changed, 19 insertions, 14 deletions
diff --git a/test/common.ml b/test/common.ml
index 0ef5cb3..98871a3 100644
--- a/test/common.ml
+++ b/test/common.ml
@@ -31,12 +31,10 @@ let content_as_json :
let () =
Seq.iter
(fun field ->
- Buffer.add_char buff '\'';
- Buffer.add_string buff (Model.Field.name field);
-
- Buffer.add_string buff "',";
- Buffer.add_string buff (Model.Field.name field);
- Buffer.add_string buff ",")
+ let name = Model.Field.name field in
+ Buffer.add_string buff
+ ( Croquette_common.Sql.quote_literal (Model.Identifier.to_string name)
+ ^ "," ^ Model.Identifier.quote name ^ "," ))
fields
in
@@ -46,21 +44,28 @@ let content_as_json :
let length = Buffer.length buff in
if length > 1 then Buffer.truncate buff (length - 1);
()
- | Some _ -> Buffer.add_string buff "'parent',parent"
+ | Some _ ->
+ Buffer.add_string buff
+ ( Croquette_common.Sql.quote_literal "parent" ^ ","
+ ^ Model.Identifier.quote (Model.Identifier.parent) )
in
let q = Buffer.contents buff in
let open Caqti.Templater in
direct T.(int64 -->! string)
- ("SELECT json_object(" ^ q ^ ") from " ^ Model.Content.id content
- ^ " where id=?")
+ ("SELECT json_object(" ^ q ^ ") from "
+ ^ Model.Identifier.quote (Model.Content.id content)
+ ^ " where " ^ Model.Identifier.quote (Model.Identifier.id) ^ "=?")
-let query_values (table : Model.Reference.t Model.Content.t) (feature : string)
- =
+let query_values (table : Model.Reference.t Model.Content.t)
+ (feature : Model.Identifier.t) =
let open Caqti.Templater in
direct T.(int64 -->* string)
- ("SELECT json_object('" ^ feature ^ "', target) from \""
- ^ Model.Content.id table ^ "." ^ feature ^ "\" where parent=?")
+ ("SELECT json_object("
+ ^ Croquette_common.Sql.quote_literal (Model.Identifier.to_string feature)
+ ^ ", " ^ Model.Identifier.quote (Model.Identifier.target) ^ ") from "
+ ^ Model.Identifier.quote (Model.Identifier.dot (Model.Content.id table) feature)
+ ^ " where " ^ Model.Identifier.quote (Model.Identifier.parent) ^ "=?")
(** Query all the multireferences tables for the given content and the given id.
Collect all the values and return them in the same json format as the same
@@ -73,7 +78,7 @@ let collect_references :
fun db content id ->
let module Db = (val db) in
let ( let* ) = Lwt.bind in
- let content_id = Model.Content.id content in
+ let content_id = Model.Identifier.to_string (Model.Content.id content) in
(* Only keep the multivaluated fields *)
let fields =