More postgres test code. Added getValue for PreparedStmts.
Ref #3569. Ref #3560.
This commit is contained in:
parent
255fee81be
commit
a34206fe84
2 changed files with 80 additions and 2 deletions
|
|
@ -162,8 +162,10 @@ proc setupQuery(db: DbConn, stmtName: SqlPrepared,
|
|||
|
||||
proc prepare*(db: DbConn; stmtName: string, query: SqlQuery;
|
||||
nParams: int): SqlPrepared =
|
||||
## Creates a new ``SqlPrepared`` statement. Parameter substitution is done
|
||||
## via ``$1``, ``$2``, ``$3``, etc.
|
||||
if nParams > 0 and not string(query).contains("$1"):
|
||||
dbError("""parameter substitution expects "$1" """)
|
||||
dbError("parameter substitution expects \"$1\"")
|
||||
var res = pqprepare(db, stmtName, query.string, int32(nParams), nil)
|
||||
if pqResultStatus(res) != PGRES_COMMAND_OK: dbError(db)
|
||||
return SqlPrepared(stmtName)
|
||||
|
|
@ -282,6 +284,15 @@ proc getValue*(db: DbConn, query: SqlQuery,
|
|||
var x = pqgetvalue(setupQuery(db, query, args), 0, 0)
|
||||
result = if isNil(x): "" else: $x
|
||||
|
||||
proc getValue*(db: DbConn, stmtName: SqlPrepared,
|
||||
args: varargs[string, `$`]): string {.
|
||||
tags: [ReadDbEffect].} =
|
||||
## 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, stmtName, args), 0, 0)
|
||||
result = if isNil(x): "" else: $x
|
||||
|
||||
proc tryInsertID*(db: DbConn, query: SqlQuery,
|
||||
args: varargs[string, `$`]): int64 {.
|
||||
tags: [WriteDbEffect].}=
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue