Skip to content

Commit 51cf30d

Browse files
authored
parameterize queries for perf and security
1 parent feb5055 commit 51cf30d

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

src/konserve_jdbc/core.clj

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,32 +102,37 @@
102102
(case db-type
103103
"h2"
104104
[(str "MERGE INTO " table " (id, header, meta, val) "
105-
"SELECT '" to "', header, meta, val FROM " table " WHERE id = '" from "';")]
105+
"SELECT ?, header, meta, val FROM " table " WHERE id = ?;")
106+
to from]
106107
("postgresql" "sqlite")
107108
[(str "INSERT INTO " table " (id, header, meta, val) "
108-
"SELECT '" to "', header, meta, val FROM " table " WHERE id = '" from "' "
109+
"SELECT ?, header, meta, val FROM " table " WHERE id = ? "
109110
"ON CONFLICT (id) DO UPDATE "
110-
"SET header = excluded.header, meta = excluded.meta, val = excluded.val;")]
111+
"SET header = excluded.header, meta = excluded.meta, val = excluded.val;")
112+
to from]
111113
("mssql" "sqlserver")
112114
[(str "MERGE dbo." table " WITH (HOLDLOCK) AS tgt "
113-
"USING (SELECT '" to "', header, meta, val FROM " table " WHERE id = '" from "') "
115+
"USING (SELECT ?, header, meta, val FROM " table " WHERE id = ?) "
114116
"AS new (id, header, meta, val) "
115-
"ON (tgt.id = new.id)"
117+
"ON (tgt.id = new.id) "
116118
"WHEN MATCHED THEN UPDATE "
117119
"SET tgt.header = new.header, tgt.meta = new.meta, tgt.val = new.val "
118120
"WHEN NOT MATCHED THEN "
119-
"INSERT (id, header, meta, val) VALUES (new.id, new.header, new.meta, new.val);")]
121+
"INSERT (id, header, meta, val) VALUES (new.id, new.header, new.meta, new.val);")
122+
to from]
120123
"mysql"
121124
[(str "REPLACE INTO " table " (id, header, meta, val) "
122-
"SELECT '" to "', header, meta, val FROM " table " WHERE id = '" from "';")]
125+
"SELECT ?, header, meta, val FROM " table " WHERE id = ?;")
126+
to from]
123127
[(str "MERGE INTO " table " AS tgt "
124-
"USING (SELECT '" to "', header, meta, val FROM " table " WHERE id = '" from "') "
128+
"USING (SELECT ?, header, meta, val FROM " table " WHERE id = ?) "
125129
"AS new (id, header, meta, val) "
126-
"ON (tgt.id = new.id)"
130+
"ON (tgt.id = new.id) "
127131
"WHEN MATCHED THEN UPDATE "
128132
"SET tgt.header = new.header, tgt.meta = new.meta, tgt.val = new.val "
129133
"WHEN NOT MATCHED THEN "
130-
"INSERT (id, header, meta, val) VALUES (new.id, new.header, new.meta, new.val);")]))
134+
"INSERT (id, header, meta, val) VALUES (new.id, new.header, new.meta, new.val);")
135+
to from]))
131136

132137
(defn delete-statement [db-type table]
133138
(case db-type
@@ -145,11 +150,11 @@
145150

146151
(defn change-row-id [connection table from to]
147152
(jdbc/execute! connection
148-
["UPDATE " table " SET id = '" to "' WHERE id = '" from "';"]))
153+
[(str "UPDATE " table " SET id = ? WHERE id = ?;") to from]))
149154

150155
(defn read-field [db-type connection table id column & {:keys [binary? locked-cb] :or {binary? false}}]
151156
(let [res (-> (jdbc/execute! connection
152-
[(str "SELECT id," (name column) " FROM " table " WHERE id = '" id "';")]
157+
[(str "SELECT id," (name column) " FROM " table " WHERE id = ?;") id]
153158
{:builder-fn rs/as-unqualified-lower-maps})
154159
first
155160
column)]
@@ -160,7 +165,7 @@
160165

161166
(defn read-all [db-type connection table id]
162167
(let [res (-> (jdbc/execute! connection
163-
[(str "SELECT id, header, meta, val FROM " table " WHERE id = '" id "';")]
168+
[(str "SELECT id, header, meta, val FROM " table " WHERE id = ?;") id]
164169
{:builder-fn rs/as-unqualified-lower-maps})
165170
first)]
166171
(into {} (for [[k v] res] [k (if (= k :id) v (extract-bytes v db-type))]))))
@@ -221,11 +226,11 @@
221226
(-delete-blob [_ store-key env]
222227
(async+sync (:sync? env) *default-sync-translation*
223228
(go-try- (jdbc/execute! connection
224-
[(str "DELETE FROM " table " WHERE id = '" store-key "';")]))))
229+
[(str "DELETE FROM " table " WHERE id = ?;") store-key]))))
225230
(-blob-exists? [_ store-key env]
226231
(async+sync (:sync? env) *default-sync-translation*
227232
(go-try- (let [res (jdbc/execute! connection
228-
[(str "SELECT 1 FROM " table " WHERE id = '" store-key "';")])]
233+
[(str "SELECT 1 FROM " table " WHERE id = ?;") store-key])]
229234
(not (nil? (first res)))))))
230235
(-copy [_ from to env]
231236
(async+sync (:sync? env) *default-sync-translation*

0 commit comments

Comments
 (0)