diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-11-18 20:24:52 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-11-18 21:57:48 +0100 |
| commit | 62123e577ee5d4066d53f8ad7e5ac3d2eb190430 (patch) | |
| tree | f569686630681a9eceb2685f69b55fe5a2bac17c /lib/query/insert.ml | |
| parent | 7087a0c2c84e796e474784cbd28d4a4e574cf2c0 (diff) | |
Securized the queries in the database with a dedicated type for the Identifiers
Diffstat (limited to 'lib/query/insert.ml')
| -rw-r--r-- | lib/query/insert.ml | 26 |
1 files changed, 17 insertions, 9 deletions
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 = |
