Fork me on GitHub

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.

 {: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"]


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