sql

All things SQL and JDBC...
Ahmed Hassan 2020-03-13T13:58:42.163Z

guides on date and time in postgresql?

Mitch 2020-03-13T14:11:41.163900Z

We are using postgres and have success just converting whatever object we have to a timestamp (string) and inserting that

jumar 2020-03-13T16:23:36.164300Z

This one was helpful for me: https://www.postgresqltutorial.com/postgresql-timestamp/ And this is very enlightening although not about PostgreSQL specifically: https://codeblog.jonskeet.uk/2019/03/27/storing-utc-is-not-a-silver-bullet/

kwladyka 2020-03-13T21:30:25.165900Z

Is there a way to use jdbc to query using multiple uuid?

(jdbc/execute-one! db [(str "SELECT * FROM " (name table) " WHERE uuid IN(?)") (clojure.string/join "," (map uuid/as-uuid uuids))])
How this should look? Has jdbc some magic to convert collection in right way, so I can not use join?

seancorfield 2020-03-13T21:31:00.166200Z

What DB are you using @kwladyka?

kwladyka 2020-03-13T21:32:02.167100Z

postgresql

seancorfield 2020-03-13T21:32:09.167200Z

Read https://cljdoc.org/d/seancorfield/next.jdbc/1.0.395/doc/getting-started/tips-tricks#postgresql which talks about IN and an array of values.

seancorfield 2020-03-13T21:33:02.168200Z

The example there uses int-array but you'd need object-array for UUIDs (and you'll need to pass them as objects I suspect -- no idea how PG handles that).

kwladyka 2020-03-13T21:33:42.169100Z

(jdbc/execute-one! db [(str "SELECT * FROM " (name table) " WHERE uuid IN(?)") (map uuid/as-uuid uuids)])
what is wrong here?

kwladyka 2020-03-13T21:33:57.169400Z

Can't infer the SQL type to use for an instance of clojure.lang.LazySeq. Use setObject() with an explicit Types value to specify the type to use.

seancorfield 2020-03-13T21:34:13.169900Z

Did you actually read the docs I linked to?

kwladyka 2020-03-13T21:34:17.170Z

oh so maybe jdbc conver it right, but still have issue about ::uuid

kwladyka 2020-03-13T21:34:43.170300Z

sure, but probably I didn’t understand

seancorfield 2020-03-13T21:35:02.170500Z

Read it again. Carefully.

seancorfield 2020-03-13T21:35:10.170800Z

And read my note above about object-array.

kwladyka 2020-03-13T21:36:29.171300Z

ok this is first time when I use object-array - I have no idea what it does, but I have:

kwladyka 2020-03-13T21:36:36.171500Z

Can't infer the SQL type to use for an instance of [Ljava.lang.Object;. Use setObject() with an explicit Types value to specify the type to use.

seancorfield 2020-03-13T21:37:29.171900Z

Your SQL is still wrong I expect.

kwladyka 2020-03-13T21:38:26.173200Z

yes, I have no idea how to write this to let know postgresql this is uuid not string

seancorfield 2020-03-13T21:38:37.173600Z

The docs I linked to show you.

kwladyka 2020-03-13T21:38:42.173700Z

(jdbc/execute-one! db [(str "SELECT * FROM " (name table) " WHERE uuid = ?::uuid") (uuid/as-uuid uuid)])
easy here

kwladyka 2020-03-13T21:41:45.174Z

sorry, don’t get it

kwladyka 2020-03-13T21:45:07.174400Z

I don’t know how to create this objects I guess

seancorfield 2020-03-13T21:46:49.175Z

First off, the docs say that in your SQL you need WHERE uuid = ANY(?)

kwladyka 2020-03-13T21:48:31.176400Z

Oh I misinterpreted this, but still doesn’t work

seancorfield 2020-03-13T21:48:46.176700Z

Then you need either an Object array with your UUID objects in it, or you might need an array of UUID objects that is typed for PostgreSQL

seancorfield 2020-03-13T21:49:18.177400Z

That will depend on what your uuids variable contains -- is it objects or strings at that point?

kwladyka 2020-03-13T21:50:24.178700Z

I can have whichever will be easier string / #uuid

seancorfield 2020-03-13T21:50:29.178800Z

Either (object-array uuids) or (into-array uuids) might work, or you might need (into-array SomeType uuids) where SomeType is whatever UUID type is acceptable to PostgreSQL.

seancorfield 2020-03-13T21:50:49.179200Z

I don't use PG so you'll have to figure this out based on the information I've given you.

kwladyka 2020-03-13T21:51:06.179600Z

Can't infer the SQL type to use for an instance of [Ljava.lang.Object;. Use setObject() with an explicit Types value to specify the type to use. this is what I have with object-array

kwladyka 2020-03-13T21:51:25.179900Z

into-array the same

kwladyka 2020-03-13T21:52:36.180600Z

