From 434dcd7166361fddd84953d8cdd0de68564096ba Mon Sep 17 00:00:00 2001 From: Milos Negovanovic Date: Wed, 20 Aug 2014 10:24:36 +0100 Subject: Preserve nil <-> NULL between Nimrod and database. --- lib/impure/db_mysql.nim | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'lib/impure') 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) -- cgit 1.4.1-2-gfad0 From a019825d8fa1dc03fa9828263cbaa7495fef1114 Mon Sep 17 00:00:00 2001 From: def Date: Fri, 22 Aug 2014 13:12:49 +0200 Subject: Move fenv to its own module --- lib/impure/fenv.nim | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/pure/math.nim | 54 ------------------------- 2 files changed, 113 insertions(+), 54 deletions(-) create mode 100644 lib/impure/fenv.nim (limited to 'lib/impure') diff --git a/lib/impure/fenv.nim b/lib/impure/fenv.nim new file mode 100644 index 000000000..9d7c8809b --- /dev/null +++ b/lib/impure/fenv.nim @@ -0,0 +1,113 @@ +# +# +# Nimrod's Runtime Library +# (c) Copyright 2014 Andreas Rumpf +# +# See the file "copying.txt", included in this +# distribution, for details about the copyright. +# + +## Floating-point environment. Handling of floating-point rounding and +## exceptions (overflow, zero-devide, etc.). + +{.deadCodeElim:on.} + +when defined(Posix) and not defined(haiku): + {.passl: "-lm".} + +var + FE_DIVBYZERO* {.importc, header: "".}: cint + ## division by zero + FE_INEXACT* {.importc, header: "".}: cint + ## inexact result + FE_INVALID* {.importc, header: "".}: cint + ## invalid operation + FE_OVERFLOW* {.importc, header: "".}: cint + ## result not representable due to overflow + FE_UNDERFLOW* {.importc, header: "".}: cint + ## result not representable due to underflow + FE_ALL_EXCEPT* {.importc, header: "".}: cint + ## bitwise OR of all supported exceptions + FE_DOWNWARD* {.importc, header: "".}: cint + ## round toward -Inf + FE_TONEAREST* {.importc, header: "".}: cint + ## round to nearest + FE_TOWARDZERO* {.importc, header: "".}: cint + ## round toward 0 + FE_UPWARD* {.importc, header: "".}: cint + ## round toward +Inf + FE_DFL_ENV* {.importc, header: "".}: cint + ## macro of type pointer to fenv_t to be used as the argument + ## to functions taking an argument of type fenv_t; in this + ## case the default environment will be used + +type + TFloatClass* = enum ## describes the class a floating point value belongs to. + ## This is the type that is returned by `classify`. + fcNormal, ## value is an ordinary nonzero floating point value + fcSubnormal, ## value is a subnormal (a very small) floating point value + fcZero, ## value is zero + fcNegZero, ## value is the negative zero + fcNan, ## value is Not-A-Number (NAN) + fcInf, ## value is positive infinity + fcNegInf ## value is negative infinity + + Tfenv* {.importc: "fenv_t", header: "", final, pure.} = + object ## Represents the entire floating-point environment. The + ## floating-point environment refers collectively to any + ## floating-point status flags and control modes supported + ## by the implementation. + Tfexcept* {.importc: "fexcept_t", header: "", final, pure.} = + object ## Represents the floating-point status flags collectively, + ## including any status the implementation associates with the + ## flags. A floating-point status flag is a system variable + ## whose value is set (but never cleared) when a floating-point + ## exception is raised, which occurs as a side effect of + ## exceptional floating-point arithmetic to provide auxiliary + ## information. A floating-point control mode is a system variable + ## whose value may be set by the user to affect the subsequent + ## behavior of floating-point arithmetic. + +proc feclearexcept*(excepts: cint): cint {.importc, header: "".} + ## Clear the supported exceptions represented by `excepts`. + +proc fegetexceptflag*(flagp: ptr Tfexcept, excepts: cint): cint {. + importc, header: "".} + ## Store implementation-defined representation of the exception flags + ## indicated by `excepts` in the object pointed to by `flagp`. + +proc feraiseexcept*(excepts: cint): cint {.importc, header: "".} + ## Raise the supported exceptions represented by `excepts`. + +proc fesetexceptflag*(flagp: ptr Tfexcept, excepts: cint): cint {. + importc, header: "".} + ## Set complete status for exceptions indicated by `excepts` according to + ## the representation in the object pointed to by `flagp`. + +proc fetestexcept*(excepts: cint): cint {.importc, header: "".} + ## Determine which of subset of the exceptions specified by `excepts` are + ## currently set. + +proc fegetround*(): cint {.importc, header: "".} + ## Get current rounding direction. + +proc fesetround*(roundingDirection: cint): cint {.importc, header: "".} + ## Establish the rounding direction represented by `roundingDirection`. + +proc fegetenv*(envp: ptr Tfenv): cint {.importc, header: "".} + ## Store the current floating-point environment in the object pointed + ## to by `envp`. + +proc feholdexcept*(envp: ptr Tfenv): cint {.importc, header: "".} + ## Save the current environment in the object pointed to by `envp`, clear + ## exception flags and install a non-stop mode (if available) for all + ## exceptions. + +proc fesetenv*(a1: ptr Tfenv): cint {.importc, header: "".} + ## Establish the floating-point environment represented by the object + ## pointed to by `envp`. + +proc feupdateenv*(envp: ptr Tfenv): cint {.importc, header: "".} + ## Save current exceptions in temporary storage, install environment + ## represented by object pointed to by `envp` and raise exceptions + ## according to saved exceptions. diff --git a/lib/pure/math.nim b/lib/pure/math.nim index cc774539d..116671874 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -40,46 +40,6 @@ const ## after the decimal point ## for Nimrod's ``float`` type. -var - FE_DIVBYZERO* {.importc, header: "".}: cint - FE_INEXACT* {.importc, header: "".}: cint - FE_INVALID* {.importc, header: "".}: cint - FE_OVERFLOW* {.importc, header: "".}: cint - FE_UNDERFLOW* {.importc, header: "".}: cint - FE_ALL_EXCEPT* {.importc, header: "".}: cint - FE_DOWNWARD* {.importc, header: "".}: cint - FE_TONEAREST* {.importc, header: "".}: cint - FE_TOWARDZERO* {.importc, header: "".}: cint - FE_UPWARD* {.importc, header: "".}: cint - FE_DFL_ENV* {.importc, header: "".}: cint - -type - TFloatClass* = enum ## describes the class a floating point value belongs to. - ## This is the type that is returned by `classify`. - fcNormal, ## value is an ordinary nonzero floating point value - fcSubnormal, ## value is a subnormal (a very small) floating point value - fcZero, ## value is zero - fcNegZero, ## value is the negative zero - fcNan, ## value is Not-A-Number (NAN) - fcInf, ## value is positive infinity - fcNegInf ## value is negative infinity - - Tfenv* {.importc: "fenv_t", header: "", final, pure.} = - object ## Represents the entire floating-point environment. The - ## floating-point environment refers collectively to any - ## floating-point status flags and control modes supported - ## by the implementation. - Tfexcept* {.importc: "fexcept_t", header: "", final, pure.} = - object ## Represents the floating-point status flags collectively, - ## including any status the implementation associates with the - ## flags. A floating-point status flag is a system variable - ## whose value is set (but never cleared) when a floating-point - ## exception is raised, which occurs as a side effect of - ## exceptional floating-point arithmetic to provide auxiliary - ## information. A floating-point control mode is a system variable - ## whose value may be set by the user to affect the subsequent - ## behavior of floating-point arithmetic. - proc classify*(x: float): TFloatClass = ## classifies a floating point value. Returns `x`'s class as specified by ## `TFloatClass`. @@ -350,20 +310,6 @@ proc standardDeviation*(s: TRunningStat): float = ## computes the current standard deviation of `s` result = sqrt(variance(s)) -proc feclearexcept*(a1: cint): cint {.importc, header: "".} -proc fegetexceptflag*(a1: ptr Tfexcept, a2: cint): cint {. - importc, header: "".} -proc feraiseexcept*(a1: cint): cint {.importc, header: "".} -proc fesetexceptflag*(a1: ptr Tfexcept, a2: cint): cint {. - importc, header: "".} -proc fetestexcept*(a1: cint): cint {.importc, header: "".} -proc fegetround*(): cint {.importc, header: "".} -proc fesetround*(a1: cint): cint {.importc, header: "".} -proc fegetenv*(a1: ptr Tfenv): cint {.importc, header: "".} -proc feholdexcept*(a1: ptr Tfenv): cint {.importc, header: "".} -proc fesetenv*(a1: ptr Tfenv): cint {.importc, header: "".} -proc feupdateenv*(a1: ptr Tfenv): cint {.importc, header: "".} - {.pop.} {.pop.} -- cgit 1.4.1-2-gfad0 From f59ac26b85302808f135aab5309c7683c4b8f405 Mon Sep 17 00:00:00 2001 From: Milos Negovanovic Date: Fri, 26 Sep 2014 11:23:13 +0100 Subject: Tweaks for postgres driver (not tested yet). --- lib/impure/db_postgres.nim | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'lib/impure') diff --git a/lib/impure/db_postgres.nim b/lib/impure/db_postgres.nim index c375d9191..67e769ed2 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].} = -- cgit 1.4.1-2-gfad0 From 11a2cfb306e50d50eb5c9a35864f5dbbfdabe75f Mon Sep 17 00:00:00 2001 From: Milos Negovanovic Date: Tue, 21 Oct 2014 15:55:02 +0100 Subject: Add comment. --- lib/impure/db_mysql.nim | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/impure') diff --git a/lib/impure/db_mysql.nim b/lib/impure/db_mysql.nim index eb4092f37..ce48a32ed 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, "''") -- cgit 1.4.1-2-gfad0 From 2a203e5340654936e3bbe1c0ac392221d15c9aeb Mon Sep 17 00:00:00 2001 From: Milos Negovanovic Date: Tue, 21 Oct 2014 15:56:00 +0100 Subject: Add comment. --- lib/impure/db_postgres.nim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/impure') diff --git a/lib/impure/db_postgres.nim b/lib/impure/db_postgres.nim index 67e769ed2..510cb8e45 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, "''") -- cgit 1.4.1-2-gfad0