mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-19 07:21:19 +00:00
Merge pull request #2337 from kemege/db_charset
Add character set options when opening DB connection, default to UTF-8
This commit is contained in:
@@ -229,3 +229,9 @@ proc open*(connection, user, password, database: string): TDbConn {.
|
||||
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
|
||||
@@ -260,3 +260,9 @@ 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
|
||||
|
||||
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
|
||||
@@ -192,7 +192,19 @@ proc open*(connection, user, password, database: string): TDbConn {.
|
||||
result = db
|
||||
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)", [])
|
||||
|
||||
Reference in New Issue
Block a user