db #
Description
db is a namespace that contains several useful modules for operating with databases (SQLite, MySQL, MSQL, etc.)
Common Driver Interface
The top-level db module exposes a small Driver interface for code that only needs common SQL operations:
import db
mut conn := db.open(db.DriverConfig{
kind: .sqlite
path: ':memory:'
})!
defer { conn.close() or {} }
rows := conn.exec('select 1 as n')!
println(rows[0].val(0))
SQLite support is available by default. The PostgreSQL, MySQL, and MSSQL adapters are compiled in only when their C client libraries are enabled:
- PostgreSQL:
-d db_pg - MySQL:
-d db_mysql - MSSQL/ODBC:
-d db_mssql
For backend-specific features, continue using db.pg, db.mysql, db.sqlite, or db.mssql directly.
Cross-driver consistency helpers
db.pg and db.mysql accept both user and username in their Config structs.
db.pg, db.mysql, and db.sqlite rows expose row.val(index) and row.values() for direct string access. In db.pg, SQL NULL remains available through row.val_opt(index).
db.pg, db.mysql, and db.sqlite also expose exec_param2(...) as a convenience wrapper around their parameterized query helpers.
fn open #
fn open(config DriverConfig) !&Driver
open creates a normalized database driver for the selected backend.
SQLite support is available by default. PostgreSQL, MySQL, and MSSQL are compiled in only when -d db_pg, -d db_mysql, or -d db_mssql is used, avoiding unconditional dependencies on their C client libraries.
fn DriverKind.from #
fn DriverKind.from[W](input W) !DriverKind
interface Driver #
interface Driver {
mut:
exec(query string) ![]DriverRow
exec_one(query string) !DriverRow
exec_param_many(query string, params []string) ![]DriverRow
validate() !bool
reset() !
close() !
}
Driver is the minimal common database connection interface exposed by the top-level db module. Backend-specific APIs remain available in their existing modules.
enum DriverKind #
enum DriverKind {
sqlite
mysql
pg
mssql
}
DriverKind selects the backend used by open.
struct DriverConfig #
struct DriverConfig {
pub:
kind DriverKind
path string
host string
port int
user string
username string
password string
dbname string
conn_str string
dsn string
driver string
server string
uid string
pwd string
options map[string]string
ssl_mode string
ssl_key string
ssl_cert string
ssl_ca string
ssl_crl string
ssl_capath string
ssl_cipher string
}
DriverConfig contains the common connection fields used by open.
SQLite uses path or, when empty, dbname. PostgreSQL/MySQL use the network/user fields. MSSQL also accepts ODBC fields such as conn_str, dsn, driver, server, uid, pwd, and options.
struct DriverRow #
struct DriverRow {
pub mut:
vals []string
names []string
}
DriverRow is the normalized row type returned by Driver implementations.
fn (DriverRow) val #
fn (row DriverRow) val(index int) string
val returns the value at index.
fn (DriverRow) values #
fn (row DriverRow) values() []string
values returns all row values.
fn (DriverRow) get_string #
fn (row DriverRow) get_string(col_name string) string
get_string returns the value for the given column name, or '' if it is missing.