# Module db_sqlite

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

# Imports

strutils, sqlite3

# Types

    TDbConn* = PSqlite3

  encapsulates a database connection

    TRow* = seq[string]

  a row of a dataset

    EDb* = object of EIO

  exception that is raised if a database error occurs

    TSqlQuery* = distinct string

  an SQL query string

# Procs

    proc sql*(query: string): TSqlQuery {.noSideEffect, inline.}

  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 an EDb exception with message msg.

    proc TryExec*(db: TDbConn; query: TSqlQuery; args: varargs[string]): bool

  tries to execute the query and returns true if successful, false otherwise.

    proc Exec*(db: TDbConn; query: TSqlQuery; args: varargs[string])

  executes the query and raises EDB if not successful.

    proc getRow*(db: TDbConn; query: TSqlQuery; args: varargs[string]): TRow

  retrieves a single row.

    proc GetAllRows*(db: TDbConn; query: TSqlQuery; args: varargs[string]): seq[TRow]

  executes the query and returns the whole result dataset.

    proc GetValue*(db: TDbConn; query: TSqlQuery; args: varargs[string]): string

  executes the query and returns the result dataset's the first column of the first row. Returns "" if the dataset contains no rows.

    proc TryInsertID*(db: TDbConn; query: TSqlQuery; args: varargs[string]): int64

  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

  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

  executes the query (typically "UPDATE") and returns the number of affected rows.

    proc Close*(db: TDbConn)

  closes the database connection.

    proc Open*(connection, user, password, database: string): TDbConn

  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

  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

  same as FastRows, but slower and safe.

Generated: 2012-09-23 21:47:54 UTC