Hi!
If I want to compose sql queries with some conditions.
I can use str
to concatenate like that but I'm sure this isn't the right way to do:
(defn retrieve-all-users-and-filter [filters]
(jdbc/execute! db [(str "SELECT id, first_name || ' ' || last_name as name, email, owner FROM users"
(when filters " WHERE first_name LIKE ?")
" ORDER BY last_name ASC, first_name ASC ") (when filters (str "%" (:search filters) "%"))]))
For more complex queries, is it interresting to use for eg. HoneySQL with Next.jdbc or is it a better solution that above with Next-jdbc?
Any clue about that?@admin055 Best way i've found is to use honeysql
(defn retrieve-all-users-query [filters]
{:select [:id :first_name]
:from [:users]
:where [:and [:= 1 1]
filters]})
That turns out well, I am trying it. Thanks for your confirmation.
(retrieve-all-users-query [:= :first_name "Joe])
I take this opportunity to ask, what is the equivalent for a pattern matching "%string%"
Ah I think you gave the answer above, I'll try that right now!
(he also advocated giving everything a docstring)
I have clj-kondo flag any public var that doesn’t have a docstring. When I start writing a function, that’s usually the first thing I do:
(defn my-cool-function
"Given <inputs>, <compute something> / return <something>.
<more long form description about the function if needed>
<any exceptional cases or caveats about usage>."
[])
and then I add arguments based on the <inputs> I described, then I sketch out the function logic in a (comment ..)
form and then “promote” it into the function body.Always interresting to hear some others workflow, thanks.