我正在尝试使用Korma连接到Oracle数据库。首先,我尝试使用此代码并成功连接。
(defdb korma-db
{:classname "oracle.jdbc.OracleDriver"
:subprotocol "oracle"
:subname "thin:@my.oracle.db:1521:testdb"
:user "xxx"
:password "xxx"
:naming {:keys str/lower-case :fields str/upper-case}})
但我发现还有一个更方便的函数korma.db.oracle
。所以代码看起来简化如下:
(defdb korma-db
(oracle {:keys ["my.oracle.db" 1521 true]
:as {:user "xxx" :password "xxx"}
:naming {:keys str/lower-case :fields str/upper-case}}))
但它失败了。它尝试localhost
。当我检查oracle
函数的结果时,我发现有一些奇怪的东西。
{:naming {:keys #<string$lower_case clojure.string$lower_case@5ba760ac>,
:fields #<string$upper_case clojure.string$upper_case@504f4c0b>},
:as {:user "xxx", :password "xxx"},
:keys ["my.oracle.db" 1521 true],
:classname "oracle.jdbc.driver.OracleDriver",
:subprotocol "oracle:thin",
:subname "@localhost:1521", :make-pool? true}
^^^^^^^^^
我检查了Korma源,oracle
函数的代码是:
(defn oracle
"Create a database specification for an Oracle database. Opts should include keys
for :user and :password. You can also optionally set host and port."
[{:keys [host port make-pool?]
:or {host "localhost", port 1521, make-pool? true}
:as opts}]
(merge {:classname "oracle.jdbc.driver.OracleDriver" ; must be in classpath
:subprotocol "oracle:thin"
:subname (str "@" host ":" port)
:make-pool? make-pool?}
opts))
简而言之,我的问题是:
oracle
函数的确切用法是什么?我错过了什么oracle
函数的代码对我来说很奇怪。有人能解释一下吗请提供代码,尤其是参数部分。。。[{:keys [host .... ] :or {...} :as ...}]
此oracle
函数正在销毁其参数。这是一篇关于此功能的文章。
因此,调用oracle
的正确方法是:
(defdb korma-db
(oracle { :user "xxx"
:password "xxx"
:host "my.oracle.db"
:port 1521
:make-pool? true }))
编辑:指定oracle SID可以这样做:
(defdb korma-db
(oracle {:subname "@//xxx.xxx.xxx.xxx:1521/testdb"
:user "xxx"
:password "xxx"
:naming {:keys str/lower-case :fields str/upper-case}}))