Fork me on GitHub
#sql
<
2023-01-05
>
Noah Bogart15:01:05

java.jdbc usage/design question: we have a function db/query which calls jdbc/query in a try-catch that passes caught exceptions to this function

(defn- handle-exception
  [exc]
  (throw+
   {:type (cond (re-find #"canceling.*statement timeout" (str exc)) ::timeout
                (re-find #"duplicate key value violates unique constraint"
                         (str exc))
                ::unique-constraint
                :else ::unknown)}
   exc
   (str "Query error: " exc)))
This seems like a bad idea to me, but I'm struggling to articulate exactly why. Do any of y'all use such wrapping functions? Do you manually write the regex logic every time you need it?

lukasz16:01:27

I had to write a macro that does something like that for PSQL and unique constraints, but I use it rarely and instead fall back to using upserts

orestis15:01:45

We try to use our own wrapper function for executing queries (we pass in a honeysql map) and add extra args etc. we don’t do custom exception handling but it would be a natural place.

👍 2
d._.b21:01:59

I'm just trying out hugsql. I initialize a hikari datasource and can do (jdbc/execute-one! datasource ["select * from table"]), but hugsql doesn't like my reference a datasource and instead seems to want a db-spec.

lukasz21:01:03

can you try passing it as {:datasource datasource} ?

lukasz21:01:16

also, which adapter are you using? I think it still defaults to clojure.java.jdbc

lukasz21:01:40

(component use strictly not required)

d._.b21:01:48

yes, my issue was the adapter

d._.b21:01:58

i threw the next adapter in there on init and it works now

d._.b21:01:15

thanks for the reply!

👍 2