slug.io.db
slug.io.db — relational database access
Connect to SQLite, MySQL, or PostgreSQL databases and execute queries and statements. Connection handles are numeric values returned by connect and passed to all subsequent operations.
Always close connections with defer close(conn) or explicit close(conn).
SQLite examples
val { connect, query, exec, close, SQLITE_DRIVER } = import("slug.io.db")
// in-memory database
val conn = connect(":memory:", SQLITE_DRIVER)
defer close(conn)
// file database with options
val conn = connect("file:app.db?cache=shared&mode=rwc", SQLITE_DRIVER)
defer close(conn)
MySQL example
val conn = connect("user:pass@tcp(localhost:3306)/dbname?charset=utf8mb4&parseTime=True", MYSQL_DRIVER)
defer close(conn)
PostgreSQL example
val conn = connect("postgres://user:password@localhost/dbname?sslmode=disable", PGSQL_DRIVER)
defer close(conn)
Transactions
val tx = begin(conn)
exec(tx, "INSERT INTO ...")
commit(tx)
@effects(‘db’)
TOC
- MYSQL_DRIVER
- PGSQL_DRIVER
- SQLITE_DRIVER
begin(connection)close(connection)commit(connection)connect(connectionString, driver)exec(connection, sql, params)query(connection, sql, params)rollback(connection)
Constants
MYSQL_DRIVER
str slug.io.db#MYSQL_DRIVER
driver string for MySQL connections.
PGSQL_DRIVER
str slug.io.db#PGSQL_DRIVER
driver string for PostgreSQL connections.
SQLITE_DRIVER
str slug.io.db#SQLITE_DRIVER
driver string for SQLite3 connections.
Functions
begin(connection)
fn slug.io.db#begin(@num connection) -> @num
begins a transaction and returns a transaction handle.
Pass the transaction handle to query, exec, commit, or rollback in place of a connection handle.
@effects(‘db’)
| Parameter | Type | Default |
|---|---|---|
connection | @num | — |
close(connection)
fn slug.io.db#close(@num connection) -> nil
closes a database connection or transaction handle.
Always close connections when done. Use defer close(conn) for safety.
@effects(‘db’)
| Parameter | Type | Default |
|---|---|---|
connection | @num | — |
commit(connection)
fn slug.io.db#commit(@num connection) -> @num
commits a transaction.
@effects(‘db’)
| Parameter | Type | Default |
|---|---|---|
connection | @num | — |
connect(connectionString, driver)
fn slug.io.db#connect(@str connectionString, @str driver) -> @num
opens a database connection and returns a connection handle.
connectionString format depends on the driver — see module doc for examples. Use defer close(conn) to ensure the connection is released.
@effects(‘db’)
| Parameter | Type | Default |
|---|---|---|
connectionString | @str | — |
driver | @str | — |
exec(connection, sql, params)
fn slug.io.db#exec(@num connection, @str sql, ...params) -> @map
executes a SQL statement and returns a result map.
The result map contains rowsAffected and lastInsertId where supported by the driver. Use ...params to pass positional ? parameter values.
@effects(‘db’)
| Parameter | Type | Default |
|---|---|---|
connection | @num | — |
sql | @str | — |
params | — |
query(connection, sql, params)
fn slug.io.db#query(@num connection, @str sql, ...params) -> @list
executes a SQL query and returns the result rows as a list of string-keyed maps.
Use ...params to pass positional ? parameter values. For named parameters, use slug.db.repo.
@effects(‘db’)
| Parameter | Type | Default |
|---|---|---|
connection | @num | — |
sql | @str | — |
params | — |
rollback(connection)
fn slug.io.db#rollback(@num connection) -> @num
rolls back a transaction.
@effects(‘db’)
| Parameter | Type | Default |
|---|---|---|
connection | @num | — |