summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rwxr-xr-xlib/impure/db_mysql.nim23
-rwxr-xr-xlib/impure/db_postgres.nim20
-rwxr-xr-xlib/impure/db_sqlite.nim20
3 files changed, 32 insertions, 31 deletions
diff --git a/lib/impure/db_mysql.nim b/lib/impure/db_mysql.nim
index 2b63137c8..6284b7995 100755
--- a/lib/impure/db_mysql.nim
+++ b/lib/impure/db_mysql.nim
@@ -34,7 +34,7 @@ proc dbError*(msg: string) {.noreturn.} =
   raise e
 
 when false:
-  proc dbQueryOpt*(db: TDbConn, query: string, args: varargs[string]) =
+  proc dbQueryOpt*(db: TDbConn, query: string, args: varargs[string, `$`]) =
     var stmt = mysql_stmt_init(db)
     if stmt == nil: dbError(db)
     if mysql_stmt_prepare(stmt, query, len(query)) != 0: 
@@ -60,12 +60,12 @@ proc dbFormat(formatstr: TSqlQuery, args: varargs[string]): string =
     else: 
       add(result, c)
   
-proc TryExec*(db: TDbConn, query: TSqlQuery, args: varargs[string]): bool =
+proc TryExec*(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]): bool =
   ## tries to execute the query and returns true if successful, false otherwise.
   var q = dbFormat(query, args)
   return mysql.RealQuery(db, q, q.len) == 0'i32
 
-proc Exec*(db: TDbConn, query: TSqlQuery, args: varargs[string]) =
+proc Exec*(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]) =
   ## executes the query and raises EDB if not successful.
   var q = dbFormat(query, args)
   if mysql.RealQuery(db, q, q.len) != 0'i32: dbError(db)
@@ -80,7 +80,7 @@ proc properFreeResult(sqlres: mysql.PRES, row: cstringArray) =
   mysql.FreeResult(sqlres)
   
 iterator FastRows*(db: TDbConn, query: TSqlQuery,
-                   args: varargs[string]): TRow =
+                   args: varargs[string, `$`]): TRow =
   ## executes the query and iterates over the result dataset. This is very 
   ## fast, but potenially dangerous: If the for-loop-body executes another
   ## query, the results can be undefined. For MySQL this is the case!.
@@ -100,7 +100,7 @@ iterator FastRows*(db: TDbConn, query: TSqlQuery,
     properFreeResult(sqlres, row)
 
 proc getRow*(db: TDbConn, query: TSqlQuery,
-             args: varargs[string]): TRow =
+             args: varargs[string, `$`]): TRow =
   ## retrieves a single row.
   Exec(db, query, args)
   var sqlres = mysql.UseResult(db)
@@ -115,7 +115,7 @@ proc getRow*(db: TDbConn, query: TSqlQuery,
     properFreeResult(sqlres, row)
 
 proc GetAllRows*(db: TDbConn, query: TSqlQuery, 
-                 args: varargs[string]): seq[TRow] =
+                 args: varargs[string, `$`]): seq[TRow] =
   ## executes the query and returns the whole result dataset.
   result = @[]
   Exec(db, query, args)
@@ -134,12 +134,12 @@ proc GetAllRows*(db: TDbConn, query: TSqlQuery,
     mysql.FreeResult(sqlres)
 
 iterator Rows*(db: TDbConn, query: TSqlQuery, 
-               args: varargs[string]): TRow =
+               args: varargs[string, `$`]): TRow =
   ## same as `FastRows`, but slower and safe.
   for r in items(GetAllRows(db, query, args)): yield r
 
 proc GetValue*(db: TDbConn, query: TSqlQuery, 
-               args: varargs[string]): string = 
+               args: varargs[string, `$`]): string = 
   ## executes the query and returns the result dataset's the first column 
   ## of the first row. Returns "" if the dataset contains no rows. This uses
   ## `FastRows`, so it inherits its fragile behaviour.
