More docgen fixes.

This commit is contained in:
Dominik Picheta 2014-09-13 16:45:07 +01:00
commit 0047172274
9 changed files with 171 additions and 233 deletions

View file

@ -78,17 +78,17 @@ proc tryExec*(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]): bool {
tags: [FReadDB, FWriteDb].} =
## tries to execute the query and returns true if successful, false otherwise.
var q = dbFormat(query, args)
return mysql.RealQuery(db, q, q.len) == 0'i32
return mysql.realQuery(db, q, q.len) == 0'i32
proc rawExec(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]) =
var q = dbFormat(query, args)
if mysql.RealQuery(db, q, q.len) != 0'i32: dbError(db)
if mysql.realQuery(db, q, q.len) != 0'i32: dbError(db)
proc exec*(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]) {.
tags: [FReadDB, FWriteDb].} =
## executes the query and raises EDB if not successful.
var q = dbFormat(query, args)
if mysql.RealQuery(db, q, q.len) != 0'i32: dbError(db)
if mysql.realQuery(db, q, q.len) != 0'i32: dbError(db)
proc newRow(L: int): TRow =
newSeq(result, L)
@ -96,8 +96,8 @@ proc newRow(L: int): TRow =
proc properFreeResult(sqlres: mysql.PRES, row: cstringArray) =
if row != nil:
while mysql.FetchRow(sqlres) != nil: discard
mysql.FreeResult(sqlres)
while mysql.fetchRow(sqlres) != nil: discard
mysql.freeResult(sqlres)
iterator fastRows*(db: TDbConn, query: TSqlQuery,
args: varargs[string, `$`]): TRow {.tags: [FReadDB].} =
@ -105,13 +105,13 @@ iterator fastRows*(db: TDbConn, query: TSqlQuery,
## fast, but potenially dangerous: If the for-loop-body executes another
## query, the results can be undefined. For MySQL this is the case!.
rawExec(db, query, args)
var sqlres = mysql.UseResult(db)
var sqlres = mysql.useResult(db)
if sqlres != nil:
var L = int(mysql.NumFields(sqlres))
var L = int(mysql.numFields(sqlres))
var result = newRow(L)
var row: cstringArray
while true:
row = mysql.FetchRow(sqlres)
row = mysql.fetchRow(sqlres)
if row == nil: break
for i in 0..L-1:
setLen(result[i], 0)
@ -124,11 +124,11 @@ proc getRow*(db: TDbConn, query: TSqlQuery,
## 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)
var sqlres = mysql.UseResult(db)
var sqlres = mysql.useResult(db)
if sqlres != nil:
var L = int(mysql.NumFields(sqlres))
var L = int(mysql.numFields(sqlres))
result = newRow(L)
var row = mysql.FetchRow(sqlres)
var row = mysql.fetchRow(sqlres)
if row != nil:
for i in 0..L-1:
setLen(result[i], 0)
@ -140,24 +140,24 @@ proc getAllRows*(db: TDbConn, query: TSqlQuery,
## executes the query and returns the whole result dataset.
result = @[]
rawExec(db, query, args)
var sqlres = mysql.UseResult(db)
var sqlres = mysql.useResult(db)
if sqlres != nil:
var L = int(mysql.NumFields(sqlres))
var L = int(mysql.numFields(sqlres))
var row: cstringArray
var j = 0
while true:
row = mysql.FetchRow(sqlres)
row = mysql.fetchRow(sqlres)
if row == nil: break
setLen(result, j+1)
newSeq(result[j], L)
for i in 0..L-1: result[j][i] = $row[i]
inc(j)
mysql.FreeResult(sqlres)
mysql.freeResult(sqlres)
iterator rows*(db: TDbConn, query: TSqlQuery,
args: varargs[string, `$`]): TRow {.tags: [FReadDB].} =
## same as `FastRows`, but slower and safe.
for r in items(GetAllRows(db, query, args)): yield r
## same as `fastRows`, but slower and safe.
for r in items(getAllRows(db, query, args)): yield r
proc getValue*(db: TDbConn, query: TSqlQuery,
args: varargs[string, `$`]): string {.tags: [FReadDB].} =
@ -165,7 +165,7 @@ proc getValue*(db: TDbConn, query: TSqlQuery,
## result dataset. Returns "" if the dataset contains no rows or the database
## value is NULL.
result = ""
for row in FastRows(db, query, args):
for row in fastRows(db, query, args):
result = row[0]
break
@ -174,16 +174,16 @@ proc tryInsertId*(db: TDbConn, query: TSqlQuery,
## executes the query (typically "INSERT") and returns the
## generated ID for the row or -1 in case of an error.
var q = dbFormat(query, args)
if mysql.RealQuery(db, q, q.len) != 0'i32:
if mysql.realQuery(db, q, q.len) != 0'i32:
result = -1'i64
else:
result = mysql.InsertId(db)
result = mysql.insertId(db)
proc insertId*(db: TDbConn, query: TSqlQuery,
args: varargs[string, `$`]): int64 {.tags: [FWriteDb].} =
## executes the query (typically "INSERT") and returns the
## generated ID for the row.
result = TryInsertID(db, query, args)
result = tryInsertID(db, query, args)
if result < 0: dbError(db)
proc execAffectedRows*(db: TDbConn, query: TSqlQuery,
@ -192,7 +192,7 @@ proc execAffectedRows*(db: TDbConn, query: TSqlQuery,
## runs the query (typically "UPDATE") and returns the
## number of affected rows
rawExec(db, query, args)
result = mysql.AffectedRows(db)
result = mysql.affectedRows(db)
proc close*(db: TDbConn) {.tags: [FDb].} =
## closes the database connection.
@ -202,7 +202,7 @@ proc open*(connection, user, password, database: string): TDbConn {.
tags: [FDb].} =
## opens a database connection. Raises `EDb` if the connection could not
## be established.
result = mysql.Init(nil)
result = mysql.init(nil)
if result == nil: dbError("could not open database connection")
let
colonPos = connection.find(':')
@ -210,9 +210,9 @@ proc open*(connection, user, password, database: string): TDbConn {.
else: substr(connection, 0, colonPos-1)
port: int32 = if colonPos < 0: 0'i32
else: substr(connection, colonPos+1).parseInt.int32
if mysql.RealConnect(result, host, user, password, database,
if mysql.realConnect(result, host, user, password, database,
port, nil, 0) == nil:
var errmsg = $mysql.error(result)
db_mysql.Close(result)
db_mysql.close(result)
dbError(errmsg)

View file

@ -37,7 +37,7 @@ proc dbError(db: TDbConn) {.noreturn.} =
## raises an EDb exception.
var e: ref EDb
new(e)
e.msg = $PQerrorMessage(db)
e.msg = $pqErrorMessage(db)
raise e
proc dbError*(msg: string) {.noreturn.} =
@ -68,17 +68,17 @@ proc tryExec*(db: TDbConn, query: TSqlQuery,
args: varargs[string, `$`]): bool {.tags: [FReadDB, FWriteDb].} =
## tries to execute the query and returns true if successful, false otherwise.
var q = dbFormat(query, args)
var res = PQExec(db, q)
result = PQresultStatus(res) == PGRES_COMMAND_OK
PQclear(res)
var res = pqExec(db, q)
result = pqresultStatus(res) == PGRES_COMMAND_OK
pqclear(res)
proc exec*(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]) {.
tags: [FReadDB, FWriteDb].} =
## executes the query and raises EDB if not successful.
var q = dbFormat(query, args)
var res = PQExec(db, q)
if PQresultStatus(res) != PGRES_COMMAND_OK: dbError(db)
PQclear(res)
var res = pqExec(db, q)
if pqresultStatus(res) != PGRES_COMMAND_OK: dbError(db)
pqclear(res)
proc newRow(L: int): TRow =
newSeq(result, L)
@ -87,13 +87,13 @@ proc newRow(L: int): TRow =
proc setupQuery(db: TDbConn, query: TSqlQuery,
args: varargs[string]): PPGresult =
var q = dbFormat(query, args)
result = PQExec(db, q)
if PQresultStatus(result) != PGRES_TUPLES_OK: dbError(db)
result = pqExec(db, q)
if pqresultStatus(result) != PGRES_TUPLES_OK: dbError(db)
proc setRow(res: PPGresult, r: var TRow, line, cols: int32) =
for col in 0..cols-1:
setLen(r[col], 0)
var x = PQgetvalue(res, line, col)
var x = pqgetvalue(res, line, col)
add(r[col], x)
iterator fastRows*(db: TDbConn, query: TSqlQuery,
@ -102,41 +102,41 @@ iterator fastRows*(db: TDbConn, query: TSqlQuery,
## fast, but potenially dangerous: If the for-loop-body executes another
## query, the results can be undefined. For Postgres it is safe though.
var res = setupQuery(db, query, args)
var L = PQnfields(res)
var L = pqnfields(res)
var result = newRow(L)
for i in 0..PQntuples(res)-1:
for i in 0..pqntuples(res)-1:
setRow(res, result, i, L)
yield result
PQclear(res)
pqclear(res)
proc getRow*(db: TDbConn, query: TSqlQuery,
args: varargs[string, `$`]): TRow {.tags: [FReadDB].} =
## 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 L = PQnfields(res)
var L = pqnfields(res)
result = newRow(L)
setRow(res, result, 0, L)
PQclear(res)
pqclear(res)
proc getAllRows*(db: TDbConn, query: TSqlQuery,
args: varargs[string, `$`]): seq[TRow] {.tags: [FReadDB].} =
## executes the query and returns the whole result dataset.
result = @[]
for r in FastRows(db, query, args):
for r in fastRows(db, query, args):
result.add(r)
iterator rows*(db: TDbConn, query: TSqlQuery,
args: varargs[string, `$`]): TRow {.tags: [FReadDB].} =
## same as `FastRows`, but slower and safe.
for r in items(GetAllRows(db, query, args)): yield r
## same as `fastRows`, but slower and safe.
for r in items(getAllRows(db, query, args)): yield r
proc getValue*(db: TDbConn, query: TSqlQuery,
args: varargs[string, `$`]): string {.tags: [FReadDB].} =
## 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.
var x = PQgetvalue(setupQuery(db, query, args), 0, 0)
var x = pqgetvalue(setupQuery(db, query, args), 0, 0)
result = if isNil(x): "" else: $x
proc tryInsertID*(db: TDbConn, query: TSqlQuery,
@ -145,10 +145,10 @@ proc tryInsertID*(db: TDbConn, query: TSqlQuery,
## 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``.
var x = PQgetvalue(setupQuery(db, TSqlQuery(string(query) & " RETURNING id"),
var x = pqgetvalue(setupQuery(db, TSqlQuery(string(query) & " RETURNING id"),
args), 0, 0)
if not isNil(x):
result = ParseBiggestInt($x)
result = parseBiggestInt($x)
else:
result = -1
@ -158,7 +158,7 @@ proc insertID*(db: TDbConn, query: TSqlQuery,
## 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``.
result = TryInsertID(db, query, args)
result = tryInsertID(db, query, args)
if result < 0: dbError(db)
proc execAffectedRows*(db: TDbConn, query: TSqlQuery,
@ -167,14 +167,14 @@ proc execAffectedRows*(db: TDbConn, query: TSqlQuery,
## executes the query (typically "UPDATE") and returns the
## number of affected rows.
var q = dbFormat(query, args)
var res = PQExec(db, q)
if PQresultStatus(res) != PGRES_COMMAND_OK: dbError(db)
result = parseBiggestInt($PQcmdTuples(res))
PQclear(res)
var res = pqExec(db, q)
if pqresultStatus(res) != PGRES_COMMAND_OK: dbError(db)
result = parseBiggestInt($pqcmdTuples(res))
pqclear(res)
proc close*(db: TDbConn) {.tags: [FDb].} =
## closes the database connection.
if db != nil: PQfinish(db)
if db != nil: pqfinish(db)
proc open*(connection, user, password, database: string): TDbConn {.
tags: [FDb].} =
@ -195,7 +195,7 @@ proc open*(connection, user, password, database: string): TDbConn {.
##
## Note that the connection parameter is not used but exists to maintain
## the nim db api.
result = PQsetdbLogin(nil, nil, nil, nil, database, user, password)
if PQStatus(result) != CONNECTION_OK: dbError(result) # result = nil
result = pqsetdbLogin(nil, nil, nil, nil, database, user, password)
if pqStatus(result) != CONNECTION_OK: dbError(result) # result = nil

View file

@ -1,63 +0,0 @@
#
#
# Nim's Runtime Library
# (c) Copyright 2012 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## This module contains simple high-level procedures for dealing with the
## web. Use cases:
##
## * requesting URLs
## * sending and retrieving emails
## * sending and retrieving files from an FTP server
##
## Currently only requesting URLs is implemented. The implementation depends
## on the libcurl library!
##
## **Deprecated since version 0.8.8:** Use the
## `httpclient <httpclient.html>`_ module instead.
##
{.deprecated.}
import libcurl, streams
proc curlwrapperWrite(p: pointer, size, nmemb: int,
data: pointer): int {.cdecl.} =
var stream = cast[PStream](data)
stream.writeData(p, size*nmemb)
return size*nmemb
proc URLretrieveStream*(url: string): PStream =
## retrieves the given `url` and returns a stream which one can read from to
## obtain the contents. Returns nil if an error occurs.
result = newStringStream()
var hCurl = easy_init()
if hCurl == nil: return nil
if easy_setopt(hCurl, OPT_URL, url) != E_OK: return nil
if easy_setopt(hCurl, OPT_WRITEFUNCTION,
curlwrapperWrite) != E_OK: return nil
if easy_setopt(hCurl, OPT_WRITEDATA, result) != E_OK: return nil
if easy_perform(hCurl) != E_OK: return nil
easy_cleanup(hCurl)
proc URLretrieveString*(url: string): TaintedString =
## retrieves the given `url` and returns the contents. Returns nil if an
## error occurs.
var stream = newStringStream()
var hCurl = easy_init()
if hCurl == nil: return
if easy_setopt(hCurl, OPT_URL, url) != E_OK: return
if easy_setopt(hCurl, OPT_WRITEFUNCTION,
curlwrapperWrite) != E_OK: return
if easy_setopt(hCurl, OPT_WRITEDATA, stream) != E_OK: return
if easy_perform(hCurl) != E_OK: return
easy_cleanup(hCurl)
result = stream.data.TaintedString
when isMainModule:
echo URLretrieveString("http://nim-code.org/")