From 434dcd7166361fddd84953d8cdd0de68564096ba Mon Sep 17 00:00:00 2001 From: Milos Negovanovic Date: Wed, 20 Aug 2014 10:24:36 +0100 Subject: [PATCH 1/4] Preserve nil <-> NULL between Nimrod and database. --- lib/impure/db_mysql.nim | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/impure/db_mysql.nim b/lib/impure/db_mysql.nim index eec4daf00d..eb4092f37c 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) From f59ac26b85302808f135aab5309c7683c4b8f405 Mon Sep 17 00:00:00 2001 From: Milos Negovanovic Date: Fri, 26 Sep 2014 11:23:13 +0100 Subject: [PATCH 2/4] Tweaks for postgres driver (not tested yet). --- lib/impure/db_postgres.nim | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/impure/db_postgres.nim b/lib/impure/db_postgres.nim index c375d91910..67e769ed20 100644 --- a/lib/impure/db_postgres.nim +++ b/lib/impure/db_postgres.nim @@ -60,7 +60,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) @@ -124,7 +127,10 @@ proc setRow(res: PPGresult, r: var TRow, line, cols: int32) = for col in 0..cols-1: setLen(r[col], 0) var x = PQgetvalue(res, line, col) - add(r[col], x) + if x == nil: + r[col] = nil + else: + add(r[col], x) iterator fastRows*(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]): TRow {.tags: [FReadDB].} = From 11a2cfb306e50d50eb5c9a35864f5dbbfdabe75f Mon Sep 17 00:00:00 2001 From: Milos Negovanovic Date: Tue, 21 Oct 2014 15:55:02 +0100 Subject: [PATCH 3/4] Add comment. --- lib/impure/db_mysql.nim | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/impure/db_mysql.nim b/lib/impure/db_mysql.nim index eb4092f37c..ce48a32edf 100644 --- a/lib/impure/db_mysql.nim +++ b/lib/impure/db_mysql.nim @@ -58,6 +58,7 @@ when false: discard mysql_stmt_close(stmt) proc dbQuote*(s: string): string = + ## DB quotes the string. result = "'" for c in items(s): if c == '\'': add(result, "''") From 2a203e5340654936e3bbe1c0ac392221d15c9aeb Mon Sep 17 00:00:00 2001 From: Milos Negovanovic Date: Tue, 21 Oct 2014 15:56:00 +0100 Subject: [PATCH 4/4] Add comment. --- lib/impure/db_postgres.nim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/impure/db_postgres.nim b/lib/impure/db_postgres.nim index 67e769ed20..510cb8e452 100644 --- a/lib/impure/db_postgres.nim +++ b/lib/impure/db_postgres.nim @@ -48,7 +48,8 @@ proc dbError*(msg: string) {.noreturn.} = e.msg = msg raise e -proc dbQuote(s: string): string = +proc dbQuote*(s: string): string = + ## DB quotes the string. result = "'" for c in items(s): if c == '\'': add(result, "''")