mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
mysql: connection fixes (#18428)
This commit is contained in:
parent
7f178d4662
commit
f634f7b01f
@ -1,14 +1,13 @@
|
|||||||
import db.mysql
|
import db.mysql
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
mut conn := mysql.Connection{
|
mut conn := mysql.connect(
|
||||||
host: 'localhost'
|
host: 'localhost'
|
||||||
port: 3306
|
port: 3306
|
||||||
username: 'root'
|
username: 'root'
|
||||||
password: ''
|
password: ''
|
||||||
dbname: 'mysql'
|
dbname: 'mysql'
|
||||||
}
|
)!
|
||||||
conn.connect()!
|
|
||||||
res := conn.query('show tables')!
|
res := conn.query('show tables')!
|
||||||
for row in res.rows() {
|
for row in res.rows() {
|
||||||
println(row.vals.join(', '))
|
println(row.vals.join(', '))
|
||||||
|
@ -102,14 +102,13 @@ fn sqlite3_array() ! {
|
|||||||
|
|
||||||
fn msql_array() ! {
|
fn msql_array() ! {
|
||||||
eprintln('------------ ${@METHOD} -----------------')
|
eprintln('------------ ${@METHOD} -----------------')
|
||||||
mut db := mysql.Connection{
|
mut db := mysql.connect(
|
||||||
host: mysql_host
|
host: mysql_host
|
||||||
port: mysql_port
|
port: mysql_port
|
||||||
username: mysql_user
|
username: mysql_user
|
||||||
password: mysql_pass
|
password: mysql_pass
|
||||||
dbname: mysql_db
|
dbname: mysql_db
|
||||||
}
|
)!
|
||||||
db.connect()!
|
|
||||||
defer {
|
defer {
|
||||||
sql db {
|
sql db {
|
||||||
drop table Parent
|
drop table Parent
|
||||||
@ -213,14 +212,13 @@ fn sqlite3() ! {
|
|||||||
|
|
||||||
fn msql() ! {
|
fn msql() ! {
|
||||||
eprintln('------------ ${@METHOD} -----------------')
|
eprintln('------------ ${@METHOD} -----------------')
|
||||||
mut conn := mysql.Connection{
|
mut conn := mysql.connect(
|
||||||
host: mysql_host
|
host: mysql_host
|
||||||
port: mysql_port
|
port: mysql_port
|
||||||
username: mysql_user
|
username: mysql_user
|
||||||
password: mysql_pass
|
password: mysql_pass
|
||||||
dbname: mysql_db
|
dbname: mysql_db
|
||||||
}
|
)!
|
||||||
conn.connect()!
|
|
||||||
defer {
|
defer {
|
||||||
conn.query('DROP TABLE IF EXISTS Module') or { eprintln(err) }
|
conn.query('DROP TABLE IF EXISTS Module') or { eprintln(err) }
|
||||||
conn.query('DROP TABLE IF EXISTS User') or { eprintln(err) }
|
conn.query('DROP TABLE IF EXISTS User') or { eprintln(err) }
|
||||||
|
@ -21,7 +21,12 @@ struct SQLError {
|
|||||||
MessageError
|
MessageError
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Connection {
|
pub struct DB {
|
||||||
|
mut:
|
||||||
|
conn &C.MYSQL = unsafe { nil }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Config {
|
||||||
mut:
|
mut:
|
||||||
conn &C.MYSQL = C.mysql_init(0)
|
conn &C.MYSQL = C.mysql_init(0)
|
||||||
pub mut:
|
pub mut:
|
||||||
@ -34,27 +39,35 @@ pub mut:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// connect attempts to establish a connection to a MySQL server.
|
// connect attempts to establish a connection to a MySQL server.
|
||||||
pub fn (mut c Connection) connect() !bool {
|
pub fn connect(config Config) !DB {
|
||||||
instance := C.mysql_init(c.conn)
|
mut db := DB{
|
||||||
c.conn = C.mysql_real_connect(instance, c.host.str, c.username.str, c.password.str,
|
conn: C.mysql_init(0)
|
||||||
c.dbname.str, c.port, 0, c.flag)
|
|
||||||
|
|
||||||
if isnil(c.conn) {
|
|
||||||
c.throw_mysql_error()!
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
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()!
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// query executes the SQL statement pointed to by the string `q`.
|
// query executes the SQL statement pointed to by the string `q`.
|
||||||
// It cannot be used for statements that contain binary data;
|
// It cannot be used for statements that contain binary data;
|
||||||
// Use `real_query()` instead.
|
// Use `real_query()` instead.
|
||||||
pub fn (c &Connection) query(q string) !Result {
|
pub fn (db &DB) query(q string) !Result {
|
||||||
if C.mysql_query(c.conn, q.str) != 0 {
|
if C.mysql_query(db.conn, q.str) != 0 {
|
||||||
c.throw_mysql_error()!
|
db.throw_mysql_error()!
|
||||||
}
|
}
|
||||||
|
|
||||||
result := C.mysql_store_result(c.conn)
|
result := C.mysql_store_result(db.conn)
|
||||||
return Result{result}
|
return Result{result}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,8 +79,8 @@ pub fn (c &Connection) query(q string) !Result {
|
|||||||
// without storing it in a temporary table or local buffer,
|
// 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().
|
// 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.
|
// You must mysql_free_result() after you are done with the result set.
|
||||||
pub fn (c &Connection) use_result() {
|
pub fn (db &DB) use_result() {
|
||||||
C.mysql_use_result(c.conn)
|
C.mysql_use_result(db.conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
// real_query makes an SQL query and receive the results.
|
// real_query makes an SQL query and receive the results.
|
||||||
@ -75,20 +88,20 @@ pub fn (c &Connection) use_result() {
|
|||||||
// (Binary data may contain the `\0` character, which `query()`
|
// (Binary data may contain the `\0` character, which `query()`
|
||||||
// interprets as the end of the statement string). In addition,
|
// interprets as the end of the statement string). In addition,
|
||||||
// `real_query()` is faster than `query()`.
|
// `real_query()` is faster than `query()`.
|
||||||
pub fn (mut c Connection) real_query(q string) !Result {
|
pub fn (mut db DB) real_query(q string) !Result {
|
||||||
if C.mysql_real_query(c.conn, q.str, q.len) != 0 {
|
if C.mysql_real_query(db.conn, q.str, q.len) != 0 {
|
||||||
c.throw_mysql_error()!
|
db.throw_mysql_error()!
|
||||||
}
|
}
|
||||||
|
|
||||||
result := C.mysql_store_result(c.conn)
|
result := C.mysql_store_result(db.conn)
|
||||||
return Result{result}
|
return Result{result}
|
||||||
}
|
}
|
||||||
|
|
||||||
// select_db causes the database specified by `db` to become
|
// select_db causes the database specified by `db` to become
|
||||||
// the default (current) database on the connection specified by mysql.
|
// the default (current) database on the connection specified by mysql.
|
||||||
pub fn (mut c Connection) select_db(dbname string) !bool {
|
pub fn (mut db DB) select_db(dbname string) !bool {
|
||||||
if C.mysql_select_db(c.conn, dbname.str) != 0 {
|
if C.mysql_select_db(db.conn, dbname.str) != 0 {
|
||||||
c.throw_mysql_error()!
|
db.throw_mysql_error()!
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
@ -97,16 +110,16 @@ pub fn (mut c Connection) select_db(dbname string) !bool {
|
|||||||
// change_user changes the mysql user for the connection.
|
// change_user changes the mysql user for the connection.
|
||||||
// Passing an empty string for the `dbname` parameter, resultsg in only changing
|
// Passing an empty string for the `dbname` parameter, resultsg in only changing
|
||||||
// the user and not changing the default database for the connection.
|
// the user and not changing the default database for the connection.
|
||||||
pub fn (mut c Connection) change_user(username string, password string, dbname string) !bool {
|
pub fn (mut db DB) change_user(username string, password string, dbname string) !bool {
|
||||||
mut result := true
|
mut result := true
|
||||||
|
|
||||||
if dbname != '' {
|
if dbname != '' {
|
||||||
result = C.mysql_change_user(c.conn, username.str, password.str, dbname.str)
|
result = C.mysql_change_user(db.conn, username.str, password.str, dbname.str)
|
||||||
} else {
|
} else {
|
||||||
result = C.mysql_change_user(c.conn, username.str, password.str, 0)
|
result = C.mysql_change_user(db.conn, username.str, password.str, 0)
|
||||||
}
|
}
|
||||||
if !result {
|
if !result {
|
||||||
c.throw_mysql_error()!
|
db.throw_mysql_error()!
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
@ -114,28 +127,28 @@ pub fn (mut c Connection) change_user(username string, password string, dbname s
|
|||||||
|
|
||||||
// affected_rows returns the number of rows changed, deleted,
|
// affected_rows returns the number of rows changed, deleted,
|
||||||
// or inserted by the last statement if it was an `UPDATE`, `DELETE`, or `INSERT`.
|
// or inserted by the last statement if it was an `UPDATE`, `DELETE`, or `INSERT`.
|
||||||
pub fn (c &Connection) affected_rows() u64 {
|
pub fn (db &DB) affected_rows() u64 {
|
||||||
return C.mysql_affected_rows(c.conn)
|
return C.mysql_affected_rows(db.conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
// autocommit turns on/off the auto-committing mode for the connection.
|
// autocommit turns on/off the auto-committing mode for the connection.
|
||||||
// When it is on, then each query is committed right away.
|
// When it is on, then each query is committed right away.
|
||||||
pub fn (mut c Connection) autocommit(mode bool) ! {
|
pub fn (mut db DB) autocommit(mode bool) ! {
|
||||||
c.check_connection_is_established()!
|
db.check_connection_is_established()!
|
||||||
result := C.mysql_autocommit(c.conn, mode)
|
result := C.mysql_autocommit(db.conn, mode)
|
||||||
|
|
||||||
if result != 0 {
|
if result != 0 {
|
||||||
c.throw_mysql_error()!
|
db.throw_mysql_error()!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// commit commits the current transaction.
|
// commit commits the current transaction.
|
||||||
pub fn (c &Connection) commit() ! {
|
pub fn (db &DB) commit() ! {
|
||||||
c.check_connection_is_established()!
|
db.check_connection_is_established()!
|
||||||
result := C.mysql_commit(c.conn)
|
result := C.mysql_commit(db.conn)
|
||||||
|
|
||||||
if result != 0 {
|
if result != 0 {
|
||||||
c.throw_mysql_error()!
|
db.throw_mysql_error()!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,10 +157,10 @@ pub fn (c &Connection) commit() ! {
|
|||||||
// The `wildcard` parameter may contain the wildcard characters `%` or `_`.
|
// The `wildcard` parameter may contain the wildcard characters `%` or `_`.
|
||||||
// If an empty string is passed, it will return all tables.
|
// If an empty string is passed, it will return all tables.
|
||||||
// Calling `tables()` is similar to executing query `SHOW TABLES [LIKE wildcard]`.
|
// Calling `tables()` is similar to executing query `SHOW TABLES [LIKE wildcard]`.
|
||||||
pub fn (c &Connection) tables(wildcard string) ![]string {
|
pub fn (db &DB) tables(wildcard string) ![]string {
|
||||||
c_mysql_result := C.mysql_list_tables(c.conn, wildcard.str)
|
c_mysql_result := C.mysql_list_tables(db.conn, wildcard.str)
|
||||||
if isnil(c_mysql_result) {
|
if isnil(c_mysql_result) {
|
||||||
c.throw_mysql_error()!
|
db.throw_mysql_error()!
|
||||||
}
|
}
|
||||||
|
|
||||||
result := Result{c_mysql_result}
|
result := Result{c_mysql_result}
|
||||||
@ -163,10 +176,10 @@ pub fn (c &Connection) tables(wildcard string) ![]string {
|
|||||||
// escape_string creates a legal SQL string for use in an SQL statement.
|
// escape_string creates a legal SQL string for use in an SQL statement.
|
||||||
// The `s` argument is encoded to produce an escaped SQL string,
|
// The `s` argument is encoded to produce an escaped SQL string,
|
||||||
// taking into account the current character set of the connection.
|
// taking into account the current character set of the connection.
|
||||||
pub fn (c &Connection) escape_string(s string) string {
|
pub fn (db &DB) escape_string(s string) string {
|
||||||
unsafe {
|
unsafe {
|
||||||
to := malloc_noscan(2 * s.len + 1)
|
to := malloc_noscan(2 * s.len + 1)
|
||||||
C.mysql_real_escape_string(c.conn, to, s.str, s.len)
|
C.mysql_real_escape_string(db.conn, to, s.str, s.len)
|
||||||
return to.vstring()
|
return to.vstring()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -174,16 +187,16 @@ pub fn (c &Connection) escape_string(s string) string {
|
|||||||
// set_option sets extra connect options that affect the behavior of
|
// set_option sets extra connect options that affect the behavior of
|
||||||
// a connection. This function may be called multiple times to set several
|
// a connection. This function may be called multiple times to set several
|
||||||
// options. To retrieve the current values for an option, use `get_option()`.
|
// options. To retrieve the current values for an option, use `get_option()`.
|
||||||
pub fn (mut c Connection) set_option(option_type int, val voidptr) {
|
pub fn (mut db DB) set_option(option_type int, val voidptr) {
|
||||||
C.mysql_options(c.conn, option_type, val)
|
C.mysql_options(db.conn, option_type, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
// get_option returns the value of an option, settable by `set_option`.
|
// get_option returns the value of an option, settable by `set_option`.
|
||||||
// https://dev.mysql.com/doc/c-api/5.7/en/mysql-get-option.html
|
// https://dev.mysql.com/doc/c-api/5.7/en/mysql-get-option.html
|
||||||
pub fn (c &Connection) get_option(option_type int) !voidptr {
|
pub fn (db &DB) get_option(option_type int) !voidptr {
|
||||||
mysql_option := unsafe { nil }
|
mysql_option := unsafe { nil }
|
||||||
if C.mysql_get_option(c.conn, option_type, &mysql_option) != 0 {
|
if C.mysql_get_option(db.conn, option_type, &mysql_option) != 0 {
|
||||||
c.throw_mysql_error()!
|
db.throw_mysql_error()!
|
||||||
}
|
}
|
||||||
|
|
||||||
return mysql_option
|
return mysql_option
|
||||||
@ -191,18 +204,18 @@ pub fn (c &Connection) get_option(option_type int) !voidptr {
|
|||||||
|
|
||||||
// refresh flush the tables or caches, or resets replication server
|
// refresh flush the tables or caches, or resets replication server
|
||||||
// information. The connected user must have the `RELOAD` privilege.
|
// information. The connected user must have the `RELOAD` privilege.
|
||||||
pub fn (mut c Connection) refresh(options u32) !bool {
|
pub fn (mut db DB) refresh(options u32) !bool {
|
||||||
if C.mysql_refresh(c.conn, options) != 0 {
|
if C.mysql_refresh(db.conn, options) != 0 {
|
||||||
c.throw_mysql_error()!
|
db.throw_mysql_error()!
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// reset resets the connection, and clear the session state.
|
// reset resets the connection, and clear the session state.
|
||||||
pub fn (mut c Connection) reset() !bool {
|
pub fn (mut db DB) reset() !bool {
|
||||||
if C.mysql_reset_connection(c.conn) != 0 {
|
if C.mysql_reset_connection(db.conn) != 0 {
|
||||||
c.throw_mysql_error()!
|
db.throw_mysql_error()!
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
@ -210,50 +223,50 @@ pub fn (mut c Connection) reset() !bool {
|
|||||||
|
|
||||||
// ping pings a server connection, or tries to reconnect if the connection
|
// ping pings a server connection, or tries to reconnect if the connection
|
||||||
// has gone down.
|
// has gone down.
|
||||||
pub fn (mut c Connection) ping() !bool {
|
pub fn (mut db DB) ping() !bool {
|
||||||
if C.mysql_ping(c.conn) != 0 {
|
if C.mysql_ping(db.conn) != 0 {
|
||||||
c.throw_mysql_error()!
|
db.throw_mysql_error()!
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// close closes the connection.
|
// close closes the connection.
|
||||||
pub fn (mut c Connection) close() {
|
pub fn (mut db DB) close() {
|
||||||
C.mysql_close(c.conn)
|
C.mysql_close(db.conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
// info returns information about the most recently executed query.
|
// 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
|
// See more on https://dev.mysql.com/doc/c-api/8.0/en/mysql-info.html
|
||||||
pub fn (c &Connection) info() string {
|
pub fn (db &DB) info() string {
|
||||||
return resolve_nil_str(C.mysql_info(c.conn))
|
return resolve_nil_str(C.mysql_info(db.conn))
|
||||||
}
|
}
|
||||||
|
|
||||||
// get_host_info returns a string describing the type of connection in use,
|
// get_host_info returns a string describing the type of connection in use,
|
||||||
// including the server host name.
|
// including the server host name.
|
||||||
pub fn (c &Connection) get_host_info() string {
|
pub fn (db &DB) get_host_info() string {
|
||||||
return unsafe { C.mysql_get_host_info(c.conn).vstring() }
|
return unsafe { C.mysql_get_host_info(db.conn).vstring() }
|
||||||
}
|
}
|
||||||
|
|
||||||
// get_server_info returns a string representing the MySQL server version.
|
// get_server_info returns a string representing the MySQL server version.
|
||||||
// For example, `8.0.24`.
|
// For example, `8.0.24`.
|
||||||
pub fn (c &Connection) get_server_info() string {
|
pub fn (db &DB) get_server_info() string {
|
||||||
return unsafe { C.mysql_get_server_info(c.conn).vstring() }
|
return unsafe { C.mysql_get_server_info(db.conn).vstring() }
|
||||||
}
|
}
|
||||||
|
|
||||||
// get_server_version returns an integer, representing the MySQL server
|
// get_server_version returns an integer, representing the MySQL server
|
||||||
// version. The value has the format `XYYZZ` where `X` is the major version,
|
// 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
|
// `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`.
|
// within the release level. For example, `8.0.24` is returned as `80024`.
|
||||||
pub fn (c &Connection) get_server_version() u64 {
|
pub fn (db &DB) get_server_version() u64 {
|
||||||
return C.mysql_get_server_version(c.conn)
|
return C.mysql_get_server_version(db.conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
// dump_debug_info instructs the server to write debugging information
|
// dump_debug_info instructs the server to write debugging information
|
||||||
// to the error log. The connected user must have the `SUPER` privilege.
|
// to the error log. The connected user must have the `SUPER` privilege.
|
||||||
pub fn (mut c Connection) dump_debug_info() !bool {
|
pub fn (mut db DB) dump_debug_info() !bool {
|
||||||
if C.mysql_dump_debug_info(c.conn) != 0 {
|
if C.mysql_dump_debug_info(db.conn) != 0 {
|
||||||
c.throw_mysql_error()!
|
db.throw_mysql_error()!
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
@ -278,13 +291,13 @@ pub fn debug(debug string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
[inline]
|
||||||
fn (c &Connection) throw_mysql_error() ! {
|
fn (db &DB) throw_mysql_error() ! {
|
||||||
return error_with_code(get_error_msg(c.conn), get_errno(c.conn))
|
return error_with_code(get_error_msg(db.conn), get_errno(db.conn))
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
[inline]
|
||||||
fn (c &Connection) check_connection_is_established() ! {
|
fn (db &DB) check_connection_is_established() ! {
|
||||||
if isnil(c.conn) {
|
if isnil(db.conn) {
|
||||||
return error('No connection to a MySQL server, use `connect()` to connect to a database for working with it')
|
return error('No connection to a MySQL server, use `connect()` to connect to a database for working with it')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,14 +36,13 @@ struct TestDefaultAtribute {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn test_mysql_orm() {
|
fn test_mysql_orm() {
|
||||||
mut db := mysql.Connection{
|
mut db := mysql.connect(
|
||||||
host: 'localhost'
|
host: 'localhost'
|
||||||
port: 3306
|
port: 3306
|
||||||
username: 'root'
|
username: 'root'
|
||||||
password: ''
|
password: ''
|
||||||
dbname: 'mysql'
|
dbname: 'mysql'
|
||||||
}
|
)!
|
||||||
db.connect() or { panic(err) }
|
|
||||||
defer {
|
defer {
|
||||||
db.close()
|
db.close()
|
||||||
}
|
}
|
||||||
@ -124,7 +123,6 @@ fn test_mysql_orm() {
|
|||||||
WHERE TABLE_NAME = 'TestCustomSqlType'
|
WHERE TABLE_NAME = 'TestCustomSqlType'
|
||||||
ORDER BY ORDINAL_POSITION
|
ORDER BY ORDINAL_POSITION
|
||||||
") or {
|
") or {
|
||||||
println(err)
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ import orm
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
// @select is used internally by V's ORM for processing `SELECT ` queries.
|
// @select is used internally by V's ORM for processing `SELECT ` queries.
|
||||||
pub fn (db Connection) @select(config orm.SelectConfig, data orm.QueryData, where orm.QueryData) ![][]orm.Primitive {
|
pub fn (db DB) @select(config orm.SelectConfig, data orm.QueryData, where orm.QueryData) ![][]orm.Primitive {
|
||||||
query := orm.orm_select_gen(config, '`', false, '?', 0, where)
|
query := orm.orm_select_gen(config, '`', false, '?', 0, where)
|
||||||
mut result := [][]orm.Primitive{}
|
mut result := [][]orm.Primitive{}
|
||||||
mut stmt := db.init_stmt(query)
|
mut stmt := db.init_stmt(query)
|
||||||
@ -128,7 +128,7 @@ pub fn (db Connection) @select(config orm.SelectConfig, data orm.QueryData, wher
|
|||||||
}
|
}
|
||||||
|
|
||||||
// insert is used internally by V's ORM for processing `INSERT ` queries
|
// insert is used internally by V's ORM for processing `INSERT ` queries
|
||||||
pub fn (db Connection) insert(table string, data orm.QueryData) ! {
|
pub fn (db DB) insert(table string, data orm.QueryData) ! {
|
||||||
mut converted_primitive_array := db.convert_query_data_to_primitives(table, data)!
|
mut converted_primitive_array := db.convert_query_data_to_primitives(table, data)!
|
||||||
|
|
||||||
converted_primitive_data := orm.QueryData{
|
converted_primitive_data := orm.QueryData{
|
||||||
@ -145,20 +145,20 @@ pub fn (db Connection) insert(table string, data orm.QueryData) ! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// update is used internally by V's ORM for processing `UPDATE ` queries
|
// update is used internally by V's ORM for processing `UPDATE ` queries
|
||||||
pub fn (db Connection) update(table string, data orm.QueryData, where orm.QueryData) ! {
|
pub fn (db DB) update(table string, data orm.QueryData, where orm.QueryData) ! {
|
||||||
query, _ := orm.orm_stmt_gen(.default, table, '`', .update, false, '?', 1, data, where)
|
query, _ := orm.orm_stmt_gen(.default, table, '`', .update, false, '?', 1, data, where)
|
||||||
mysql_stmt_worker(db, query, data, where)!
|
mysql_stmt_worker(db, query, data, where)!
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete is used internally by V's ORM for processing `DELETE ` queries
|
// delete is used internally by V's ORM for processing `DELETE ` queries
|
||||||
pub fn (db Connection) delete(table string, where orm.QueryData) ! {
|
pub fn (db DB) delete(table string, where orm.QueryData) ! {
|
||||||
query, _ := orm.orm_stmt_gen(.default, table, '`', .delete, false, '?', 1, orm.QueryData{},
|
query, _ := orm.orm_stmt_gen(.default, table, '`', .delete, false, '?', 1, orm.QueryData{},
|
||||||
where)
|
where)
|
||||||
mysql_stmt_worker(db, query, orm.QueryData{}, where)!
|
mysql_stmt_worker(db, query, orm.QueryData{}, where)!
|
||||||
}
|
}
|
||||||
|
|
||||||
// last_id is used internally by V's ORM for post-processing `INSERT ` queries
|
// last_id is used internally by V's ORM for post-processing `INSERT ` queries
|
||||||
pub fn (db Connection) last_id() int {
|
pub fn (db DB) last_id() int {
|
||||||
query := 'SELECT last_insert_id();'
|
query := 'SELECT last_insert_id();'
|
||||||
id := db.query(query) or { return 0 }
|
id := db.query(query) or { return 0 }
|
||||||
|
|
||||||
@ -166,7 +166,7 @@ pub fn (db Connection) last_id() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create is used internally by V's ORM for processing table creation queries (DDL)
|
// create is used internally by V's ORM for processing table creation queries (DDL)
|
||||||
pub fn (db Connection) create(table string, fields []orm.TableField) ! {
|
pub fn (db DB) create(table string, fields []orm.TableField) ! {
|
||||||
query := orm.orm_table_gen(table, '`', true, 0, fields, mysql_type_from_v, false) or {
|
query := orm.orm_table_gen(table, '`', true, 0, fields, mysql_type_from_v, false) or {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -174,7 +174,7 @@ pub fn (db Connection) create(table string, fields []orm.TableField) ! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// drop is used internally by V's ORM for processing table destroying queries (DDL)
|
// drop is used internally by V's ORM for processing table destroying queries (DDL)
|
||||||
pub fn (db Connection) drop(table string) ! {
|
pub fn (db DB) drop(table string) ! {
|
||||||
query := 'DROP TABLE `${table}`;'
|
query := 'DROP TABLE `${table}`;'
|
||||||
mysql_stmt_worker(db, query, orm.QueryData{}, orm.QueryData{})!
|
mysql_stmt_worker(db, query, orm.QueryData{}, orm.QueryData{})!
|
||||||
}
|
}
|
||||||
@ -182,7 +182,7 @@ pub fn (db Connection) drop(table string) ! {
|
|||||||
// mysql_stmt_worker executes the `query` with the provided `data` and `where` parameters
|
// mysql_stmt_worker executes the `query` with the provided `data` and `where` parameters
|
||||||
// without returning the result.
|
// without returning the result.
|
||||||
// This is commonly used for `INSERT`, `UPDATE`, `CREATE`, `DROP`, and `DELETE` queries.
|
// This is commonly used for `INSERT`, `UPDATE`, `CREATE`, `DROP`, and `DELETE` queries.
|
||||||
fn mysql_stmt_worker(db Connection, query string, data orm.QueryData, where orm.QueryData) ! {
|
fn mysql_stmt_worker(db DB, query string, data orm.QueryData, where orm.QueryData) ! {
|
||||||
mut stmt := db.init_stmt(query)
|
mut stmt := db.init_stmt(query)
|
||||||
stmt.prepare()!
|
stmt.prepare()!
|
||||||
|
|
||||||
@ -360,7 +360,7 @@ fn mysql_type_from_v(typ int) !string {
|
|||||||
|
|
||||||
// convert_query_data_to_primitives converts the `data` representing the `QueryData`
|
// convert_query_data_to_primitives converts the `data` representing the `QueryData`
|
||||||
// into an array of `Primitive`.
|
// into an array of `Primitive`.
|
||||||
fn (db Connection) convert_query_data_to_primitives(table string, data orm.QueryData) ![]orm.Primitive {
|
fn (db DB) convert_query_data_to_primitives(table string, data orm.QueryData) ![]orm.Primitive {
|
||||||
mut column_type_map := db.get_table_column_type_map(table)!
|
mut column_type_map := db.get_table_column_type_map(table)!
|
||||||
mut converted_data := []orm.Primitive{}
|
mut converted_data := []orm.Primitive{}
|
||||||
|
|
||||||
@ -381,7 +381,7 @@ fn (db Connection) convert_query_data_to_primitives(table string, data orm.Query
|
|||||||
|
|
||||||
// get_table_column_type_map returns a map where the key represents the column name,
|
// get_table_column_type_map returns a map where the key represents the column name,
|
||||||
// and the value represents its data type.
|
// and the value represents its data type.
|
||||||
fn (db Connection) get_table_column_type_map(table string) !map[string]string {
|
fn (db DB) get_table_column_type_map(table string) !map[string]string {
|
||||||
data_type_query := "SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '${table}'"
|
data_type_query := "SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '${table}'"
|
||||||
mut column_type_map := map[string]string{}
|
mut column_type_map := map[string]string{}
|
||||||
results := db.query(data_type_query)!
|
results := db.query(data_type_query)!
|
||||||
|
@ -77,7 +77,7 @@ pub fn (s &Stmt) str() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// init_stmt creates a new statement, given the `query`.
|
// init_stmt creates a new statement, given the `query`.
|
||||||
pub fn (db Connection) init_stmt(query string) Stmt {
|
pub fn (db DB) init_stmt(query string) Stmt {
|
||||||
return Stmt{
|
return Stmt{
|
||||||
stmt: C.mysql_stmt_init(db.conn)
|
stmt: C.mysql_stmt_init(db.conn)
|
||||||
query: query
|
query: query
|
||||||
|
Loading…
Reference in New Issue
Block a user