@@ -149,7 +149,7 @@ proc GetValue*(db: TDbConn, query: TSqlQuery,
     break
 
 proc TryInsertID*(db: TDbConn, query: TSqlQuery, 
-                  args: varargs[string]): int64 =
+                  args: varargs[string, `$`]): int64 =
   ## executes the query (typically "INSERT") and returns the 
   ## generated ID for the row or -1 in case of an error.
   var q = dbFormat(query, args)
@@ -158,14 +158,15 @@ proc TryInsertID*(db: TDbConn, query: TSqlQuery,
   else:
     result = mysql.InsertId(db)
   
-proc InsertID*(db: TDbConn, query: TSqlQuery, args: varargs[string]): int64 = 
+proc InsertID*(db: TDbConn, query: TSqlQuery, 
+               args: varargs[string, `$`]): int64 = 
   ## executes the query (typically "INSERT") and returns the 
   ## generated ID for the row.
   result = TryInsertID(db, query, args)
   if result < 0: dbError(db)
 
 proc ExecAffectedRows*(db: TDbConn, query: TSqlQuery, 
-                       args: varargs[string]): int64 = 
+                       args: varargs[string, `$`]): int64 = 
   ## runs the query (typically "UPDATE") and returns the
   ## number of affected rows
   Exec(db, query, args)
diff --git a/lib/impure/db_postgres.nim b/lib/impure/db_postgres.nim
index 46909b9b2..cb8da75b8 100755
--- a/lib/impure/db_postgres.nim
+++ b/lib/impure/db_postgres.nim
@@ -60,14 +60,14 @@ proc dbFormat(formatstr: TSqlQuery, args: varargs[string]): string =
       add(result, c)
   
 proc TryExec*(db: TDbConn, query: TSqlQuery, 
-              args: varargs[string]): bool =
+              args: varargs[string, `$`]): bool =
   ## tries to execute the query and returns true if successful, false otherwise.
   var q = dbFormat(query, args)
   var res = PQExec(db, q)
   result = PQresultStatus(res) == PGRES_COMMAND_OK
   PQclear(res)
 
-proc Exec*(db: TDbConn, query: TSqlQuery, args: varargs[string]) =
+proc Exec*(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]) =
   ## executes the query and raises EDB if not successful.
   var q = dbFormat(query, args)
   var res = PQExec(db, q)
@@ -91,7 +91,7 @@ proc setRow(res: PPGresult, r: var TRow, line, cols: int32) =
     add(r[col], x)
   
 iterator FastRows*(db: TDbConn, query: TSqlQuery,
-                   args: varargs[string]): TRow =
+                   args: varargs[string, `$`]): TRow =
   ## executes the query and iterates over the result dataset. This is very 
   ## fast, but potenially dangerous: If the for-loop-body executes another
   ## query, the results can be undefined. For Postgres it is safe though.
@@ -104,7 +104,7 @@ iterator FastRows*(db: TDbConn, query: TSqlQuery,
   PQclear(res)
 
 proc getRow*(db: TDbConn, query: TSqlQuery,
-             args: varargs[string]): TRow =
+             args: varargs[string, `$`]): TRow =
   ## retrieves a single row.
   var res = setupQuery(db, query, args)
   var L = PQnfields(res)
@@ -113,26 +113,26 @@ proc getRow*(db: TDbConn, query: TSqlQuery,
   PQclear(res)
 
 proc GetAllRows*(db: TDbConn, query: TSqlQuery, 
-                 args: varargs[string]): seq[TRow] =
+                 args: varargs[string, `$`]): seq[TRow] =
   ## executes the query and returns the whole result dataset.
   result = @[]
   for r in FastRows(db, query, args):
     result.add(r)
 
 iterator Rows*(db: TDbConn, query: TSqlQuery, 
-               args: varargs[string]): TRow =
+               args: varargs[string, `$`]): TRow =
   ## same as `FastRows`, but slower and safe.
   for r in items(GetAllRows(db, query, args)): yield r
 
 proc GetValue*(db: TDbConn, query: TSqlQuery, 
-               args: varargs[string]): string = 
+               args: varargs[string, `$`]): string = 
   ## executes the query and returns the result dataset's the first column 
   ## of the first row. Returns "" if the dataset contains no rows.
   var x = PQgetvalue(setupQuery(db, query, args), 0, 0)
   result = if isNil(x): "" else: $x
   
 proc TryInsertID*(db: TDbConn, query: TSqlQuery, 
-                  args: varargs[string]): int64 =
+                  args: varargs[string, `$`]): int64 =
   ## executes the query (typically "INSERT") and returns the 
   ## generated ID for the row or -1 in case of an error. For Postgre this adds
   ## ``RETURNING id`` to the query, so it only works if your primary key is
