summary refs log tree commit diff stats
path: root/lib/impure/db_sqlite.nim
diff options
context:
space:
mode:
authorKeMeGe <chaoskmg@gmail.com>2015-03-16 13:53:38 +0800
committerKeMeGe <chaoskmg@gmail.com>2015-03-16 13:53:38 +0800
commit171d51a08c62b069edf389d4e7ead097dd3dfb32 (patch)
treecaa7dec6d0be4492dcb219a7463147505a90a010 /lib/impure/db_sqlite.nim
parentca8102b96d086bcf95d8859eafe4021f334b5542 (diff)
downloadNim-171d51a08c62b069edf389d4e7ead097dd3dfb32.tar.gz
move database encoding options to setEncoding(), leave open() as it is
Diffstat (limited to 'lib/impure/db_sqlite.nim')
-rw-r--r--lib/impure/db_sqlite.nim19
1 files changed, 15 insertions, 4 deletions
diff --git a/lib/impure/db_sqlite.nim b/lib/impure/db_sqlite.nim
index 6873e35f7..4be692f39 100644
--- a/lib/impure/db_sqlite.nim
+++ b/lib/impure/db_sqlite.nim
@@ -183,17 +183,28 @@ proc close*(db: TDbConn) {.tags: [FDb].} =
   ## closes the database connection.
   if sqlite3.close(db) != SQLITE_OK: dbError(db)
     
-proc open*(connection, user, password, database: string, 
-           charset: string = "UTF-8"): TDbConn {.tags: [FDb].} =
+proc open*(connection, user, password, database: string): TDbConn {.
+  tags: [FDb].} =
   ## opens a database connection. Raises `EDb` if the connection could not
   ## be established. Only the ``connection`` parameter is used for ``sqlite``.
   var db: TDbConn
   if sqlite3.open(connection, db) == SQLITE_OK:
     result = db
-    exec(result, sql"PRAGMA encoding = ?", [charset])
   else:
     dbError(db)
-   
+
+proc setEncoding*(connection: TDbConn, encoding: string): bool {.
+  tags: [FDb].} =
+  ## sets the encoding of a database connection, returns true for 
+  ## success, false for failure.
+  ##
+  ## Note that the encoding cannot be changed once it's been set.
+  ## According to SQLite3 documentation, any attempt to change 
+  ## the encoding after the database is created will be silently 
+  ## ignored.
+  exec(connection, sql"PRAGMA encoding = ?", [encoding])
+  result = connection.getValue(sql"PRAGMA encoding") == encoding
+
 when isMainModule:
   var db = open("db.sql", "", "", "")
   exec(db, sql"create table tbl1(one varchar(10), two smallint)", [])