summary refs log tree commit diff stats
path: root/lib/impure/db_mysql.nim
diff options
context:
space:
mode:
authorOscar Campbell <oscar@campbell.nu>2015-06-10 19:08:45 +0200
committerOscar Campbell <oscar@campbell.nu>2015-06-10 19:08:45 +0200
commitb3eb8ab81c325d21dd234e945224b0e14a1b88f4 (patch)
tree58217a7430f79b3215b3f44fd89eabb9ecc5422d /lib/impure/db_mysql.nim
parentf0b6c03b2686aca85670cd35f84c6b0bdc3ff23d (diff)
parentd892887c937a1e07418afb620098d4cd98275b2a (diff)
downloadNim-b3eb8ab81c325d21dd234e945224b0e14a1b88f4.tar.gz
Merge branch 'devel' into bugfix-2858-testament-sources-system-wide
Diffstat (limited to 'lib/impure/db_mysql.nim')
-rw-r--r--lib/impure/db_mysql.nim27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/impure/db_mysql.nim b/lib/impure/db_mysql.nim
index 619c2a656..7c2901efd 100644
--- a/lib/impure/db_mysql.nim
+++ b/lib/impure/db_mysql.nim
@@ -16,6 +16,9 @@ type
   DbConn* = PMySQL    ## encapsulates a database connection
   Row* = seq[string]   ## a row of a dataset. NULL database values will be
                        ## transformed always to the empty string.
+  InstantRow* = tuple[row: cstringArray, len: int]  ## a handle that can be
+                                                    ## used to get a row's
+                                                    ## column text on demand
   EDb* = object of IOError ## exception that is raised if a database error occurs
 
   SqlQuery* = distinct string ## an SQL query string
@@ -127,6 +130,30 @@ iterator fastRows*(db: DbConn, query: SqlQuery,
       yield result
     properFreeResult(sqlres, row)
 
+iterator instantRows*(db: DbConn, query: SqlQuery,
+                      args: varargs[string, `$`]): InstantRow
+                      {.tags: [FReadDb].} =
+  ## same as fastRows but returns a handle that can be used to get column text
+  ## on demand using []. Returned handle is valid only within interator body.
+  rawExec(db, query, args)
+  var sqlres = mysql.useResult(db)
+  if sqlres != nil:
+    let L = int(mysql.numFields(sqlres))
+    var row: cstringArray
+    while true:
+      row = mysql.fetchRow(sqlres)
+      if row == nil: break
+      yield (row: row, len: L)
+    properFreeResult(sqlres, row)
+
+proc `[]`*(row: InstantRow, col: int): string {.inline.} =
+  ## returns text for given column of the row
+  $row.row[col]
+
+proc len*(row: InstantRow): int {.inline.} =
+  ## returns number of columns in the row
+  row.len
+
 proc getRow*(db: DbConn, query: SqlQuery,
              args: varargs[string, `$`]): Row {.tags: [FReadDB].} =
   ## retrieves a single row. If the query doesn't return any rows, this proc