From 81e41dc795f489fd77c94e2b31d37270476a19aa Mon Sep 17 00:00:00 2001 From: JamesP Date: Sun, 6 Sep 2015 22:34:04 +1000 Subject: [PATCH 1/4] Add example (similar to db_mysql) with changes to table definition and transaction to match sqlite SQL syntax --- lib/impure/db_sqlite.nim | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/impure/db_sqlite.nim b/lib/impure/db_sqlite.nim index 66276e9a4..b3b62109c 100644 --- a/lib/impure/db_sqlite.nim +++ b/lib/impure/db_sqlite.nim @@ -9,6 +9,36 @@ ## A higher level `SQLite`:idx: database wrapper. This interface ## is implemented for other databases too. +## +## Example: +## +## .. code-block:: nim +## +## import db_sqlite, math +## +## let theDb = open("mytest.db", nil, nil, nil) +## +## theDb.exec(sql"Drop table if exists myTestTbl") +## theDb.exec(sql("""create table myTestTbl ( +## Id INTEGER PRIMARY KEY, +## Name VARCHAR(50) NOT NULL, +## i INT(11), +## f DECIMAL(18,10))""")) +## +## theDb.exec(sql"BEGIN") +## 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, sqlite3 From 5ae33434f84602c02b4a6ad52ffab140208eabe5 Mon Sep 17 00:00:00 2001 From: JamesP Date: Sun, 6 Sep 2015 22:39:23 +1000 Subject: [PATCH 2/4] fastRows() iterator doco updated (similar to db_mysql) with sqlite specific [Edb] error text --- lib/impure/db_sqlite.nim | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/impure/db_sqlite.nim b/lib/impure/db_sqlite.nim index b3b62109c..53d7570e4 100644 --- a/lib/impure/db_sqlite.nim +++ b/lib/impure/db_sqlite.nim @@ -130,9 +130,13 @@ proc setRow(stmt: Pstmt, r: var Row, cols: cint) = 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 Sqlite it is safe though. + ## Executes the query and iterates over the result dataset. + ## + ## This is very fast, but potentially 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 ``unable to close due to ...``. var stmt = setupQuery(db, query, args) var L = (column_count(stmt)) var result = newRow(L) From 9aa992060292d8d68276a2173c8b7d90eb4e4159 Mon Sep 17 00:00:00 2001 From: JamesP Date: Sun, 6 Sep 2015 22:40:15 +1000 Subject: [PATCH 3/4] instantRows doco added "the" to improve readability --- lib/impure/db_sqlite.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/impure/db_sqlite.nim b/lib/impure/db_sqlite.nim index 53d7570e4..fe605fbfd 100644 --- a/lib/impure/db_sqlite.nim +++ b/lib/impure/db_sqlite.nim @@ -149,7 +149,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. var stmt = setupQuery(db, query, args) while step(stmt) == SQLITE_ROW: yield stmt From 0edb04288befcc656ef9e28da7f1b525fe8a5ea3 Mon Sep 17 00:00:00 2001 From: JamesP Date: Sun, 6 Sep 2015 23:02:28 +1000 Subject: [PATCH 4/4] Clean up a line ending --- lib/impure/db_sqlite.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/impure/db_sqlite.nim b/lib/impure/db_sqlite.nim index fe605fbfd..8366fdadc 100644 --- a/lib/impure/db_sqlite.nim +++ b/lib/impure/db_sqlite.nim @@ -130,7 +130,7 @@ proc setRow(stmt: Pstmt, r: var Row, cols: cint) = iterator fastRows*(db: DbConn, query: SqlQuery, args: varargs[string, `$`]): Row {.tags: [FReadDb].} = - ## Executes the query and iterates over the result dataset. + ## Executes the query and iterates over the result dataset. ## ## This is very fast, but potentially dangerous. Use this iterator only ## if you require **ALL** the rows.