summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorMilos Negovanovic <milos.negovanovic@gmail.com>2014-08-20 10:24:36 +0100
committerMilos Negovanovic <milos.negovanovic@gmail.com>2014-08-20 10:24:36 +0100
commit434dcd7166361fddd84953d8cdd0de68564096ba (patch)
tree3b0c96a42c8c264ee3730f1a590feee587625929 /lib
parente5fd84c5591f5ed0a1e53c1509b25fd2a3f00cd5 (diff)
downloadNim-434dcd7166361fddd84953d8cdd0de68564096ba.tar.gz
Preserve nil <-> NULL between Nimrod and database.
Diffstat (limited to 'lib')
-rw-r--r--lib/impure/db_mysql.nim23
1 files changed, 18 insertions, 5 deletions
diff --git a/lib/impure/db_mysql.nim b/lib/impure/db_mysql.nim
index eec4daf00..eb4092f37 100644
--- a/lib/impure/db_mysql.nim
+++ b/lib/impure/db_mysql.nim
@@ -57,7 +57,7 @@ when false:
       binding: seq[MYSQL_BIND]
     discard mysql_stmt_close(stmt)
 
-proc dbQuote(s: string): string =
+proc dbQuote*(s: string): string =
   result = "'"
   for c in items(s):
     if c == '\'': add(result, "''")
@@ -69,7 +69,10 @@ proc dbFormat(formatstr: TSqlQuery, args: varargs[string]): string =
   var a = 0
   for c in items(string(formatstr)):
     if c == '?':
-      add(result, dbQuote(args[a]))
+      if args[a] == nil:
+        add(result, "NULL")
+      else:
+        add(result, dbQuote(args[a]))
       inc(a)
     else: 
       add(result, c)
@@ -115,7 +118,10 @@ iterator fastRows*(db: TDbConn, query: TSqlQuery,
       if row == nil: break
       for i in 0..L-1: 
         setLen(result[i], 0)
-        add(result[i], row[i])
+        if row[i] == nil:
+          result[i] = nil
+        else:
+          add(result[i], row[i])
       yield result
     properFreeResult(sqlres, row)
 
@@ -132,7 +138,10 @@ proc getRow*(db: TDbConn, query: TSqlQuery,
     if row != nil: 
       for i in 0..L-1: 
         setLen(result[i], 0)
-        add(result[i], row[i])
+        if row[i] == nil:
+          result[i] = nil
+        else:
+          add(result[i], row[i])
     properFreeResult(sqlres, row)
 
 proc getAllRows*(db: TDbConn, query: TSqlQuery, 
@@ -150,7 +159,11 @@ proc getAllRows*(db: TDbConn, query: TSqlQuery,
       if row == nil: break
       setLen(result, j+1)
       newSeq(result[j], L)
-      for i in 0..L-1: result[j][i] = $row[i]
+      for i in 0..L-1:
+        if row[i] == nil:
+          result[j][i] = nil
+        else:
+          result[j][i] = $row[i]
       inc(j)
     mysql.FreeResult(sqlres)