(jdbc/execute-one! db ["SELECT * FROM shops WHERE uuid = ANY(?)" (into-array #{#uuid "00000000-0000-0000-0000-000000000001", #uuid "00000000-0000-0000-0000-000000000000"})])

kwladyka 2020-03-13T21:52:50.180900Z

this is my current state and fighting with this

seancorfield 2020-03-13T21:53:18.181300Z

Then you may need the "some type" variant I mentioned above

seancorfield 2020-03-13T21:53:33.181800Z

(but I've no idea what UUID type you'll need there)

kwladyka 2020-03-13T21:53:40.182Z

yes, trying to figure out how to write this

kwladyka 2020-03-13T21:54:39.182600Z

(jdbc/execute-one! db ["SELECT * FROM shops WHERE uuid = ANY(?)" (into-array java.util.UUID #{#uuid "00000000-0000-0000-0000-000000000001", #uuid "00000000-0000-0000-0000-000000000000"})])
doesn’t work

kwladyka 2020-03-13T21:55:09.182900Z

Can't infer the SQL type to use for an instance of [Ljava.util.UUID;. Use setObject() with an explicit Types value to specify the type to use.

seancorfield 2020-03-13T21:55:10.183Z

Good ol' PostgreSQL strikes again... ¯\(ツ)

seancorfield 2020-03-13T21:56:38.184500Z

You could always break down and do this:

(jdbc/execute-one! db (into ["SELECT * FROM shops WHERE uuid IN (" (str/join "," (repeat (count uuids) "?")) ")"] (map uuid/as-uuid uuids)))
(off the top of my head)

kwladyka 2020-03-13T22:00:52.185Z

no way to make uuid to be understandable by jdbc?

kwladyka 2020-03-13T22:01:04.185400Z

any hints how it can be done?

kwladyka 2020-03-13T22:01:10.185700Z

Similar to JSON example?

kwladyka 2020-03-13T22:01:21.186Z

this is the part with I don’t have too much experience

seancorfield 2020-03-13T22:01:35.186300Z

Each database is different.

seancorfield 2020-03-13T22:02:46.187500Z

The variant I gave with repeat etc should work just like the single UUID example you had working above. You may need "?::uuid" instead of just plan "?".

kwladyka 2020-03-13T22:03:08.187800Z

there is 1 thing which I can’t understand

kwladyka 2020-03-13T22:04:02.188400Z

this works

(jdbc/execute-one! db [(str "SELECT * FROM " (name table) " WHERE uuid = ?") (uuid/as-uuid uuid)])
this too
(jdbc/execute-one! db [(str "SELECT * FROM " (name table) " WHERE uuid = ?::uuid") uuid])
this NOT
(jdbc/execute-one! db [(str "SELECT * FROM " (name table) " WHERE uuid = ?") uuid])

kwladyka 2020-03-13T22:04:11.188700Z

so it looks like this understand #uuid

kwladyka 2020-03-13T22:04:16.188900Z

but not when in array

seancorfield 2020-03-13T22:04:34.189200Z

Did you try the repeat variant I posted above?

kwladyka 2020-03-13T22:04:40.189400Z

yes, it works

seancorfield 2020-03-13T22:04:47.189600Z

Then you have a solution.

kwladyka 2020-03-13T22:04:57.189800Z

(jdbc/execute! db (into ["SELECT * FROM shops WHERE uuid IN (?,?)"] (map uuid/as-uuid #{#uuid "00000000-0000-0000-0000-000000000001", #uuid "00000000-0000-0000-0000-000000000000"})))

seancorfield 2020-03-13T22:06:07.190900Z

Good. Chalk the rest up to PostgreSQL/JDBC weirdness. Sometimes JDBC stuff is just... tears hair out 🔥

😟 1
kwladyka 2020-03-13T22:08:23.191600Z

Thank you for help. I will still try to find the solution with array, but I don’t give myself too much chance 🙂

kwladyka 2020-03-13T22:23:05.191800Z

this one work

(jdbc/execute-one! db ["SELECT * FROM shops WHERE uuid = ANY(?::uuid[])" "{00000000-0000-0000-0000-000000000001,00000000-0000-0000-0000-000000000000}"])

kwladyka 2020-03-13T22:23:24.192200Z

not perfect but good enough

seancorfield 2020-03-13T22:24:41.193100Z

Interesting. So I guess that's a PG cast to an array type? Kind of weird that it takes a string of that format tho'... with braces around a comma-separated list...

kwladyka 2020-03-13T22:25:23.193400Z

https://clojurians.slack.com/archives/C1Q164V29/p1584137042188400 still this one confuse me

kwladyka 2020-03-13T22:25:39.193900Z

beucase it work with #uuid object

kwladyka 2020-03-13T22:25:45.194100Z

but not when I use ANY

kwladyka 2020-03-13T22:25:51.194300Z

so with arrray

kwladyka 2020-03-13T22:26:59.194700Z

Can't infer the SQL type to use for an instance of [Ljava.util.UUID;. Use setObject() with an explicit Types value to specify the type to use. using array as an input always throw error like this