mysql: wrap more APIs & organize module

This commit is contained in:
Don Alfons Nisnoni 2020-02-02 19:39:57 +08:00 committed by GitHub
parent 377d8dc42c
commit 4fc52948b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 445 additions and 95 deletions

View file

@ -1,118 +1,169 @@
module mysql
// if mysql.h is not in your CPATH (include path), set environment CPATH
// export CPATH=$CPATH:/usr/include/mysql
// or include -cflags flag to v compiler
// v -cflags '-I/usr/include/mysql' program.v
#flag -lmysqlclient
#flag linux -I/usr/include/mysql
#include <mysql.h>
pub struct DB {
conn &C.MYSQL
pub struct Connection {
host string
port u32
username string
password string
dbname string
flag int
mut:
conn &MYSQL
}
pub struct Result {
result &C.MYSQL_RES
}
pub struct Row {
pub mut:
vals []string
}
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_select_db(mysql &C.MYSQL, db byteptr) int
fn C.mysql_error(mysql &C.MYSQL) byteptr
fn C.mysql_errno(mysql &C.MYSQL) int
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_real_escape_string_quote(mysql &C.MYSQL, to byteptr, from byteptr, len u64, quote byte) u64
fn C.mysql_close(sock &C.MYSQL)
pub fn connect(server, user, passwd, dbname string) ?DB {
conn := C.mysql_init(0)
if isnil(conn) {
return error_with_code(get_error_msg(conn), get_errno(conn))
pub fn new_connection(host, username, password, dbname string) ?Connection {
instance := mysql_init(0)
if isnil(instance) {
return error_with_code(get_error_msg(instance), get_errno(instance))
}
conn2 := C.mysql_real_connect(conn, server.str, user.str, passwd.str, dbname.str, 0, 0, 0)
if isnil(conn2) {
return error_with_code(get_error_msg(conn), get_errno(conn))
return Connection{ host, 0, username, password, dbname, 0, instance }
}
pub fn (conn mut Connection) connect() ?bool {
mut instance := mysql_init(0)
if !isnil(conn.conn) {
instance = conn.conn
}
return DB {conn: conn2}
}
pub fn (db DB) query(q string) ?Result {
ret := C.mysql_query(db.conn, q.str)
if ret != 0 {
return error_with_code(get_error_msg(db.conn), get_errno(db.conn))
if isnil(instance) {
return error_with_code(get_error_msg(instance), get_errno(instance))
}
res := C.mysql_store_result(db.conn)
return Result {result: res}
}
pub fn (db DB) escape_string(s string) string {
len := strlen(s.str)
to := malloc(2 * len + 1)
quote := byte(39) // single quote
C.mysql_real_escape_string_quote(db.conn, to, s.str, len, quote)
return string(to)
}
pub fn (db DB) select_db(dbname string) ?bool {
ret := mysql_select_db(db.conn, dbname.str)
if ret != 0 {
return error_with_code(get_error_msg(db.conn), get_errno(db.conn))
conn.conn = C.mysql_real_connect(
instance,
conn.host.str,
conn.username.str,
conn.password.str,
conn.dbname.str,
conn.port,
0,
conn.flag
)
if isnil(conn.conn) {
return error_with_code(get_error_msg(instance), get_errno(instance))
}
return true
}
pub fn (db DB) close() {
C.mysql_close(db.conn)
}
pub fn (r Result) fetch_row() &byteptr {
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
pub fn (conn Connection) query(q string) ?Result {
if mysql_query(conn.conn, q.str) != 0 {
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
}
return rows
res := mysql_store_result(conn.conn)
return Result{res}
}
pub fn (r Result) free() {
C.mysql_free_result(r.result)
pub fn (conn Connection) select_db(dbname string) ?bool {
if mysql_select_db(conn.conn, dbname.str) != 0 {
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
}
return true
}
fn get_error_msg(conn &C.MYSQL) string {
return string(C.mysql_error(conn))
pub fn (conn Connection) change_user(username, password, dbname string) ?bool {
mut ret := true
if (dbname != '') {
ret = mysql_change_user(conn.conn, username.str, password.str, dbname.str)
} else {
ret = mysql_change_user(conn.conn, username.str, password.str, 0)
}
if !ret {
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
}
return ret
}
fn get_errno(conn &C.MYSQL) int {
return C.mysql_errno(conn)
pub fn (conn Connection) affected_rows() u64 {
return mysql_affected_rows(conn.conn)
}
pub fn (conn Connection) autocommit(mode bool) {
mysql_autocommit(conn.conn, mode)
}
pub fn (conn Connection) escape_string(s string) string {
len := strlen(s.str)
to := malloc(2 * len + 1)
quote := byte(39) // single quote
mysql_real_escape_string_quote(conn.conn, to, s.str, len, quote)
return string(to)
}
pub fn (conn Connection) set_option(option_type int, val voidptr) {
mysql_options(conn.conn, option_type, val)
}
pub fn (conn Connection) get_option(option_type int) ?voidptr {
ret := voidptr(0)
if mysql_get_option(conn.conn, option_type, &ret) != 0 {
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
}
return ret
}
pub fn (conn Connection) refresh(options u32) ?bool {
if mysql_refresh(conn.conn, options) != 0 {
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
}
return true
}
pub fn (conn Connection) reset_connection() ?bool {
if mysql_reset_connection(conn.conn) != 0 {
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
}
return true
}
pub fn (conn Connection) ping() ?bool {
if mysql_ping(conn.conn) != 0 {
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
}
return true
}
pub fn (conn Connection) close() {
mysql_close(conn.conn)
}
/* MYSQL INFO & VERSION */
pub fn (conn Connection) info() string {
return string(mysql_info(conn.conn))
}
pub fn (conn Connection) get_host_info() string {
return string(mysql_get_host_info(conn.conn))
}
pub fn (conn Connection) get_server_info() string {
return string(mysql_get_server_info(conn.conn))
}
pub fn (conn Connection) get_server_version() u64 {
return mysql_get_server_version(conn.conn)
}
pub fn get_client_version() u64 {
return mysql_get_client_version()
}
pub fn get_client_info() string {
return string(mysql_get_client_info())
}
/* MYSQL DEBUG */
pub fn (conn Connection) dump_debug_info() ?bool {
if mysql_dump_debug_info(conn.conn) != 0 {
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
}
return true
}
pub fn debug(debug string) {
mysql_debug(debug.str)
}