sql

All things SQL and JDBC...
2021-05-12T14:44:35.183100Z

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?

emccue 2021-05-12T15:45:03.184400Z

@admin055 Best way i've found is to use honeysql

emccue 2021-05-12T15:46:39.186600Z

(defn retrieve-all-users-query [filters]
  {:select [:id :first_name]
   :from [:users]
   :where [:and [:= 1 1]
                filters]})

đź‘Ť 1
2021-05-12T15:46:52.187100Z

That turns out well, I am trying it. Thanks for your confirmation.

emccue 2021-05-12T15:47:21.187700Z

(retrieve-all-users-query [:= :first_name "Joe])

2021-05-12T15:48:47.187900Z

I take this opportunity to ask, what is the equivalent for a pattern matching "%string%"

2021-05-12T15:50:35.188500Z

Ah I think you gave the answer above, I'll try that right now!

ghadi 2021-05-12T18:15:16.190900Z

@admin055 @emccue Stu Halloway gave a talk last night called "Better Bugs Make Better Programs", and he advocated naming Datomic queries top-level in the same way as the SQL query a few messages above đź‘Ť:skin-tone-3:

đź‘Ť 1
ghadi 2021-05-12T18:15:40.191400Z

(he also advocated giving everything a docstring)

seancorfield 2021-05-12T18:23:00.194500Z

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.

2021-05-12T18:59:55.195500Z

Always interresting to hear some others workflow, thanks.