sql

All things SQL and JDBC...
Daniel 2021-06-15T04:53:27.203500Z

Hi there, I’m reposting my question here

Daniel 2021-06-15T04:55:41.204800Z

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:<postgresql://localhost/foobar>") 

(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"]]

Daniel 2021-06-15T04:57:02.205500Z

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

Daniel 2021-06-15T04:57:34.206100Z

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.

seancorfield 2021-06-15T05:03:02.207100Z

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

Daniel 2021-06-15T05:03:24.207500Z

Let me try that

Daniel 2021-06-15T05:08:47.208Z

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

seancorfield 2021-06-15T05:17:47.208500Z

There’s a #hugsql channel if that helps.

seancorfield 2021-06-15T05:18:05.209300Z

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

Daniel 2021-06-15T05:18:18.209600Z

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

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

seancorfield 2021-06-15T05:18:43.210200Z

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

Daniel 2021-06-15T05:19:08.210700Z

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

Daniel 2021-06-15T05:19:45.210900Z

Thanks for the help!

seancorfield 2021-06-15T05:19:57.211300Z

There’s a HugSQL quick start in the next.jdbc docs BTW https://cljdoc.org/d/com.github.seancorfield/next.jdbc/1.2.659/doc/getting-started/friendly-sql-functions#hugsql-quick-start

Daniel 2021-06-15T05:20:48.212100Z

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

Daniel 2021-06-15T05:28:37.214100Z

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-&gt;url+etc  connection.clj:  192
clojure.lang.ExceptionInfo: Unknown dbtype:
    datasource: #object[net.ttddyy.dsproxy.support.ProxyDataSource 0x51ce4746 "net.ttddyy.dsproxy.support.ProxyDataSource@51ce4746"]
The same db value works with clojure.jdbc though

Daniel 2021-06-15T05:31:14.214800Z

Where (:db config) has the following value:

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

Daniel 2021-06-15T05:32:16.215400Z

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

Daniel 2021-06-15T05:33:11.216100Z

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

seancorfield 2021-06-15T06:08:00.217400Z

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