Fork me on GitHub
#honeysql
<
2021-08-22
>
bartuka14:08:43

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?

seancorfield19:08:36

@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.

seancorfield19:08:25

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).

seancorfield19:08:31

Perhaps you can explain what problem you're trying to solve? Maybe you're misunderstanding why you would register a new formatting function?

bartuka22:08:20

I am writing a to_tsvector function e.g:

(sql/format {:select [[[:to-tsvector "test"]]]})
;; => ["SELECT to_tsvector('simple', 'test')"]

bartuka22:08:12

the syntax above is already good enough, I was curious about how to support bindable params

seancorfield22:08:22

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.

seancorfield22:08:02

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.

seancorfield22:08:00

But if someone called your new function as [:to-tsvector :?val], then they'd provide :params {:val "test"} in the call to sql/format.

seancorfield22:08:49

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).

seancorfield22:08:55

Does that make sense @iagwanderson?

bartuka22:08:43

yes, I will play with format-expr a bit more. Do you mind expanding more on your first comment about fixed names?

bartuka22:08:05

oh, I think I got it. The case above should return

(sql/format {:select [[[:to-tsvector "test"]]]})
;; => ["SELECT to_tsvector('simple', ?)" "teste"]

seancorfield22:08:30

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).

👍 2