2023-01-13 18:02:32 +03:00
|
|
|
module mysql
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
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
|
|
|
|
client_multi_statements = C.CLIENT_MULTI_STATEMENTS
|
|
|
|
client_no_schema = C.CLIENT_NO_SCHEMA
|
|
|
|
client_odbc = C.CLIENT_ODBC
|
|
|
|
client_ssl = C.CLIENT_SSL
|
|
|
|
client_remember_options = C.CLIENT_REMEMBER_OPTIONS
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SQLError {
|
|
|
|
MessageError
|
|
|
|
}
|
|
|
|
|
2023-06-13 08:49:41 +03:00
|
|
|
pub struct DB {
|
|
|
|
mut:
|
|
|
|
conn &C.MYSQL = unsafe { nil }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Config {
|
2023-01-13 18:02:32 +03:00
|
|
|
mut:
|
|
|
|
conn &C.MYSQL = C.mysql_init(0)
|
|
|
|
pub mut:
|
|
|
|
host string = '127.0.0.1'
|
|
|
|
port u32 = 3306
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
dbname string
|
|
|
|
flag ConnectionFlag
|
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// connect attempts to establish a connection to a MySQL server.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn connect(config Config) !DB {
|
|
|
|
mut db := DB{
|
|
|
|
conn: C.mysql_init(0)
|
|
|
|
}
|
2023-05-25 03:50:15 +03:00
|
|
|
|
2023-06-13 08:49:41 +03:00
|
|
|
connection := C.mysql_real_connect(db.conn, config.host.str, config.username.str,
|
|
|
|
config.password.str, config.dbname.str, config.port, 0, config.flag)
|
|
|
|
|
|
|
|
if isnil(connection) {
|
|
|
|
db.throw_mysql_error()!
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
2023-05-25 03:50:15 +03:00
|
|
|
|
2023-06-13 08:49:41 +03:00
|
|
|
// Sets `db.conn` after checking for `null`
|
|
|
|
// because `throw_mysql_error` can't extract an error from a `null` connection,
|
|
|
|
// and `panic` will be with an empty message.
|
|
|
|
db.conn = connection
|
|
|
|
|
|
|
|
return db
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// query executes the SQL statement pointed to by the string `q`.
|
|
|
|
// It cannot be used for statements that contain binary data;
|
2023-01-13 18:02:32 +03:00
|
|
|
// Use `real_query()` instead.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (db &DB) query(q string) !Result {
|
|
|
|
if C.mysql_query(db.conn, q.str) != 0 {
|
|
|
|
db.throw_mysql_error()!
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
2023-05-25 03:50:15 +03:00
|
|
|
|
2023-06-13 08:49:41 +03:00
|
|
|
result := C.mysql_store_result(db.conn)
|
2023-05-25 03:50:15 +03:00
|
|
|
return Result{result}
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// use_result reads the result of a query
|
2023-01-13 18:02:32 +03:00
|
|
|
// used after invoking mysql_real_query() or mysql_query(),
|
|
|
|
// for every statement that successfully produces a result set
|
|
|
|
// (SELECT, SHOW, DESCRIBE, EXPLAIN, CHECK TABLE, and so forth).
|
|
|
|
// This reads the result of a query directly from the server
|
|
|
|
// without storing it in a temporary table or local buffer,
|
|
|
|
// mysql_use_result is faster and uses much less memory than C.mysql_store_result().
|
|
|
|
// You must mysql_free_result() after you are done with the result set.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (db &DB) use_result() {
|
|
|
|
C.mysql_use_result(db.conn)
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// real_query makes an SQL query and receive the results.
|
2023-01-13 18:02:32 +03:00
|
|
|
// `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()`.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (mut db DB) real_query(q string) !Result {
|
|
|
|
if C.mysql_real_query(db.conn, q.str, q.len) != 0 {
|
|
|
|
db.throw_mysql_error()!
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
2023-05-25 03:50:15 +03:00
|
|
|
|
2023-06-13 08:49:41 +03:00
|
|
|
result := C.mysql_store_result(db.conn)
|
2023-05-25 03:50:15 +03:00
|
|
|
return Result{result}
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// select_db causes the database specified by `db` to become
|
|
|
|
// the default (current) database on the connection specified by mysql.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (mut db DB) select_db(dbname string) !bool {
|
|
|
|
if C.mysql_select_db(db.conn, dbname.str) != 0 {
|
|
|
|
db.throw_mysql_error()!
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
2023-05-25 03:50:15 +03:00
|
|
|
|
2023-01-13 18:02:32 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// change_user changes the mysql user for the connection.
|
2023-01-13 18:02:32 +03:00
|
|
|
// Passing an empty string for the `dbname` parameter, resultsg in only changing
|
|
|
|
// the user and not changing the default database for the connection.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (mut db DB) change_user(username string, password string, dbname string) !bool {
|
2023-05-25 03:50:15 +03:00
|
|
|
mut result := true
|
|
|
|
|
2023-01-13 18:02:32 +03:00
|
|
|
if dbname != '' {
|
2023-06-13 08:49:41 +03:00
|
|
|
result = C.mysql_change_user(db.conn, username.str, password.str, dbname.str)
|
2023-01-13 18:02:32 +03:00
|
|
|
} else {
|
2023-06-13 08:49:41 +03:00
|
|
|
result = C.mysql_change_user(db.conn, username.str, password.str, 0)
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
2023-05-25 03:50:15 +03:00
|
|
|
if !result {
|
2023-06-13 08:49:41 +03:00
|
|
|
db.throw_mysql_error()!
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
2023-05-25 03:50:15 +03:00
|
|
|
|
|
|
|
return result
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// affected_rows returns the number of rows changed, deleted,
|
|
|
|
// or inserted by the last statement if it was an `UPDATE`, `DELETE`, or `INSERT`.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (db &DB) affected_rows() u64 {
|
|
|
|
return C.mysql_affected_rows(db.conn)
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// autocommit turns on/off the auto-committing mode for the connection.
|
|
|
|
// When it is on, then each query is committed right away.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (mut db DB) autocommit(mode bool) ! {
|
|
|
|
db.check_connection_is_established()!
|
|
|
|
result := C.mysql_autocommit(db.conn, mode)
|
2023-05-26 03:16:02 +03:00
|
|
|
|
|
|
|
if result != 0 {
|
2023-06-13 08:49:41 +03:00
|
|
|
db.throw_mysql_error()!
|
2023-05-26 03:16:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// commit commits the current transaction.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (db &DB) commit() ! {
|
|
|
|
db.check_connection_is_established()!
|
|
|
|
result := C.mysql_commit(db.conn)
|
2023-05-26 03:16:02 +03:00
|
|
|
|
|
|
|
if result != 0 {
|
2023-06-13 08:49:41 +03:00
|
|
|
db.throw_mysql_error()!
|
2023-05-26 03:16:02 +03:00
|
|
|
}
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// tables returns a list of the names of the tables in the current database,
|
2023-01-13 18:02:32 +03:00
|
|
|
// 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]`.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (db &DB) tables(wildcard string) ![]string {
|
|
|
|
c_mysql_result := C.mysql_list_tables(db.conn, wildcard.str)
|
2023-05-25 03:50:15 +03:00
|
|
|
if isnil(c_mysql_result) {
|
2023-06-13 08:49:41 +03:00
|
|
|
db.throw_mysql_error()!
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
2023-05-25 03:50:15 +03:00
|
|
|
|
|
|
|
result := Result{c_mysql_result}
|
2023-01-13 18:02:32 +03:00
|
|
|
mut tables := []string{}
|
2023-05-25 03:50:15 +03:00
|
|
|
|
|
|
|
for row in result.rows() {
|
2023-01-13 18:02:32 +03:00
|
|
|
tables << row.vals[0]
|
|
|
|
}
|
2023-05-25 03:50:15 +03:00
|
|
|
|
2023-01-13 18:02:32 +03:00
|
|
|
return tables
|
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// escape_string creates a legal SQL string for use in an SQL statement.
|
2023-01-13 18:02:32 +03:00
|
|
|
// The `s` argument is encoded to produce an escaped SQL string,
|
|
|
|
// taking into account the current character set of the connection.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (db &DB) escape_string(s string) string {
|
2023-01-13 18:02:32 +03:00
|
|
|
unsafe {
|
|
|
|
to := malloc_noscan(2 * s.len + 1)
|
2023-06-13 08:49:41 +03:00
|
|
|
C.mysql_real_escape_string(db.conn, to, s.str, s.len)
|
2023-01-13 18:02:32 +03:00
|
|
|
return to.vstring()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// set_option sets extra connect options that affect the behavior of
|
2023-01-13 18:02:32 +03:00
|
|
|
// a connection. This function may be called multiple times to set several
|
|
|
|
// options. To retrieve the current values for an option, use `get_option()`.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (mut db DB) set_option(option_type int, val voidptr) {
|
|
|
|
C.mysql_options(db.conn, option_type, val)
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// get_option returns the value of an option, settable by `set_option`.
|
2023-01-13 18:02:32 +03:00
|
|
|
// https://dev.mysql.com/doc/c-api/5.7/en/mysql-get-option.html
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (db &DB) get_option(option_type int) !voidptr {
|
2023-05-25 03:50:15 +03:00
|
|
|
mysql_option := unsafe { nil }
|
2023-06-13 08:49:41 +03:00
|
|
|
if C.mysql_get_option(db.conn, option_type, &mysql_option) != 0 {
|
|
|
|
db.throw_mysql_error()!
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
2023-05-25 03:50:15 +03:00
|
|
|
|
|
|
|
return mysql_option
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// refresh flush the tables or caches, or resets replication server
|
2023-01-13 18:02:32 +03:00
|
|
|
// information. The connected user must have the `RELOAD` privilege.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (mut db DB) refresh(options u32) !bool {
|
|
|
|
if C.mysql_refresh(db.conn, options) != 0 {
|
|
|
|
db.throw_mysql_error()!
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
2023-05-25 03:50:15 +03:00
|
|
|
|
2023-01-13 18:02:32 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// reset resets the connection, and clear the session state.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (mut db DB) reset() !bool {
|
|
|
|
if C.mysql_reset_connection(db.conn) != 0 {
|
|
|
|
db.throw_mysql_error()!
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
2023-05-25 03:50:15 +03:00
|
|
|
|
2023-01-13 18:02:32 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// ping pings a server connection, or tries to reconnect if the connection
|
2023-01-13 18:02:32 +03:00
|
|
|
// has gone down.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (mut db DB) ping() !bool {
|
|
|
|
if C.mysql_ping(db.conn) != 0 {
|
|
|
|
db.throw_mysql_error()!
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
2023-05-25 03:50:15 +03:00
|
|
|
|
2023-01-13 18:02:32 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// close closes the connection.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (mut db DB) close() {
|
|
|
|
C.mysql_close(db.conn)
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// info returns information about the most recently executed query.
|
2023-01-13 18:02:32 +03:00
|
|
|
// See more on https://dev.mysql.com/doc/c-api/8.0/en/mysql-info.html
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (db &DB) info() string {
|
|
|
|
return resolve_nil_str(C.mysql_info(db.conn))
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// get_host_info returns a string describing the type of connection in use,
|
2023-01-13 18:02:32 +03:00
|
|
|
// including the server host name.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (db &DB) get_host_info() string {
|
|
|
|
return unsafe { C.mysql_get_host_info(db.conn).vstring() }
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// get_server_info returns a string representing the MySQL server version.
|
2023-01-13 18:02:32 +03:00
|
|
|
// For example, `8.0.24`.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (db &DB) get_server_info() string {
|
|
|
|
return unsafe { C.mysql_get_server_info(db.conn).vstring() }
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// get_server_version returns an integer, representing the MySQL server
|
2023-01-13 18:02:32 +03:00
|
|
|
// 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`.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (db &DB) get_server_version() u64 {
|
|
|
|
return C.mysql_get_server_version(db.conn)
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// dump_debug_info instructs the server to write debugging information
|
2023-01-13 18:02:32 +03:00
|
|
|
// to the error log. The connected user must have the `SUPER` privilege.
|
2023-06-13 08:49:41 +03:00
|
|
|
pub fn (mut db DB) dump_debug_info() !bool {
|
|
|
|
if C.mysql_dump_debug_info(db.conn) != 0 {
|
|
|
|
db.throw_mysql_error()!
|
2023-01-13 18:02:32 +03:00
|
|
|
}
|
2023-05-25 03:50:15 +03:00
|
|
|
|
2023-01-13 18:02:32 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// get_client_info returns client version information as a string.
|
2023-01-13 18:02:32 +03:00
|
|
|
pub fn get_client_info() string {
|
|
|
|
return unsafe { C.mysql_get_client_info().vstring() }
|
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// get_client_version returns the client version information as an integer.
|
2023-01-13 18:02:32 +03:00
|
|
|
pub fn get_client_version() u64 {
|
|
|
|
return C.mysql_get_client_version()
|
|
|
|
}
|
|
|
|
|
2023-05-25 03:50:15 +03:00
|
|
|
// debug does a `DBUG_PUSH` with the given string.
|
2023-01-13 18:02:32 +03:00
|
|
|
// `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
|
|
|
|
pub fn debug(debug string) {
|
|
|
|
C.mysql_debug(debug.str)
|
|
|
|
}
|
2023-05-26 03:16:02 +03:00
|
|
|
|
|
|
|
[inline]
|
2023-06-13 08:49:41 +03:00
|
|
|
fn (db &DB) throw_mysql_error() ! {
|
|
|
|
return error_with_code(get_error_msg(db.conn), get_errno(db.conn))
|
2023-05-26 03:16:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2023-06-13 08:49:41 +03:00
|
|
|
fn (db &DB) check_connection_is_established() ! {
|
|
|
|
if isnil(db.conn) {
|
2023-05-26 03:16:02 +03:00
|
|
|
return error('No connection to a MySQL server, use `connect()` to connect to a database for working with it')
|
|
|
|
}
|
|
|
|
}
|