Skip to content

mysql #


The mysql module has been moved to db.mysql. Update your code to do: import db.mysql instead.

Constants #

const (
	refresh_grant   = u32(C.REFRESH_GRANT)
	refresh_log     = u32(C.REFRESH_LOG)
	refresh_tables  = u32(C.REFRESH_TABLES)
	refresh_hosts   = u32(C.REFRESH_HOSTS)
	refresh_status  = u32(C.REFRESH_STATUS)
	refresh_threads = u32(C.REFRESH_THREADS)
	refresh_slave   = u32(C.REFRESH_SLAVE)
	refresh_master  = u32(C.REFRESH_MASTER)


fn debug #

fn debug(debug string)

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.

fn get_client_info #

fn get_client_info() string

get_client_info - returns client version information as a string.

fn get_client_version #

fn get_client_version() u64

get_client_version - returns the client version information as an integer.

fn (Stmt) prepare #

fn (stmt Stmt) prepare() !

fn (Stmt) bind_params #

fn (stmt Stmt) bind_params() !

fn (Stmt) execute #

fn (stmt Stmt) execute() !int

fn (Stmt) next #

fn (stmt Stmt) next() !int

fn (Stmt) gen_metadata #

fn (stmt Stmt) gen_metadata() &C.MYSQL_RES

fn (Stmt) fetch_fields #

fn (stmt Stmt) fetch_fields(res &C.MYSQL_RES) &C.MYSQL_FIELD

fn (Stmt) fetch_stmt #

fn (stmt Stmt) fetch_stmt() !int

fn (Stmt) close #

fn (stmt Stmt) close() !

fn (Stmt) error #

fn (stmt Stmt) error(code int) IError

fn (Stmt) bind_bool #

fn (mut stmt Stmt) bind_bool(b &bool)

fn (Stmt) bind_byte #

fn (mut stmt Stmt) bind_byte(b &byte)

fn (Stmt) bind_u8 #

fn (mut stmt Stmt) bind_u8(b &u8)

fn (Stmt) bind_i8 #

fn (mut stmt Stmt) bind_i8(b &i8)

fn (Stmt) bind_i16 #

fn (mut stmt Stmt) bind_i16(b &i16)

fn (Stmt) bind_u16 #

fn (mut stmt Stmt) bind_u16(b &u16)

fn (Stmt) bind_int #

fn (mut stmt Stmt) bind_int(b &int)

fn (Stmt) bind_u32 #

fn (mut stmt Stmt) bind_u32(b &u32)

fn (Stmt) bind_i64 #

fn (mut stmt Stmt) bind_i64(b &i64)

fn (Stmt) bind_u64 #

fn (mut stmt Stmt) bind_u64(b &u64)

fn (Stmt) bind_f32 #

fn (mut stmt Stmt) bind_f32(b &f32)

fn (Stmt) bind_f64 #

fn (mut stmt Stmt) bind_f64(b &f64)

fn (Stmt) bind_text #

fn (mut stmt Stmt) bind_text(b string)

fn (Stmt) bind #

fn (mut stmt Stmt) bind(typ int, buffer voidptr, buf_len u32)

fn (Stmt) bind_res #

fn (mut stmt Stmt) bind_res(fields &C.MYSQL_FIELD, dataptr []&u8, lens []u32, num_fields int)

fn (Stmt) bind_result_buffer #

fn (mut stmt Stmt) bind_result_buffer() !

fn (Stmt) store_result #

fn (mut stmt Stmt) store_result() !

enum ConnectionFlag #

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_optional_resultset_metadata = C.CLIENT_OPTIONAL_RESULTSET_METADATA
	client_ssl = C.CLIENT_SSL
	client_remember_options = C.CLIENT_REMEMBER_OPTIONS

Values for the capabilities flag bitmask used by the MySQL protocol.
See more on

enum FieldType #

enum FieldType {
	type_json = 245

fn (FieldType) str #

fn (f FieldType) str() string

fn (FieldType) get_len #

fn (f FieldType) get_len() u32

struct Connection #

struct Connection {
	conn &C.MYSQL = C.mysql_init(0)
pub mut:
	host     string = ''
	port     u32    = 3306
	username string
	password string
	dbname   string
	flag     ConnectionFlag

TODO: Documentation

fn (Connection) @select #

fn (db Connection) @select(config orm.SelectConfig, data orm.QueryData, where orm.QueryData) ![][]orm.Primitive

sql expr

fn (Connection) affected_rows #

fn (conn &Connection) affected_rows() u64

affected_rows - return the number of rows changed/deleted/inserted by the last UPDATE, DELETE, or INSERT query.

fn (Connection) autocommit #

fn (mut conn Connection) autocommit(mode bool)

autocommit - turns on/off the auto-committing mode for the connection.
When it is on, then each query is commited right away.

fn (Connection) change_user #

fn (mut conn Connection) change_user(username string, password string, dbname string) !bool

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.

fn (Connection) close #

fn (mut conn Connection) close()

close - closes the connection.

fn (Connection) connect #

fn (mut conn Connection) connect() !bool

connect - create a new connection to the MySQL server.

fn (Connection) create #

fn (db Connection) create(table string, fields []orm.TableField) !


fn (Connection) delete #

fn (db Connection) delete(table string, where orm.QueryData) !

fn (Connection) drop #

fn (db Connection) drop(table string) !

fn (Connection) dump_debug_info #

fn (mut conn Connection) dump_debug_info() !bool

dump_debug_info - instructs the server to write debugging information to the error log. The connected user must have the SUPER privilege.

fn (Connection) escape_string #

fn (conn &Connection) escape_string(s string) string

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.

fn (Connection) get_host_info #

fn (conn &Connection) get_host_info() string

get_host_info - returns a string describing the type of connection in use, including the server host name.

fn (Connection) get_option #

fn (conn &Connection) get_option(option_type int) !voidptr

get_option - return the value of an option, settable by set_option.

fn (Connection) get_server_info #

fn (conn &Connection) get_server_info() string

get_server_info - returns a string representing the MySQL server version.
For example, 8.0.24.

fn (Connection) get_server_version #

fn (conn &Connection) get_server_version() u64

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.

fn (Connection) info #

fn (conn &Connection) info() string

info - returns information about the most recently executed query.
See more on

fn (Connection) init_stmt #

fn (db Connection) init_stmt(query string) Stmt

fn (Connection) insert #

fn (db Connection) insert(table string, data orm.QueryData) !

sql stmt

fn (Connection) last_id #

fn (db Connection) last_id() int

fn (Connection) ping #

fn (mut conn Connection) ping() !bool

ping - pings a server connection, or tries to reconnect if the connection has gone down.

fn (Connection) query #

fn (conn Connection) query(q string) !Result

query - make an SQL query and receive the results.
query() cannot be used for statements that contain binary data; Use real_query() instead.

fn (Connection) real_query #

fn (mut conn Connection) real_query(q string) !Result

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().

fn (Connection) refresh #

fn (mut conn Connection) refresh(options u32) !bool

refresh - flush the tables or caches, or resets replication server information. The connected user must have the RELOAD privilege.

fn (Connection) reset #

fn (mut conn Connection) reset() !bool

reset - resets the connection, and clear the session state.

fn (Connection) select_db #

fn (mut conn Connection) select_db(dbname string) !bool

select_db - change the default database for database queries.

fn (Connection) set_option #

fn (mut conn Connection) set_option(option_type int, val voidptr)

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().

fn (Connection) tables #

fn (conn &Connection) tables(wildcard string) ![]string

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].

fn (Connection) update #

fn (db Connection) update(table string, data orm.QueryData, where orm.QueryData) !

fn (Connection) use_result #

fn (conn Connection) use_result()

use_result - reads the result of a query 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.

struct Field #

struct Field {
	name             string
	org_name         string
	table            string
	org_table        string
	db               string
	catalog          string
	def              string
	length           int
	max_length       int
	name_length      u32
	org_name_length  u32
	table_length     u32
	org_table_length u32
	db_length        u32
	catalog_length   u32
	def_length       u32
	flags            u32
	decimals         u32
	charsetnr        u32
	type_            FieldType

fn (Field) str #

fn (f Field) str() string

str - serializes the field

struct Result #

struct Result {
	result &C.MYSQL_RES = unsafe { nil }

fn (Result) fetch_row #

fn (r Result) fetch_row() &&u8

fetch_row - fetches the next row from a result.

fn (Result) n_rows #

fn (r Result) n_rows() u64

n_rows - returns the number of rows from a result.

fn (Result) n_fields #

fn (r Result) n_fields() int

n_fields - returns the number of columns from a result.

fn (Result) rows #

fn (r Result) rows() []Row

rows - returns array of rows, each containing an array of values, one for each column.

fn (Result) maps #

fn (r Result) maps() []map[string]string

maps - returns an array of maps, each containing a set of field name: field value pairs.

fn (Result) fields #

fn (r Result) fields() []Field

fields - returns an array of fields/columns.
The definitions apply primarily for columns of results, such as those produced by SELECT statements.

fn (Result) free #

fn (r &Result) free()

free - frees the memory used by a result

struct Row #

struct Row {
pub mut:
	vals []string