This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-08-22
Channels
- # anglican (2)
- # announcements (8)
- # babashka (37)
- # beginners (13)
- # calva (16)
- # clj-kondo (20)
- # cljdoc (1)
- # cljsrn (3)
- # clojure (6)
- # clojure-europe (3)
- # clojurescript (3)
- # clojureverse-ops (3)
- # conjure (1)
- # core-async (9)
- # cursive (6)
- # cypress (2)
- # datomic (5)
- # fulcro (1)
- # honeysql (15)
- # luminus (2)
- # malli (35)
- # music (3)
- # nbb (1)
- # off-topic (7)
- # pathom (12)
- # practicalli (1)
- # re-frame (15)
- # reagent (37)
- # sci (9)
- # shadow-cljs (3)
- # show-and-tell (3)
- # spacemacs (3)
- # workspaces (3)
hello, I am extending honeysql 2.x by adding a new function however I could not easily find a way to support bindable params in my new function.
(sql/format
{:select [:user]
:from [:t]
:where [:my-function :?path]}
{:params {:path [:meta "title"]}})
the my-function
body only receives the keyword :?path
how can I get access to the params
provided in sql/format?@iagwanderson The :params
are only applied right at the end. The expectation is that your new function would call format-expr
etc on its argument, or whatever semantics it wants to have.
When you call format-expr
on :?path
, you get ["?" <param function>]
and that (opaque) <param function>
is turned into an actual value as the last step of format
(the mapv
unwrap
code there).
Perhaps you can explain what problem you're trying to solve? Maybe you're misunderstanding why you would register a new formatting function?
I am writing a to_tsvector
function e.g:
(sql/format {:select [[[:to-tsvector "test"]]]})
;; => ["SELECT to_tsvector('simple', 'test')"]
the syntax above is already good enough, I was curious about how to support bindable params
In the same way you wouldn't want macros to generally to introduce fixed names, you generally wouldn't want to introduce fixed names for bindable params.
But the answer is that (sql/format-expr :?some-name)
is going to put ?
in your SQL and will bind to :some-name
in the :params
passed to sql/format
.
But if someone called your new function as [:to-tsvector :?val]
, then they'd provide :params {:val "test"}
in the call to sql/format
.
Your function should call sql/format-expr
on its argument to get [sql & params]
from it, and merge that into the function's overall SQL (and any other params).
Does that make sense @iagwanderson?
yes, I will play with format-expr
a bit more. Do you mind expanding more on your first comment about fixed names?
oh, I think I got it. The case above should return
(sql/format {:select [[[:to-tsvector "test"]]]})
;; => ["SELECT to_tsvector('simple', ?)" "teste"]
Right.
I was cautioning against your function injecting new named parameters (since you couldn't use your function twice in a single query and expect it to get different arguments).