seancorfield / next-jdbc

A modern low-level Clojure wrapper for JDBC-based access to databases.
https://cljdoc.org/d/com.github.seancorfield/next.jdbc/
Eclipse Public License 1.0
750 stars 89 forks source link

Easier counting with `next.jdbc.sql/find-by-keys` #274

Closed vemv closed 4 months ago

vemv commented 5 months ago

Is your feature request related to a problem? Please describe.

I'd like to sql-count more easily while using find-by-keys.

I can pass :columns [["count(*)" :total]] and then -> ,,, first :total the result.

But neither bit seems intuitive to me.

Describe the solution you'd like

An option in find-by-keys that translates to a count, maybe?

Describe alternatives you've considered

A separate function in the same ns that takes similar options to find-by-keys, but that performs a count and returns a number (the count) rather than a coll of results.

Thanks - V

seancorfield commented 5 months ago

I'll think about it -- I really don't want to expand the "Friendly SQL Functions": I'd rather folks used HoneySQL for more complex SQL and plan/select-one! to get a single column value out of a result set.

vemv commented 5 months ago

Yes, agreed. I much appreciate Honey but in my current context, socially we decided to stick to next.jdbc.sql for the time being.

seancorfield commented 5 months ago

How would you feel about this:

(aggregate-by-keys ds :table "count(*)" :total where-map opts)
;; would call:
(-> (find-by-keys ds :table where-map (assoc opts :columns [["count(*)" :total]])) first :total)

This would allow for more general aggregation. Perhaps the alias keyword (:total here) could be some weird, internal gensym'd name so you didn't need to specify it at all...?

vemv commented 5 months ago

Yes the :total would seem like some friction for users.

Other than that, it looks reasonable to me!

vemv commented 4 months ago

Nice, thank you!