sql

All things SQL and JDBC...
borkdude 2020-04-27T09:50:27.349200Z

Hi. I added postgres support via JDBC to babashka: https://github.com/borkdude/babashka/#jdbc I'm currently testing this with a Docker container locally. But I saw that next.jdbc itself tests with a library called "embedded postgres". How does this library work? Does it mock postgres, or does it really include it? Can it also be used to create something like sqlite, so beyond testing?

Karol Wójcik 2020-04-27T11:08:32.351500Z

Hi. I'm looking for a way to automatically convert Clojure vector types to array of some type so that I can inject the data to pg database. I'm using next.jdbc and I cannot find the same parameters as in clojure.jdbc which would allow it. Any hints?

bartuka 2020-04-27T11:10:28.351700Z

hi @karol.wojcik , in this documentation page https://github.com/seancorfield/next-jdbc/blob/master/doc/tips-and-tricks.md there is a snippet about converting clojure map to json/jsonb data types

bartuka 2020-04-27T11:10:44.351900Z

it might be very similar with what you are trying to achieve if I understood correctly

borkdude 2020-04-27T11:11:49.352500Z

@karol.wojcik I have this function in my codebase:

(defn pg-array
  [conn v]
  (.createArrayOf  ^java.sql.Connection (:connection conn) "text" (into-array String v)))

bartuka 2020-04-27T11:13:21.352600Z

@borkdude would be a wrong approach to extend the protocols and handle this?

borkdude 2020-04-27T11:14:39.353500Z

@iagwanderson don't know. I found it a little weird that this function needs a connection, but maybe it needs to get some platform specific information via the connection to produce the array. Not sure if the protocols in next.jdbc support that

bartuka 2020-04-27T11:30:30.354700Z

I looked into the code of the clj-postgresql and they have the conversion there through protocols. Would be like this following the examples in next.jdbc tips and tricks page:

(extend-protocol prepare/SettableParameter
  clojure.lang.IPersistentVector
  (set-parameter [v ^PreparedStatement s ^long i]
    (let [conn (.getConnection s)
          meta (.getParameterMetaData s)
          type-name (.getParameterTypeName meta i)]
      (if-let [elem-type (when type-name (second (re-find #"^_(.*)" type-name)))]
        (.setObject s i (.createArrayOf conn elem-type (to-array v)))
        (.setObject s i (vec->parameter v type-name))))))
@borkdude the conn is there yet 🙂

borkdude 2020-04-27T11:40:20.355200Z

do any one you use embedded postgres for testing btw? (https://clojurians.slack.com/archives/C1Q164V29/p1587981027349200)

Karol Wójcik 2020-04-27T12:01:03.356Z

@iagwanderson @borkdude Thanks! Works like a charm!

seancorfield 2020-04-27T18:16:09.356300Z

It's almost full-blown PostgreSQL just as an embedded server.

seancorfield 2020-04-27T18:17:16.356500Z

https://github.com/opentable/otj-pg-embedded

💯 1
seancorfield 2020-04-27T18:18:12.356800Z

"The JAR file contains bundled version of Postgres. You can pass different Postgres version by implementing PgBinaryResolver."

seancorfield 2020-04-27T18:19:11.357Z

(I don't think it has any persistence options, but I didn't dig very deep)

borkdude 2020-04-27T18:20:32.357200Z

thanks. they explicitly mention "for testing" so that kind of hints at "don't use for anything serious" like a replacement for sqlite

seancorfield 2020-04-27T18:26:44.358200Z

@karol.wojcik It sounds like you didn't find the answer in the next.jdbc docs? Here's the section in Getting Started that talks about working with additional data types https://cljdoc.org/d/seancorfield/next.jdbc/1.0.424/doc/getting-started#working-with-additional-data-types

seancorfield 2020-04-27T18:28:18.359200Z

That links to the docs for extending how parameters are set (data going into the database) and how data is read from the DB.