# Module /sqlite/client

Synchronous embedded SQLite connection pool with parameterized queries, row maps, and closure transactions (begin).

```tangu
(use /sqlite/client)
```

## Functions & symbols

### begin

`(begin conn callback) -> Any`

Runs `callback` inside an ACID database transaction (or nested SAVEPOINT if already in a transaction), automatically committing on success or rolling back on error. Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Connection pool, writer, or parent Transaction (creates savepoint) |
| `callback` | `Func` | no | Transaction body `(Func tx ...)` — commits on normal return, rolls back on error or thrown panic |

```tangu
(db/begin conn (Func tx
    (db/exec tx #sql "UPDATE accounts SET balance = balance - 10 WHERE id = ?" [1])
))
```

### changes

`(changes conn) -> Int`

Returns the number of rows modified by the most recent SQLite statement on the connection.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | SQLite pool or active Transaction |

```tangu
(let affected (sqlite/changes db))
```

### command

`(command conn sql [params]) -> Int`

Executes a SQL command/DDL statement on the primary writer and returns the affected row count. Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using ? placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(db/command conn "CREATE TABLE IF NOT EXISTS items (id INTEGER PRIMARY KEY, name TEXT)")
```

### connect

`(connect uri-or-config) -> Pool`

Opens a SQL connection pool (or primary/reader cluster when `:reader-uri` / `:reader` is provided). Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `config` | `String | Map` | no | Connection URI string or config Map {:uri ... :reader-uri ... :max-connections ...} |

```tangu
(let db (sqlite/connect "./data.sqlite"))
```

### exec

`(exec conn sql [params]) -> Int`

Executes a DML statement (`INSERT`, `UPDATE`, `DELETE`) on the primary writer and returns affected rows. Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using ? placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(db/exec conn #sql "DELETE FROM sessions WHERE id = ?" [sid])
```

### exec-batch

`(exec-batch conn sql) -> None`

Executes a batch of raw semicolon-separated SQL statements on the primary writer (useful for migrations). Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Connection pool or writer |
| `sql` | `String` | no | Multi-statement SQL script (e.g. schema migration) |

```tangu
(db/exec-batch conn migration-sql)
```

### get-reader

`(get-reader pool) -> Pool`

Returns the read-replica pool handle (or writer pool in single-node setups).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `pool` | `Pool` | no | Database cluster or pool |

```tangu
(let reader (db/get-reader pool))
```

### get-writer

`(get-writer pool) -> Pool`

Returns the underlying primary writer pool handle.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `pool` | `Pool` | no | Database cluster or pool |

```tangu
(let writer (db/get-writer pool))
```

### is-cluster?

`(is-cluster? pool) -> Bool`

Returns `true` if `pool` was configured with separate writer and read-replica pools.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `pool` | `Pool` | no | Database connection pool |

```tangu
(db/is-cluster? pool)
```

### pool-stats

`(pool-stats pool) -> Map`

Alias for `pool-status`: returns live connection pool utilization metrics.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `pool` | `Pool` | no | Database connection pool |

```tangu
(db/pool-stats pool)
```

### pool-status

`(pool-status pool) -> Map`

Returns connection pool health metrics (active connections, idle count, capacity).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `pool` | `Pool` | no | Database connection pool |

```tangu
(db/pool-status pool)
```

### query

`(query conn sql [params]) -> (Array Map)`

Executes a `SELECT` query (routed to read replicas when configured, so reads are eventually consistent) and returns an Array of row Maps. Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using ? placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(db/query conn #sql "SELECT id, name FROM users WHERE active = ?" [true])
```

### query-exists-strong?

`(query-exists-strong? conn sql [params]) -> Bool`

Primary-writer variant of `query-exists?` guaranteeing strong consistency. Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using ? placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(db/query-exists-strong? conn #sql "SELECT 1 FROM users WHERE id = ?" [id])
```

### query-exists?

`(query-exists? conn sql [params]) -> Bool`

Returns `true` if the SQL query matches at least one row. Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using ? placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(db/query-exists? conn #sql "SELECT 1 FROM users WHERE email = ?" [email])
```

### query-maps

`(query-maps conn sql [params]) -> (Array Map)`

Executes a `SELECT` query and returns each row as a Map. Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using ? placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(db/query-maps conn #sql "SELECT * FROM items WHERE org_id = ?" [org-id])
```

### query-maps-strong

`(query-maps-strong conn sql [params]) -> (Array Map)`

Primary-writer variant of `query-maps` guaranteeing linearizable read-after-write visibility. Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using ? placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(db/query-maps-strong conn #sql "UPDATE items SET done = true WHERE id = ? RETURNING *" [id])
```

### query-one

`(query-one conn sql [params]) -> (Or Map None)`

Executes a query and returns the first row as a `Map`, or `()` (`None`) if zero rows matched. Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using ? placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(db/query-one conn #sql "SELECT * FROM users WHERE id = ?" [id])
```

### query-one-strong

`(query-one-strong conn sql [params]) -> (Or Map None)`

Executes a single-row query on the primary writer (`INSERT/UPDATE ... RETURNING` or immediate post-write lookup), returning a `Map` or `()`. Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using ? placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(db/query-one-strong conn #sql "INSERT INTO users (name) VALUES (?) RETURNING id, name" ["Ada"])
```

### query-one-tuple

`(query-one-tuple conn sql [params]) -> (Or Array None)`

Alias for `query-tuples-one`: returns the first row as an Array or `()`. Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using ? placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(db/query-one-tuple conn #sql "SELECT id, email FROM users WHERE id = ?" [id])
```

### query-scalar

`(query-scalar conn sql [params]) -> Any`

Executes a query expecting one column from the first row and returns that scalar value (or `()` if empty). Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using ? placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(db/query-scalar conn #sql "SELECT count(*) FROM users")
```

### query-scalar-strong

`(query-scalar-strong conn sql [params]) -> Any`

Primary-writer variant of `query-scalar` for strong read-after-write scalar queries. Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using ? placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(db/query-scalar-strong conn #sql "INSERT INTO jobs (name) VALUES (?) RETURNING id" [name])
```

### query-strong

`(query-strong conn sql [params]) -> (Array Map)`

Executes a query strictly on the primary writer for strong read-after-write consistency (required for `INSERT/UPDATE/DELETE ... RETURNING`). Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using ? placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(db/query-strong conn #sql "INSERT INTO users (email) VALUES (?) RETURNING *" [email])
```

### query-tuples

`(query-tuples conn sql [params]) -> (Array Array)`

Executes a query and returns rows as positional Arrays of column values. Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using ? placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(db/query-tuples conn #sql "SELECT id, title FROM posts")
```

### query-tuples-one

`(query-tuples-one conn sql [params]) -> (Or Array None)`

Executes a query and returns the first row as an Array of positional columns, or `()`. Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using ? placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(db/query-tuples-one conn #sql "SELECT id, email FROM users WHERE id = ?" [id])
```

### query-tuples-one-strong

`(query-tuples-one-strong conn sql [params]) -> (Or Array None)`

Primary-writer variant of `query-tuples-one` guaranteeing strong consistency. Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using ? placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(db/query-tuples-one-strong conn #sql "INSERT INTO tags (slug) VALUES (?) RETURNING id, slug" [slug])
```

### query-tuples-strong

`(query-tuples-strong conn sql [params]) -> (Array Array)`

Primary-writer variant of `query-tuples` for read-after-write consistency. Executes synchronously on the embedded runtime and returns the value directly (do not use `await`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `conn` | `Pool | Transaction` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using ? placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(db/query-tuples-strong conn #sql "SELECT id, title FROM posts")
```
