honeysql

m3tti 2024-10-21T11:24:31.230909Z

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 available

m3tti 2024-10-21T11:46:33.653899Z

ok what i did was now

(-> 
  (db/execute-one! {:select [[[:count :id]]] :from :jobs}
  vals
  first)

p-himik 2024-10-21T11:54:20.160329Z

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))

igrishaev 2024-10-21T13:04:32.390739Z

btw, if it's postgres, use count(*) but not count(id). The first form is a special case which is faster

😲 1
m3tti 2024-10-21T13:07:56.580829Z

Ah ok good to know will do!

seancorfield 2024-10-21T16:32:45.180409Z

You can use {:select [ [ [:count :*] :total ] ] :from :jobs} to give COUNT(*) an alias that you can reference as a keyword :total.

p-himik 2024-10-21T16:34:50.150609Z

BTW [:count :*] can be written as :%count.*.

❤️ 1
m3tti 2024-10-21T16:38:14.262649Z

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)

m3tti 2024-10-21T16:38:31.183869Z

in babashka this seems to work

p-himik 2024-10-21T16:39:08.388499Z

> so the key is :/count or :/samename This looks more like a bug than anything.

igrishaev 2024-10-21T16:40:41.034669Z

most likely it does (str the-namespace "/" the-name) without checking if the-namespace is nil

seancorfield 2024-10-21T16:40:47.823429Z

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.

seancorfield 2024-10-21T16:41:58.829039Z

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.

m3tti 2024-10-21T16:42:42.261139Z

oh so i found maybe something 😄 should i create a bug ticket and a small script?

seancorfield 2024-10-21T16:43:07.106059Z

Explain what you're talking about first.

m3tti 2024-10-21T16:43:28.038839Z

@seancorfield yeah honeysql is just creating sql i know next.jdbc is getting the results

seancorfield 2024-10-21T16:43:32.227219Z

:/count is not a legal keyword in Clojure so I want to understand how/where exactly you are finding that?

m3tti 2024-10-21T16:44:59.293479Z

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 😕

seancorfield 2024-10-21T16:44:59.484979Z

user=> (sql/format {:select [ [ [:count :*] :total ] ] :from :jobs})
["SELECT COUNT(*) AS total FROM jobs"]
HoneySQL is doing the right thing here -- producing valid SQL.

seancorfield 2024-10-21T16:45:12.701059Z

Yeah, that sounds more like it 🙂

m3tti 2024-10-21T16:45:16.518039Z

therefore i was updating keys on my own and here we have it my fault

m3tti 2024-10-21T16:46:02.645039Z

sorry to bother you guys ... nothing to see here go on !!!