db.core

Core functionality for db

*current-connection*

dynamic

Dynamic var holding the current active JDBC connection, if any. Used by with-connection and query functions to manage transactions or reuse a connection.

*datasource*

dynamic

Dynamic var holding the application’s datasource (connection pool). Must be bound before calling query functions without an explicit connectable.

*debug*

dynamic

Dynamic var for emitting debug information for database queries.

Connectable

protocol

members

connectable?

(connectable? this)

do-with-connection

(do-with-connection f)

Internal helper that executes a function within a connection context.

When *current-connection* is already bound and non-nil, executes the function directly using that connection. Otherwise, if *datasource* is bound and non-nil, obtains a new connection from the datasource, binds it to *current-connection*, executes the function, and closes the connection afterwards.

The function f receives one argument (the connection) and its return value is returned from this function. Throws IllegalStateException when neither a current connection nor a datasource is available.

do-with-transaction

(do-with-transaction connectable-or-nil thunk)

Implementation for the with-transaction macro.

Takes an optional connectable and a thunk (function of one argument). When a connectable is provided, opens a transaction on it. When nil, uses the current connection or obtains one from the datasource. The thunk receives the transaction connection as its argument and its return value is returned from this function.

execute!

(execute! sql)(execute! connectable-or-sql sql-or-opts)(execute! connectable-or-sql sql-or-opts opts)

Executes SQL commands like DDL, INSERT, UPDATE, or DELETE.

Can be called with just SQL (using *datasource* for the connection), with an explicit connectable and SQL, or with a connectable, SQL, and options map. The SQL can be either a HoneySQL map or a standard sql params… vector.

Returns a vector of update count integers indicating how many rows were affected by each statement.

execute-one!

(execute-one! sql)(execute-one! connectable-or-sql sql-or-opts)(execute-one! connectable-or-sql sql-or-opts opts)

Executes SQL (typically SELECT) that returns a single row.

Can be called with just SQL (using *datasource* for the connection), with an explicit connectable and SQL, or with a connectable, SQL, and options map. The SQL can be either a HoneySQL map or a standard sql params… vector.

Returns a single row map, or nil if the query produces no results.

plan

(plan sql)(plan connectable-or-sql sql-or-opts)(plan connectable-or-sql sql-or-opts opts)

Executes a query that returns a reducible collection of rows.

Can be called with just SQL (using *datasource* for the connection), with an explicit connectable and SQL, or with a connectable, SQL, and options map. The SQL can be either a HoneySQL map or a standard sql params… vector.

The reducible result allows efficient processing of large result sets without loading all rows into memory at once. Returns a reducible collection of row maps.

with-connection

macro

(with-connection [conn] & body)

Ensures database operations within the body run within a connection context. If current-connection is already bound, uses it. Otherwise, obtains a new connection from datasource, binds it to current-connection, and executes the body. The new connection is closed afterwards.

with-debug

macro

(with-debug & body)

Set the debug dynamic var for any queries executed within the macro body

with-transaction

macro

(with-transaction [bind & [maybe-connectable]] & body)

Open a transaction from the supplied connection, current-connection, or a new connection from datasource.