A small Ard wrapper around Go's database/sql package.
It provides a consistent API for PostgreSQL, SQLite, and MySQL using pure-Go drivers, including named parameters, transactions, and query results represented as opaque Ard Any values.
- Ard 0.26.0 or newer
ard add github.com/akonwi/ard-sql@latest as sqlThen import the module:
use sql
For local development:
[dependencies]
sql = { path = "../sql" }use sql
fn users(database_url: Str) [Any]!Str {
let db = try sql::open(database_url)
defer db.close()
let rows = try db
.query("SELECT id, name FROM users WHERE active = @active")
.all(["active": true])
Result::ok(rows)
}
Queries use @name parameters. They are rewritten to the placeholder syntax required by the selected database driver.
Transactions expose the same exec and query operations:
let tx = try db.begin()
try tx.query("DELETE FROM users WHERE id = @id").run(["id": 42])
try tx.commit()
The driver is inferred from the connection string:
- PostgreSQL:
postgres://...orpostgresql://... - MySQL: DSNs containing
@tcp(or@unix( - SQLite: all other values, such as
:memory:orapp.db
openopens a database connectionDatabase.closecloses itDatabase.execexecutes SQL without parametersDatabase.querycreates a parameterized queryDatabase.beginstarts a transactionQuery.runexecutes a statementQuery.allreturns every matching rowQuery.firstreturns the first row, if presentTransaction.commitandTransaction.rollbackcomplete a transaction
ard format .
ard test
go test ./...