Common API to communicate with PostgreSQL server.
Common API to communicate with PostgreSQL server.
(->tx-level level)
Turn a keyword or a string into on instance of TxLevel.
Turn a keyword or a string into on instance of TxLevel.
(begin conn)
(begin conn tx-level)
(begin conn tx-level read-only?)
Open a new transaction. Possible arguments:
tx-level
: the custom isolation level, either a :kebab-case
keyword, or a CAPS STRING, for example :read-committed
or 'READ COMMITTED'
;read-only?
: whether to run the transaction in read-only mode
(default is false).Open a new transaction. Possible arguments: - `tx-level`: the custom isolation level, either a `:kebab-case` keyword, or a CAPS STRING, for example `:read-committed` or `'READ COMMITTED'`; - `read-only?`: whether to run the transaction in read-only mode (default is false).
(cancel-request conn)
Send a cancellation request to the server. MUST be called in another thread! The cancellation is meant to interrupt a query that has frozen the server. There is no 100% guarantee it will work.
Not recommended to use directly. See the with-timeout
macro.
Send a cancellation request to the server. MUST be called in another thread! The cancellation is meant to interrupt a query that has frozen the server. There is no 100% guarantee it will work. Not recommended to use directly. See the `with-timeout` macro.
(clone src)
Produce another instance of a source with the same configuration.
Produce another instance of a source with the same configuration.
(close src)
Close a data source. Closing a Connection terminates a session on the server side. Closing a Pool object terminates all open connections.
Close a data source. Closing a Connection terminates a session on the server side. Closing a Pool object terminates all open connections.
(close-cached-statements conn)
Close all the cached prepared statements and clean up the cache.
Close all the cached prepared statements and clean up the cache.
(close-statement conn statement)
Close a prepared statement.
Close a prepared statement.
(closed? src)
Whether the data source was closed. A closed source cannot be reused again.
Whether the data source was closed. A closed source cannot be reused again.
(commit conn)
Commit the current transaction.
Commit the current transaction.
(connect src)
(connect host port user password database)
Connect to the database. Given a Clojure config map or a URI string, establish a TCP connection with the server and run the authentication pipeline. Returns an instance of the Connection class.
Connect to the database. Given a Clojure config map or a URI string, establish a TCP connection with the server and run the authentication pipeline. Returns an instance of the Connection class.
(connection? x)
True of the passed option is a Connection instance.
True of the passed option is a Connection instance.
(copy-in src sql in)
(copy-in src sql in opt)
Transfer the data from the client to the server using COPY protocol. The SQL expression must be something liek this:
COPY ... FROM STDIN ...
The in
parameter is an instance of InputStream
. The function
doesn't close the stream assuming you can reuse it.
The opt
map is used to specify format, CSV delimiters, type hints
and other options (see README).
Return the number of rows processed by the server.
Transfer the data from the client to the server using COPY protocol. The SQL expression must be something liek this: `COPY ... FROM STDIN ...` The `in` parameter is an instance of `InputStream`. The function doesn't close the stream assuming you can reuse it. The `opt` map is used to specify format, CSV delimiters, type hints and other options (see README). Return the number of rows processed by the server.
(copy-in-maps src sql maps keys)
(copy-in-maps src sql maps keys opt)
Like copy-in
but accepts a list of Clojure maps.
The keys
argument is list of keys which is used to convert
each map into a tuple.
The opt
argument is a map of options (COPY format, delimiters, etc).
Return the number of rows processed by the server.
Like `copy-in` but accepts a list of Clojure maps. The `keys` argument is list of keys which is used to convert each map into a tuple. The `opt` argument is a map of options (COPY format, delimiters, etc). Return the number of rows processed by the server.
(copy-in-rows src sql rows)
(copy-in-rows src sql rows opt)
Like copy-in
but accepts not an input stream but a list
of rows. Each row must be a list of values. The list might be
lazy. Return a number of rows processed.
Like `copy-in` but accepts not an input stream but a list of rows. Each row must be a list of values. The list might be lazy. Return a number of rows processed.
(copy-out src sql out)
(copy-out src sql out opt)
Transfer the data from the server to the client using COPY protocol. The SQL expression must be something like this:
COPY ... TO STDOUT ...
The out
parameter must be an instance of OutputStream.
The function doesn't close the stream assuming you can reuse it for multiple COPY OUT sessions.
The opt
map allows to specify the data format, CSV delimiters
other options (see the docs in README).
Return the number of rows read from the server.
Transfer the data from the server to the client using COPY protocol. The SQL expression must be something like this: `COPY ... TO STDOUT ...` The `out` parameter must be an instance of OutputStream. The function doesn't close the stream assuming you can reuse it for multiple COPY OUT sessions. The `opt` map allows to specify the data format, CSV delimiters other options (see the docs in README). Return the number of rows read from the server.
(created-at conn)
Get the connection creation time as a Unix timestamp (ms).
Get the connection creation time as a Unix timestamp (ms).
(decode-bin buf oid)
(decode-bin buf oid opt)
Decode a binary-encoded value from a ByteBuffer.
Decode a binary-encoded value from a ByteBuffer.
(decode-txt obj oid)
(decode-txt obj oid opt)
Decode a text-encoded value from a ByteBuffer.
Decode a text-encoded value from a ByteBuffer.
(encode-bin obj)
(encode-bin obj oid)
(encode-bin obj oid opt)
Binary-encode a value into a ByteBuffer.
Binary-encode a value into a ByteBuffer.
(encode-txt obj)
(encode-txt obj oid)
(encode-txt obj oid opt)
Text-encode a value into a string.
Text-encode a value into a string.
(execute src sql)
(execute src sql opt)
Execute a SQL expression and return a result. Arguments:
src
is a data source (a Connection, a Pool, a map, a URI string);sql
is a SQL string expression;opt
is a map of options.This function is a series of steps:
Execute a SQL expression and return a result. Arguments: - `src` is a data source (a Connection, a Pool, a map, a URI string); - `sql` is a SQL string expression; - `opt` is a map of options. This function is a series of steps: - prepare a statement; - bind parameters and obtain a portal; - describe the portal; - get the data from the portal; - close the portal; - close the statement; - process the result of the fly.
(execute-statement conn statement)
(execute-statement conn statement opt)
Execute a given prepared statement and return a result. The way the result is processed heavily depends on options.
Execute a given prepared statement and return a result. The way the result is processed heavily depends on options.
(get-error-fields e)
Get a map of error fields from an instance of
PGErrorResponse
. Deprecated, use ex-data
instead.
Get a map of error fields from an instance of `PGErrorResponse`. **Deprecated**, use `ex-data` instead.
(get-parameter conn param)
Return a certain connection parameter by its name, e.g. 'server_encoding', 'application_name', etc.
Return a certain connection parameter by its name, e.g. 'server_encoding', 'application_name', etc.
(get-parameters conn)
Return a {String->String} map of all the connection parameters.
Return a {String->String} map of all the connection parameters.
(id src)
Get a unique ID of this source (a Connection or a Pool).
Get a unique ID of this source (a Connection or a Pool).
(idle? conn)
True if the connection is in the IDLE state.
True if the connection is in the IDLE state.
(in-transaction? conn)
True if the connection is in TRANSACTION at the moment.
True if the connection is in TRANSACTION at the moment.
(is-ssl? conn)
True if the Connection is SSL-encrypted.
True if the Connection is SSL-encrypted.
(json-wrap x)
Wrap a value into a JSON.Wrapper class to force JSON encoding.
Wrap a value into a JSON.Wrapper class to force JSON encoding.
(listen conn channel)
Subscribe a connection to a given channel.
Subscribe a connection to a given channel.
(notify src channel message)
Send a text message to a given channel.
Send a text message to a given channel.
(pid conn)
Get PID of the connection on the server.
Get PID of the connection on the server.
(poll-notifications conn)
Perform an empty query so that pending notifications get flushed to the client. Doesn't guarantee though they will be sent for sure due to their async nature. The result is a number of notifications got and processed.
Perform an empty query so that pending notifications get flushed to the client. Doesn't guarantee though they will be sent for sure due to their async nature. The result is a number of notifications got and processed.
(pool src)
(pool host port user password database)
Run a new Pool from a config map or a URI.
Run a new Pool from a config map or a URI.
(pool? x)
True if a value is a Pool instance.
True if a value is a Pool instance.
(prepare conn sql)
(prepare conn sql opt)
Get a new prepared statement from a raw SQL string.
The SQL might have parameters. There is an third
optional parameter oids
to specify the non-default types
of the parameters. Must be a List of OID enum.
The function returns an instance of the PreparedStatement
class bound to the current connection.
Must be called against a Connection object since prepared statements cannot be shared across multiple connections.
Get a new prepared statement from a raw SQL string. The SQL might have parameters. There is an third optional parameter `oids` to specify the non-default types of the parameters. Must be a List of OID enum. The function returns an instance of the `PreparedStatement` class bound to the current connection. Must be called against a Connection object since prepared statements cannot be shared across multiple connections.
(prepared-statement? x)
True if it's an instance of the PreparedStatement class.
True if it's an instance of the PreparedStatement class.
(query src sql)
(query src sql opt)
Run a SQL expression WITH NO parameters. The result is always sent back in text mode (binary mode doesn't work with the QUERY API). Arguments:
src
is a data source (a Connection, a Pool, a map, a URI string);sql
is a SQL string without parameters (e.g. $1, etc);opt
is a map of options.Run a SQL expression WITH NO parameters. The result is always sent back in text mode (binary mode doesn't work with the QUERY API). Arguments: - `src` is a data source (a Connection, a Pool, a map, a URI string); - `sql` is a SQL string without parameters (e.g. $1, etc); - `opt` is a map of options.
(rollback conn)
Rollback the current transaction.
Rollback the current transaction.
(set-read-only conn)
Set the current transaction read-only.
Set the current transaction read-only.
(set-tx-level conn level)
Set transaction isolation level for the current transaction.
Set transaction isolation level for the current transaction.
(status conn)
Return the current transaction status, one of :I, :T, or :E.
Return the current transaction status, one of :I, :T, or :E.
(tx-error? conn)
True if the transaction has failed but hasn't been rolled back yet.
True if the transaction has failed but hasn't been rolled back yet.
(unlisten conn channel)
Unsubscribe a connection from a given channel.
Unsubscribe a connection from a given channel.
(with-conn [bind src] & body)
Just a shorter version of with-connection
.
Just a shorter version of `with-connection`.
(with-connection [bind src] & body)
Perform a block of code while the bind
symbol is bound
to a Connection
object. If the src
is a config map,
the connection gets closed afterwards. When the src
is
a pool, the connection gets borrowed and returned to the
pool without closing. For a connection, nothing happens
when exiting the macro.
Perform a block of code while the `bind` symbol is bound to a `Connection` object. If the `src` is a config map, the connection gets closed afterwards. When the `src` is a pool, the connection gets borrowed and returned to the pool without closing. For a connection, nothing happens when exiting the macro.
(with-lock [conn] & body)
Perform a block of code while the connection is locked (no other threads can interfere).
Perform a block of code while the connection is locked (no other threads can interfere).
(with-pool [bind config] & body)
Execute the body while the bind
symbol is bound
to a new Pool instance. Close the pool afterwards.
Execute the body while the `bind` symbol is bound to a new Pool instance. Close the pool afterwards.
(with-statement [bind conn sql oids] & body)
Execute the body while the bind
symbol is bound to the
new PreparedStatement instance. The statement gets created
from the SQL expression and optional list of OIDs. It gets
closed when exiting the macro.
Execute the body while the `bind` symbol is bound to the new PreparedStatement instance. The statement gets created from the SQL expression and optional list of OIDs. It gets closed when exiting the macro.
(with-timeout [conn ms-timeout] & body)
Set a timeout in which, if a query has not been executed in time,
a cancellation request is sent to the server. Wrap a query with
that macro when there is a chance for a server to freeze completely
due to a non-optimized SQL. The ms-timeout
is amount of milliseconds
in which the cancel request will be sent.
Set a timeout in which, if a query has not been executed in time, a cancellation request is sent to the server. Wrap a query with that macro when there is a chance for a server to freeze completely due to a non-optimized SQL. The `ms-timeout` is amount of milliseconds in which the cancel request will be sent.
(with-transaction [tx src] & body)
(with-transaction [tx src {:keys [isolation-level read-only? rollback?]}]
&
body)
Obtain a connection from a source and perform a block of code wrapping the connection with a transaction, namely:
Arguments:
tx
is a symbol which a transactional connection is bound to;src
is a data source (a map, a Pool, a Connection, a URI string).The third argument is an optional map of parameters:
isolation-level
: a keyword/string to set the isolation level;read-only?
: to set the transaction read only;rollback?
: to ROLLBACK a transaction even if it was successful.Nested transactions are consumed by the most outer transaction.
For example, you have two nested with-transaction
blocks:
(with-transaction [tx1 {...}] ;; 1 (do-this ...) (with-transaction [tx2 tx1] ;; 2 (do-that ...)))
In this case, only the first block will produce BEGIN
and COMMIT
commands. The second block will expand into the body only:
(pg/begin ...) (do-this ...) (do-that ...) (pg/commit ...)
Obtain a connection from a source and perform a block of code wrapping the connection with a transaction, namely: - run BEGIN before the code block; - capture all possible exceptions; - should an exception was caught, ROLLBACK... - and re-throw it; - if no exception was caught, COMMIT. Arguments: - `tx` is a symbol which a transactional connection is bound to; - `src` is a data source (a map, a Pool, a Connection, a URI string). The third argument is an optional map of parameters: - `isolation-level`: a keyword/string to set the isolation level; - `read-only?`: to set the transaction read only; - `rollback?`: to ROLLBACK a transaction even if it was successful. Nested transactions are consumed by the most outer transaction. For example, you have two nested `with-transaction` blocks: (with-transaction [tx1 {...}] ;; 1 (do-this ...) (with-transaction [tx2 tx1] ;; 2 (do-that ...))) In this case, only the first block will produce `BEGIN` and `COMMIT` commands. The second block will expand into the body only: (pg/begin ...) (do-this ...) (do-that ...) (pg/commit ...)
(with-tx [conn opts] & body)
with-transaction
above.Acts like with-transaction
but accepts a connection,
not a data source. Thus, no a binding symbol required.
**DEPRECATED**: use `with-transaction` above. --------------------------------------------- Acts like `with-transaction` but accepts a connection, not a data source. Thus, no a binding symbol required.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close