# `db` API reference

This page lists the current public source surface. All fallible operations use
`Result`; nullable columns use `Option` inside that result. Import the types
from `"db"`. See [provider availability](index.md#provider-status) before
choosing a driver; enum membership does not imply a configured native provider.

## Enums

### `DbDriver`

`Sqlite`, `Postgres`, `Mysql`, `Redis`, `Mongo`.

### `DbErrorKind`

`Connect`, `Execution`, `NotFound`, `InvalidParameter`, `Unsupported`, `Decode`,
`Closed`, `InvalidState`, `OutOfMemory`, `Unknown`.

### `DbValue`

`Null`, `Bool(bool)`, `I64(i64)`, `F64(f64)`, `Text(string)`, `Bytes(Vec<u8>)`,
`Json(string)`.

### `DbColumnType`

`Null`, `Bool`, `I64`, `F64`, `Text`, `Bytes`, `Json`, `Unknown`.

## `DbError`

- `DbError.new(kind, message)`
- `kind()` / `isKind(kind)`
- `message()` / `messageView()`
- `toString()` / `debug()`
- `clone()`

## `DbCapabilities`

- `sql()`
- `preparedStatements()`
- `binaryParams()`
- `transactions()`

## `Connection`

- constructors: `sqlite`, `postgres`, `mysql`, `redis`, `mongo`, `open`
- metadata: `driver`, `capabilities`
- one-shot operations: `exec`, `execParams`, `query`, `queryParams`, `queryValue`
- reusable operation: `prepare`
- transaction: `beginTransaction`
- `clone`

## `PreparedStatement`

- `parameterCount()`
- `query()` / `queryParams(params)`
- `exec()` / `execParams(params)`
- `clone()`

## `QueryResult`

- `rowsAffected()`
- `columnCount()` / `columnName(index)` / `columnType(index)`
- `next()`
- `isNull(index)` / `length(index)`
- `text(index)` / `bytes(index)`
- `getI64(index)` / `getF64(index)` / `getBool(index)`

## `Transaction`

- `isActive()`
- `connection()`
- `commit()` / `rollback()`

An active transaction rolls back on drop. Use explicit `commit()` or
`rollback()` when the caller must observe the provider error; drop cannot
return a rollback error to application code.

## `Db<T>`

- `Db.of<T>(connection, table)`
- `filterRaw`, `filter1Raw`, `orderBy`, `limit`, `offset`, `allRows`
- `find`, `first`, `countRows`
- `create`, `update`, `delete`, `autoMigrate`
- `beginTransaction`

See [the ORM guide](orm.md) for supported field types and raw-fragment rules.

## Common result and ownership contracts

| Operation | Result | Meaning |
| --- | --- | --- |
| `Connection.sqlite(path)` | `Result<Connection, DbError>` | Owns the opened provider connection |
| `connection.exec(sql)` / `execParams(sql, &values)` | `Result<i64, DbError>` | Rows affected; bind values separately from SQL structure |
| `connection.query(sql)` | `Result<QueryResult, DbError>` | Owns a cursor and keeps the connection alive |
| `connection.prepare(sql)` | `Result<PreparedStatement, DbError>` | Reusable provider statement with connection ownership |
| `rows.next()` | `Result<bool, DbError>` | Mutates cursor; false is end-of-rows, not failure |
| `rows.text(column)` | `Result<Option<string>, DbError>` | Zero-based column; None is SQL NULL, Some("") is empty text |
| `rows.bytes(column)` | `Result<Option<Vec<u8>>, DbError>` | Owns returned bytes; no borrowed provider row buffer escapes |
| `rows.getI64(column)` | `Result<Option<i64>, DbError>` | Typed nullable extraction; invalid column/conversion is an error |
| `transaction.commit()` / `rollback()` | `Result<(), DbError>` | Mutates the active transaction and reports provider failure |

The text/byte results remain valid after advancing or dropping the cursor.
`queryValue(sql)` returns `Result<Option<string>, DbError>` as a convenience;
use an explicit cursor when row presence and SQL NULL must be distinguished.
