2019-08-18 17:24:43 +03:00
|
|
|
module mysql
|
|
|
|
|
2021-01-02 16:09:20 +03:00
|
|
|
// Values for the capabilities flag bitmask used by the MySQL protocol.
|
|
|
|
// See more on https://dev.mysql.com/doc/dev/mysql-server/latest/group__group__cs__capabilities__flags.html#details
|
|
|
|
pub enum ConnectionFlag {
|
|
|
|
// CAN_HANDLE_EXPIRED_PASSWORDS = C.CAN_HANDLE_EXPIRED_PASSWORDS
|
2021-01-09 11:40:21 +03:00
|
|
|
client_compress = C.CLIENT_COMPRESS
|
|
|
|
client_found_rows = C.CLIENT_FOUND_ROWS
|
|
|
|
client_ignore_sigpipe = C.CLIENT_IGNORE_SIGPIPE
|
|
|
|
client_ignore_space = C.CLIENT_IGNORE_SPACE
|
|
|
|
client_interactive = C.CLIENT_INTERACTIVE
|
|
|
|
client_local_files = C.CLIENT_LOCAL_FILES
|
|
|
|
client_multi_results = C.CLIENT_MULTI_RESULTS
|
2021-01-02 16:09:20 +03:00
|
|
|
client_multi_statements = C.CLIENT_MULTI_STATEMENTS
|
2021-01-09 11:40:21 +03:00
|
|
|
client_no_schema = C.CLIENT_NO_SCHEMA
|
|
|
|
client_odbc = C.CLIENT_ODBC
|
2021-01-02 16:09:20 +03:00
|
|
|
// client_optional_resultset_metadata = C.CLIENT_OPTIONAL_RESULTSET_METADATA
|
2021-01-09 11:40:21 +03:00
|
|
|
client_ssl = C.CLIENT_SSL
|
2021-01-02 16:09:20 +03:00
|
|
|
client_remember_options = C.CLIENT_REMEMBER_OPTIONS
|
|
|
|
}
|
|
|
|
|
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:
|
2020-10-19 21:11:04 +03:00
|
|
|
host string = '127.0.0.1'
|
2021-01-09 11:40:21 +03:00
|
|
|
port u32 = 3306
|
2020-02-02 14:39:57 +03:00
|
|
|
username string
|
|
|
|
password string
|
|
|
|
dbname string
|
2021-01-02 16:09:20 +03:00
|
|
|
flag ConnectionFlag
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// connect - create a new connection to the 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-10-19 21:11:04 +03:00
|
|
|
conn.conn = C.mysql_real_connect(conn.conn, conn.host.str, conn.username.str, conn.password.str,
|
|
|
|
conn.dbname.str, conn.port, 0, conn.flag)
|
2020-02-02 14:39:57 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// query - make an SQL query and receive the results.
|
|
|
|
// `query()` cannot be used for statements that contain binary data;
|
|
|
|
// Use `real_query()` instead.
|
|
|
|
pub fn (mut 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
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// real_query - make an SQL query and receive the results.
|
|
|
|
// `real_query()` can be used for statements containing binary data.
|
|
|
|
// (Binary data may contain the `\0` character, which `query()`
|
|
|
|
// interprets as the end of the statement string). In addition,
|
|
|
|
// `real_query()` is faster than `query()`.
|
|
|
|
pub fn (mut conn Connection) real_query(q string) ?Result {
|
2020-06-11 11:07:17 +03:00
|
|
|
if C.mysql_real_query(conn.conn, q.str, q.len) != 0 {
|
|
|
|
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
|
|
|
}
|
|
|
|
res := C.mysql_store_result(conn.conn)
|
|
|
|
return Result{res}
|
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// select_db - change the default database for database queries.
|
|
|
|
pub fn (mut 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
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// change_user - change the mysql user for the connection.
|
|
|
|
// Passing an empty string for the `dbname` parameter, resultsg in only changing
|
|
|
|
// the user and not changing the default database for the connection.
|
|
|
|
pub fn (mut conn Connection) change_user(username string, password string, dbname string) ?bool {
|
2020-02-02 14:39:57 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// affected_rows - return the number of rows changed/deleted/inserted
|
|
|
|
// by the last `UPDATE`, `DELETE`, or `INSERT` query.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// autocommit - turns on/off the auto-committing mode for the connection.
|
|
|
|
// When it is on, then each query is commited right away.
|
|
|
|
pub fn (mut 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
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// tables - returns a list of the names of the tables in the current database,
|
|
|
|
// that match the simple regular expression specified by the `wildcard` parameter.
|
|
|
|
// The `wildcard` parameter may contain the wildcard characters `%` or `_`.
|
|
|
|
// If an empty string is passed, it will return all tables.
|
|
|
|
// Calling `tables()` is similar to executing query `SHOW TABLES [LIKE wildcard]`.
|
|
|
|
pub fn (conn &Connection) tables(wildcard string) ?[]string {
|
2020-06-01 12:26:39 +03:00
|
|
|
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))
|
|
|
|
}
|
2020-10-19 21:11:04 +03:00
|
|
|
res := Result{cres}
|
2020-06-01 12:26:39 +03:00
|
|
|
mut tables := []string{}
|
|
|
|
for row in res.rows() {
|
|
|
|
tables << row.vals[0]
|
|
|
|
}
|
|
|
|
return tables
|
|
|
|
}
|
2020-02-02 14:39:57 +03:00
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// escape_string - creates a legal SQL string for use in an SQL statement.
|
|
|
|
// The `s` argument is encoded to produce an escaped SQL string,
|
|
|
|
// taking into account the current character set of the connection.
|
|
|
|
pub fn (conn &Connection) escape_string(s string) string {
|
2020-10-26 12:21:28 +03:00
|
|
|
to := malloc(2 * s.len + 1)
|
|
|
|
C.mysql_real_escape_string_quote(conn.conn, to, s.str, s.len, `\'`)
|
2021-01-09 11:40:21 +03:00
|
|
|
return unsafe { to.vstring() }
|
2019-12-13 19:12:42 +03:00
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// set_option - sets extra connect options that affect the behavior of
|
|
|
|
// a connection. This function may be called multiple times to set several
|
|
|
|
// options. To retrieve the current values for an option, use `get_option()`.
|
|
|
|
pub fn (mut 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
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// get_option - return the value of an option, settable by `set_option`.
|
|
|
|
// https://dev.mysql.com/doc/c-api/5.7/en/mysql-get-option.html
|
|
|
|
pub fn (conn &Connection) get_option(option_type int) ?voidptr {
|
2020-02-02 14:39:57 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// refresh - flush the tables or caches, or resets replication server
|
|
|
|
// information. The connected user must have the `RELOAD` privilege.
|
|
|
|
pub fn (mut 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
|
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// reset - resets the connection, and clear the session state.
|
|
|
|
pub fn (mut conn Connection) reset() ?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
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// ping - pings a server connection, or tries to reconnect if the connection
|
|
|
|
// has gone down.
|
|
|
|
pub fn (mut 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
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// close - closes the connection.
|
|
|
|
pub fn (mut conn Connection) close() {
|
2020-05-04 15:58:24 +03:00
|
|
|
C.mysql_close(conn.conn)
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// info - returns information about the most recently executed query.
|
|
|
|
// See more on https://dev.mysql.com/doc/c-api/8.0/en/mysql-info.html
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// get_host_info - returns a string describing the type of connection in use,
|
|
|
|
// including the server host name.
|
|
|
|
pub fn (conn &Connection) get_host_info() string {
|
|
|
|
return unsafe { C.mysql_get_host_info(conn.conn).vstring() }
|
2020-02-02 14:39:57 +03:00
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// get_server_info - returns a string representing the MySQL server version.
|
|
|
|
// For example, `8.0.24`.
|
|
|
|
pub fn (conn &Connection) get_server_info() string {
|
|
|
|
return unsafe { C.mysql_get_server_info(conn.conn).vstring() }
|
2020-02-02 14:39:57 +03:00
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// get_server_version - returns an integer, representing the MySQL server
|
|
|
|
// version. The value has the format `XYYZZ` where `X` is the major version,
|
|
|
|
// `YY` is the release level (or minor version), and `ZZ` is the sub-version
|
|
|
|
// within the release level. For example, `8.0.24` is returned as `80024`.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// dump_debug_info - instructs the server to write debugging information
|
|
|
|
// to the error log. The connected user must have the `SUPER` privilege.
|
|
|
|
pub fn (mut conn Connection) dump_debug_info() ?bool {
|
|
|
|
if C.mysql_dump_debug_info(conn.conn) != 0 {
|
|
|
|
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_client_info - returns client version information as a string.
|
2020-02-02 14:39:57 +03:00
|
|
|
pub fn get_client_info() string {
|
2021-01-09 11:40:21 +03:00
|
|
|
return unsafe { C.mysql_get_client_info().vstring() }
|
2020-02-02 14:39:57 +03:00
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// get_client_version - returns the client version information as an integer.
|
2020-06-01 12:26:39 +03:00
|
|
|
pub fn get_client_version() u64 {
|
|
|
|
return C.mysql_get_client_version()
|
|
|
|
}
|
|
|
|
|
2021-01-09 11:40:21 +03:00
|
|
|
// debug - does a `DBUG_PUSH` with the given string.
|
|
|
|
// `debug()` uses the Fred Fish debug library.
|
|
|
|
// To use this function, you must compile the client library to support debugging.
|
|
|
|
// See https://dev.mysql.com/doc/c-api/8.0/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
|
|
|
}
|