A higher level PostgreSQL database wrapper. This interface is implemented for other databases too.
Types
TDbConn = PPGconn
- encapsulates a database connection Source
TRow = seq[string]
- a row of a dataset. NULL database values will be transformed always to the empty string. Source
EDb = object of IOError
- exception that is raised if a database error occurs Source
TSqlQuery = distinct string
- an SQL query string Source
TSqlPrepared = distinct string
- a identifier for the prepared queries Source
FDb = object of IOEffect
- effect that denotes a database operation Source
FReadDb = object of FDb
- effect that denotes a read operation Source
FWriteDb = object of FDb
- effect that denotes a write operation Source
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.
Source proc dbError(db: TDbConn) {.noreturn, raises: [EDb], tags: [].}
- raises an EDb exception. Source
proc dbError(msg: string) {.noreturn, raises: [EDb], tags: [].}
- raises an EDb exception with message msg. Source
proc dbQuote(s: string): string {.raises: [], tags: [].}
- DB quotes the string. Source
proc tryExec(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): bool {. tags: [FReadDb, FWriteDb], raises: [Exception].}
- tries to execute the query and returns true if successful, false otherwise. Source
proc exec(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]) {. tags: [FReadDb, FWriteDb], raises: [Exception, EDb].}
- executes the query and raises EDB if not successful. Source
proc exec(db: TDbConn; stmtName: TSqlPrepared; args: varargs[string]) {. tags: [FReadDb, FWriteDb], raises: [Exception, EDb].}
- Source
proc prepare(db: TDbConn; stmtName: string; query: TSqlQuery; nParams: int): TSqlPrepared {. raises: [EDb], tags: [].}
- Source
proc getRow(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): TRow {. tags: [FReadDb], raises: [Exception, 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. Source
proc getRow(db: TDbConn; stmtName: TSqlPrepared; args: varargs[string, `$`]): TRow {. tags: [FReadDb], raises: [Exception, EDb].}
- Source
proc getAllRows(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): seq[ TRow] {.tags: [FReadDb], raises: [Exception, EDb].}
- executes the query and returns the whole result dataset. Source
proc getAllRows(db: TDbConn; stmtName: TSqlPrepared; args: varargs[string, `$`]): seq[ TRow] {.tags: [FReadDb], raises: [Exception, EDb].}
- executes the prepared query and returns the whole result dataset. Source
proc getValue(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): string {. tags: [FReadDb], raises: [Exception, 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. Source
proc tryInsertID(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): int64 {. tags: [FWriteDb], raises: [Exception, EDb, ValueError].}
- 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. Source
proc insertID(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): int64 {. tags: [FWriteDb], raises: [Exception, EDb, ValueError].}
- 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. Source
proc execAffectedRows(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): int64 {. tags: [FReadDb, FWriteDb], raises: [EDb, ValueError].}
- executes the query (typically "UPDATE") and returns the number of affected rows. Source
proc close(db: TDbConn) {.tags: [FDb], raises: [].}
- closes the database connection. Source
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.
Clients can also use Postgres keyword/value connection strings to connect.
Example:
con = open("", "", "", "host=localhost port=5432 dbname=mydb")
See http://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING for more information.
Note that the connection parameter is not used but exists to maintain the nim db api.
Source proc setEncoding(connection: TDbConn; encoding: string): bool {.tags: [FDb], raises: [].}
- sets the encoding of a database connection, returns true for success, false for failure. Source
Iterators
iterator fastRows(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): TRow {. tags: [FReadDb], raises: [Exception, 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. Source
iterator fastRows(db: TDbConn; stmtName: TSqlPrepared; args: varargs[string, `$`]): TRow {.tags: [FReadDb], raises: [Exception, EDb].}
- executes the prepared query and iterates over the result dataset. Source
iterator rows(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): TRow {. tags: [FReadDb], raises: [Exception, EDb].}
- same as fastRows, but slower and safe. Source