Fork me on GitHub
#clojure
<
2016-08-20
>
emil0r08:08:45

Anyone got an idea how to convert postgres arrays into clojure vectors with newer versions of jdbc? (extend-protocol clojure.java.jdbc/ISQLParameter clojure.lang.IPersistentVector (set-parameter [v ^java.sql.PreparedStatement stmt ^long i] (let [conn (.getConnection stmt) meta (.getParameterMetaData stmt) type-name (.getParameterTypeName meta i)] (if-let [elem-type (when (= (first type-name) \_) (apply str (rest type-name)))] (.setObject stmt i (.createArrayOf conn elem-type (to-array v))) (.setObject stmt i v))))) the above code no longer works and it’s deep in java land, so I’m lost >_>

emil0r08:08:59

clojure.java.jdbc i meant 🙂

hiredman08:08:42

that code does the reverse

emil0r08:08:03

ah yeah. clojure vector -> postgres arrays

emil0r08:08:24

is what i want. the postgres arrays -> clojure vector conversion still works

emil0r08:08:41

early in the morning

hiredman08:08:10

what does it do instead of working?

emil0r08:08:51

AbstractJdbc2Statement.java: 2746 org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler/handleError QueryExecutorImpl.java: 457 org.postgresql.core.v3.QueryExecutorImpl$1/handleError QueryExecutorImpl.java: 1887 org.postgresql.core.v3.QueryExecutorImpl/processResults QueryExecutorImpl.java: 405 org.postgresql.core.v3.QueryExecutorImpl/execute AbstractJdbc2Statement.java: 2893 org.postgresql.jdbc2.AbstractJdbc2Statement/executeBatch jdbc.clj: 404 clojure.java.jdbc/execute-batch jdbc.clj: 397 clojure.java.jdbc/execute-batch jdbc.clj: 755 clojure.java.jdbc/db-do-execute-prepared-statement/fn jdbc.clj: 589 clojure.java.jdbc/db-transaction* jdbc.clj: 562 clojure.java.jdbc/db-transaction* RestFn.java: 425 clojure.lang.RestFn/invoke jdbc.clj: 754 clojure.java.jdbc/db-do-execute-prepared-statement jdbc.clj: 742 clojure.java.jdbc/db-do-execute-prepared-statement jdbc.clj: 774 clojure.java.jdbc/db-do-prepared jdbc.clj: 758 clojure.java.jdbc/db-do-prepared RestFn.java: 464 clojure.lang.RestFn/invoke jdbc.clj: 879 clojure.java.jdbc/execute!/execute-helper jdbc.clj: 884 clojure.java.jdbc/execute! jdbc.clj: 863 clojure.java.jdbc/execute! RestFn.java: 425 clojure.lang.RestFn/invoke

emil0r08:08:59

throw that error

hiredman08:08:24

where is the message?

hiredman08:08:09

you likely need to call getNextException on the exception object to get the real exception

hiredman08:08:48

as almost all postgres exceptions really annoyingly say, "Call getNextException to see the cause"

emil0r08:08:03

yeah. it’s there

hiredman08:08:33

all the exceptions in that blob you pasted say "Call getNextException to see the cause"

emil0r08:08:21

"ERROR: syntax error at or near \"NULL\"\n Position: 109"

emil0r08:08:27

at the very top

hiredman08:08:47

ok, so what makes you think that is related to the array stuff?

emil0r08:08:01

so a nil value is tried to be passed in for category_ref instead of the vector i tried to pass in

emil0r08:08:45

and it’s related to that because the insert works when i remove the column that has the vector in the code

emil0r08:08:56

ie, it doesn’t try to do the conversion

hiredman08:08:49

INSERT INTO

   advertisements (title, description, url, category_ref, visible_p,

   our_id) VALUES ('StartShop', 'StartShop', '', 1 NULL, FALSE, -1)

hiredman08:08:18

that 1 NULL with no coma looks very suspicious

hiredman08:08:00

it isn't just passing NULL for category_ref, it is passing 1 NULL

hiredman08:08:15

do you have an extra nil in your vector?

emil0r08:08:21

no. just [1]

hiredman08:08:00

how do you know it is just [1]?

emil0r08:08:17

because that’s what i have in the code >_>

hiredman08:08:35

where in the code?

hiredman08:08:58

is there something between you and java.jdbc that could be doing stuff?

hiredman08:08:56

it looks like you are using honeysql via ez-database, my guess is honeysql doesn't handle arrays

emil0r08:08:54

i’ve had it working

emil0r08:08:07

then i did an upgrade to clojure.java.jdbc

emil0r08:08:08

and it broke

hiredman08:08:31

I would double check to see that is all you changed

hiredman08:08:19

I don't know that much about honeysql internals, but it looks like it tries to treat all sequential collections as a cast, where the first thing is the value and the second thing is the type

hiredman08:08:36

which seems to line up with 1 NULL for the vector [1]

emil0r08:08:00

hmm maybe. but i got the same version of honeysql as well. sigh… thanks for being a soundboard. got some places to dig now at least

hiredman08:08:18

https://github.com/jkk/honeysql/issues/85 looks like honesql uses a special reader macro for arrays

emil0r08:08:00

that worked. still… weird

hiredman08:08:00

computers 🎊

