jdbc.core
Alternative implementation of jdbc wrapper for clojure.
atomic
macro
(atomic conn & body)
Creates a context that evaluates in transaction (or nested transaction). This is a more idiomatic way to execute some database operations in atomic way.
(jdbc/atomic conn
(jdbc/execute conn "DROP TABLE foo;")
(jdbc/execute conn "DROP TABLE bar;"))
Also, you can pass additional options to transaction:
(jdbc/atomic conn {:read-only true}
(jdbc/execute conn "DROP TABLE foo;")
(jdbc/execute conn "DROP TABLE bar;"))
atomic-apply
(atomic-apply conn func & [{:keys [savepoints strategy], :or {savepoints true}, :as opts}])
Wrap function in one transaction. This function accepts as a parameter a transaction strategy. If no one is specified, DefaultTransactionStrategy
is used.
With DefaultTransactionStrategy
, if current connection is already in transaction, it uses truly nested transactions for properly handle it. The availability of this feature depends on database support for it.
(with-open [conn (jdbc/connection)]
(atomic-apply conn (fn [conn] (execute! conn 'DROP TABLE foo;'))))
For more idiomatic code, you should use atomic
macro.
Depending on transaction strategy you are using, this function can accept additional parameters. The default transaction strategy exposes two additional parameters:
:isolation-level
- set isolation level for this transaction:read-only
- set current transaction to read only
connection
(connection dbspec)
(connection dbspec options)
Creates a connection to a database. As parameter accepts:
- dbspec map containing connection parameters
- dbspec map containing a datasource (deprecated)
- URI or string (interpreted as uri)
- DataSource instance
The dbspec map has this possible variants:
Classic approach:
:subprotocol
-> (required) string that represents a vendor name (ex: postgresql):subname
-> (required) string that represents a database name (ex: test) (many others options that are pased directly as driver parameters)
Pretty format:
:vendor
-> (required) string that represents a vendor name (ex: postgresql):name
-> (required) string that represents a database name (ex: test):host
-> (optional) string that represents a database hostname (default: 127.0.0.1):port
-> (optional) long number that represents a database port (default: driver default) (many others options that are pased directly as driver parameters)
URI or String format: vendor://user:password@host:post/dbname?param1=value
Additional options:
:schema
-> string that represents a schema name (default: nil):read-only
-> boolean for mark entire connection read only.:isolation-level
-> keyword that represents a isolation level (:none
,:read-committed
,:read-uncommitted
,:repeatable-read
,:serializable
)
Opions can be passed as part of dbspec map, or as optional second argument. For more details, see documentation.
cursor->lazyseq
(cursor->lazyseq cursor)
(cursor->lazyseq cursor opts)
Transform a cursor in a lazyseq.
The returned lazyseq will return values until a cursor is closed or all values are fetched.
execute
(execute conn q)
(execute conn q opts)
Execute a query and return a number of rows affected.
(with-open [conn (jdbc/connection dbspec)]
(jdbc/execute conn "create table foo (id integer);"))
This function also accepts sqlvec format.
fetch
(fetch conn q)
(fetch conn q opts)
Fetch eagerly results executing a query.
This function returns a vector of records (default) or rows (depending on specified opts). Resources are relased inmediatelly without specific explicit action for it.
It accepts a sqlvec, plain sql or prepared statement as query parameter.
fetch-lazy
(fetch-lazy conn q)
(fetch-lazy conn q opts)
Fetch lazily results executing a query.
(with-open [cursor (jdbc/fetch-lazy conn sql)]
(doseq [item (jdbc/cursor->lazyseq cursor)]
(do-something-with item)))
This function returns a cursor instead of result. You should explicitly close the cursor at the end of iteration for release resources.
prepared-statement
(prepared-statement conn sqlvec)
(prepared-statement conn sqlvec options)
Given a string or parametrized sql in sqlvec format return an instance of prepared statement.
set-rollback!
(set-rollback! conn)
Mark a current connection for rollback.
It ensures that on the end of the current transaction instead of commit changes, rollback them.
This function should be used inside of a transaction block, otherwise this function does nothing.
(jdbc/atomic conn
(make-some-queries-without-changes conn)
(jdbc/set-rollback! conn))