next-jdbc/src/next/jdbc/protocols.clj

62 lines
2.9 KiB
Clojure
Raw Normal View History

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-21 23:13:52 +00:00
`Sourceable` protocol:
`get-datasource` -- turn something into a `javax.sql.DataSource`; implementations
are provided for strings, hash maps (`db-spec` structures), and also a
`DataSource` (which just returns itself).
`Connectable` protocol:
`get-connection` -- create 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 possibly be turned into a `DataSource`.
`Executable` protocol:
`-execute` -- given SQL and parameters, produce a 'reducible' that, when
reduced, executes the SQL and produces a `ResultSet` that can be processed;
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-one` -- given SQL and parameters, executes the SQL and produces
the first row of the `ResultSet` as a datafiable hash map (by default);
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-all` -- given SQL and parameters, executes the SQL and produces
either a vector of datafiable hash maps from the `ResultSet` (default)
or a vector of column names followed by vectors of row values;
2019-04-21 23:13:52 +00:00
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`).
2019-04-21 23:13:52 +00:00
`Preparable` protocol:
`prepare` -- given SQL and parameters, produce a `PreparedStatement` that can
2019-04-01 06:17:12 +00:00
be executed (by -execute above); implementation is provided for
2019-04-21 23:13:52 +00:00
`Connection` only.
2019-04-01 06:17:12 +00:00
2019-04-21 23:13:52 +00:00
`Transactable` protocol:
`-transact` -- given a function (presumably containing SQL operations),
2019-04-01 06:17:12 +00:00
run the function inside a SQL transaction; implementations are provided
2019-04-21 23:13:52 +00:00
for `Connection`, `DataSource`, and `Object` (on the assumption that an
`Object` can be turned into a `DataSource`).")
2019-03-31 23:54:34 +00:00
(set! *warn-on-reflection* true)
(defprotocol Sourceable :extend-via-metadata true
2019-04-21 23:13:52 +00:00
(get-datasource ^javax.sql.DataSource [this]))
2019-03-31 23:54:34 +00:00
(defprotocol Connectable
(get-connection ^java.lang.AutoCloseable [this opts]))
(defprotocol Executable
(-execute ^clojure.lang.IReduceInit [this sql-params opts])
(-execute-one [this sql-params opts])
(-execute-all [this sql-params opts]))
2019-03-31 23:54:34 +00:00
(defprotocol Preparable
(prepare ^java.sql.PreparedStatement [this sql-params opts]))
(defprotocol Transactable
(-transact [this body-fn opts]))