2019-08-18 17:24:43 +03:00
|
|
|
module mysql
|
|
|
|
|
|
|
|
#flag -lmysqlclient
|
|
|
|
#include <mysql.h>
|
|
|
|
|
2019-12-02 17:26:58 +03:00
|
|
|
pub struct DB {
|
|
|
|
conn &C.MYSQL
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
|
|
|
|
2019-12-02 17:26:58 +03:00
|
|
|
pub struct Result {
|
|
|
|
result &C.MYSQL_RES
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
|
|
|
|
2019-12-02 17:26:58 +03:00
|
|
|
pub struct Row {
|
2019-09-26 03:18:26 +03:00
|
|
|
pub mut:
|
2019-08-18 17:24:43 +03:00
|
|
|
vals []string
|
|
|
|
}
|
|
|
|
|
2019-12-02 17:26:58 +03:00
|
|
|
struct C.MYSQL
|
|
|
|
struct C.MYSQL_RES
|
|
|
|
|
|
|
|
fn C.mysql_init(mysql &C.MYSQL) &C.MYSQL
|
|
|
|
fn C.mysql_real_connect(mysql &C.MYSQL, host byteptr, user byteptr, passwd byteptr, db byteptr, port u32, unix_socket byteptr, clientflag u64) &C.MYSQL
|
|
|
|
fn C.mysql_query(mysql &C.MYSQL, q byteptr) int
|
|
|
|
fn C.mysql_error(mysql &C.MYSQL) byteptr
|
2019-12-03 11:18:20 +03:00
|
|
|
fn C.mysql_errno(mysql &C.MYSQL) int
|
2019-12-02 17:26:58 +03:00
|
|
|
fn C.mysql_num_fields(res &C.MYSQL_RES) int
|
|
|
|
fn C.mysql_store_result(mysql &C.MYSQL) &C.MYSQL_RES
|
|
|
|
fn C.mysql_fetch_row(res &C.MYSQL_RES) &byteptr
|
|
|
|
fn C.mysql_free_result(res &C.MYSQL_RES)
|
|
|
|
fn C.mysql_close(sock &C.MYSQL)
|
2019-08-18 17:24:43 +03:00
|
|
|
|
2019-12-03 11:18:20 +03:00
|
|
|
pub fn connect(server, user, passwd, dbname string) ?DB {
|
2019-08-18 17:24:43 +03:00
|
|
|
conn := C.mysql_init(0)
|
|
|
|
if isnil(conn) {
|
2019-12-03 11:18:20 +03:00
|
|
|
return error_with_code(get_error_msg(conn), get_errno(conn))
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
|
|
|
conn2 := C.mysql_real_connect(conn, server.str, user.str, passwd.str, dbname.str, 0, 0, 0)
|
|
|
|
if isnil(conn2) {
|
2019-12-03 11:18:20 +03:00
|
|
|
return error_with_code(get_error_msg(conn), get_errno(conn))
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
|
|
|
return DB {conn: conn2}
|
|
|
|
}
|
|
|
|
|
2019-12-03 11:18:20 +03:00
|
|
|
pub fn (db DB) query(q string) ?Result {
|
2019-08-18 17:24:43 +03:00
|
|
|
ret := C.mysql_query(db.conn, q.str)
|
|
|
|
if ret != 0 {
|
2019-12-03 11:18:20 +03:00
|
|
|
return error_with_code(get_error_msg(db.conn), get_errno(db.conn))
|
2019-08-18 17:24:43 +03:00
|
|
|
}
|
|
|
|
res := C.mysql_store_result(db.conn)
|
|
|
|
return Result {result: res}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (db DB) close() {
|
|
|
|
C.mysql_close(db.conn)
|
|
|
|
}
|
|
|
|
|
2019-09-26 03:18:26 +03:00
|
|
|
pub fn (r Result) fetch_row() &byteptr {
|
2019-08-18 17:24:43 +03:00
|
|
|
return C.mysql_fetch_row(r.result)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (r Result) num_fields() int {
|
|
|
|
return C.mysql_num_fields(r.result)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (r Result) rows() []Row {
|
|
|
|
mut rows := []Row
|
|
|
|
nr_cols := r.num_fields()
|
|
|
|
for rr := r.fetch_row(); rr; rr = r.fetch_row() {
|
|
|
|
mut row := Row{}
|
|
|
|
for i := 0; i < nr_cols; i++ {
|
|
|
|
if rr[i] == 0 {
|
|
|
|
row.vals << ''
|
|
|
|
} else {
|
|
|
|
row.vals << string(rr[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rows << row
|
|
|
|
}
|
|
|
|
return rows
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (r Result) free() {
|
|
|
|
C.mysql_free_result(r.result)
|
|
|
|
}
|
2019-12-03 11:18:20 +03:00
|
|
|
|
|
|
|
fn get_error_msg(conn &C.MYSQL) string {
|
|
|
|
return string(C.mysql_error(conn))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_errno(conn &C.MYSQL) int {
|
|
|
|
return C.mysql_errno(conn)
|
|
|
|
}
|