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-06-01 12:26:39 +03:00
|
|
|
// TODO: Documentation
|
2020-02-02 14:39:57 +03:00
|
|
|
pub struct Connection {
|
2020-06-01 12:26:39 +03:00
|
|
|
mut:
|
|
|
|
conn &C.MYSQL = C.mysql_init(0)
|
|
|
|
pub mut:
|
|
|
|
host string = '127.0.0.1'
|
|
|
|
port u32 = 3306
|
2020-02-02 14:39:57 +03:00
|
|
|
username string
|
|
|
|
password string
|
|
|
|
dbname string
|
|
|
|
flag int
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
|
|
|
|
2020-06-01 12:26:39 +03:00
|
|
|
// connect Connects to a MySQL server
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut conn Connection) connect() ?bool {
|
2020-06-01 12:26:39 +03:00
|
|
|
instance := C.mysql_init(conn.conn)
|
2020-02-02 14:39:57 +03:00
|
|
|
conn.conn = C.mysql_real_connect(
|
2020-06-01 12:26:39 +03:00
|
|
|
conn.conn,
|
2020-02-02 14:39:57 +03:00
|
|
|
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-06-01 12:26:39 +03:00
|
|
|
// query Performs a query on the database
|
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-06-01 12:26:39 +03:00
|
|
|
// select_db Selects the default database for database queries
|
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-06-01 12:26:39 +03:00
|
|
|
// change_user Changes the user of the specified database connection.
|
|
|
|
// If desired, the empty string value can be passed to the `dbname` parameter
|
|
|
|
// resulting in only changing the user and not selecting a database
|
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-06-01 12:26:39 +03:00
|
|
|
// affected_rows Returns the number of rows changed/deleted/inserted
|
|
|
|
// by the last UPDATE, DELETE, or INSERT query
|
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
|
|
|
}
|
|
|
|
|
2020-06-01 12:26:39 +03:00
|
|
|
// autocommit Turns on or off auto-committing database modifications
|
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-06-01 12:26:39 +03:00
|
|
|
// tables Returns list of tables that match the `wildcard` parameter.
|
|
|
|
// If empty string is passed, will return all tables
|
|
|
|
pub fn (conn Connection) tables(wildcard string) ?[]string {
|
|
|
|
cres := C.mysql_list_tables(conn.conn, wildcard.str)
|
|
|
|
if isnil(cres) {
|
|
|
|
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
|
|
|
}
|
|
|
|
res := Result{cres}
|
|
|
|
mut tables := []string{}
|
|
|
|
for row in res.rows() {
|
|
|
|
tables << row.vals[0]
|
|
|
|
}
|
|
|
|
res.free()
|
|
|
|
return tables
|
|
|
|
}
|
2020-02-02 14:39:57 +03:00
|
|
|
|
2020-06-01 12:26:39 +03:00
|
|
|
// escape_string Creates a legal SQL string for use in an SQL statement
|
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-06-01 12:26:39 +03:00
|
|
|
// set_option Can be used to set extra connect options and affect behavior for a connection.
|
|
|
|
// This function may be called multiple times to set several options.
|
|
|
|
// To retrieve option values, use `get_option()`
|
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
|
|
|
}
|
|
|
|
|
2020-06-01 12:26:39 +03:00
|
|
|
// get_option Returns the current value of an option settable `set_option`.
|
|
|
|
// The value should be treated as read only
|
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
|
|
|
|
}
|
|
|
|
|
2020-06-01 12:26:39 +03:00
|
|
|
// refresh Flushes tables or caches, or resets replication server information.
|
|
|
|
// The connected user must have the `RELOAD` privilege
|
2020-02-02 14:39:57 +03:00
|
|
|
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-06-01 12:26:39 +03:00
|
|
|
// reset_connection Resets the connection to clear the session state
|
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-06-01 12:26:39 +03:00
|
|
|
// ping Pings a server connection, or tries to reconnect if the connection has gone down
|
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-06-01 12:26:39 +03:00
|
|
|
// close Closes a previously opened database connection
|
|
|
|
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-06-01 12:26:39 +03:00
|
|
|
/* -------------------------- MYSQL INFO & VERSION -------------------------- */
|
2020-02-02 14:39:57 +03:00
|
|
|
|
2020-06-01 12:26:39 +03:00
|
|
|
// info Returns information about the most recently executed query
|
2020-02-02 14:39:57 +03:00
|
|
|
pub fn (conn Connection) info() string {
|
2020-06-01 12:26:39 +03:00
|
|
|
return resolve_nil_str(C.mysql_info(conn.conn))
|
2020-02-02 14:39:57 +03:00
|
|
|
}
|
|
|
|
|
2020-06-01 12:26:39 +03:00
|
|
|
// get_host_info Returns a string describing the connection
|
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
|
|
|
}
|
|
|
|
|
2020-06-01 12:26:39 +03:00
|
|
|
// get_server_info Returns the server version number as a string
|
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
|
|
|
}
|
|
|
|
|
2020-06-01 12:26:39 +03:00
|
|
|
// get_server_version Returns the server version number as an integer
|
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-06-01 12:26:39 +03:00
|
|
|
/* --------------------------------- CLIENT --------------------------------- */
|
2019-12-03 11:18:20 +03:00
|
|
|
|
2020-06-01 12:26:39 +03:00
|
|
|
// get_client_info Returns client version information as a string
|
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
|
|
|
}
|
|
|
|
|
2020-06-01 12:26:39 +03:00
|
|
|
// get_client_version Returns client version information as an integer
|
|
|
|
pub fn get_client_version() u64 {
|
|
|
|
return C.mysql_get_client_version()
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------- MYSQL DEBUG ------------------------------ */
|
|
|
|
|
|
|
|
// dump_debug_info Causes the server to write debug information to the log
|
2020-02-02 14:39:57 +03:00
|
|
|
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-06-01 12:26:39 +03:00
|
|
|
// debug Does a `DBUG_PUSH` with the given string.
|
|
|
|
// See https://dev.mysql.com/doc/refman/5.7/en/mysql-debug.html
|
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
|
|
|
}
|