2019-08-18 17:24:43 +03:00
|
|
|
module mysql
|
|
|
|
|
|
|
|
#flag -lmysqlclient
|
2019-12-20 23:18:23 +03:00
|
|
|
#flag linux -I/usr/include/mysql
|
2019-08-18 17:24:43 +03:00
|
|
|
#include <mysql.h>
|
|
|
|
|
2020-02-02 14:39:57 +03:00
|
|
|
pub struct Connection {
|
|
|
|
host string
|
|
|
|
port u32
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
dbname string
|
|
|
|
flag int
|
|
|
|
mut:
|
2020-05-04 15:58:24 +03:00
|
|
|
conn &C.MYSQL
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:39:57 +03:00
|
|
|
pub fn new_connection(host, username, password, dbname string) ?Connection {
|
2020-05-04 15:58:24 +03:00
|
|
|
instance := C.mysql_init(0)
|
2020-02-02 14:39:57 +03:00
|
|
|
if isnil(instance) {
|
|
|
|
return error_with_code(get_error_msg(instance), get_errno(instance))
|
|
|
|
}
|
|
|
|
return Connection{ host, 0, username, password, dbname, 0, instance }
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:39:57 +03:00
|
|
|
pub fn (conn mut Connection) connect() ?bool {
|
2020-05-04 15:58:24 +03:00
|
|
|
mut instance := C.mysql_init(0)
|
2020-02-02 14:39:57 +03:00
|
|
|
if !isnil(conn.conn) {
|
|
|
|
instance = conn.conn
|
|
|
|
}
|
|
|
|
if isnil(instance) {
|
|
|
|
return error_with_code(get_error_msg(instance), get_errno(instance))
|
|
|
|
}
|
|
|
|
conn.conn = C.mysql_real_connect(
|
|
|
|
instance,
|
|
|
|
conn.host.str,
|
|
|
|
conn.username.str,
|
|
|
|
conn.password.str,
|
|
|
|
conn.dbname.str,
|
|
|
|
conn.port,
|
|
|
|
0,
|
|
|
|
conn.flag
|
|
|
|
)
|
|
|
|
if isnil(conn.conn) {
|
|
|
|
return error_with_code(get_error_msg(instance), get_errno(instance))
|
|
|
|
}
|
|
|
|
return true
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:39:57 +03:00
|
|
|
pub fn (conn Connection) query(q string) ?Result {
|
2020-05-04 15:58:24 +03:00
|
|
|
if C.mysql_query(conn.conn, q.str) != 0 {
|
2020-02-02 14:39:57 +03:00
|
|
|
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
|
|
|
}
|
2020-05-04 15:58:24 +03:00
|
|
|
res := C.mysql_store_result(conn.conn)
|
2020-02-02 14:39:57 +03:00
|
|
|
return Result{res}
|
|
|
|
}
|
2019-12-02 17:26:58 +03:00
|
|
|
|
2020-02-02 14:39:57 +03:00
|
|
|
pub fn (conn Connection) select_db(dbname string) ?bool {
|
2020-05-04 15:58:24 +03:00
|
|
|
if C.mysql_select_db(conn.conn, dbname.str) != 0 {
|
2020-02-02 14:39:57 +03:00
|
|
|
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2019-08-18 17:24:43 +03:00
|
|
|
|
2020-02-02 14:39:57 +03:00
|
|
|
pub fn (conn Connection) change_user(username, password, dbname string) ?bool {
|
|
|
|
mut ret := true
|
2020-03-29 11:08:42 +03:00
|
|
|
if dbname != '' {
|
2020-05-04 15:58:24 +03:00
|
|
|
ret = C.mysql_change_user(conn.conn, username.str, password.str, dbname.str)
|
2020-02-02 14:39:57 +03:00
|
|
|
} else {
|
2020-05-04 15:58:24 +03:00
|
|
|
ret = C.mysql_change_user(conn.conn, username.str, password.str, 0)
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
2020-02-02 14:39:57 +03:00
|
|
|
if !ret {
|
|
|
|
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
2020-02-02 14:39:57 +03:00
|
|
|
return ret
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:39:57 +03:00
|
|
|
pub fn (conn Connection) affected_rows() u64 {
|
2020-05-04 15:58:24 +03:00
|
|
|
return C.mysql_affected_rows(conn.conn)
|
2020-02-02 14:39:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (conn Connection) autocommit(mode bool) {
|
2020-05-04 15:58:24 +03:00
|
|
|
C.mysql_autocommit(conn.conn, mode)
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:39:57 +03:00
|
|
|
|
|
|
|
pub fn (conn Connection) escape_string(s string) string {
|
2020-05-04 15:58:24 +03:00
|
|
|
len := C.strlen(s.str)
|
2019-12-13 19:12:42 +03:00
|
|
|
to := malloc(2 * len + 1)
|
|
|
|
quote := byte(39) // single quote
|
|
|
|
|
2020-05-04 15:58:24 +03:00
|
|
|
C.mysql_real_escape_string_quote(conn.conn, to, s.str, len, quote)
|
2019-12-13 19:12:42 +03:00
|
|
|
return string(to)
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:39:57 +03:00
|
|
|
pub fn (conn Connection) set_option(option_type int, val voidptr) {
|
2020-05-04 15:58:24 +03:00
|
|
|
C.mysql_options(conn.conn, option_type, val)
|
2020-02-02 14:39:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (conn Connection) get_option(option_type int) ?voidptr {
|
|
|
|
ret := voidptr(0)
|
2020-05-04 15:58:24 +03:00
|
|
|
if C.mysql_get_option(conn.conn, option_type, &ret) != 0 {
|
2020-02-02 14:39:57 +03:00
|
|
|
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (conn Connection) refresh(options u32) ?bool {
|
2020-05-04 15:58:24 +03:00
|
|
|
if C.mysql_refresh(conn.conn, options) != 0 {
|
2020-02-02 14:39:57 +03:00
|
|
|
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
2020-01-11 15:25:59 +03:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:39:57 +03:00
|
|
|
pub fn (conn Connection) reset_connection() ?bool {
|
2020-05-04 15:58:24 +03:00
|
|
|
if C.mysql_reset_connection(conn.conn) != 0 {
|
2020-02-02 14:39:57 +03:00
|
|
|
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
|
|
|
}
|
|
|
|
return true
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:39:57 +03:00
|
|
|
pub fn (conn Connection) ping() ?bool {
|
2020-05-04 15:58:24 +03:00
|
|
|
if C.mysql_ping(conn.conn) != 0 {
|
2020-02-02 14:39:57 +03:00
|
|
|
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
|
|
|
}
|
|
|
|
return true
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:39:57 +03:00
|
|
|
pub fn (conn Connection) close() {
|
2020-05-04 15:58:24 +03:00
|
|
|
C.mysql_close(conn.conn)
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:39:57 +03:00
|
|
|
/* MYSQL INFO & VERSION */
|
|
|
|
|
|
|
|
pub fn (conn Connection) info() string {
|
2020-05-04 15:58:24 +03:00
|
|
|
return string(C.mysql_info(conn.conn))
|
2020-02-02 14:39:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (conn Connection) get_host_info() string {
|
2020-05-04 15:58:24 +03:00
|
|
|
return string(C.mysql_get_host_info(conn.conn))
|
2020-02-02 14:39:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (conn Connection) get_server_info() string {
|
2020-05-04 15:58:24 +03:00
|
|
|
return string(C.mysql_get_server_info(conn.conn))
|
2020-02-02 14:39:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (conn Connection) get_server_version() u64 {
|
2020-05-04 15:58:24 +03:00
|
|
|
return C.mysql_get_server_version(conn.conn)
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:39:57 +03:00
|
|
|
pub fn get_client_version() u64 {
|
2020-05-04 15:58:24 +03:00
|
|
|
return C.mysql_get_client_version()
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
2019-12-03 11:18:20 +03:00
|
|
|
|
2020-02-02 14:39:57 +03:00
|
|
|
pub fn get_client_info() string {
|
2020-05-04 15:58:24 +03:00
|
|
|
return string(C.mysql_get_client_info())
|
2020-02-02 14:39:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* MYSQL DEBUG */
|
|
|
|
pub fn (conn Connection) dump_debug_info() ?bool {
|
2020-05-04 15:58:24 +03:00
|
|
|
if C.mysql_dump_debug_info(conn.conn) != 0 {
|
2020-02-02 14:39:57 +03:00
|
|
|
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
|
|
|
}
|
|
|
|
return true
|
2019-12-03 11:18:20 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:39:57 +03:00
|
|
|
pub fn debug(debug string) {
|
2020-05-04 15:58:24 +03:00
|
|
|
C.mysql_debug(debug.str)
|
2019-12-03 11:18:20 +03:00
|
|
|
}
|