Fork me on GitHub
#sql
<
2022-02-04
>
seancorfield14:02:53

Moving this conversation to the #sql channel... Looking at the hikari-cp code, make-datasource should return a DataSource object that next.jdbc should be happy with so I guess I'd need to see how you're using it to provide more details...

seancorfield14:02:38

It seems to work for me @errelinaaron:

(! 521)-> clojure -Sdeps '{:deps {hikari-cp {:mvn/version "RELEASE"}}}' -M:test:rebel:dev/repl
DEPRECATED: Libs must be qualified, change hikari-cp => hikari-cp/hikari-cp 
Downloading: com/bhauman/rebel-readline/maven-metadata.xml from clojars
Selected port 57739 for the Socket REPL...
Starting Rebel Readline as the REPL...
[Rebel readline] Type :repl/help for online help info
dev=> (require '[hikari-cp.core :refer [make-datasource]] '[next.jdbc :as jdbc])
nil
dev=> (def ds (make-datasource {:jdbc-url "jdbc:" :username "root" :password (System/getenv "MYSQL_ROOT_PASSWORD")}))
#'dev/ds
dev=> (jdbc/execute! ds ["select * from status"])
[#:status{:id 1, :name "approved"} #:status{:id 2, :name "new"} #:status{:id 3, :name "rejected"}]
dev=> 
(I ran that in the next.jdbc project so :test brings in all the drivers)

erre lin15:02:21

After seeing your example, I realized I used wrong syntax:

(def datasource-options
  {:auto-commit        true
   :read-only          false
   :connection-timeout 30000
   :validation-timeout 5000
   :idle-timeout       600000
   :max-lifetime       1800000
   :minimum-idle       10
   :maximum-pool-size  20
   :pool-name          "db-pool"
   :adapter            "postgresql"
   :username           "user"
   :password           "passwd"
   :database-name      "mydbname"
   :server-name        "localhost"
   :port-number        5432
   :register-mbeans    false})
(defonce datasource
  (delay (hcp/make-datasource datasource-options)))

(def database-connection {:datasource @datasource})

;; add-rush-orders is a fn generated by HugSQL
;; it accepts a db, ds, or connection
defn first-connect []
  (add-rush-orders (jdbc/get-connection database-connection) {:orders sh/first-five-rows}))
It seems, after making the datasource, I should start passing it to next.jdbc's fns, other than making that connection var? I'll follow your example. Really helpful. Thank you again

seancorfield16:02:19

No, just use the DataSource object directly.

seancorfield16:02:19

next.jdbc can work with Connection and DataSource (Java) objects directly. If you pass it something else, it will try to turn it into a datasource and then get the connection from that.

seancorfield16:02:43

I would strongly recommend not using hikari-cp -- it's pointless when HikariCP is directly supported in next.jdbc.

seancorfield16:02:19

That way you can just follow the next.jdbc documentation and examples (instead of getting confused trying to combine two libraries that don't know about each other).

andersmurphy15:02:42

Hi, are migrating to next.jdbc and the documentation has been fantastic. 🙏 Thanks @seancorfield. I’ve been playing around with plan and wondering if there’s a way to make it support kebab case (like execute!):

(->> (plan db {:select [:*]
                  :from   [:account]})
        (into [] (comp
                  (map :account/kebab_data)
                  (map :foo)
                  (take 1))))
works, but:
(->> (plan db {:select [:*]
                  :from   [:account]})
        (into [] (comp
                  (map :account/kebab-data)
                  (map :foo)
                  (take 1))))
Does not work. Any ideas?

👍 1
erre lin15:02:07

Sorry for being off-topic, but wondering in the future would you consider adding examples of using next.jdbc in hikari-cp's doc? I think that would be greatly helpful as next.jdbc is quite popular and nice to use.

lukasz15:02:44

@errelinaaron hikari-cp is "just" a connection pool, so once it's setup it shouldn't be any different from a regular JDBC connection - next.jdbc doesn't really care as long as it's valid

erre lin16:02:51

Yes, that's correct. I'm new to Clojure and know little about Java, so sometimes even a small difference between syntaxes of two similar libs could confuse me. Since I see there are examples for different types of databases, I think it might also be helpful if the doc could include examples of next.jdbc . Anyway, thanks for making that point clear, helpful too:grin:👍

andersmurphy16:02:24

(defonce datasource
  (delay
    (connection/->pool
     HikariDataSource
     {:dbtype          "postgresql"
      :dbname          "plan-b"
      :minimumIdle     5
      :maximumPoolSize 5})))

andersmurphy16:02:32

You don’t need to use the clojure https://github.com/tomekw/hikari-cp wrapper with next.jdbc. You can just use the HikariCP java library.

erre lin16:02:08

Hi @U0JEEGD4N, thank you for the reply. You're right. That's what I found later when reading next.jdbc's doc. I was just curious if I guessed it right. Based on @seancorfield’s reply, I found I should directly pass @datasource to next.jdbc Fns. But, again, as you already pointed out, the wrapper becomes optional in my case. I'll stay out of this thread to avoid distracting your original post too much. Thank you all:clap:

seancorfield16:02:53

I don't use hikari-cp and know nothing about it -- the code I pasted was my first ever interaction with it -- and since next.jdbc supports HikariCP directly (and c3p0 too) there's no point in me confusing next.jdbc's users by pointing to a library they do not need to use.

👍 1
seancorfield16:02:38

@errelinaaron And please don't hijack other people's threads.

seancorfield16:02:21

@U0JEEGD4N plan operates on a lower level than execute! and is specifically intended to avoid using builders -- and stuff like snake_case/kebab-case is only handled in builders.

andersmurphy16:02:07

Thought that might be the case. Thanks for the prompt reply!

seancorfield16:02:44

So the answer is "no". The reducing functions for plan only have access to the simple names in the ResultSet. In particular, :account/snake_case is a bit pointless since there's no qualifiers inside plan -- that's also about naming inside builders.

👍 2