A higher level PostgreSQL database wrapper. This interface is implemented for other databases also.
See also: db_odbc, db_sqlite, db_mysql.
All db_*
modules support the same form of parameter substitution. That is, using the ?
(question mark) to signify the place where a value should be placed. For example:
sql"INSERT INTO myTable (colA, colB, colC) VALUES (?, ?, ?)"
Note: There are two approaches to parameter substitution support by this module.
1. SqlQuery
using ?, ?, ?, ...
(same as all the db_*
modules)
2. SqlPrepared
using $1, $2, $3, ...
prepare(db, "myExampleInsert", sql"""INSERT INTO myTable (colA, colB, colC) VALUES ($1, $2, $3)""", 3)
import db_postgres let db = open("localhost", "user", "password", "dbname") db.close()
db.exec(sql"DROP TABLE IF EXISTS myTable") db.exec(sql("""CREATE TABLE myTable ( id integer, name varchar(50) not null)"""))
db.exec(sql"INSERT INTO myTable (id, name) VALUES (0, ?)", "Dominik")
DbConn = PPGconn
Row = seq[string]
InstantRow = object res: PPGresult ## used to get a row's line: int ## column text on demand
SqlPrepared = distinct string
proc dbError(db: DbConn) {...}{.noreturn, raises: [DbError], tags: [].}
proc dbQuote(s: string): string {...}{.raises: [], tags: [].}
proc tryExec(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): bool {...}{. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].}
proc tryExec(db: DbConn; stmtName: SqlPrepared; args: varargs[string, `$`]): bool {...}{. tags: [ReadDbEffect, WriteDbEffect], raises: [Exception].}
proc exec(db: DbConn; query: SqlQuery; args: varargs[string, `$`]) {...}{. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].}
proc exec(db: DbConn; stmtName: SqlPrepared; args: varargs[string]) {...}{. tags: [ReadDbEffect, WriteDbEffect], raises: [Exception, DbError].}
not (x == y)
. proc prepare(db: DbConn; stmtName: string; query: SqlQuery; nParams: int): SqlPrepared {...}{. raises: [DbError], tags: [].}
SqlPrepared
statement. Parameter substitution is done via $1
, $2
, $3
, etc. proc `[]`(row: InstantRow; col: int): string {...}{.inline, raises: [], tags: [].}
proc len(row: InstantRow): int {...}{.inline, raises: [], tags: [].}
proc getRow(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): Row {...}{. tags: [ReadDbEffect], raises: [DbError].}
proc getRow(db: DbConn; stmtName: SqlPrepared; args: varargs[string, `$`]): Row {...}{. tags: [ReadDbEffect], raises: [Exception, DbError].}
proc getAllRows(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): seq[Row] {...}{. tags: [ReadDbEffect], raises: [DbError].}
proc getAllRows(db: DbConn; stmtName: SqlPrepared; args: varargs[string, `$`]): seq[Row] {...}{. tags: [ReadDbEffect], raises: [Exception, DbError].}
proc getValue(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): string {...}{. tags: [ReadDbEffect], raises: [DbError].}
proc getValue(db: DbConn; stmtName: SqlPrepared; args: varargs[string, `$`]): string {...}{. tags: [ReadDbEffect], raises: [Exception, DbError].}
proc tryInsertID(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): int64 {...}{. tags: [WriteDbEffect], raises: [DbError, ValueError].}
RETURNING id
to the query, so it only works if your primary key is named id
. proc insertID(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): int64 {...}{. tags: [WriteDbEffect], raises: [DbError, ValueError].}
RETURNING id
to the query, so it only works if your primary key is named id
. proc execAffectedRows(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): int64 {...}{. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError, ValueError].}
proc execAffectedRows(db: DbConn; stmtName: SqlPrepared; args: varargs[string, `$`]): int64 {...}{. tags: [ReadDbEffect, WriteDbEffect], raises: [Exception, DbError, ValueError].}
proc close(db: DbConn) {...}{.tags: [DbEffect], raises: [].}
proc open(connection, user, password, database: string): DbConn {...}{.tags: [DbEffect], raises: [DbError].}
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.
proc setEncoding(connection: DbConn; encoding: string): bool {...}{.tags: [DbEffect], raises: [].}
iterator fastRows(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): Row {...}{. tags: [ReadDbEffect], raises: [DbError].}
iterator fastRows(db: DbConn; stmtName: SqlPrepared; args: varargs[string, `$`]): Row {...}{. tags: [ReadDbEffect], raises: [Exception, DbError].}
iterator instantRows(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): InstantRow {...}{. tags: [ReadDbEffect], raises: [DbError].}
iterator instantRows(db: DbConn; stmtName: SqlPrepared; args: varargs[string, `$`]): InstantRow {...}{. tags: [ReadDbEffect], raises: [Exception, DbError].}
iterator instantRows(db: DbConn; columns: var DbColumns; query: SqlQuery; args: varargs[string, `$`]): InstantRow {...}{.tags: [ReadDbEffect], raises: [DbError].}
iterator rows(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): Row {...}{. tags: [ReadDbEffect], raises: [DbError].}
iterator rows(db: DbConn; stmtName: SqlPrepared; args: varargs[string, `$`]): Row {...}{. tags: [ReadDbEffect], raises: [Exception, DbError].}
© 2006–2018 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/db_postgres.html