A higher level
mySQL database wrapper. The same interface is implemented for other databases too.
TDbConn = PMySQL
-
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
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.
proc execAffectedRows(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): int64 {.
tags: [FReadDb, FWriteDb], raises: [EDb].}
-
runs 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, EOverflow, EInvalidValue].}
-
opens a database connection. Raises EDb if the connection could not be established.
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 MySQL this is the case!.
iterator rows(db: TDbConn; query: TSqlQuery; args: varargs[string, `$`]): TRow {.
tags: [FReadDb], raises: [EDb].}
-
same as FastRows, but slower and safe.