summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-02-10 16:41:38 +0100
committerAndreas Rumpf <rumpf_a@web.de>2018-02-10 16:41:48 +0100
commit94141b8f68322a44f309d44d885a6e506a61b405 (patch)
tree5261a59862d4591dc63592c165e54c34fd264b50 /lib
parent45437eb113c10c6a1d57374b112e720377a38dc3 (diff)
downloadNim-94141b8f68322a44f309d44d885a6e506a61b405.tar.gz
fixes #7200
Diffstat (limited to 'lib')
-rw-r--r--lib/impure/db_mysql.nim41
1 files changed, 21 insertions, 20 deletions
diff --git a/lib/impure/db_mysql.nim b/lib/impure/db_mysql.nim
index 1b79b3543..3b461d2f6 100644
--- a/lib/impure/db_mysql.nim
+++ b/lib/impure/db_mysql.nim
@@ -89,7 +89,7 @@ import db_common
 export db_common
 
 type
-  DbConn* = PMySQL     ## encapsulates a database connection
+  DbConn* = distinct PMySQL ## encapsulates a database connection
   Row* = seq[string]   ## a row of a dataset. NULL database values will be
                        ## converted to nil.
   InstantRow* = object ## a handle that can be used to get a row's
@@ -102,7 +102,7 @@ proc dbError*(db: DbConn) {.noreturn.} =
   ## raises a DbError exception.
   var e: ref DbError
   new(e)
-  e.msg = $mysql.error(db)
+  e.msg = $mysql.error(PMySQL db)
   raise e
 
 when false:
@@ -140,17 +140,17 @@ proc tryExec*(db: DbConn, query: SqlQuery, args: varargs[string, `$`]): bool {.
   tags: [ReadDbEffect, WriteDbEffect].} =
   ## 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
+  return mysql.realQuery(PMySQL db, q, q.len) == 0'i32
 
 proc rawExec(db: DbConn, query: SqlQuery, args: varargs[string, `$`]) =
   var q = dbFormat(query, args)
-  if mysql.realQuery(db, q, q.len) != 0'i32: dbError(db)
+  if mysql.realQuery(PMySQL db, q, q.len) != 0'i32: dbError(db)
 
 proc exec*(db: DbConn, query: SqlQuery, args: varargs[string, `$`]) {.
   tags: [ReadDbEffect, WriteDbEffect].} =
   ## 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)
+  if mysql.realQuery(PMySQL db, q, q.len) != 0'i32: dbError(db)
 
 proc newRow(L: int): Row =
   newSeq(result, L)
@@ -171,7 +171,7 @@ iterator fastRows*(db: DbConn, query: SqlQuery,
   ## Breaking the fastRows() iterator during a loop will cause the next
   ## database query to raise an [EDb] exception ``Commands out of sync``.
   rawExec(db, query, args)
-  var sqlres = mysql.useResult(db)
+  var sqlres = mysql.useResult(PMySQL db)
   if sqlres != nil:
     var
       L = int(mysql.numFields(sqlres))
@@ -210,7 +210,7 @@ iterator instantRows*(db: DbConn, query: SqlQuery,
   ## Same as fastRows but returns a handle that can be used to get column text
   ## on demand using []. Returned handle is valid only within the iterator body.
   rawExec(db, query, args)
-  var sqlres = mysql.useResult(db)
+  var sqlres = mysql.useResult(PMySQL db)
   if sqlres != nil:
     let L = int(mysql.numFields(sqlres))
     var row: cstringArray
@@ -290,7 +290,7 @@ iterator instantRows*(db: DbConn; columns: var DbColumns; query: SqlQuery;
   ## Same as fastRows but returns a handle that can be used to get column text
   ## on demand using []. Returned handle is valid only within the iterator body.
   rawExec(db, query, args)
-  var sqlres = mysql.useResult(db)
+  var sqlres = mysql.useResult(PMySQL db)
   if sqlres != nil:
     let L = int(mysql.numFields(sqlres))
     setColumnInfo(columns, sqlres, L)
@@ -315,7 +315,7 @@ proc getRow*(db: DbConn, query: SqlQuery,
   ## Retrieves a single row. If the query doesn't return any rows, this proc
   ## will return a Row with empty strings for each column.
   rawExec(db, query, args)
-  var sqlres = mysql.useResult(db)
+  var sqlres = mysql.useResult(PMySQL db)
   if sqlres != nil:
     var L = int(mysql.numFields(sqlres))
     result = newRow(L)
@@ -334,7 +334,7 @@ proc getAllRows*(db: DbConn, query: SqlQuery,
   ## executes the query and returns the whole result dataset.
   result = @[]
   rawExec(db, query, args)
-  var sqlres = mysql.useResult(db)
+  var sqlres = mysql.useResult(PMySQL db)
   if sqlres != nil:
     var L = int(mysql.numFields(sqlres))
     var row: cstringArray
@@ -369,10 +369,10 @@ proc tryInsertId*(db: DbConn, query: SqlQuery,
   ## 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)
-  if mysql.realQuery(db, q, q.len) != 0'i32:
+  if mysql.realQuery(PMySQL db, q, q.len) != 0'i32:
     result = -1'i64
   else:
-    result = mysql.insertId(db)
+    result = mysql.insertId(PMySQL db)
 
 proc insertId*(db: DbConn, query: SqlQuery,
                args: varargs[string, `$`]): int64 {.tags: [WriteDbEffect].} =
@@ -387,32 +387,33 @@ proc execAffectedRows*(db: DbConn, query: SqlQuery,
   ## runs the query (typically "UPDATE") and returns the
   ## number of affected rows
   rawExec(db, query, args)
-  result = mysql.affectedRows(db)
+  result = mysql.affectedRows(PMySQL db)
 
 proc close*(db: DbConn) {.tags: [DbEffect].} =
   ## closes the database connection.
-  if db != nil: mysql.close(db)
+  if PMySQL(db) != nil: mysql.close(PMySQL db)
 
 proc open*(connection, user, password, database: string): DbConn {.
   tags: [DbEffect].} =
   ## opens a database connection. Raises `EDb` if the connection could not
   ## be established.
-  result = mysql.init(nil)
-  if result == nil: dbError("could not open database connection")
+  var res = mysql.init(nil)
+  if res == nil: dbError("could not open database connection")
   let
     colonPos = connection.find(':')
     host = if colonPos < 0: connection
            else: substr(connection, 0, colonPos-1)
     port: int32 = if colonPos < 0: 0'i32
                   else: substr(connection, colonPos+1).parseInt.int32
-  if mysql.realConnect(result, host, user, password, database,
+  if mysql.realConnect(res, host, user, password, database,
                        port, nil, 0) == nil:
-    var errmsg = $mysql.error(result)
-    db_mysql.close(result)
+    var errmsg = $mysql.error(res)
+    mysql.close(res)
     dbError(errmsg)
+  result = DbConn(res)
 
 proc setEncoding*(connection: DbConn, encoding: string): bool {.
   tags: [DbEffect].} =
   ## sets the encoding of a database connection, returns true for
   ## success, false for failure.
-  result = mysql.set_character_set(connection, encoding) == 0
+  result = mysql.set_character_set(PMySQL connection, encoding) == 0