std.sql — Database Primitives
std.sql provides typed SQL access with connection, transaction, and result set primitives.
Current provider status:
- SQLite: supported (
sqlite::memory:,sqlite:///path/to/file.db) - PostgreSQL: supported (
postgres://...,postgresql://...) - MySQL/MariaDB: supported (
mysql://...,mariadb://...) - SQL Server: URI recognized, currently unsupported
Connect
| Function | Signature | Return | Description |
|---|---|---|---|
sql.connect(uri) | string | result<Connection, string> | Opens a database connection. Use sqlite::memory: for in-memory SQLite. |
Connection
| Method | Signature | Return | Description |
|---|---|---|---|
conn.close() | — | void | Closes the connection handle. |
conn.execute(sql) | string | result<int, string> | Executes INSERT/UPDATE/DELETE and returns affected rows. |
conn.execute_params(sql, params) | string, list<SqlValue> | result<int, string> | Parameterized execute. |
conn.query(sql) | string | result<ResultSet, string> | Executes SELECT and returns a result set cursor. |
conn.query_params(sql, params) | string, list<SqlValue> | result<ResultSet, string> | Parameterized query. |
conn.begin_transaction() | — | result<Transaction, string> | Starts a transaction bound to this connection. |
Transaction
| Method | Signature | Return | Description |
|---|---|---|---|
tx.commit() | — | result<void, string> | Commits the active transaction. |
tx.rollback() | — | result<void, string> | Rolls back the active transaction. |
tx.execute(sql) | string | result<int, string> | Executes a statement inside the transaction. |
tx.execute_params(sql, params) | string, list<SqlValue> | result<int, string> | Parameterized execute inside the transaction. |
tx.query(sql) | string | result<ResultSet, string> | Executes a query inside the transaction. |
tx.query_params(sql, params) | string, list<SqlValue> | result<ResultSet, string> | Parameterized query inside the transaction. |
ResultSet
| Method | Signature | Return | Description |
|---|---|---|---|
rs.rows() | — | list<map<string, SqlValue>> | Materializes all rows. |
rs.next() | — | optional<map<string, SqlValue>> | Cursor-style row iteration. |
rs.columns() | — | list<string> | Column names in result order. |
SqlValue
Use SqlValue for typed parameters and typed row decoding.
Constructors
| Function | Signature | Return |
|---|---|---|
sql.int(v) | int | SqlValue |
sql.float(v) | float | SqlValue |
sql.bool(v) | bool | SqlValue |
sql.string(v) | string | SqlValue |
sql.bytes(v) | list<int> | SqlValue |
sql.null() | — | SqlValue |
Methods
| Method | Return | Description |
|---|---|---|
value.is_null() | bool | True when value is SQL NULL. |
value.as_bool() | result<bool, string> | Converts SQL value to bool. |
value.as_int() | result<int, string> | Converts SQL value to int. |
value.as_float() | result<float, string> | Converts SQL value to float. |
value.as_string() | result<string, string> | Converts SQL value to string. |
value.as_bytes() | result<list<int>, string> | Converts SQL value to bytes. |
value.to_string() | string | Generic string representation. |