sql

All things SQL and JDBC...
borkdude 2021-03-10T14:31:55.010200Z

Would it be a bad idea to install these settings:

(import  '[java.sql Array])
(require '[next.jdbc.result-set :as rs])

(extend-protocol rs/ReadableColumn
  Array
  (read-column-by-label [^Array v _]    (vec (.getArray v)))
  (read-column-by-index [^Array v _ _]  (vec (.getArray v))))
in babashka-sql-pods? This would always return a vector if you select an array from a database. I think this is what you want 99% or maybe 100% of the time, but are there any situations where you would rather preserve the array?

borkdude 2021-03-10T14:33:25.010600Z

Note: this doesn't affect any other libraries because pods live in their own process

borkdude 2021-03-10T14:36:15.011200Z

I could make it so that you would get a (non sql, just normal java) array, but why the heck would anyone want that

borkdude 2021-03-10T14:36:46.011600Z

This isn't breaking because arrays did not work at all before

borkdude 2021-03-10T14:37:05.011900Z

Thanks. :duckie:

borkdude 2021-03-10T14:41:59.012100Z

(testing "inserting an array"
      (is (db/execute! db ["create table bar ( bar integer[] );"]))
      (is (db/execute! db ["insert into bar values (?);" (into-array [1 2 3])]))
      (is (= [#:bar{:bar [1 2 3]}] (db/execute! db ["select * from bar"]))))

emccue 2021-03-10T15:25:07.012700Z

@borkdude if you extend the protocols to support json it wouldn't round trip right

emccue 2021-03-10T15:25:36.013300Z

like, often what I will do is make it so vectors and maps will write as json

emccue 2021-03-10T15:26:23.014100Z

idk if thats relevant

borkdude 2021-03-10T15:27:53.015800Z

this is about Java arrays, but your point about JSON is something I considering too. So for inserting you would always use a Java array, but in the query result you would get back a vector so round-tripping doesn't work, but does that have to work? For JSON: I think just use Clojure data structures for inserting? And for the result, what to do, also deserialize as Clojure?

borkdude 2021-03-10T15:33:42.016200Z

For JSON there are more things to consider though: keywordize or not?

borkdude 2021-03-10T15:33:57.016500Z

This should probably be an option (defaulting to true?)

borkdude 2021-03-10T16:11:41.016700Z

2021-03-10T16:20:39.018500Z

@borkdude Here are my opinionated translations between Postgres and Clojure. I'm pretty happy with this, with the exception of the enum, which is a horrific kludge... https://github.com/dcj/coerce/blob/develop/src/coerce/jdbc/pg.clj

💯 1
borkdude 2021-03-10T16:23:04.018800Z

Nice, thank you

2021-03-10T16:24:04.019Z

FWIW, YMMV 🙂, your goals are broader than mine were when I wrote this.... but perhaps fodder for thought

seancorfield 2021-03-10T17:50:15.019400Z

Nice work @dcj!

2021-03-10T17:52:44.022100Z

Thanks!, but please note that I stand on the shoulders of others, please refer to the README in that repo for credit to original sources

2021-03-10T18:06:52.022400Z

https://github.com/dcj/coerce#credits

😊 1
danielglauser 2021-03-10T21:18:11.023600Z

Is anyone using this https://github.com/impossibl/pgjdbc-ng with next-jdbc? If so did you encounter any issues? We're going to try it for ourselves just wanted to know if there were any known issues.

seancorfield 2021-03-10T21:25:18.024500Z

@danielglauser I used to test clojure.java.jdbc with a version of that: https://github.com/clojure/java.jdbc/blob/master/deps.edn#L16 -- I haven't tested it recently (with next.jdbc).

seancorfield 2021-03-10T21:25:44.025100Z

I don't remember encountering any specific issues but it's been a while.

danielglauser 2021-03-10T21:26:55.025200Z

Thanks Sean! We're about to test it with our system, I'll post back here if we encounter any issues.