Fork me on GitHub
#sql
<
2021-06-15
>
Daniel04:06:27

Hi there, I’m reposting my question here

Daniel04:06:41

My sample code is as follows:

(ns test-db.core
  (:require [java-time :as time]
            [next.jdbc.date-time]
            [hugsql.adapter.next-jdbc :as next-adapter]
            [hugsql.core :as hugsql]))

(hugsql/def-db-fns "test_db/main.sql")

(def db "jdbc:") 

(defn test1
  []
  (insert-user! db {:user-id "222" :updated-at (time/instant)}))
The content of main.sql:
-- :name insert-user! :! :n
INSERT INTO users (user_id, updated_at)
  VALUES (:user-id, :updated-at);
Project dependencies:
:dependencies [[org.clojure/clojure "1.10.0"]
                 [org.postgresql/postgresql "42.2.19"]
                 [cheshire "5.10.0"]
                 [clojure.java-time "0.3.2"]
                 [com.github.seancorfield/next.jdbc "1.2.659"]
                 [com.layerware/hugsql "0.5.1"]
                 [com.layerware/hugsql-adapter-next-jdbc "0.5.1"]]

Daniel04:06:02

The table was created using:

CREATE TABLE users
       (id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
        user_id varchar(50),
        updated_at timestamp with time zone);

Daniel04:06:34

The error I’m getting is:

Execution error (PSQLException) at org.postgresql.jdbc.PgPreparedStatement/setObject (PgPreparedStatement.java:1011).
Can't infer the SQL type to use for an instance of java.time.Instant. Use setObject() with an explicit Types value to specify the type to use.

seancorfield05:06:02

Can you try doing the insert directly with next.jdbc to take HugSQL out of the mix?

Daniel05:06:24

Let me try that

Daniel05:06:47

Ah, it works directly with next.jdbc, so something funny is going on with hugsql

seancorfield05:06:47

There’s a #hugsql channel if that helps.

seancorfield05:06:05

(I’ll be curious to find out what the problem is)

Daniel05:06:18

Thanks @seancorfield Anyway it seems to work after I set the adapter

(hugsql/set-adapter! (next-adapter/hugsql-adapter-next-jdbc))

seancorfield05:06:43

Oh, it would be using clojure.java.jdbc I think without you doing that…

Daniel05:06:08

Yes, that’s correct, after double-checking the document, that is what it is doing

Daniel05:06:45

Thanks for the help!

Daniel05:06:48

Yes, I did the setting of the adapter earlier, but took that code out while debugging some other issue

Daniel05:06:37

Actually I was trying to get next.jdbc to run with duct but I was having an issue so I removed the adapter line (so it ends up it was calling clojure.jdbc instead)

(defmethod ig/init-key :trade-sniffer/crawler
  [_ {:keys [logger] {db :spec} :db :as config}]
  (next.jdbc.sql/insert! db :users {:user_id "222" :updated_at (time/instant)}))
This results in an error:
next.jdbc.sql/insert!         sql.clj:  190
                  next.jdbc.sql/insert!         sql.clj:  192
                 next.jdbc/execute-one!        jdbc.clj:  198
     next.jdbc.protocols/eval21234/fn/G   protocols.clj:   33
      next.jdbc.result-set/eval21823/fn  result_set.clj:  609
     next.jdbc.protocols/eval21172/fn/G   protocols.clj:   14
      next.jdbc.connection/eval22955/fn  connection.clj:  258
     next.jdbc.connection/spec->url+etc  connection.clj:  192
clojure.lang.ExceptionInfo: Unknown dbtype:
    datasource: #object[net.ttddyy.dsproxy.support.ProxyDataSource 0x51ce4746 "[email protected]"]
The same db value works with clojure.jdbc though

Daniel05:06:14

Where (:db config) has the following value:

duct.database.sql.Boundary{:spec {:datasource #object[net.ttddyy.dsproxy.support.ProxyDataSource 0x307dad3d "[email protected]"]}}

Daniel05:06:16

I think I have figured it out, it needs one more redirection down to :datasource

Daniel05:06:11

So instead of (insert! db xxx) it had to be (insert! (:datasource db) xxx)

seancorfield06:06:00

That suggests that whatever you're using to set up the datasource is using (or at least assuming) clojure.java.jdbc