This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-11-19
Channels
- # aleph (5)
- # announcements (1)
- # babashka (5)
- # beginners (123)
- # biff (9)
- # calva (8)
- # cider (1)
- # clj-on-windows (8)
- # clojure (20)
- # clojure-europe (7)
- # clojure-hungary (3)
- # clojure-norway (1)
- # clojure-sweden (32)
- # clojurescript (2)
- # core-async (2)
- # emacs (6)
- # events (3)
- # fulcro (30)
- # graphql (4)
- # gratitude (3)
- # helix (10)
- # honeysql (7)
- # introduce-yourself (11)
- # kaocha (1)
- # malli (16)
- # matcher-combinators (1)
- # off-topic (7)
- # portal (1)
- # re-frame (12)
- # reagent (3)
- # ring (7)
- # scittle (3)
- # shadow-cljs (1)
- # sql (1)
- # tools-deps (8)
Hi guys! I have had and issue writing simple query to fetch records
based on primary keys given in vector to get something like this
SELECT * FROM s_lab_results WHERE key IN (23, 456, 890)
I have started with query building function as:
(defn records-for-keys-alt
[table-name primary-key-column records-keys]
(let [table-name-for-hsql (keyword table-name)
primary-key-hsql (keyword primary-key-column)]
(first (hsql/format {:select [:*]
:from [table-name-for-hsql]
:where [:in primary-key-hsql :?values]}
{:params {:values records-keys}
:pretty true
:inline true}))))
but it has resulted with :
"\nSELECT *\nFROM s_lab_results\nWHERE key IN [23, 456, 890]\n"
which is not correct due to usage of [] brackets.
What was working after colleague had helped me was:
(defn records-for-keys
[table-name primary-key-column records-keys]
(let [table-name-for-hsql (keyword table-name)
primary-key-hsql (keyword primary-key-column)
records-keys-inline (mapv #(vector :inline %) records-keys)]
(first (hsql/format {:select [:*]
:from [table-name-for-hsql]
:where [:in primary-key-hsql records-keys-inline]}
{:pretty true}))))
which gives:
"\nSELECT *\nFROM s_lab_results\nWHERE key IN (23, 456, 890)\n"
but I am not sure where I have made error in initial function.
What do you think?The documentation explains why you can't do that (named parameter vector with :in). And that second example isn't safe either. Why are you inlining values? That's not safe in general (SQL injection risks)