Merge pull request #1497 from milosn/devel

Preserve nil <-> NULL between Nimrod and database.
This commit is contained in:
Varriount 2014-10-24 21:07:54 -04:00
commit d482051710
2 changed files with 29 additions and 8 deletions

View file

@ -57,7 +57,8 @@ when false:
binding: seq[MYSQL_BIND] binding: seq[MYSQL_BIND]
discard mysql_stmt_close(stmt) discard mysql_stmt_close(stmt)
proc dbQuote(s: string): string = proc dbQuote*(s: string): string =
## DB quotes the string.
result = "'" result = "'"
for c in items(s): for c in items(s):
if c == '\'': add(result, "''") if c == '\'': add(result, "''")
@ -69,7 +70,10 @@ proc dbFormat(formatstr: TSqlQuery, args: varargs[string]): string =
var a = 0 var a = 0
for c in items(string(formatstr)): for c in items(string(formatstr)):
if c == '?': if c == '?':
add(result, dbQuote(args[a])) if args[a] == nil:
add(result, "NULL")
else:
add(result, dbQuote(args[a]))
inc(a) inc(a)
else: else:
add(result, c) add(result, c)
@ -115,7 +119,10 @@ iterator fastRows*(db: TDbConn, query: TSqlQuery,
if row == nil: break if row == nil: break
for i in 0..L-1: for i in 0..L-1:
setLen(result[i], 0) setLen(result[i], 0)
add(result[i], row[i]) if row[i] == nil:
result[i] = nil
else:
add(result[i], row[i])
yield result yield result
properFreeResult(sqlres, row) properFreeResult(sqlres, row)
@ -132,7 +139,10 @@ proc getRow*(db: TDbConn, query: TSqlQuery,
if row != nil: if row != nil:
for i in 0..L-1: for i in 0..L-1:
setLen(result[i], 0) 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) properFreeResult(sqlres, row)
proc getAllRows*(db: TDbConn, query: TSqlQuery, proc getAllRows*(db: TDbConn, query: TSqlQuery,
@ -150,7 +160,11 @@ proc getAllRows*(db: TDbConn, query: TSqlQuery,
if row == nil: break if row == nil: break
setLen(result, j+1) setLen(result, j+1)
newSeq(result[j], L) 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) inc(j)
mysql.FreeResult(sqlres) mysql.FreeResult(sqlres)

View file

@ -48,7 +48,8 @@ proc dbError*(msg: string) {.noreturn.} =
e.msg = msg e.msg = msg
raise e raise e
proc dbQuote(s: string): string = proc dbQuote*(s: string): string =
## DB quotes the string.
result = "'" result = "'"
for c in items(s): for c in items(s):
if c == '\'': add(result, "''") if c == '\'': add(result, "''")
@ -60,7 +61,10 @@ proc dbFormat(formatstr: TSqlQuery, args: varargs[string]): string =
var a = 0 var a = 0
for c in items(string(formatstr)): for c in items(string(formatstr)):
if c == '?': if c == '?':
add(result, dbQuote(args[a])) if args[a] == nil:
add(result, "NULL")
else:
add(result, dbQuote(args[a]))
inc(a) inc(a)
else: else:
add(result, c) add(result, c)
@ -124,7 +128,10 @@ proc setRow(res: PPGresult, r: var TRow, line, cols: int32) =
for col in 0..cols-1: for col in 0..cols-1:
setLen(r[col], 0) setLen(r[col], 0)
var x = PQgetvalue(res, line, col) 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, iterator fastRows*(db: TDbConn, query: TSqlQuery,
args: varargs[string, `$`]): TRow {.tags: [FReadDB].} = args: varargs[string, `$`]): TRow {.tags: [FReadDB].} =