Home

Awesome

clj-jdbcutil

Clojure JDBC utility functions meant for other higher-level libraries.

Usage

On Clojars: https://clojars.org/clj-jdbcutil

Leiningen dependency: [clj-jdbcutil "0.1.0"]

Everything exists in the clj-jdbcutil.core namespace.

(ns example.app
  (:require [clj-jdbcutil.core :as ju]))

Dynamic var for database configuration keys

The dynamic var *dbspec* may be bound to a map containing well known keys and corresponding values (that users should honor):

keytypedefaultdescription
:datasourcejavax.sql.DataSourcenil
:connectionjava.sql.Connectionnil
:dbmetadatamap{}use dbmeta to retrieve this value
:catalogString, Keyword etc.nilSHOULD be converted using db-iden
:schemaString, Keyword etc.nilSHOULD be converted using db-iden
:read-only?booleanfalsetrue SHOULD disallow write operations
:show-sql?booleantruetrue SHOULD print SQL statements
:show-sql-fnfunction (w/ 1 arg)prints SQL using printlnyou may rebind this to fn that sends to logger
:clj-to-dbfunction (w/ 1 arg, returns string)converts hyphen to underscoredictates how should identifiers be converted from Clojure to the database
:db-to-cljfunction (w/ 1 arg, returns clj form)converts to lower-case keyworddictates how should identifiers be converted from the database to Clojure
:fetch-sizeInteger1000number of rows to fetch per DB roundtrip; helps throttle/optimize large DB reads; 0 means unlimited
:query-timeoutInteger0number of seconds to wait for query to execute, after which timeout occurs raising SqlException (not all JDBC drivers support this so check driver manual before use)

Notes:

Creating datasources

You must already have the JDBC driver in the classpath. Then you can follow either of the following to create a datasource.

(make-datasource driver-classname jdbc-url)
(make-datasource driver-classname jdbc-url username password)

Note that the API above will not create a pooling datasource, and hence may not be suitable for production use. For information on how to create pooling JDBC datasources, check out clj-dbcp

Connecting

The macro with-connection can execute a body of code in the context of JDBC connection as :connection (unless already populated). This is like clojure.java.jdbc/with-connection with the exception that you must provide :datasource in the map.

(with-connection {key1 val1 key2 val2 ...}
  ...)

License

Copyright © 2012 Shantanu Kumar

Distributed under the Eclipse Public License, the same as Clojure.