clojurekormasqlkorma

Exact usage of `oracle` function in Korma and the meaning of clojure code?


I'm trying to connect to Oracle database using Korma. First I tried to use this code and successfully connected.

(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}})

But I found that there is a more convenient function korma.db.oracle. So the code looked be simplified like this:

(defdb korma-db
  (oracle {:keys ["my.oracle.db" 1521 true]
           :as {:user "xxx" :password "xxx"}
           :naming {:keys str/lower-case :fields str/upper-case}}))

But it fails. It tried to localhost. When I check the result of oracle function, I found out that there's something strange.

{: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}
            ^^^^^^^^^

I checked the Korma source, and the code of oracle function is:

(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))

In short, my questions are:


Solution

  • This oracle function destructuring its arguments. Here is an article about this feature.

    So, the right way to call oracle is:

    (defdb korma-db
      (oracle { :user "xxx"
                :password "xxx"
                :host "my.oracle.db"
                :port 1521
                :make-pool? true }))
    

    EDIT: specifying the oracle SID could be done like this:

    (defdb korma-db
      (oracle {:subname "@//xxx.xxx.xxx.xxx:1521/testdb"
               :user "xxx"
               :password "xxx"
               :naming {:keys str/lower-case :fields str/upper-case}}))