From ca8102b96d086bcf95d8859eafe4021f334b5542 Mon Sep 17 00:00:00 2001 From: KeMeGe Date: Sat, 14 Mar 2015 15:21:38 +0800 Subject: [PATCH 1/2] Add character set options when opening DB connection --- lib/impure/db_mysql.nim | 8 ++++++-- lib/impure/db_postgres.nim | 5 +++-- lib/impure/db_sqlite.nim | 5 +++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/impure/db_mysql.nim b/lib/impure/db_mysql.nim index dab84c2d50..cd2be2ff9d 100644 --- a/lib/impure/db_mysql.nim +++ b/lib/impure/db_mysql.nim @@ -212,8 +212,8 @@ proc close*(db: TDbConn) {.tags: [FDb].} = ## closes the database connection. if db != nil: mysql.close(db) -proc open*(connection, user, password, database: string): TDbConn {. - tags: [FDb].} = +proc open*(connection, user, password, database: string, + charset: string = "utf8"): TDbConn {.tags: [FDb].} = ## opens a database connection. Raises `EDb` if the connection could not ## be established. result = mysql.init(nil) @@ -229,3 +229,7 @@ proc open*(connection, user, password, database: string): TDbConn {. var errmsg = $mysql.error(result) db_mysql.close(result) dbError(errmsg) + if mysql.set_character_set(result, charset) == 0: + var errmsg = $mysql.error(result) + db_mysql.close(result) + dbError(errmsg) diff --git a/lib/impure/db_postgres.nim b/lib/impure/db_postgres.nim index d8ccc4c16a..a42432a7de 100644 --- a/lib/impure/db_postgres.nim +++ b/lib/impure/db_postgres.nim @@ -239,8 +239,8 @@ proc close*(db: TDbConn) {.tags: [FDb].} = ## closes the database connection. if db != nil: pqfinish(db) -proc open*(connection, user, password, database: string): TDbConn {. - tags: [FDb].} = +proc open*(connection, user, password, database: string, + charset: string = "UTF-8"): TDbConn {.tags: [FDb].} = ## opens a database connection. Raises `EDb` if the connection could not ## be established. ## @@ -260,3 +260,4 @@ proc open*(connection, user, password, database: string): TDbConn {. ## the nim db api. result = pqsetdbLogin(nil, nil, nil, nil, database, user, password) if pqStatus(result) != CONNECTION_OK: dbError(result) # result = nil + if pqsetClientEncoding(result, charset) != 0: dbError(result) \ No newline at end of file diff --git a/lib/impure/db_sqlite.nim b/lib/impure/db_sqlite.nim index bf1ce75ef7..6873e35f72 100644 --- a/lib/impure/db_sqlite.nim +++ b/lib/impure/db_sqlite.nim @@ -183,13 +183,14 @@ 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): TDbConn {. - tags: [FDb].} = +proc open*(connection, user, password, database: string, + charset: string = "UTF-8"): 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) From 171d51a08c62b069edf389d4e7ead097dd3dfb32 Mon Sep 17 00:00:00 2001 From: KeMeGe Date: Mon, 16 Mar 2015 13:53:38 +0800 Subject: [PATCH 2/2] move database encoding options to setEncoding(), leave open() as it is --- lib/impure/db_mysql.nim | 14 ++++++++------ lib/impure/db_postgres.nim | 11 ++++++++--- lib/impure/db_sqlite.nim | 19 +++++++++++++++---- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/lib/impure/db_mysql.nim b/lib/impure/db_mysql.nim index cd2be2ff9d..b8180cd875 100644 --- a/lib/impure/db_mysql.nim +++ b/lib/impure/db_mysql.nim @@ -212,8 +212,8 @@ proc close*(db: TDbConn) {.tags: [FDb].} = ## closes the database connection. if db != nil: mysql.close(db) -proc open*(connection, user, password, database: string, - charset: string = "utf8"): 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. result = mysql.init(nil) @@ -229,7 +229,9 @@ proc open*(connection, user, password, database: string, var errmsg = $mysql.error(result) db_mysql.close(result) dbError(errmsg) - if mysql.set_character_set(result, charset) == 0: - var errmsg = $mysql.error(result) - db_mysql.close(result) - dbError(errmsg) + +proc setEncoding*(connection: TDbConn, encoding: string): bool {. + tags: [FDb].} = + ## sets the encoding of a database connection, returns true for + ## success, false for failure. + result = mysql.set_character_set(connection, encoding) == 0 \ No newline at end of file diff --git a/lib/impure/db_postgres.nim b/lib/impure/db_postgres.nim index a42432a7de..ffb8bbcda2 100644 --- a/lib/impure/db_postgres.nim +++ b/lib/impure/db_postgres.nim @@ -239,8 +239,8 @@ proc close*(db: TDbConn) {.tags: [FDb].} = ## closes the database connection. if db != nil: pqfinish(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. ## @@ -260,4 +260,9 @@ proc open*(connection, user, password, database: string, ## the nim db api. result = pqsetdbLogin(nil, nil, nil, nil, database, user, password) if pqStatus(result) != CONNECTION_OK: dbError(result) # result = nil - if pqsetClientEncoding(result, charset) != 0: dbError(result) \ No newline at end of file + +proc setEncoding*(connection: TDbConn, encoding: string): bool {. + tags: [FDb].} = + ## sets the encoding of a database connection, returns true for + ## success, false for failure. + return pqsetClientEncoding(connection, encoding) == 0 \ No newline at end of file diff --git a/lib/impure/db_sqlite.nim b/lib/impure/db_sqlite.nim index 6873e35f72..4be692f390 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)", [])