how do i get the count of a database ? what i did is this
(defn item-count []
(:count (db/execute-one! {:select [[[:count :id]]] :from :jobs})))
but somehow :count is not availableok what i did was now
(->
(db/execute-one! {:select [[[:count :id]]] :from :jobs}
vals
first)
I created a bunch of functions for querying different kinds of things. For this case, I have
(def base-map-jdbc-opts {:column-fn cached-kebab->snake
:table-fn cached-kebab->snake
:label-fn cached-snake->kebab
:qualifier-fn cached-snake->kebab
:builder-fn next.jdbc.result-set/as-unqualified-modified-maps})
(def base-vec-jdbc-opts (assoc base-map-jdbc-opts
:builder-fn next.jdbc.result-set/as-unqualified-modified-arrays))
(defn- do-query-one! [c q params]
(let [fq (format-query q)]
(try
(next.jdbc/execute-one! c fq params)
(catch Throwable t
(tap> [:error t [:query (first fq) (rest fq) q]])
(throw t)))))
(defn query-value [c q]
(nth (do-query-one! c q base-vec-jdbc-opts) 0))btw, if it's postgres, use count(*) but not count(id). The first form is a special case which is faster
Ah ok good to know will do!
You can use {:select [ [ [:count :*] :total ] ] :from :jobs} to give COUNT(*) an alias that you can reference as a keyword :total.
BTW [:count :*] can be written as :%count.*.
thank you guys i'm using the pod-babashka-sql and i always get a namespace with my table even if i give it a name so the key is :/count or :/samename and that way clojure (jvm) is complaining if i use it in shortform with something like (:/count myresult)
in babashka this seems to work
> so the key is :/count or :/samename
This looks more like a bug than anything.
most likely it does (str the-namespace "/" the-name) without checking if the-namespace is nil
Don't conflate HoneySQL and next.jdbc stuff @mathaeus.peter.sander -- HoneySQL just produces SQL, next.jdbc executes SQL. The table names being added is a deliberate default in next.jdbc.
I strongly encourage folks to work with :table/column as much as possible.
:/count definitely sounds like a bug -- it's not a valid keyword in Clojure.
oh so i found maybe something 😄 should i create a bug ticket and a small script?
Explain what you're talking about first.
@seancorfield yeah honeysql is just creating sql i know next.jdbc is getting the results
:/count is not a legal keyword in Clojure so I want to understand how/where exactly you are finding that?
WAIT!!! i have a bug 😮 it's my fault sorry -.- it is due to the fact that babashka doesn't support to transmit the builder function 😕
user=> (sql/format {:select [ [ [:count :*] :total ] ] :from :jobs})
["SELECT COUNT(*) AS total FROM jobs"]
HoneySQL is doing the right thing here -- producing valid SQL.Yeah, that sounds more like it 🙂
therefore i was updating keys on my own and here we have it my fault
sorry to bother you guys ... nothing to see here go on !!!