summary refs log tree commit diff stats
path: root/lib/impure
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2015-12-17 14:39:29 +0100
committerAndreas Rumpf <rumpf_a@web.de>2015-12-17 14:39:40 +0100
commit1386592aab784f194c54ee2dad6ea115df27ce6f (patch)
tree903289d07dde405378c69c4dcc27820f21a53136 /lib/impure
parent3313170b8db620e32cf5b02c2f9b2dfa540c5b02 (diff)
downloadNim-1386592aab784f194c54ee2dad6ea115df27ce6f.tar.gz
implements column information retrival for db_sqlite
Diffstat (limited to 'lib/impure')
-rw-r--r--lib/impure/db_sqlite.nim33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/impure/db_sqlite.nim b/lib/impure/db_sqlite.nim
index 17f031a52..5cfed1eba 100644
--- a/lib/impure/db_sqlite.nim
+++ b/lib/impure/db_sqlite.nim
@@ -137,6 +137,39 @@ iterator instantRows*(db: DbConn, query: SqlQuery,
     yield stmt
   if finalize(stmt) != SQLITE_OK: dbError(db)
 
+proc toTypeKind(t: var DbType; x: int32) =
+  case x
+  of SQLITE_INTEGER:
+    t.kind = dbInt
+    t.size = 8
+  of SQLITE_FLOAT:
+    t.kind = dbFloat
+    t.size = 8
+  of SQLITE_BLOB: t.kind = dbBlob
+  of SQLITE_NULL: t.kind = dbNull
+  of SQLITE_TEXT: t.kind = dbVarchar
+  else: t.kind = dbUnknown
+
+proc setColumns(columns: var DbColumns; x: PStmt) =
+  let L = column_count(x)
+  setLen(columns, L)
+  for i in 0'i32 ..< L:
+    columns[i].name = $column_name(x, i)
+    columns[i].typ.name = $column_decltype(x, i)
+    toTypeKind(columns[i].typ, column_type(x, i))
+    columns[i].tableName = $column_table_name(x, i)
+
+iterator instantRows*(db: DbConn; columns: var DbColumns; query: SqlQuery,
+                      args: varargs[string, `$`]): InstantRow
+                      {.tags: [ReadDbEffect].} =
+  ## same as fastRows but returns a handle that can be used to get column text
+  ## on demand using []. Returned handle is valid only within the iterator body.
+  var stmt = setupQuery(db, query, args)
+  setColumns(columns, stmt)
+  while step(stmt) == SQLITE_ROW:
+    yield stmt
+  if finalize(stmt) != SQLITE_OK: dbError(db)
+
 proc `[]`*(row: InstantRow, col: int32): string {.inline.} =
   ## returns text for given column of the row
   $column_text(row, col)