# Module db_postgres

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

# Imports

strutils, postgres

# Types

    TDbConn = PPGconn

  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: [EDb, EInvalidValue].}

  executes the query (typically "INSERT") and returns the generated ID for the row or -1 in case of an error. For Postgre this adds `RETURNING id` to the query, so it only works if your primary key is named `id`.

    proc InsertID(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): int64 {.
        tags: [FWriteDb], raises: [EDb, EInvalidValue].}

  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, EInvalidValue].}

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

    proc Close(db: TDbConn) {.tags: [FDb], raises: [].}

  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.

# 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 Postgres 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: 2013-05-21 09:55:13 UTC