Module db_sqlite

A higher level SQLite database wrapper. This interface is implemented for other databases too.

Types

TDbConn = PSqlite3
encapsulates a database connection
TRow = seq[string]
a row of a dataset. NULL database values will be transformed always to the empty string.
EDb = object of EIO
exception that is raised if a database error occurs
TSqlQuery = distinct string
an SQL query string
FDb = object of FIO
effect that denotes a database operation
FReadDb = object of FDb
effect that denotes a read operation
FWriteDb = object of FDb
effect that denotes a write operation

Procs

proc sql(query: string): TSqlQuery {.noSideEffect, inline, raises: [], tags: [].}

constructs a TSqlQuery from the string query. This is supposed to be used as a raw-string-literal modifier: sql"update user set counter = counter + 1"

If assertions are turned off, it does nothing. If assertions are turned on, later versions will check the string for valid syntax.

proc dbError(msg: string) {.noreturn, raises: [EDb], tags: [].}
raises an EDb exception with message msg.
proc tryExec(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): bool {.
    tags: [FReadDb, FWriteDb], raises: [].}
tries to execute the query and returns true if successful, false otherwise.
proc exec(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]) {.
    tags: [FReadDb, FWriteDb], raises: [EDb].}
executes the query and raises EDB if not successful.
proc getRow(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): TRow {.
    tags: [FReadDb], raises: [EDb].}
retrieves a single row. If the query doesn't return any rows, this proc will return a TRow with empty strings for each column.
proc getAllRows(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): seq[
    TRow] {.tags: [FReadDb], raises: [EDb].}
executes the query and returns the whole result dataset.
proc getValue(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): string {.
    tags: [FReadDb], raises: [EDb].}
executes the query and returns the first column of the first row of the result dataset. Returns "" if the dataset contains no rows or the database value is NULL.
proc tryInsertID(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): int64 {.
    tags: [FWriteDb], raises: [].}
executes the query (typically "INSERT") and returns the generated ID for the row or -1 in case of an error.
proc insertID(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): int64 {.
    tags: [FWriteDb], raises: [EDb].}
executes the query (typically "INSERT") and returns the generated ID for the row. For Postgre this adds RETURNING id to the query, so it only works if your primary key is named id.
proc execAffectedRows(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): int64 {.
    tags: [FReadDb, FWriteDb], raises: [EDb].}
executes the query (typically "UPDATE") and returns the number of affected rows.
proc close(db: TDbConn) {.tags: [FDb], raises: [EDb].}
closes the database connection.
proc open(connection, user, password, database: string): TDbConn {.tags: [FDb], 
    raises: [EDb].}
opens a database connection. Raises EDb if the connection could not be established. Only the connection parameter is used for sqlite.

Iterators

iterator fastRows(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): TRow {.
    tags: [FReadDb], raises: [EDb].}
executes the query and iterates over the result dataset. This is very fast, but potenially dangerous: If the for-loop-body executes another query, the results can be undefined. For Sqlite it is safe though.
iterator rows(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): TRow {.
    tags: [FReadDb], raises: [EDb].}
same as FastRows, but slower and safe.
Generated: 2014-03-11 21:26:44 UTC