resolved conflict

This commit is contained in:
Araq
2014-09-13 17:59:37 +02:00
committed by ringabout
parent 00e4d53100
commit 0e5874a93a

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