Refactor spec / string to common code

This commit is contained in:
Sean Corfield 2019-01-09 23:30:46 -08:00
parent 320f40ecca
commit d25171e434

View file

@ -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,29 +254,33 @@
(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]))
(reify DataSource
(getConnection [this]
(get-driver-connection url etc))
(getConnection [this username password]
(get-driver-connection url
(assoc etc
:username username
:password password))))))
(defn- string->spec (defn- string->url+etc
"" ""
[s] [s]
{}) [s {}])
(defn- url+etc->datasource
""
[[url etc]]
(reify DataSource
(getConnection [this]
(get-driver-connection url etc))
(getConnection [this username password]
(get-driver-connection url
(assoc etc
:username username
:password password)))))
(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))