Skip to content

Commit

Permalink
updated to use hikari-cp library for config
Browse files Browse the repository at this point in the history
  • Loading branch information
yogthos committed May 8, 2016
1 parent af2138d commit fce1c01
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 44 deletions.
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,10 @@ The function accepts a map with the database specification.
(connect! pool-spec)
```

The following options are supported:
For the complete list of configuration options refer to the official [hikari-cp](https://github.com/tomekw/hikari-cp) library documentation. Conman supports the following additional options:

* `:jdbc-url`
* `:datasource`
* `:datasource-classname`
* `:username`
* `:password`
* `:auto-commit?`
* `:conn-timeout`
* `:idle-timeout`
* `:max-lifetime`
* `:min-idle`
* `:max-pool-size`
* `:pool-name`

The connection can be terminated by running the `disconnect!` function:

Expand Down
5 changes: 3 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject conman "0.5.2"
(defproject conman "0.5.3"
:description "a database connection management library"
:url "https://github.com/luminus-framework/conman"
:license {:name "Eclipse Public License"
Expand All @@ -7,7 +7,8 @@
[com.zaxxer/HikariCP "2.4.6"]
[com.layerware/hugsql "0.4.7"]
[com.carouselapps/to-jdbc-uri "0.5.0"]
[org.clojure/java.jdbc "0.6.0-rc1"]]
[org.clojure/java.jdbc "0.6.0-rc1"]
[hikari-cp "1.6.1"]]
:profiles
{:dev
{:dependencies [[com.h2database/h2 "1.4.191"]]}})
56 changes: 25 additions & 31 deletions src/conman/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
(:require [to-jdbc-uri.core :refer [to-jdbc-uri]]
[hugsql.core :as hugsql]
[clojure.java.io :as io]
[hikari-cp.core :refer [make-datasource datasource-config]]
[clojure.set :refer [rename-keys]]
clojure.java.jdbc)
(:import [com.zaxxer.hikari HikariConfig HikariDataSource]))

Expand All @@ -26,41 +28,33 @@
filenames))

(defmacro bind-connection [conn & filenames]
`(let [{snips# :snips fns# :fns :as queries#} (conman.core/load-queries '~filenames)]
(doseq [[id# {fn# :fn {doc# :doc} :meta}] snips#]
(intern *ns* (with-meta (symbol (name id#)) {:doc doc#}) fn#))
(doseq [[id# {fn# :fn {doc# :doc} :meta}] fns#]
(intern *ns* (with-meta (symbol (name id#)) {:doc doc#})
(fn
([] (fn# ~conn {}))
([params#] (fn# ~conn params#))
([conn# params#] (fn# conn# params#))
([conn# params# opts# & command-opts#]
(apply fn# conn# params# opts# command-opts#)))))
queries#))
`(let [{snips# :snips fns# :fns :as queries#} (conman.core/load-queries '~filenames)]
(doseq [[id# {fn# :fn {doc# :doc} :meta}] snips#]
(intern *ns* (with-meta (symbol (name id#)) {:doc doc#}) fn#))
(doseq [[id# {fn# :fn {doc# :doc} :meta}] fns#]
(intern *ns* (with-meta (symbol (name id#)) {:doc doc#})
(fn
([] (fn# ~conn {}))
([params#] (fn# ~conn params#))
([conn# params#] (fn# conn# params#))
([conn# params# opts# & command-opts#]
(apply fn# conn# params# opts# command-opts#)))))
queries#))

(defn- make-config
[{:keys [jdbc-url datasource datasource-classname username
password auto-commit? conn-timeout idle-timeout
max-lifetime min-idle max-pool-size pool-name]}]
(let [cfg (HikariConfig.)
uri (when jdbc-url (to-jdbc-uri jdbc-url))]
(when (not (or uri datasource datasource-classname ))
(throw (Exception. ":jdbc-url, :datasource, or :datasource-classname is required to initialize the connection!")))
(when uri (.setJdbcUrl cfg uri))
(defn- make-config [{:keys [jdbc-url adapter datasource datasource-classname] :as pool-spec}]
(when (not (or jdbc-url adapter datasource datasource-classname))
(throw (Exception. "one of :jdbc-url, :adapter, :datasource, or :datasource-classname is required to initialize the connection!")))
(let [cfg (datasource-config
(-> pool-spec
(update :jdbc-url #(when % (to-jdbc-uri %)))
;;backwards compatibility
(rename-keys {:auto-commit? :auto-commit
:conn-timeout :connection-timeout
:min-idle :minimum-idle
:max-pool-size :maximum-pool-size})))]
(when datasource (.setDataSource cfg datasource))
(when datasource-classname (.setDataSourceClassName cfg datasource-classname))
(when username (.setUsername cfg username))
(when password (.setPassword cfg password))
(when (some? auto-commit?) (.setAutoCommit cfg auto-commit?))
(when conn-timeout (.setConnectionTimeout cfg conn-timeout))
(when idle-timeout (.setIdleTimeout cfg conn-timeout))
(when max-lifetime (.setMaxLifetime cfg max-lifetime))
(when max-pool-size (.setMaximumPoolSize cfg max-pool-size))
(when min-idle (.setMinimumIdle cfg min-idle))
(when pool-name (.setPoolName cfg pool-name))
cfg))

(defn connect!
"attempts to create a new connection and set it as the value of the conn atom,
does nothing if conn atom is already populated"
Expand Down

0 comments on commit fce1c01

Please sign in to comment.