summary refs log tree commit diff stats
path: root/lib/impure
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@gmail.com>2016-01-18 13:19:07 +0000
committerDominik Picheta <dominikpicheta@gmail.com>2016-01-18 13:19:07 +0000
commita34206fe84a3f02c0ee89fe32611ac4496ea9677 (patch)
tree71836a6e7a414ef2d81c4e35b6a3604576d71aa4 /lib/impure
parent255fee81be11843df67ff5dfd456c7aea28afc1a (diff)
downloadNim-a34206fe84a3f02c0ee89fe32611ac4496ea9677.tar.gz
More postgres test code. Added getValue for PreparedStmts.
Ref #3569. Ref #3560.
Diffstat (limited to 'lib/impure')
-rw-r--r--lib/impure/db_postgres.nim13
1 files changed, 12 insertions, 1 deletions
diff --git a/lib/impure/db_postgres.nim b/lib/impure/db_postgres.nim
index 9bdbae4c2..ef26214b7 100644
--- a/lib/impure/db_postgres.nim
+++ b/lib/impure/db_postgres.nim
@@ -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].}=