Back

dbtypes (clj)

(source)

variable

A map of all known database types (including aliases) to the class name(s) and port that `next.jdbc` supports out of the box. For databases that have non-standard prefixes for the `:dbname` and/or `:host` values in the JDBC string, this table includes `:dbname-separator` and/or `:host-prefix`. The default prefix for `:dbname` is either `/` or `:` and for `:host` it is `//`. For local databases, with no `:host`/`:port` segment in their JDBC URL, a value of `:none` is provided for `:host` in this table. In addition, `:property-separator` can specify how you build the JDBC URL. For known database types, you can use `:dbtype` (and omit `:classname`). If you want to use a database that is not in this list, you can specify a new `:dbtype` along with the class name of the JDBC driver in `:classname`. You will also need to specify `:port`. For example: `{:dbtype "acme" :classname "com.acme.JdbcDriver" ...}` The value of `:dbtype` should be the string that the driver is associated with in the JDBC URL, i.e., the value that comes between the `jdbc:` prefix and the `://<host>...` part. In the above example, the JDBC URL that would be generated would be `jdbc:acme://<host>:<port>/<dbname>`. If you want `next.jdbc` to omit the host/port part of the URL, specify `:host :none`, which would produce a URL like: `jdbc:acme:<dbname>`, which allows you to work with local databases (or drivers that do not need host/port information). The default prefix for the host name (or IP address) is `//`. You can override this via the `:host-prefix` option. The default separator between the host/port and the database name is `/`. The default separator between the subprotocol and the database name, for local databases with no host/port, is `:`. You can override this via the `:dbname-separator` option. JDBC drivers are not provided by `next.jdbc` -- you need to specify the driver(s) you need as additional dependencies in your project. For example: `[com.acme/jdbc "1.2.3"] ; lein/boot` or: `com.acme/jdbc {:mvn/version "1.2.3"} ; CLI/deps.edn` Note: the `:classname` value can be a string or a vector of strings. If a vector of strings is provided, an attempt will be made to load each named class in order, until one succeeds. This allows for a given `:dbtype` to be used with different versions of a JDBC driver, if the class name has changed over time (such as with MySQL).

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 custom-dbtypes
  (is (= ["jdbc:acme:my-db" {} nil]
         (#'c/spec->url+etc {:dbtype "acme" :classname "java.lang.String"
                             :dbname "my-db" :host :none})))
  (is (= ["jdbc:acme://127.0.0.1/my-db" {} nil]
         (#'c/spec->url+etc {:dbtype "acme" :classname "java.lang.String"
                             :dbname "my-db"})))
  (is (= ["jdbc:acme://12.34.56.70:1234/my-db" {} nil]
         (#'c/spec->url+etc {:dbtype "acme" :classname "java.lang.String"
                             :dbname "my-db" :host "12.34.56.70" :port 1234})))
  (is (= ["jdbc:acme:dsn=my-db" {} nil]
         (#'c/spec->url+etc {:dbtype "acme" :classname "java.lang.String"
                             :dbname "my-db" :host :none
                             :dbname-separator ":dsn="})))
  (is (= ["jdbc:acme:(*)127.0.0.1/my-db" {} nil]
         (#'c/spec->url+etc {:dbtype "acme" :classname "java.lang.String"
                             :dbname "my-db"
                             :host-prefix "(*)"})))
  (is (= ["jdbc:acme:(*)12.34.56.70:1234/my-db" {} nil]
         (#'c/spec->url+etc {:dbtype "acme" :classname "java.lang.String"
                             :dbname "my-db" :host "12.34.56.70" :port 1234
                             :host-prefix "(*)"})))
  (is (= ["jdbc:acme:(*)12.34.56.70/my-db" {} nil]
         (#'c/spec->url+etc {:dbtype "acme" :classname "java.lang.String"
                             :dbname "my-db" :host "12.34.56.70" :port :none
                             :host-prefix "(*)"}))))
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 custom-dbtypes
  (is (= ["jdbc:acme:my-db" {} nil]
         (#'c/spec->url+etc {:dbtype "acme" :classname "java.lang.String"
                             :dbname "my-db" :host :none})))
  (is (= ["jdbc:acme://127.0.0.1/my-db" {} nil]
         (#'c/spec->url+etc {:dbtype "acme" :classname "java.lang.String"
                             :dbname "my-db"})))
  (is (= ["jdbc:acme://12.34.56.70:1234/my-db" {} nil]
         (#'c/spec->url+etc {:dbtype "acme" :classname "java.lang.String"
                             :dbname "my-db" :host "12.34.56.70" :port 1234})))
  (is (= ["jdbc:acme:dsn=my-db" {} nil]
         (#'c/spec->url+etc {:dbtype "acme" :classname "java.lang.String"
                             :dbname "my-db" :host :none
                             :dbname-separator ":dsn="})))
  (is (= ["jdbc:acme:(*)127.0.0.1/my-db" {} nil]
         (#'c/spec->url+etc {:dbtype "acme" :classname "java.lang.String"
                             :dbname "my-db"
                             :host-prefix "(*)"})))
  (is (= ["jdbc:acme:(*)12.34.56.70:1234/my-db" {} nil]
         (#'c/spec->url+etc {:dbtype "acme" :classname "java.lang.String"
                             :dbname "my-db" :host "12.34.56.70" :port 1234
                             :host-prefix "(*)"})))
  (is (= ["jdbc:acme:(*)12.34.56.70/my-db" {} nil]
         (#'c/spec->url+etc {:dbtype "acme" :classname "java.lang.String"
                             :dbname "my-db" :host "12.34.56.70" :port :none
                             :host-prefix "(*)"}))))