Fork me on GitHub
#sql
<
2021-05-26
>
Michaël Salihi16:05:37

For this project, I had a few advanced queries (search, dynamic filters) to first do with Clojure Next.jdbc / HoneySQL. Does a query like this sound reasonable to you? https://github.com/prestancedesign/clojure-inertia-pingcrm-demo/blob/main/src/pingcrm/models/users.clj#L16-L35

Michaël Salihi16:05:50

Some suggestions for improvement?

seancorfield16:05:18

@admin055 I think that’s fine. Personally, I’d probably use cond-> to thread the various where clauses but that’s just a stylistic issue.

seancorfield16:05:46

As an example (from part of our codebase still using HoneySQL v1):

([self status exclude items qualifier]
   (cond-> (-> (select      [:uc.senderuserid :id])
               (from        [:usercovalence :uc])
               (where       [:= :uc.receiveruserid (:id self)])
               (user-status :uc.senderuserid)
               (user-gender :uc.receiveruserid))
     ;; WS-13047 status must be conditional because it can be nil:
     status
     (merge-where [:= :uc.covalence status])
     qualifier
     (merge-where qualifier)
     (seq items)
     (within-items items :user-id :uc.senderuserid)
     (seq exclude)
     (merge-where [:not-in :uc.senderuserid exclude])))

Michaël Salihi13:05:18

I use HoneySQL v2 and merge-where helper is not here anymore. What replaces it please?

Michaël Salihi13:05:29

OK just where, right? I see the generic function.

Michaël Salihi18:05:07

Yeah, I like the cond-> style too.. Brilliant!