This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-10-28
Channels
- # announcements (5)
- # babashka (7)
- # beginners (101)
- # biff (9)
- # calva (46)
- # cider (6)
- # clj-yaml (2)
- # cljsrn (13)
- # clojure (11)
- # clojure-europe (43)
- # clojure-nl (13)
- # clojure-norway (22)
- # clojurescript (20)
- # conjure (1)
- # cursive (7)
- # data-science (2)
- # datomic (26)
- # emacs (38)
- # graphql (27)
- # gratitude (5)
- # hoplon (8)
- # hugsql (22)
- # humbleui (2)
- # hyperfiddle (6)
- # introduce-yourself (8)
- # joyride (3)
- # lsp (79)
- # malli (6)
- # nbb (67)
- # portal (16)
- # rdf (27)
- # reagent (42)
- # releases (2)
- # remote-jobs (1)
- # shadow-cljs (36)
- # test-check (17)
- # tools-deps (1)
- # xtdb (15)
Good day! I have a question regarding type casting, I hope, I am on the right track with asking it here 🙂 Here is the SQL:
-- :name list-goods :? :n
-- :doc retrieves all goods created by a chat
SELECT * FROM goods
WHERE created_by = :created_by
And the actual call:
(db/list-goods {:created_by (-> dialog :chat :id)})
The function is already bound to the connection, I am using conman
.
The type of created_by
parameter is Long
but in the query statements log I actually see a string
2022-10-28 11:50:48.822 GMT [86] LOG: execute <unnamed>: SELECT * FROM goods
WHERE created_by = $1
2022-10-28 11:50:48.822 GMT [86] DETAIL: parameters: $1 = '97469468'
Thanks!Something like this should fix it:
-- :name list-goods :? :n
-- :doc retrieves all goods created by a chat
SELECT * FROM goods
WHERE created_by = :created_by::integer
Still no luck!
2022-10-28 14:34:47.329 GMT [90] LOG: execute <unnamed>: SELECT * FROM goods
WHERE created_by = $1::integer
2022-10-28 14:34:47.329 GMT [90] DETAIL: parameters: $1 = '97469468'
Right, so the second issue is that you're not passing the connection:
(db/list-goods db-conn {:created_by (-> dialog :chat :id)})
or something like that - where db-conn can be provided by a connection pool, "raw" JDBC connection or transactionNot sure how conman works, but looking at it I'd say It's a Bad Idea ™️ (in my opinion)
Yea,I don't have an opinion about that, I was just trying out luminus. So the query gets executed, so it definitely works, the only problem is casting
it's one thing that was getting me early on when working with HugSQL: you have to reload the ns after editing SQL files, since changes are not reflected automatically in the REPL
Yea, I even restarted the repl to be sure 🙂
That's odd - I'd suggest removing the conman/mount machinery, and trying out your hugsql queries with a "raw" jdbc connection
because the error you've posted doesn't look like a typical SQL error with a type mismatch I think
Okay, thanks, I will try!
The SQL data type is derived from the Java data type in the JDBC driver. So, are you sure that the Java (Clojure) data type you are passing in is actually a Long?
I can confirm, the output for type
function for that var is java.lang.Long
Ok, I can confirm that problem is not in the hugsql.
I tried to call def-sqlvec-fns
and it generated proper SQL and params.
I am pretty confident, that it's somewhere in luminus generated code, I will figure it out and open an issue/fix in luminus
Maybe also check which jdbc library you are using. Looks like PG for your database. Are you using the standard postgresql jdbc library?
Yes, I think it should be next-jdbc
okay, it's really embarrassing, but it turns out, that problem is that I put :n
instead of :*
into the query annotation...
ha, I keep forgetting about these, at least everyone in this thread was a helpful https://en.wikipedia.org/wiki/Rubber_duck_debugging 😉
Indeed! Thank you, I definitely boosted up my knowledge of jdbc and how to implement custom type conversions in next-jdbc 🙂
Does it even make sense to have :n
for query statements? Maybe such misusage should throw exception?
Probably for something like this:
SELECT COUNT(*) FROM users;