mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
mysql: update code & add a feature to get value as map
This commit is contained in:

committed by
GitHub

parent
f27fd63f8c
commit
23df9b052e
@@ -12,11 +12,11 @@ pub struct Connection {
|
||||
dbname string
|
||||
flag int
|
||||
mut:
|
||||
conn &MYSQL
|
||||
conn &C.MYSQL
|
||||
}
|
||||
|
||||
pub fn new_connection(host, username, password, dbname string) ?Connection {
|
||||
instance := mysql_init(0)
|
||||
instance := C.mysql_init(0)
|
||||
if isnil(instance) {
|
||||
return error_with_code(get_error_msg(instance), get_errno(instance))
|
||||
}
|
||||
@@ -24,7 +24,7 @@ pub fn new_connection(host, username, password, dbname string) ?Connection {
|
||||
}
|
||||
|
||||
pub fn (conn mut Connection) connect() ?bool {
|
||||
mut instance := mysql_init(0)
|
||||
mut instance := C.mysql_init(0)
|
||||
if !isnil(conn.conn) {
|
||||
instance = conn.conn
|
||||
}
|
||||
@@ -48,15 +48,15 @@ pub fn (conn mut Connection) connect() ?bool {
|
||||
}
|
||||
|
||||
pub fn (conn Connection) query(q string) ?Result {
|
||||
if mysql_query(conn.conn, q.str) != 0 {
|
||||
if C.mysql_query(conn.conn, q.str) != 0 {
|
||||
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
||||
}
|
||||
res := mysql_store_result(conn.conn)
|
||||
res := C.mysql_store_result(conn.conn)
|
||||
return Result{res}
|
||||
}
|
||||
|
||||
pub fn (conn Connection) select_db(dbname string) ?bool {
|
||||
if mysql_select_db(conn.conn, dbname.str) != 0 {
|
||||
if C.mysql_select_db(conn.conn, dbname.str) != 0 {
|
||||
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
||||
}
|
||||
return true
|
||||
@@ -65,9 +65,9 @@ pub fn (conn Connection) select_db(dbname string) ?bool {
|
||||
pub fn (conn Connection) change_user(username, password, dbname string) ?bool {
|
||||
mut ret := true
|
||||
if dbname != '' {
|
||||
ret = mysql_change_user(conn.conn, username.str, password.str, dbname.str)
|
||||
ret = C.mysql_change_user(conn.conn, username.str, password.str, dbname.str)
|
||||
} else {
|
||||
ret = mysql_change_user(conn.conn, username.str, password.str, 0)
|
||||
ret = C.mysql_change_user(conn.conn, username.str, password.str, 0)
|
||||
}
|
||||
if !ret {
|
||||
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
||||
@@ -76,94 +76,94 @@ pub fn (conn Connection) change_user(username, password, dbname string) ?bool {
|
||||
}
|
||||
|
||||
pub fn (conn Connection) affected_rows() u64 {
|
||||
return mysql_affected_rows(conn.conn)
|
||||
return C.mysql_affected_rows(conn.conn)
|
||||
}
|
||||
|
||||
pub fn (conn Connection) autocommit(mode bool) {
|
||||
mysql_autocommit(conn.conn, mode)
|
||||
C.mysql_autocommit(conn.conn, mode)
|
||||
}
|
||||
|
||||
|
||||
pub fn (conn Connection) escape_string(s string) string {
|
||||
len := strlen(s.str)
|
||||
len := C.strlen(s.str)
|
||||
to := malloc(2 * len + 1)
|
||||
quote := byte(39) // single quote
|
||||
|
||||
mysql_real_escape_string_quote(conn.conn, to, s.str, len, quote)
|
||||
C.mysql_real_escape_string_quote(conn.conn, to, s.str, len, quote)
|
||||
return string(to)
|
||||
}
|
||||
|
||||
pub fn (conn Connection) set_option(option_type int, val voidptr) {
|
||||
mysql_options(conn.conn, option_type, val)
|
||||
C.mysql_options(conn.conn, option_type, val)
|
||||
}
|
||||
|
||||
pub fn (conn Connection) get_option(option_type int) ?voidptr {
|
||||
ret := voidptr(0)
|
||||
if mysql_get_option(conn.conn, option_type, &ret) != 0 {
|
||||
if C.mysql_get_option(conn.conn, option_type, &ret) != 0 {
|
||||
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
pub fn (conn Connection) refresh(options u32) ?bool {
|
||||
if mysql_refresh(conn.conn, options) != 0 {
|
||||
if C.mysql_refresh(conn.conn, options) != 0 {
|
||||
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
pub fn (conn Connection) reset_connection() ?bool {
|
||||
if mysql_reset_connection(conn.conn) != 0 {
|
||||
if C.mysql_reset_connection(conn.conn) != 0 {
|
||||
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
pub fn (conn Connection) ping() ?bool {
|
||||
if mysql_ping(conn.conn) != 0 {
|
||||
if C.mysql_ping(conn.conn) != 0 {
|
||||
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
pub fn (conn Connection) close() {
|
||||
mysql_close(conn.conn)
|
||||
C.mysql_close(conn.conn)
|
||||
}
|
||||
|
||||
/* MYSQL INFO & VERSION */
|
||||
|
||||
pub fn (conn Connection) info() string {
|
||||
return string(mysql_info(conn.conn))
|
||||
return string(C.mysql_info(conn.conn))
|
||||
}
|
||||
|
||||
pub fn (conn Connection) get_host_info() string {
|
||||
return string(mysql_get_host_info(conn.conn))
|
||||
return string(C.mysql_get_host_info(conn.conn))
|
||||
}
|
||||
|
||||
pub fn (conn Connection) get_server_info() string {
|
||||
return string(mysql_get_server_info(conn.conn))
|
||||
return string(C.mysql_get_server_info(conn.conn))
|
||||
}
|
||||
|
||||
pub fn (conn Connection) get_server_version() u64 {
|
||||
return mysql_get_server_version(conn.conn)
|
||||
return C.mysql_get_server_version(conn.conn)
|
||||
}
|
||||
|
||||
pub fn get_client_version() u64 {
|
||||
return mysql_get_client_version()
|
||||
return C.mysql_get_client_version()
|
||||
}
|
||||
|
||||
pub fn get_client_info() string {
|
||||
return string(mysql_get_client_info())
|
||||
return string(C.mysql_get_client_info())
|
||||
}
|
||||
|
||||
/* MYSQL DEBUG */
|
||||
pub fn (conn Connection) dump_debug_info() ?bool {
|
||||
if mysql_dump_debug_info(conn.conn) != 0 {
|
||||
if C.mysql_dump_debug_info(conn.conn) != 0 {
|
||||
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
pub fn debug(debug string) {
|
||||
mysql_debug(debug.str)
|
||||
C.mysql_debug(debug.str)
|
||||
}
|
||||
|
Reference in New Issue
Block a user