pshk4r15:08:35

I am trying to test my API (`composure-api` + midje) to see if it returns correct entities from “DB” (atom) for each type of request but I am very new to Clojure and don’t really understand how to override (mock) that DB in tests. How do I do that?

plexus16:08:15

@pshk4r with-redefs is your friend, something like this

plexus16:08:19

(untested)

plexus16:08:26

(def db (atom {}))

(deftest my-test
  (with-redefs [db (atom {:some :values)]
    (= (my-business-logic) {:some :output)))

pshk4r16:08:49

@plexus that’s exactly what I need! thank you a lot

pshk4r17:08:03

@plexus thanks to you I found about the provided from Midje, and it's more flexible - just FYI 🙂

abolduc17:08:36

Hello dear Clojure friends, I'd like to build a Full Stack Clojure web application but I get stuck with dependency injection problems. Let say I want to use a Postgresql database in my production environment but id like to swap this and use an atomic db whenever I develop. I have looked into https://github.com/stuartsierra/component but it looks like I have to build the entire application in the component/Lifecycle framework and I will live and die with it. Do you have any advice for me about that problem ? Am I getting something wrong ? I am opened for discussion about that topic !

chrisjd17:08:16

@abolduc: Try https://github.com/tolitius/mount if you're finding component too prescriptive. I personally find it much easier to work with.

abolduc17:08:35

Oh I'll take a deeper look at this. I love the mount/start-with-states function, thank you @chrisjd

lvh19:08:39

(def prefix-paths
  "Given a coll of items, returns a coll of the prefixes of the coll."
  (comp rest (partial reductions conj [])))
^ That’s reasonably idiomatic, right?

lvh19:08:18

(`(prefix-paths [:a :b :c]) ;; => '([:a] [:a :b] [:a :b :c])`)

chrisjd20:08:24

@lvh The only thing I might expect is for it to take/return seqs rather than colls.

brabster21:08:12

hey all, I'm trying to write a leiningen plugin that uses clojure.spec - not sure how (if) I can make Leiningen use clojure 1.9?

gfredericks22:08:34

I doubt you can

gfredericks22:08:02

you want it to use clojure.spec just to make it easier for you to write?

brabster22:08:24

@gfredericks no, I wrote another lib that uses spec and I wanted to write a leiningen plugin to support that

brabster22:08:47

but my plugin would need to import that lib

brabster22:08:03

It's no big deal if it's not possible, just wanted to check I wasn't missing anything 🙂

gfredericks22:08:00

brabster: not sure how familiar you are with the lein plugin architecture, but a lot of times plugins can be really thin and can execute the library code in the application jvm

gfredericks22:08:12

if yours can be structured that way then this wouldn't be a problem presumably

brabster22:08:00

that would probably work - the library turns clojure into aws cloudformation json and I want to make it easy to keep the templates in the same project as the code

brabster22:08:21

I'll go check the docs for running the plugin in the app jvm thanks!

gfredericks22:08:32

brabster: it'd probably consist of adding the lib to the deps list and calling the lein run task

brabster22:08:13

oh right cheers

lvh22:08:38

@chrisjd: good point, I guess the implementation already does that correctly

amacdougall23:08:56

Running into a point of confusion here... I'm trying to use the Specter library, including it as follows:

(ns mazes.core
  "Basic tools for maze wrangling."
  (:require [clojure.spec :as s]
            [com.rpl.specter :as specter]))
In my code, I can call (specter/transform <whatever>) and, although it fails because I'm doing something wrong, that function is definitely in existence. However, in the REPL, I can't seem to load the library:
user=> (load "mazes/core")

CompilerException java.lang.RuntimeException: No such var: specter/transform, compiling:(mazes/core.cljc:83:5) 
Of course, it's defined right there in my project.clj:
:dependencies [[org.clojure/clojure "1.9.0-alpha10"],
               [com.rpl/specter "0.12.0"]]

amacdougall23:08:03

...what gives? I've never run into this before.

amacdougall23:08:37

If I (require '[com.rpl.specter :as specter]) in the REPL, it returns nil.

nathanmarz23:08:07

@amacdougall transform is in the com.rpl.specter.macros namespace

amacdougall23:08:43

Very strange... but that code does seem to successfully invoke transform when I use the same namespace in a test:

com.rpl.specter$transform.invokeStatic (specter.clj:85)
 com.rpl.specter$transform.invoke (specter.clj:81)
 mazes.core$link.invokeStatic (core.cljc:83)

amacdougall23:08:35

Also, is that new since 0.9.2? I have a project set to that version, and it doesn't include macros but does seem to have access to transform and setval.

nathanmarz23:08:52

in previous versions transform was a function in the com.rpl.specter namespace

nathanmarz23:08:58

that changed as of 0.11.0

amacdougall23:08:30

Ah, so ... okay, I just bumped to 0.12.0, so maybe the test env is still running the 0.9.2 version somehow. Let me see if I can kick it in the pants.

nathanmarz23:08:00

yea that sounds like the issue

amacdougall23:08:52

Yeah, that was it. Thanks! And thanks for making such a handy library. In that previous project, I only needed a bit of it, but I expect to be hitting it pretty hard in this new one. ...and now that I've got it working in the REPL, I can start figuring out exactly what I was doing wrong...