Merge branch 'master' of github.com:Araq/Nimrod
This commit is contained in:
commit
2a2f41ee98
3 changed files with 21 additions and 13 deletions
|
|
@ -14,7 +14,8 @@ import strutils, mysql
|
||||||
|
|
||||||
type
|
type
|
||||||
TDbConn* = PMySQL ## encapsulates a database connection
|
TDbConn* = PMySQL ## encapsulates a database connection
|
||||||
TRow* = seq[string] ## a row of a dataset
|
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
|
EDb* = object of EIO ## exception that is raised if a database error occurs
|
||||||
|
|
||||||
TSqlQuery* = distinct string ## an SQL query string
|
TSqlQuery* = distinct string ## an SQL query string
|
||||||
|
|
@ -111,7 +112,8 @@ iterator FastRows*(db: TDbConn, query: TSqlQuery,
|
||||||
|
|
||||||
proc getRow*(db: TDbConn, query: TSqlQuery,
|
proc getRow*(db: TDbConn, query: TSqlQuery,
|
||||||
args: varargs[string, `$`]): TRow {.tags: [FReadDB].} =
|
args: varargs[string, `$`]): TRow {.tags: [FReadDB].} =
|
||||||
## retrieves a single row.
|
## retrieves a single row. If the query doesn't return any rows, this proc
|
||||||
|
## will return a TRow with empty strings for each column.
|
||||||
rawExec(db, query, args)
|
rawExec(db, query, args)
|
||||||
var sqlres = mysql.UseResult(db)
|
var sqlres = mysql.UseResult(db)
|
||||||
if sqlres != nil:
|
if sqlres != nil:
|
||||||
|
|
@ -150,9 +152,9 @@ iterator Rows*(db: TDbConn, query: TSqlQuery,
|
||||||
|
|
||||||
proc GetValue*(db: TDbConn, query: TSqlQuery,
|
proc GetValue*(db: TDbConn, query: TSqlQuery,
|
||||||
args: varargs[string, `$`]): string {.tags: [FReadDB].} =
|
args: varargs[string, `$`]): string {.tags: [FReadDB].} =
|
||||||
## executes the query and returns the result dataset's the first column
|
## executes the query and returns the first column of the first row of the
|
||||||
## of the first row. Returns "" if the dataset contains no rows. This uses
|
## result dataset. Returns "" if the dataset contains no rows or the database
|
||||||
## `FastRows`, so it inherits its fragile behaviour.
|
## value is NULL.
|
||||||
result = ""
|
result = ""
|
||||||
for row in FastRows(db, query, args):
|
for row in FastRows(db, query, args):
|
||||||
result = row[0]
|
result = row[0]
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,8 @@ import strutils, postgres
|
||||||
|
|
||||||
type
|
type
|
||||||
TDbConn* = PPGconn ## encapsulates a database connection
|
TDbConn* = PPGconn ## encapsulates a database connection
|
||||||
TRow* = seq[string] ## a row of a dataset
|
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
|
EDb* = object of EIO ## exception that is raised if a database error occurs
|
||||||
|
|
||||||
TSqlQuery* = distinct string ## an SQL query string
|
TSqlQuery* = distinct string ## an SQL query string
|
||||||
|
|
@ -110,7 +111,8 @@ iterator FastRows*(db: TDbConn, query: TSqlQuery,
|
||||||
|
|
||||||
proc getRow*(db: TDbConn, query: TSqlQuery,
|
proc getRow*(db: TDbConn, query: TSqlQuery,
|
||||||
args: varargs[string, `$`]): TRow {.tags: [FReadDB].} =
|
args: varargs[string, `$`]): TRow {.tags: [FReadDB].} =
|
||||||
## retrieves a single row.
|
## retrieves a single row. If the query doesn't return any rows, this proc
|
||||||
|
## will return a TRow with empty strings for each column.
|
||||||
var res = setupQuery(db, query, args)
|
var res = setupQuery(db, query, args)
|
||||||
var L = PQnfields(res)
|
var L = PQnfields(res)
|
||||||
result = newRow(L)
|
result = newRow(L)
|
||||||
|
|
@ -131,8 +133,9 @@ iterator Rows*(db: TDbConn, query: TSqlQuery,
|
||||||
|
|
||||||
proc GetValue*(db: TDbConn, query: TSqlQuery,
|
proc GetValue*(db: TDbConn, query: TSqlQuery,
|
||||||
args: varargs[string, `$`]): string {.tags: [FReadDB].} =
|
args: varargs[string, `$`]): string {.tags: [FReadDB].} =
|
||||||
## executes the query and returns the result dataset's the first column
|
## executes the query and returns the first column of the first row of the
|
||||||
## of the first row. Returns "" if the dataset contains no rows.
|
## result dataset. Returns "" if the dataset contains no rows or the database
|
||||||
|
## value is NULL.
|
||||||
var x = PQgetvalue(setupQuery(db, query, args), 0, 0)
|
var x = PQgetvalue(setupQuery(db, query, args), 0, 0)
|
||||||
result = if isNil(x): "" else: $x
|
result = if isNil(x): "" else: $x
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,8 @@ import strutils, sqlite3
|
||||||
|
|
||||||
type
|
type
|
||||||
TDbConn* = PSqlite3 ## encapsulates a database connection
|
TDbConn* = PSqlite3 ## encapsulates a database connection
|
||||||
TRow* = seq[string] ## a row of a dataset
|
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
|
EDb* = object of EIO ## exception that is raised if a database error occurs
|
||||||
|
|
||||||
TSqlQuery* = distinct string ## an SQL query string
|
TSqlQuery* = distinct string ## an SQL query string
|
||||||
|
|
@ -108,7 +109,8 @@ iterator FastRows*(db: TDbConn, query: TSqlQuery,
|
||||||
|
|
||||||
proc getRow*(db: TDbConn, query: TSqlQuery,
|
proc getRow*(db: TDbConn, query: TSqlQuery,
|
||||||
args: varargs[string, `$`]): TRow {.tags: [FReadDB].} =
|
args: varargs[string, `$`]): TRow {.tags: [FReadDB].} =
|
||||||
## retrieves a single row.
|
## retrieves a single row. If the query doesn't return any rows, this proc
|
||||||
|
## will return a TRow with empty strings for each column.
|
||||||
var stmt = setupQuery(db, query, args)
|
var stmt = setupQuery(db, query, args)
|
||||||
var L = (columnCount(stmt))
|
var L = (columnCount(stmt))
|
||||||
result = newRow(L)
|
result = newRow(L)
|
||||||
|
|
@ -130,8 +132,9 @@ iterator Rows*(db: TDbConn, query: TSqlQuery,
|
||||||
|
|
||||||
proc GetValue*(db: TDbConn, query: TSqlQuery,
|
proc GetValue*(db: TDbConn, query: TSqlQuery,
|
||||||
args: varargs[string, `$`]): string {.tags: [FReadDB].} =
|
args: varargs[string, `$`]): string {.tags: [FReadDB].} =
|
||||||
## executes the query and returns the result dataset's the first column
|
## executes the query and returns the first column of the first row of the
|
||||||
## of the first row. Returns "" if the dataset contains no rows.
|
## result dataset. Returns "" if the dataset contains no rows or the database
|
||||||
|
## value is NULL.
|
||||||
var stmt = setupQuery(db, query, args)
|
var stmt = setupQuery(db, query, args)
|
||||||
if step(stmt) == SQLITE_ROW:
|
if step(stmt) == SQLITE_ROW:
|
||||||
let cb = column_bytes(stmt, 0)
|
let cb = column_bytes(stmt, 0)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue