move database encoding options to setEncoding(), leave open() as it is
This commit is contained in:
parent
ca8102b96d
commit
171d51a08c
3 changed files with 31 additions and 13 deletions
|
|
@ -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,
|
proc open*(connection, user, password, database: string): TDbConn {.
|
||||||
charset: string = "utf8"): TDbConn {.tags: [FDb].} =
|
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,7 +229,9 @@ proc open*(connection, user, password, database: string,
|
||||||
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)
|
proc setEncoding*(connection: TDbConn, encoding: string): bool {.
|
||||||
db_mysql.close(result)
|
tags: [FDb].} =
|
||||||
dbError(errmsg)
|
## sets the encoding of a database connection, returns true for
|
||||||
|
## success, false for failure.
|
||||||
|
result = mysql.set_character_set(connection, encoding) == 0
|
||||||
|
|
@ -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,
|
proc open*(connection, user, password, database: string): TDbConn {.
|
||||||
charset: string = "UTF-8"): TDbConn {.tags: [FDb].} =
|
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,4 +260,9 @@ proc open*(connection, user, password, database: string,
|
||||||
## 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)
|
|
||||||
|
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
|
||||||
|
|
@ -183,17 +183,28 @@ 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,
|
proc open*(connection, user, password, database: string): TDbConn {.
|
||||||
charset: string = "UTF-8"): TDbConn {.tags: [FDb].} =
|
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)
|
||||||
|
|
||||||
|
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:
|
when isMainModule:
|
||||||
var db = open("db.sql", "", "", "")
|
var db = open("db.sql", "", "", "")
|
||||||
exec(db, sql"create table tbl1(one varchar(10), two smallint)", [])
|
exec(db, sql"create table tbl1(one varchar(10), two smallint)", [])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue