summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2015-09-05 21:14:37 +0100
committerDominik Picheta <dominikpicheta@googlemail.com>2015-09-05 21:14:37 +0100
commit14a233dc75593697c4fb38f2f1b5b3bf89786f8a (patch)
tree518b65a9a07e1ba83fe4dc9a08dc0bb9ef6a85e7
parent30b1baf1aff3ecdd69c2c2899e1557d34803d4f6 (diff)
parent22d56fc494519769724fa4301f5d17d77c9a43e4 (diff)
downloadNim-14a233dc75593697c4fb38f2f1b5b3bf89786f8a.tar.gz
Merge branch 'db_mysqlExamples' of https://github.com/jlp765/Nim into jlp765-db_mysqlExamples
Conflicts:
	lib/impure/db_mysql.nim
-rw-r--r--lib/impure/db_mysql.nim43
1 files changed, 39 insertions, 4 deletions
diff --git a/lib/impure/db_mysql.nim b/lib/impure/db_mysql.nim
index d61d951ba..9a03f9783 100644
--- a/lib/impure/db_mysql.nim
+++ b/lib/impure/db_mysql.nim
@@ -9,6 +9,37 @@
 
 ## A higher level `mySQL`:idx: database wrapper. The same interface is
 ## implemented for other databases too.
+##
+## Example:
+##
+## .. code-block:: Nim
+##
+##  import db_mysql, math
+##
+##  let theDb = open("localhost", "nim", "nim", "test")
+##
+##  theDb.exec(sql"Drop table if exists myTestTbl")
+##  theDb.exec(sql("create table myTestTbl (" &
+##      " Id    INT(11)     NOT NULL AUTO_INCREMENT PRIMARY KEY, " &
+##      " Name  VARCHAR(50) NOT NULL, " &
+##      " i     INT(11), " &
+##      " f     DECIMAL(18,10))"))
+##
+##  theDb.exec(sql"START TRANSACTION")
+##  for i in 1..1000:
+##    theDb.exec(sql"INSERT INTO myTestTbl (name,i,f) VALUES (?,?,?)",
+##          "Item#" & $i, i, sqrt(i.float))
+##  theDb.exec(sql"COMMIT")
+##
+##  for x in theDb.fastRows(sql"select * from myTestTbl"):
+##    echo x
+##
+##  let id = theDb.tryInsertId(sql"INSERT INTO myTestTbl (name,i,f) VALUES (?,?,?)",
+##          "Item#1001", 1001, sqrt(1001.0))
+##  echo "Inserted item: ", theDb.getValue(sql"SELECT name FROM myTestTbl WHERE id=?", id)
+##
+##  theDb.close()
+
 
 import strutils, mysql
 
@@ -109,9 +140,13 @@ proc properFreeResult(sqlres: mysql.PRES, row: cstringArray) =
 
 iterator fastRows*(db: DbConn, query: SqlQuery,
                    args: varargs[string, `$`]): Row {.tags: [FReadDB].} =
-  ## 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!.
+  ## executes the query and iterates over the result dataset.
+  ##
+  ## This is very fast, but potenially dangerous.  Use this iterator only
+  ## if you require ALL the rows.
+  ##
+  ## 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)
   if sqlres != nil:
@@ -134,7 +169,7 @@ iterator instantRows*(db: DbConn, query: SqlQuery,
                       args: varargs[string, `$`]): InstantRow
                       {.tags: [FReadDb].} =
   ## same as fastRows but returns a handle that can be used to get column text
-  ## on demand using []. Returned handle is valid only within interator body.
+  ## on demand using []. Returned handle is valid only within the interator body.
   rawExec(db, query, args)
   var sqlres = mysql.useResult(db)
   if sqlres != nil: