sql

All things SQL and JDBC...
this.rob 2020-04-07T19:43:40.185300Z

Hi everyone. I’ve just started using next.jdbc with Postgres, and am struggling to get it to read date columns correctly. I’m using next.jdbc.sql/insert! to insert a row containing a value of "2020-04-07" for a date column, but the return value has this converted to #inst \"2020-04-06T23:00:00.000-00:00\" which looks to be the wrong day. I don’t care about the time element - I just want it to return the same date I inserted, either as a string, keyword or a date object of some sort. What is the recommended way to achieve this?

dharrigan 2020-04-07T20:01:04.186Z

It’s suspicous that it’s 1 hour out, BST vs UTC. Do you have a timezone issue?

this.rob 2020-04-07T20:05:18.186600Z

If I do I shouldn’t do, I don’t want any timezone information stored with the date

this.rob 2020-04-07T20:06:33.187900Z

I’ve got as far as figuring out it’s a java.sql.date i’m getting back, and that if I call .toString on this I get a string with the correct date e.g. "2020-04-07" for that example

this.rob 2020-04-07T20:07:00.188400Z

But i’d rather next.jdbc perform that conversion for me if possible

dharrigan 2020-04-07T20:07:05.188600Z

Is this of help?

dharrigan 2020-04-07T20:07:13.189100Z

Working with Date and Time

this.rob 2020-04-07T20:08:41.190200Z

I think that is talking about date conversion problems encountered when inserting, not problems with the return vals

this.rob 2020-04-07T20:08:57.190500Z

but ty

this.rob 2020-04-07T20:09:23.191Z

I’m reading this at the moment as I have a feeling the required info is in here somewhere… https://cljdoc.org/d/seancorfield/next.jdbc/1.0.409/doc/getting-started/result-set-builders

this.rob 2020-04-07T20:14:10.191400Z

ah yes, got it working :simple_smile:

👍 1
dharrigan 2020-04-07T20:14:16.191600Z

fantastic! 🙂

this.rob 2020-04-07T20:14:40.192100Z

(extend-protocol result-set/ReadableColumn
  java.sql.Date
  (read-column-by-label ^java.time.LocalDate [^java.sql.Date v _]
    (.toString v))
  (read-column-by-index ^java.time.LocalDate [^java.sql.Date v _2 _3]
    (.toString v)))

dharrigan 2020-04-07T20:14:46.192300Z

great!

this.rob 2020-04-07T20:15:03.192700Z

curious to know why the 2 different arity versions are required there

2020-04-07T20:36:52.193900Z

they are not different arities of the same thing

👍 1
seancorfield 2020-04-07T20:37:26.194700Z

@rs Did you require next.jdbc.date-time? Or did you manually add the extend-protocol?

2020-04-07T20:37:52.195600Z

and the localdate typehint is bogus

this.rob 2020-04-07T20:38:28.196Z

I manually added it, copy paste error on the typehint

seancorfield 2020-04-07T20:40:37.196800Z

https://cljdoc.org/d/seancorfield/next.jdbc/1.0.409/api/next.jdbc.date-time -- you can get the same effect by just calling (read-as-local) from that namespace (somewhere in your program startup)

seancorfield 2020-04-07T20:46:39.197Z

Not sure how those ended up in the docs. They're not in the source code. And they are now not in the docs either (at least on GitHub -- they'll make it to cljdoc in the 1.0.next release 🙂 )

this.rob 2020-04-07T20:52:18.199100Z

thanks@seancorfield. I did see that namespace mentioned in the tips & tricks section @dharrigan posted a link to, but it didn’t mention it extended the ReadableColumn protocol as well as the SettableParameter one!

seancorfield 2020-04-07T20:56:57.199300Z

"In addition, there are several read-as-* functions here that will extend next.jdbc.result-set/ReadableColumn to allow java.sql.Date and java.sql.Timestamp columns to be read as (converted to) various Java Time types automatically."

seancorfield 2020-04-07T20:57:39.200Z

What change can I make so that is clearer about what it does? Would it be better as a separate paragraph in the ns docstring?

seancorfield 2020-04-07T20:58:13.200300Z

Perhaps moving that sentence up into the first paragraph?

seancorfield 2020-04-07T21:00:31.200500Z

^ @rs

seancorfield 2020-04-07T21:01:08.200900Z

Or is it that the Tips & Tricks reference to it could be better worded?

seancorfield 2020-04-07T21:01:37.201600Z

(I'm always looking to improve the documentation based on people's feedback, especially when they're just learning to use the library!)

this.rob 2020-04-07T21:03:05.202200Z

Yes it was just the Tips & Tricks reference that lead me to believe it wouldn’t have worked for my case

this.rob 2020-04-07T21:07:05.204500Z

I read this part: …does not always perform conversions from java.util.Date to a SQL data type. and thought this meant it wasn’t relevant because I was trying to convert from an SQL data type. Am I right in thinking that the purpose of extending SettableParameter is to make sure the dates we are inserting are converted to their SQL types? Where as extending ReadableColumn is for converting values as they come back out of the db?

this.rob 2020-04-07T21:08:09.204600Z

I’ve found them to be clear enough so far! Thanks :simple_smile:

seancorfield 2020-04-07T21:15:10.204900Z

That's correct.

seancorfield 2020-04-07T21:15:36.205400Z

OK, I'll update the text in Tips & Tricks to make next.jdbc.date-time's purpose clearer. Thank you!

kirill.salykin 2020-04-08T17:25:29.212600Z

does it make sense to have both from and to java.time conversion enables by default? without need to invoke a function?

kirill.salykin 2020-04-08T17:26:24.214200Z

now it seems confusing that it is converted by default it one direction only...

seancorfield 2020-04-08T17:27:52.215500Z

No, it definitely does not make sense to convert to Java Time by default. Protocols are whole program and that should definitely be opt-in for consumption.

seancorfield 2020-04-08T17:28:33.215700Z

It's fine to enable them by default going into next.jdbc since you're augmenting the JDBC behavior.

seancorfield 2020-04-08T17:30:25.218100Z

A real-world example: we have a large code base that spans about ten years. A lot of the early code worked with dates/times via date-clj, based on java.util.Date etc. Then we introduced clj-time and Joda Time. Over the last several years we've been moving to Java Time. Having every single JDBC query suddenly returning Java Time is going to break all the "legacy" code that expects java.util.Date and conversion to Joda Time.

seancorfield 2020-04-08T17:30:56.218900Z

Eventually, we hope to be able to switch everything to Java Time but we're years away from that.

kirill.salykin 2020-04-08T17:32:41.219500Z

ok, makes sense

kirill.salykin 2020-04-08T17:32:48.219900Z

thanks for answer