@@ -144,7 +144,7 @@ proc TryInsertID*(db: TDbConn, query: TSqlQuery,
     result = -1
 
 proc InsertID*(db: TDbConn, query: TSqlQuery, 
-               args: varargs[string]): int64 = 
+               args: varargs[string, `$`]): int64 = 
   ## executes the query (typically "INSERT") and returns the 
   ## generated ID for the row. For Postgre this adds
   ## ``RETURNING id`` to the query, so it only works if your primary key is
@@ -153,7 +153,7 @@ proc InsertID*(db: TDbConn, query: TSqlQuery,
   if result < 0: dbError(db)
   
 proc ExecAffectedRows*(db: TDbConn, query: TSqlQuery, 
-                       args: varargs[string]): int64 = 
+                       args: varargs[string, `$`]): int64 = 
   ## executes the query (typically "UPDATE") and returns the
   ## number of affected rows.
   var q = dbFormat(query, args)
diff --git a/lib/impure/db_sqlite.nim b/lib/impure/db_sqlite.nim
index d3ff3c0d2..4abc2ca97 100755
--- a/lib/impure/db_sqlite.nim
+++ b/lib/impure/db_sqlite.nim
@@ -60,7 +60,7 @@ proc dbFormat(formatstr: TSqlQuery, args: varargs[string]): string =
       add(result, c)
   
 proc TryExec*(db: TDbConn, query: TSqlQuery, 
-              args: varargs[string]): bool =
+              args: varargs[string, `$`]): bool =
   ## tries to execute the query and returns true if successful, false otherwise.
   var q = dbFormat(query, args)
   var stmt: sqlite3.PStmt
@@ -68,7 +68,7 @@ proc TryExec*(db: TDbConn, query: TSqlQuery,
     if step(stmt) == SQLITE_DONE:
       result = finalize(stmt) == SQLITE_OK
 
-proc Exec*(db: TDbConn, query: TSqlQuery, args: varargs[string]) =
+proc Exec*(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]) =
   ## executes the query and raises EDB if not successful.
   if not TryExec(db, query, args): dbError(db)
   
@@ -89,7 +89,7 @@ proc setRow(stmt: PStmt, r: var TRow, cols: cint) =
     if not isNil(x): add(r[col], x)
   
 iterator FastRows*(db: TDbConn, query: TSqlQuery,
-                   args: varargs[string]): TRow =
+                   args: varargs[string, `$`]): TRow =
   ## executes the query and iterates over the result dataset. This is very 
   ## fast, but potenially dangerous: If the for-loop-body executes another
   ## query, the results can be undefined. For Sqlite it is safe though.
@@ -102,7 +102,7 @@ iterator FastRows*(db: TDbConn, query: TSqlQuery,
   if finalize(stmt) != SQLITE_OK: dbError(db)
 
 proc getRow*(db: TDbConn, query: TSqlQuery,
-             args: varargs[string]): TRow =
+             args: varargs[string, `$`]): TRow =
   ## retrieves a single row.
   var stmt = setupQuery(db, query, args)
   var L = (columnCount(stmt))
@@ -112,19 +112,19 @@ proc getRow*(db: TDbConn, query: TSqlQuery,
   if finalize(stmt) != SQLITE_OK: dbError(db)
 
 proc GetAllRows*(db: TDbConn, query: TSqlQuery, 
-                 args: varargs[string]): seq[TRow] =
+                 args: varargs[string, `$`]): seq[TRow] =
   ## executes the query and returns the whole result dataset.
   result = @[]
   for r in FastRows(db, query, args):
     result.add(r)
 
 iterator Rows*(db: TDbConn, query: TSqlQuery, 
-               args: varargs[string]): TRow =
+               args: varargs[string, `$`]): TRow =
   ## same as `FastRows`, but slower and safe.
   for r in FastRows(db, query, args): yield r
 
 proc GetValue*(db: TDbConn, query: TSqlQuery, 
-               args: varargs[string]): string = 
+               args: varargs[string, `$`]): string = 
   ## executes the query and returns the result dataset's the first column 
   ## of the first row. Returns "" if the dataset contains no rows.
   var stmt = setupQuery(db, query, args)
@@ -140,7 +140,7 @@ proc GetValue*(db: TDbConn, query: TSqlQuery,
     result = ""
   
 proc TryInsertID*(db: TDbConn, query: TSqlQuery, 
-                  args: varargs[string]): int64 =
+                  args: varargs[string, `$`]): int64 =
   ## executes the query (typically "INSERT") and returns the 
   ## generated ID for the row or -1 in case of an error. 
   if tryExec(db, query, args): 
@@ -149,7 +149,7 @@ proc TryInsertID*(db: TDbConn, query: TSqlQuery,
     result = -1
 
 proc InsertID*(db: TDbConn, query: TSqlQuery, 
-               args: varargs[string]): int64 = 
+               args: varargs[string, `$`]): int64 = 
   ## executes the query (typically "INSERT") and returns the 
   ## generated ID for the row. For Postgre this adds
   ## ``RETURNING id`` to the query, so it only works if your primary key is
@@ -158,7 +158,7 @@ proc InsertID*(db: TDbConn, query: TSqlQuery,
   if result < 0: dbError(db)
   
 proc ExecAffectedRows*(db: TDbConn, query: TSqlQuery, 
-                       args: varargs[string]): int64 = 
+                       args: varargs[string, `$`]): int64 = 
   ## executes the query (typically "UPDATE") and returns the
   ## number of affected rows.
   Exec(db, query, args)
1 +0200 committer Araq <rumpf_a@web.de> 2011-10-18 17:21:51 +0200 much more efficient rod file generation' href='/ahoang/Nim/commit/compiler/rodutils.nim?h=devel&id=4de84024e5e1b91fcd66d4f093cec4d1a985194a'>4de84024e ^
7ebaf4489 ^


d68181246 ^
7ebaf4489 ^

d68181246 ^

4de84024e ^
7ebaf4489 ^
4de84024e ^




d68181246 ^
4de84024e ^


d68181246 ^
4de84024e ^






7ebaf4489 ^
4de84024e ^

d68181246 ^
4de84024e ^

92b8fac94 ^
4de84024e ^












1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

 
                            
                                         




                                                   
                                            
                     
 
                                                                                                                      
 
                                                                       

                  
                  


                                    
                                   



                   
       







                                                                                                
 
                                                
                                  



                                                           
                                    



                                                          
               



                                                   
             
             
            




                          
                                         

                       
               




                                                                          
                                                                          

                                                                        


               
                              


                     
             


                     
       






                               



                                                              

                                                            


                                              
                                                

                                                     

                                              
                                                     
                                       




                                                                     
                 


            
             






                                                                     
                                     

         
                                                 

                 
                                                               












                                              
#
#
#           The Nim Compiler
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## Serialization utilities for the compiler.
import strutils, math

proc c_snprintf(s: cstring; n:uint; frmt: cstring): cint {.importc: "snprintf", header: "<stdio.h>", nodecl, varargs.}

proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string =
  case classify(f)
  of fcNaN:
    result = "NAN"
  of fcNegZero:
    result = "-0.0" & literalPostfix
  of fcZero:
    result = "0.0" & literalPostfix
  of fcInf:
    result = "INF"
  of fcNegInf:
    result = "-INF"
  else:
    when defined(nimNoArrayToCstringConversion):
      result = newString(81)
      let n = c_snprintf(result.cstring, result.len.uint, "%#.16e%s", f, literalPostfix.cstring)
      setLen(result, n)
    else:
      var buf: array[0..80, char]
      discard c_snprintf(buf.cstring, buf.len.uint, "%#.16e%s", f, literalPostfix.cstring)
      result = $buf.cstring

proc encodeStr*(s: string, result: var string) =
  for i in countup(0, len(s) - 1):
    case s[i]
    of 'a'..'z', 'A'..'Z', '0'..'9', '_': add(result, s[i])
    else: add(result, '\\' & toHex(ord(s[i]), 2))

proc hexChar(c: char, xi: var int) =
  case c
  of '0'..'9': xi = (xi shl 4) or (ord(c) - ord('0'))
  of 'a'..'f': xi = (xi shl 4) or (ord(c) - ord('a') + 10)
  of 'A'..'F': xi = (xi shl 4) or (ord(c) - ord('A') + 10)
  else: discard

proc decodeStr*(s: cstring, pos: var int): string =
  var i = pos
  result = ""
  while true:
    case s[i]
    of '\\':
      inc(i, 3)
      var xi = 0
      hexChar(s[i-2], xi)
      hexChar(s[i-1], xi)
      add(result, chr(xi))
    of 'a'..'z', 'A'..'Z', '0'..'9', '_':
      add(result, s[i])
      inc(i)
    else: break
  pos = i

const
  chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

# since negative numbers require a leading '-' they use up 1 byte. Thus we
# subtract/add `vintDelta` here to save space for small negative numbers
# which are common in ROD files:
const
  vintDelta = 5

template encodeIntImpl(self) =
  var d: char
  var v = x
  var rem = v mod 190
  if rem < 0:
    add(result, '-')
    v = - (v div 190)
    rem = - rem
  else:
    v = v div 190
  var idx = int(rem)
  if idx < 62: d = chars[idx]
  else: d = chr(idx - 62 + 128)
  if v != 0: self(v, result)
  add(result, d)

proc encodeVBiggestIntAux(x: BiggestInt, result: var string) =
  ## encode a biggest int as a variable length base 190 int.
  encodeIntImpl(encodeVBiggestIntAux)

proc encodeVBiggestInt*(x: BiggestInt, result: var string) =
  ## encode a biggest int as a variable length base 190 int.
  encodeVBiggestIntAux(x +% vintDelta, result)
  #  encodeIntImpl(encodeVBiggestInt)

proc encodeVIntAux(x: int, result: var string) =
  ## encode an int as a variable length base 190 int.
  encodeIntImpl(encodeVIntAux)

proc encodeVInt*(x: int, result: var string) =
  ## encode an int as a variable length base 190 int.
  encodeVIntAux(x +% vintDelta, result)

template decodeIntImpl() =
  var i = pos
  var sign = - 1
  assert(s[i] in {'a'..'z', 'A'..'Z', '0'..'9', '-', '\x80'..'\xFF'})
  if s[i] == '-':
    inc(i)
    sign = 1
  result = 0
  while true:
    case s[i]
    of '0'..'9': result = result * 190 - (ord(s[i]) - ord('0'))
    of 'a'..'z': result = result * 190 - (ord(s[i]) - ord('a') + 10)
    of 'A'..'Z': result = result * 190 - (ord(s[i]) - ord('A') + 36)
    of '\x80'..'\xFF': result = result * 190 - (ord(s[i]) - 128 + 62)
    else: break
    inc(i)
  result = result * sign -% vintDelta
  pos = i

proc decodeVInt*(s: cstring, pos: var int): int =
  decodeIntImpl()

proc decodeVBiggestInt*(s: cstring, pos: var int): BiggestInt =
  decodeIntImpl()

iterator decodeVIntArray*(s: cstring): int =
  var i = 0
  while s[i] != '\0':
    yield decodeVInt(s, i)
    if s[i] == ' ': inc i

iterator decodeStrArray*(s: cstring): string =
  var i = 0
  while s[i] != '\0':
    yield decodeStr(s, i)
    if s[i] == ' ': inc i