Refactor spec / string to common code
This commit is contained in:
parent
320f40ecca
commit
d25171e434
1 changed files with 21 additions and 17 deletions
|
|
@ -214,12 +214,13 @@
|
||||||
"Common logic for loading the DriverManager and the designed JDBC driver
|
"Common logic for loading the DriverManager and the designed JDBC driver
|
||||||
class and obtaining the appropriate Connection object."
|
class and obtaining the appropriate Connection object."
|
||||||
[url etc]
|
[url etc]
|
||||||
|
;; force DriverManager to be loaded
|
||||||
|
(DriverManager/getLoginTimeout)
|
||||||
(-> (DriverManager/getConnection url (as-properties etc))
|
(-> (DriverManager/getConnection url (as-properties etc))
|
||||||
(modify-connection etc)))
|
(modify-connection etc)))
|
||||||
|
|
||||||
(defn- spec->datasource
|
(defn- spec->url+etc
|
||||||
""
|
""
|
||||||
^DataSource
|
|
||||||
[{:keys [dbtype dbname host port classname] :as db-spec}]
|
[{:keys [dbtype dbname host port classname] :as db-spec}]
|
||||||
(let [;; allow aliases for dbtype
|
(let [;; allow aliases for dbtype
|
||||||
subprotocol (aliases dbtype dbtype)
|
subprotocol (aliases dbtype dbtype)
|
||||||
|
|
@ -240,8 +241,6 @@
|
||||||
;; verify the datasource is loadable
|
;; verify the datasource is loadable
|
||||||
(if-let [class-name (or classname (classnames subprotocol))]
|
(if-let [class-name (or classname (classnames subprotocol))]
|
||||||
(do
|
(do
|
||||||
;; force DriverManager to be loaded
|
|
||||||
(DriverManager/getLoginTimeout)
|
|
||||||
(if (string? class-name)
|
(if (string? class-name)
|
||||||
(clojure.lang.RT/loadClassForName class-name)
|
(clojure.lang.RT/loadClassForName class-name)
|
||||||
(loop [[clazz & more] class-name]
|
(loop [[clazz & more] class-name]
|
||||||
|
|
@ -255,7 +254,16 @@
|
||||||
(recur more)
|
(recur more)
|
||||||
(throw load-failure))))))
|
(throw load-failure))))))
|
||||||
(throw (ex-info (str "Unknown dbtype: " dbtype) db-spec)))
|
(throw (ex-info (str "Unknown dbtype: " dbtype) db-spec)))
|
||||||
;; return a DataSource
|
[url etc]))
|
||||||
|
|
||||||
|
(defn- string->url+etc
|
||||||
|
""
|
||||||
|
[s]
|
||||||
|
[s {}])
|
||||||
|
|
||||||
|
(defn- url+etc->datasource
|
||||||
|
""
|
||||||
|
[[url etc]]
|
||||||
(reify DataSource
|
(reify DataSource
|
||||||
(getConnection [this]
|
(getConnection [this]
|
||||||
(get-driver-connection url etc))
|
(get-driver-connection url etc))
|
||||||
|
|
@ -263,21 +271,16 @@
|
||||||
(get-driver-connection url
|
(get-driver-connection url
|
||||||
(assoc etc
|
(assoc etc
|
||||||
:username username
|
:username username
|
||||||
:password password))))))
|
:password password)))))
|
||||||
|
|
||||||
(defn- string->spec
|
|
||||||
""
|
|
||||||
[s]
|
|
||||||
{})
|
|
||||||
|
|
||||||
(extend-protocol
|
(extend-protocol
|
||||||
Sourceable
|
Sourceable
|
||||||
clojure.lang.Associative
|
clojure.lang.Associative
|
||||||
(get-datasource [this] (spec->datasource this))
|
(get-datasource [this] (url+etc->datasource (spec->url+etc this)))
|
||||||
DataSource
|
DataSource
|
||||||
(get-datasource [this] this)
|
(get-datasource [this] this)
|
||||||
String
|
String
|
||||||
(get-datasource [this] (get-datasource (string->spec this))))
|
(get-datasource [this] (url+etc->datasource (string->url+etc this))))
|
||||||
|
|
||||||
(extend-protocol
|
(extend-protocol
|
||||||
Connectable
|
Connectable
|
||||||
|
|
@ -393,6 +396,7 @@
|
||||||
(comment
|
(comment
|
||||||
(def db-spec {:dbtype "mysql" :dbname "worldsingles" :user "root" :password "visual" :useSSL false})
|
(def db-spec {:dbtype "mysql" :dbname "worldsingles" :user "root" :password "visual" :useSSL false})
|
||||||
(def db-spec {:dbtype "h2:mem" :dbname "perf"})
|
(def db-spec {:dbtype "h2:mem" :dbname "perf"})
|
||||||
|
(def con db-spec)
|
||||||
(def con (get-connection db-spec))
|
(def con (get-connection db-spec))
|
||||||
(println con)
|
(println con)
|
||||||
(def ds (get-datasource db-spec))
|
(def ds (get-datasource db-spec))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue