Fork me on GitHub
#sql
<
2020-04-27
>
borkdude09:04:27

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?

seancorfield18:04:09

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

seancorfield18:04:12

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

seancorfield18:04:11

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

borkdude18:04:32

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

Karol W贸jcik11:04:32

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?

bartuka11:04:28

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

bartuka11:04:44

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

borkdude11:04:49

@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)))

bartuka11:04:21

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

borkdude11:04:39

@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

bartuka11:04:30

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 馃檪

borkdude11:04:20

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

seancorfield18:04:44

@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

seancorfield18:04:18

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