hugsql

Eduard Dyckman 2022-10-28T12:04:40.770209Z

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!

Eduard Dyckman 2022-10-30T10:37:27.152329Z

Yes, I think it should be next-jdbc

Eduard Dyckman 2022-10-30T10:57:37.498679Z

okay, it's really embarrassing, but it turns out, that problem is that I put :n instead of :* into the query annotation...

lukasz 2022-10-30T15:18:40.296009Z

ha, I keep forgetting about these, at least everyone in this thread was a helpful https://en.wikipedia.org/wiki/Rubber_duck_debugging 😉

Eduard Dyckman 2022-10-30T17:51:03.592589Z

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?

lukasz 2022-10-30T17:53:58.198809Z

I thinks so - :n should be allowed if your query type is :?

Eduard Dyckman 2022-10-30T18:45:51.455359Z

Probably for something like this:

SELECT COUNT(*) FROM users;

lukasz 2022-10-28T14:10:57.541619Z

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

Eduard Dyckman 2022-10-28T14:36:22.901459Z

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'

lukasz 2022-10-28T14:46:54.528949Z

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 transaction

lukasz 2022-10-28T14:47:56.993019Z

Not sure how conman works, but looking at it I'd say It's a Bad Idea ™️ (in my opinion)

Eduard Dyckman 2022-10-28T14:50:19.229849Z

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

lukasz 2022-10-28T14:56:10.000109Z

one thing: have you fully reloaded the namespace that defines queries?

lukasz 2022-10-28T14:56:42.444709Z

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

Eduard Dyckman 2022-10-28T15:04:46.512029Z

Yea, I even restarted the repl to be sure 🙂

lukasz 2022-10-28T18:46:39.008909Z

That's odd - I'd suggest removing the conman/mount machinery, and trying out your hugsql queries with a "raw" jdbc connection

lukasz 2022-10-28T18:47:08.664969Z

because the error you've posted doesn't look like a typical SQL error with a type mismatch I think

Eduard Dyckman 2022-10-28T18:55:44.534469Z

Okay, thanks, I will try!

curtis.summers 2022-10-28T23:56:25.898449Z

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?

Eduard Dyckman 2022-10-29T06:22:23.626469Z

I can confirm, the output for type function for that var is java.lang.Long

Eduard Dyckman 2022-10-29T20:04:07.955669Z

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

curtis.summers 2022-10-29T21:09:43.567459Z

Maybe also check which jdbc library you are using. Looks like PG for your database. Are you using the standard postgresql jdbc library?