2019-03-31 23:54:34 +00:00
|
|
|
;; copyright (c) 2018-2019 Sean Corfield, all rights reserved
|
|
|
|
|
|
|
|
|
|
(ns next.jdbc.protocols
|
2019-04-01 06:17:12 +00:00
|
|
|
"This is the extensible core of the next generation java.jdbc library.
|
|
|
|
|
|
2019-04-27 05:42:27 +00:00
|
|
|
* `Sourceable` -- for producing `javax.sql.DataSource` objects,
|
|
|
|
|
* `Connectable` -- for producing new `java.sql.Connection` objects,
|
|
|
|
|
* `Executable` -- for executing SQL operations,
|
|
|
|
|
* `Preparable` -- for producing new `java.sql.PreparedStatement` objects,
|
|
|
|
|
* `Transactable` -- for executing SQL operations transactionally.")
|
2019-03-31 23:54:34 +00:00
|
|
|
|
|
|
|
|
(set! *warn-on-reflection* true)
|
|
|
|
|
|
2019-04-02 06:22:59 +00:00
|
|
|
(defprotocol Sourceable :extend-via-metadata true
|
2019-04-23 00:41:31 +00:00
|
|
|
"Protocol for producing a `javax.sql.DataSource`.
|
|
|
|
|
|
|
|
|
|
Implementations are provided for strings, hash maps (`db-spec` structures),
|
|
|
|
|
and also a `DataSource` (which just returns itself).
|
|
|
|
|
|
|
|
|
|
Extension via metadata is supported."
|
|
|
|
|
(get-datasource ^javax.sql.DataSource [this]
|
|
|
|
|
"Produce a `javax.sql.DataSource`."))
|
|
|
|
|
|
2019-03-31 23:54:34 +00:00
|
|
|
(defprotocol Connectable
|
2019-04-23 00:41:31 +00:00
|
|
|
"Protocol for producing a new JDBC connection that should be closed when you
|
|
|
|
|
are finished with it.
|
|
|
|
|
|
|
|
|
|
Implementations are provided for `DataSource`, `PreparedStatement`, and
|
|
|
|
|
`Object`, on the assumption that an `Object` can be turned into a `DataSource`."
|
|
|
|
|
(get-connection ^java.lang.AutoCloseable [this opts]
|
|
|
|
|
"Produce a new `java.sql.Connection` for use with `with-open`."))
|
|
|
|
|
|
2019-03-31 23:54:34 +00:00
|
|
|
(defprotocol Executable
|
2019-04-23 00:41:31 +00:00
|
|
|
"Protocol for executing SQL operations.
|
|
|
|
|
|
|
|
|
|
Implementations are provided for `Connection`, `DataSource`,
|
|
|
|
|
`PreparedStatement`, and `Object`, on the assumption that an `Object` can be
|
|
|
|
|
turned into a `DataSource` and therefore used to get a `Connection`."
|
|
|
|
|
(-execute ^clojure.lang.IReduceInit [this sql-params opts]
|
|
|
|
|
"Produce a 'reducible' that, when reduced, executes the SQL and
|
|
|
|
|
processes the rows of the `ResultSet` directly.")
|
|
|
|
|
(-execute-one [this sql-params opts]
|
|
|
|
|
"Executes the SQL and produces the first row of the `ResultSet`
|
|
|
|
|
as a fully-realized, datafiable hash map (by default).")
|
|
|
|
|
(-execute-all [this sql-params opts]
|
|
|
|
|
"Executes the SQL and produces (by default) a vector of
|
|
|
|
|
fully-realized, datafiable hash maps from the `ResultSet`."))
|
|
|
|
|
|
2019-03-31 23:54:34 +00:00
|
|
|
(defprotocol Preparable
|
2019-04-23 00:41:31 +00:00
|
|
|
"Protocol for producing a new `java.sql.PreparedStatement` that should
|
|
|
|
|
be closed after use. Can be used by `Executable` functions.
|
|
|
|
|
|
|
|
|
|
Implementation is provided for `Connection` only."
|
|
|
|
|
(prepare ^java.sql.PreparedStatement [this sql-params opts]
|
|
|
|
|
"Produce a new `java.sql.PreparedStatement` for use with `with-open`."))
|
|
|
|
|
|
2019-03-31 23:54:34 +00:00
|
|
|
(defprotocol Transactable
|
2019-04-23 00:41:31 +00:00
|
|
|
"Protocol for running SQL operations in a transaction.
|
|
|
|
|
|
|
|
|
|
Implementations are provided for `Connection`, `DataSource`, and `Object`
|
|
|
|
|
(on the assumption that an `Object` can be turned into a `DataSource`)."
|
|
|
|
|
(-transact [this body-fn opts]
|
|
|
|
|
"Run the `body-fn` inside a transaction."))
|