Back

get-datasource (clj)

(source)

function

(get-datasource spec)
Given some sort of specification of a database, return a `DataSource`. A specification can be a JDBC URL string (which is passed to the JDBC driver as-is), or a hash map. For the hash map, there are two formats accepted: In the first format, these keys are required: * `:dbtype` -- a string indicating the type of the database * `:dbname` -- a string indicating the name of the database to be used The following optional keys are commonly used: * `:user` -- the username to authenticate with * `:password` -- the password to authenticate with * `:host` -- the hostname or IP address of the database (default: `127.0.0.1`); can be `:none` which means the host/port segment of the JDBC URL should be omitted entirely (for 'local' databases) * `:port` -- the port for the database connection (the default is database- specific -- see below); can be `:none` which means the port segment of the JDBC URL should be omitted entirely * `:classname` -- if you need to override the default for the `:dbtype` (or you want to use a database that next.jdbc does not know about!) The following optional keys can be used to control how JDBC URLs are assembled. This may be needed for `:dbtype` values that `next.jdbc` does not recognize: * `:dbname-separator` -- override the `/` or `:` that normally precedes the database name in the JDBC URL * `:host-prefix` -- override the `//` that normally precedes the IP address or hostname in the JDBC URL * `:property-separator` -- an optional string that can be used to override the separators used in `jdbc-url` for the properties (after the initial JDBC URL portion); by default `?` and `&` are used to build JDBC URLs with properties; for SQL Server drivers (both MS and jTDS) `:property-separator ";"` is used In the second format, this key is required: * `:jdbcUrl` -- a JDBC URL string Any additional options provided will be passed to the JDBC driver's `.getConnection` call as a `java.util.Properties` structure. Database types supported (for `:dbtype`), and their defaults: * `derby` -- `org.apache.derby.jdbc.EmbeddedDriver` -- also pass `:create true` if you want the database to be automatically created * `duckdb` -- `org.duckdb.DuckDBDriver` -- embedded database * `h2` -- `org.h2.Driver` -- for an on-disk database * `h2:mem` -- `org.h2.Driver` -- for an in-memory database * `hsqldb`, `hsql` -- `org.hsqldb.jdbcDriver` * `jtds:sqlserver`, `jtds` -- `net.sourceforge.jtds.jdbc.Driver` -- `1433` * `mariadb` -- `org.mariadb.jdbc.Driver` -- `3306` * `mysql` -- `com.mysql.cj.jdbc.Driver`, `com.mysql.jdbc.Driver` -- `3306` * `oracle:oci` -- `oracle.jdbc.OracleDriver` -- `1521` * `oracle:thin`, `oracle` -- `oracle.jdbc.OracleDriver` -- `1521` * `oracle:sid` -- `oracle.jdbc.OracleDriver` -- `1521` -- uses the legacy `:` separator for the database name but otherwise behaves like `oracle:thin` * `postgresql`, `postgres` -- `org.postgresql.Driver` -- `5432` * `pgsql` -- `com.impossibl.postgres.jdbc.PGDriver` -- no default port * `redshift` -- `com.amazon.redshift.jdbc.Driver` -- no default port * `sqlite` -- `org.sqlite.JDBC` * `sqlserver`, `mssql` -- `com.microsoft.sqlserver.jdbc.SQLServerDriver` -- `1433` * `timesten:client` -- `com.timesten.jdbc.TimesTenClientDriver` * `timesten:direct` -- `com.timesten.jdbc.TimesTenDriver` * `xtdb` -- `xtdb.jdbc.Driver` -- an XTDB wrapper around `postgresql` For more details about `:dbtype` and `:classname` values, see: https://cljdoc.org/d/com.github.seancorfield/next.jdbc/CURRENT/api/next.jdbc.connection#dbtypes

Examples