• (get-connection {:keys [factory classname subprotocol subname datasource username password name environment], :as db-spec})
Creates a connection to a database. db-spec is a map containing values
for one of the following parameter sets:

Factory:
:factory (required) a function of one argument, a map of params
(others) (optional) passed to the factory function in a map

DriverManager:
:classname (required) a String, the jdbc driver class name
:subprotocol (required) a String, the jdbc subprotocol
:subname (required) a String, the jdbc subname
(others) (optional) passed to the driver as properties.

DataSource:
:datasource (required) a javax.sql.DataSource
:username (optional) a String
:password (optional) a String, required if :username is supplied

JNDI:
:name (required) a String or javax.naming.Name
:environment (optional) a java.util.Map

0 Examples top

Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/contrib/sql/internal.clj:51 top

(defn get-connection
  "Creates a connection to a database. db-spec is a map containing values
  for one of the following parameter sets:

  Factory:
    :factory     (required) a function of one argument, a map of params
    (others)     (optional) passed to the factory function in a map

  DriverManager:
    :classname   (required) a String, the jdbc driver class name
    :subprotocol (required) a String, the jdbc subprotocol
    :subname     (required) a String, the jdbc subname
    (others)     (optional) passed to the driver as properties.

  DataSource:
    :datasource  (required) a javax.sql.DataSource
    :username    (optional) a String
    :password    (optional) a String, required if :username is supplied

  JNDI:
    :name        (required) a String or javax.naming.Name
    :environment (optional) a java.util.Map"
  [{:keys [factory
           classname subprotocol subname
           datasource username password
           name environment]
    :as db-spec}]
  (cond
   factory
   (factory (dissoc db-spec :factory))
   (and classname subprotocol subname)
   (let [url (format "jdbc:%s:%s" subprotocol subname)
         etc (dissoc db-spec :classname :subprotocol :subname)]
     (RT/loadClassForName classname)
     (DriverManager/getConnection url (as-properties etc)))
   (and datasource username password)
   (.getConnection datasource username password)
   datasource
   (.getConnection datasource)
   name
   (let [env (and environment (Hashtable. environment))
         context (InitialContext. env)
         datasource (.lookup context name)]
     (.getConnection datasource))
   :else
   (throw-arg "db-spec %s is missing a required parameter" db-spec)))
Vars in clojure.contrib.sql.internal/get-connection: and cond defn dissoc let name
Used in 0 other vars

Comments top

No comments for get-connection. Log in to add a comment.