Add character set options when opening DB connection

This commit is contained in:
KeMeGe 2015-03-14 15:21:38 +08:00
commit ca8102b96d
3 changed files with 12 additions and 6 deletions

View file

@ -212,8 +212,8 @@ proc close*(db: TDbConn) {.tags: [FDb].} =
## closes the database connection. ## closes the database connection.
if db != nil: mysql.close(db) if db != nil: mysql.close(db)
proc open*(connection, user, password, database: string): TDbConn {. proc open*(connection, user, password, database: string,
tags: [FDb].} = charset: string = "utf8"): TDbConn {.tags: [FDb].} =
## opens a database connection. Raises `EDb` if the connection could not ## opens a database connection. Raises `EDb` if the connection could not
## be established. ## be established.
result = mysql.init(nil) result = mysql.init(nil)
@ -229,3 +229,7 @@ proc open*(connection, user, password, database: string): TDbConn {.
var errmsg = $mysql.error(result) var errmsg = $mysql.error(result)
db_mysql.close(result) db_mysql.close(result)
dbError(errmsg) dbError(errmsg)
if mysql.set_character_set(result, charset) == 0:
var errmsg = $mysql.error(result)
db_mysql.close(result)
dbError(errmsg)

View file

@ -239,8 +239,8 @@ proc close*(db: TDbConn) {.tags: [FDb].} =
## closes the database connection. ## closes the database connection.
if db != nil: pqfinish(db) if db != nil: pqfinish(db)
proc open*(connection, user, password, database: string): TDbConn {. proc open*(connection, user, password, database: string,
tags: [FDb].} = charset: string = "UTF-8"): TDbConn {.tags: [FDb].} =
## opens a database connection. Raises `EDb` if the connection could not ## opens a database connection. Raises `EDb` if the connection could not
## be established. ## be established.
## ##
@ -260,3 +260,4 @@ proc open*(connection, user, password, database: string): TDbConn {.
## the nim db api. ## the nim db api.
result = pqsetdbLogin(nil, nil, nil, nil, database, user, password) result = pqsetdbLogin(nil, nil, nil, nil, database, user, password)
if pqStatus(result) != CONNECTION_OK: dbError(result) # result = nil if pqStatus(result) != CONNECTION_OK: dbError(result) # result = nil
if pqsetClientEncoding(result, charset) != 0: dbError(result)

View file

@ -183,13 +183,14 @@ proc close*(db: TDbConn) {.tags: [FDb].} =
## closes the database connection. ## closes the database connection.
if sqlite3.close(db) != SQLITE_OK: dbError(db) if sqlite3.close(db) != SQLITE_OK: dbError(db)
proc open*(connection, user, password, database: string): TDbConn {. proc open*(connection, user, password, database: string,
tags: [FDb].} = charset: string = "UTF-8"): TDbConn {.tags: [FDb].} =
## opens a database connection. Raises `EDb` if the connection could not ## opens a database connection. Raises `EDb` if the connection could not
## be established. Only the ``connection`` parameter is used for ``sqlite``. ## be established. Only the ``connection`` parameter is used for ``sqlite``.
var db: TDbConn var db: TDbConn
if sqlite3.open(connection, db) == SQLITE_OK: if sqlite3.open(connection, db) == SQLITE_OK:
result = db result = db
exec(result, sql"PRAGMA encoding = ?", [charset])
else: else:
dbError(db) dbError(db)