Back

jdbc-url (clj)

(source)

function

(jdbc-url db-spec)
Given a database spec (as a hash map), return a JDBC URL with all the attributes added to the query string. The result is suitable for use in calls to `->pool` and `component` as the `:jdbcUrl` key in the parameter map for the connection pooling library. This allows you to build a connection-pooled datasource that needs additional settings that the pooling library does not support, such as `:serverTimezone`: ```clojure (def db-spec {:dbtype .. :dbname .. :user .. :password .. :serverTimezone "UTC"}) (def ds (next.jdbc.connection/->pool HikariCP {:jdbcUrl (next.jdbc.connection/jdbc-url db-spec) :maximumPoolSize 15})) ``` This also clearly separates the attributes that should be part of the JDBC URL from the attributes that should be configured on the pool. Since JDBC drivers can handle URL encoding differently, if you are trying to pass attributes that might need encoding, you should make sure they are properly URL-encoded as values in the database spec hash map. This function does **not** attempt to URL-encode values for you!

Examples

next-jdbc
  At some point, the datasource/connection tests should probably be extended
  to accept EDN specs from an external source (environment variables?)."
  (:require [clojure.string :as str]
            [clojure.test :refer [deftest is testing]]
            [next.jdbc.connection :as c]
            [next.jdbc.protocols :as p])
  (:import (com.zaxxer.hikari HikariDataSource)
           (com.mchange.v2.c3p0 ComboPooledDataSource PooledDataSource)))

(deftest jdbc-url-tests
  (testing "basic URLs work"
    (is (= "jdbc:acme:my-db"
           (c/jdbc-url {:dbtype "acme" :classname "java.lang.String"
                        :dbname "my-db" :host :none})))
    (is (= "jdbc:acme://127.0.0.1/my-db"
           (c/jdbc-url {:dbtype "acme" :classname "java.lang.String"
                        :dbname "my-db"})))
    (is (= "jdbc:acme://12.34.56.70:1234/my-db"
           (c/jdbc-url {:dbtype "acme" :classname "java.lang.String"
                        :dbname "my-db" :host "12.34.56.70" :port 1234})))
    (is (= "jdbc:acme:dsn=my-db"
           (c/jdbc-url {:dbtype "acme" :classname "java.lang.String"
                        :dbname "my-db" :host :none
                        :dbname-separator ":dsn="})))
    (is (= "jdbc:acme:(*)127.0.0.1/my-db"
           (c/jdbc-url {:dbtype "acme" :classname "java.lang.String"
                        :dbname "my-db"
                        :host-prefix "(*)"})))
    (is (= "jdbc:acme:(*)12.34.56.70:1234/my-db"
           (c/jdbc-url {:dbtype "acme" :classname "java.lang.String"
                        :dbname "my-db" :host "12.34.56.70" :port 1234
                        :host-prefix "(*)"}))))
  (testing "URLs with properties work"
    (is (= "jdbc:acme:my-db?useSSL=true"
           (c/jdbc-url {:dbtype "acme" :classname "java.lang.String"
                        :dbname "my-db" :host :none
                        :useSSL true})))
    (is (boolean (#{"jdbc:acme:my-db?useSSL=true&user=dba"
                    "jdbc:acme:my-db?user=dba&useSSL=true"}
                  (c/jdbc-url {:dbtype "acme" :classname "java.lang.String"
                               :dbname "my-db" :host :none
                               :useSSL true :user "dba"}))))

    (is (= "jdbc:jtds:sqlserver:my-db;useSSL=true"
           (c/jdbc-url {:dbtype "jtds"
                        :dbname "my-db" :host :none
                        :useSSL true})))
    (is (boolean (#{"jdbc:jtds:sqlserver:my-db;useSSL=true;user=dba"
                    "jdbc:jtds:sqlserver:my-db;user=dba;useSSL=true"}
                  (c/jdbc-url {:dbtype "jtds"
                               :dbname "my-db" :host :none
                               :useSSL true :user "dba"}))))))
seancorfield/next-jdbc
  At some point, the datasource/connection tests should probably be extended
  to accept EDN specs from an external source (environment variables?)."
  (:require [clojure.string :as str]
            [clojure.test :refer [deftest is testing]]
            [next.jdbc.connection :as c]
            [next.jdbc.protocols :as p])
  (:import (com.zaxxer.hikari HikariDataSource)
           (com.mchange.v2.c3p0 ComboPooledDataSource PooledDataSource)))

(deftest jdbc-url-tests
  (testing "basic URLs work"
    (is (= "jdbc:acme:my-db"
           (c/jdbc-url {:dbtype "acme" :classname "java.lang.String"
                        :dbname "my-db" :host :none})))
    (is (= "jdbc:acme://127.0.0.1/my-db"
           (c/jdbc-url {:dbtype "acme" :classname "java.lang.String"
                        :dbname "my-db"})))
    (is (= "jdbc:acme://12.34.56.70:1234/my-db"
           (c/jdbc-url {:dbtype "acme" :classname "java.lang.String"
                        :dbname "my-db" :host "12.34.56.70" :port 1234})))
    (is (= "jdbc:acme:dsn=my-db"
           (c/jdbc-url {:dbtype "acme" :classname "java.lang.String"
                        :dbname "my-db" :host :none
                        :dbname-separator ":dsn="})))
    (is (= "jdbc:acme:(*)127.0.0.1/my-db"
           (c/jdbc-url {:dbtype "acme" :classname "java.lang.String"
                        :dbname "my-db"
                        :host-prefix "(*)"})))
    (is (= "jdbc:acme:(*)12.34.56.70:1234/my-db"
           (c/jdbc-url {:dbtype "acme" :classname "java.lang.String"
                        :dbname "my-db" :host "12.34.56.70" :port 1234
                        :host-prefix "(*)"}))))
  (testing "URLs with properties work"
    (is (= "jdbc:acme:my-db?useSSL=true"
           (c/jdbc-url {:dbtype "acme" :classname "java.lang.String"
                        :dbname "my-db" :host :none
                        :useSSL true})))
    (is (boolean (#{"jdbc:acme:my-db?useSSL=true&user=dba"
                    "jdbc:acme:my-db?user=dba&useSSL=true"}
                  (c/jdbc-url {:dbtype "acme" :classname "java.lang.String"
                               :dbname "my-db" :host :none
                               :useSSL true :user "dba"}))))

    (is (= "jdbc:jtds:sqlserver:my-db;useSSL=true"
           (c/jdbc-url {:dbtype "jtds"
                        :dbname "my-db" :host :none
                        :useSSL true})))
    (is (boolean (#{"jdbc:jtds:sqlserver:my-db;useSSL=true;user=dba"
                    "jdbc:jtds:sqlserver:my-db;user=dba;useSSL=true"}
                  (c/jdbc-url {:dbtype "jtds"
                               :dbname "my-db" :host :none
                               :useSSL true :user "dba"}))))))