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
EDb* = object of EIO
-
exception that is raised if a database error occurs
TSqlQuery* = distinct string
-
an SQL query string
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. This uses FastRows, so it inherits its fragile behaviour.
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.
proc ExecAffectedRows*(db: TDbConn; query: TSqlQuery; args: varargs[string]): int64
-
runs 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.
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 MySQL this is the case!.
iterator Rows*(db: TDbConn; query: TSqlQuery; args: varargs[string]): TRow
-
same as FastRows, but slower and safe.