1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

60
vlib/mysql/_cdefs.v Normal file
View File

@ -0,0 +1,60 @@
module mysql
struct C.MYSQL
struct C.MYSQL_RES
struct C.MYSQL_FIELD {
name byteptr /* Name of column */
org_name byteptr /* Original column name, if an alias */
table byteptr /* Table of column if column was a field */
org_table byteptr /* Org table name, if table was an alias */
db byteptr /* Database for table */
catalog byteptr /* Catalog for table */
def byteptr /* Default value (set by mysql_list_fields) */
length int /* Width of column (create length) */
max_length int /* Max width for selected set */
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 /* Div flags */
decimals u32 /* Number of decimals in field */
charsetnr u32 /* Character set */
@type int /* Type of field. See mysql_com.h for types */
}
fn C.mysql_init(mysql &MYSQL) &MYSQL
fn C.mysql_real_connect(mysql &MYSQL, host byteptr, user byteptr, passwd byteptr, db byteptr, port u32, unix_socket byteptr, clientflag u64) &MYSQL
fn C.mysql_query(mysql &MYSQL, q byteptr) int
fn C.mysql_select_db(mysql &MYSQL, db byteptr) int
fn C.mysql_change_user(mysql &MYSQL, user byteptr, password byteptr, db byteptr) bool
fn C.mysql_affected_rows(mysql &MYSQL) u64
fn C.mysql_options(mysql &MYSQL, option int, arg voidptr) int
fn C.mysql_get_option(mysql &MYSQL, option int, arg voidptr) int
fn C.mysql_num_fields(res &MYSQL_RES) int
fn C.mysql_autocommit(mysql MYSQL, mode bool)
fn C.mysql_refresh(mysql MYSQL, options u32) int
fn C.mysql_reset_connection(mysql MYSQL) int
fn C.mysql_ping(mysql MYSQL) int
fn C.mysql_store_result(mysql &MYSQL) &MYSQL_RES
fn C.mysql_fetch_row(res &MYSQL_RES) &byteptr
fn C.mysql_fetch_fields(res &MYSQL_RES) &MYSQL_FIELD
fn C.mysql_free_result(res &MYSQL_RES)
fn C.mysql_real_escape_string_quote(mysql &MYSQL, to byteptr, from byteptr, len u64, quote byte) u64
fn C.mysql_close(sock &MYSQL)
/* INFO & VERSION */
fn C.mysql_info(mysql &MYSQL) byteptr
fn C.mysql_get_host_info(mysql &MYSQL) byteptr
fn C.mysql_get_server_info(mysql &MYSQL) byteptr
fn C.mysql_get_server_version(mysql &MYSQL) u64
fn C.mysql_get_client_version() u64
fn C.mysql_get_client_info() byteptr
/* DEBUG & ERROR INFO */
fn C.mysql_error(mysql &MYSQL) byteptr
fn C.mysql_errno(mysql &MYSQL) int
fn C.mysql_dump_debug_info(mysql &MYSQL) int
fn C.mysql_debug(debug byteptr)

31
vlib/mysql/consts.v Normal file
View File

@ -0,0 +1,31 @@
module mysql
/* MYSQL CONNECT FLAGS */
pub const (
// CAN_HANDLE_EXPIRED_PASSWORDS = C.CAN_HANDLE_EXPIRED_PASSWORDS
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
)
/* MYSQL REFRESH FLAGS */
pub 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)
)

72
vlib/mysql/enums.v Normal file
View File

@ -0,0 +1,72 @@
module mysql
pub enum FieldType {
type_decimal
type_tiny
type_short
type_long
type_float
type_double
type_null
type_timestamp
type_longlong
type_int24
type_date
type_time
type_datetime
type_year
type_newdate
type_varchar
type_bit
type_timestamp2
type_datetime2
type_time2
type_json = 245
type_newdecimal
type_enum
type_set
type_tiny_blob
type_medium_blob
type_long_blob
type_blob
type_var_string
type_string
type_geometry
}
pub fn (f FieldType) str() string {
return match f {
0 { 'decimal' }
1 { 'tiny' }
2 { 'short' }
3 { 'long' }
4 { 'float' }
5 { 'double' }
6 { 'null' }
7 { 'timestamp' }
8 { 'longlong' }
9 { 'int24' }
10 { 'date' }
11 { 'time' }
12 { 'datetime' }
13 { 'year' }
14 { 'newdate' }
15 { 'varchar' }
16 { 'bit' }
17 { 'timestamp2' }
18 { 'datetime2' }
19 { 'time2' }
245 { 'json' }
246 { 'newdecimal' }
247 { 'enum' }
248 { 'set' }
249 { 'tiny_blob' }
250 { 'medium_blob' }
251 { 'long_blob' }
252 { 'blob' }
253 { 'var_string' }
254 { 'string' }
255 { 'geometry' }
else { 'unknown' }
}
}

View File

@ -1,118 +1,169 @@
module mysql 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 -lmysqlclient
#flag linux -I/usr/include/mysql #flag linux -I/usr/include/mysql
#include <mysql.h> #include <mysql.h>
pub struct DB { pub struct Connection {
conn &C.MYSQL host string
port u32
username string
password string
dbname string
flag int
mut:
conn &MYSQL
} }
pub struct Result { pub fn new_connection(host, username, password, dbname string) ?Connection {
result &C.MYSQL_RES instance := mysql_init(0)
} if isnil(instance) {
return error_with_code(get_error_msg(instance), get_errno(instance))
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))
} }
conn2 := C.mysql_real_connect(conn, server.str, user.str, passwd.str, dbname.str, 0, 0, 0) return Connection{ host, 0, username, password, dbname, 0, instance }
if isnil(conn2) { }
return error_with_code(get_error_msg(conn), get_errno(conn))
pub fn (conn mut Connection) connect() ?bool {
mut instance := mysql_init(0)
if !isnil(conn.conn) {
instance = conn.conn
} }
return DB {conn: conn2} if isnil(instance) {
} return error_with_code(get_error_msg(instance), get_errno(instance))
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))
} }
res := C.mysql_store_result(db.conn) conn.conn = C.mysql_real_connect(
return Result {result: res} instance,
} conn.host.str,
conn.username.str,
pub fn (db DB) escape_string(s string) string { conn.password.str,
len := strlen(s.str) conn.dbname.str,
to := malloc(2 * len + 1) conn.port,
quote := byte(39) // single quote 0,
conn.flag
C.mysql_real_escape_string_quote(db.conn, to, s.str, len, quote) )
return string(to) if isnil(conn.conn) {
} return error_with_code(get_error_msg(instance), get_errno(instance))
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))
} }
return true return true
} }
pub fn (db DB) close() { pub fn (conn Connection) query(q string) ?Result {
C.mysql_close(db.conn) if mysql_query(conn.conn, q.str) != 0 {
} return error_with_code(get_error_msg(conn.conn), get_errno(conn.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
} }
return rows res := mysql_store_result(conn.conn)
return Result{res}
} }
pub fn (r Result) free() { pub fn (conn Connection) select_db(dbname string) ?bool {
C.mysql_free_result(r.result) 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 { pub fn (conn Connection) change_user(username, password, dbname string) ?bool {
return string(C.mysql_error(conn)) 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 { pub fn (conn Connection) affected_rows() u64 {
return C.mysql_errno(conn) 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)
} }

120
vlib/mysql/result.v Normal file
View File

@ -0,0 +1,120 @@
module mysql
pub struct Result {
result &MYSQL_RES
}
pub struct Row {
pub mut:
vals []string
}
pub 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
}
pub fn (r Result) fetch_row() &byteptr {
return mysql_fetch_row(r.result)
}
pub fn (r Result) num_fields() int {
return 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) fetch_fields() []Field {
mut fields := []Field
nr_cols := r.num_fields()
orig_fields := mysql_fetch_fields(r.result)
for i := 0; i < nr_cols; i++ {
fields << Field{
name: string(orig_fields[i].name)
org_name: string(orig_fields[i].org_name)
table: string(orig_fields[i].table)
org_table: string(orig_fields[i].org_table)
db: string(orig_fields[i].db)
catalog: string(orig_fields[i].catalog)
def: resolve_nil_str(orig_fields[i].def)
length: orig_fields.length
max_length: orig_fields.max_length
name_length: orig_fields.name_length
org_name_length: orig_fields.org_name_length
table_length: orig_fields.table_length
org_table_length: orig_fields.org_table_length
db_length: orig_fields.db_length
catalog_length: orig_fields.catalog_length
def_length: orig_fields.def_length
flags: orig_fields.flags
decimals: orig_fields.decimals
charsetnr: orig_fields.charsetnr
type_: FieldType(orig_fields.@type)
}
}
return fields
}
pub fn (f Field) str() string {
return '
{
name: "$f.name"
org_name: "$f.org_name"
table: "$f.table"
org_table: "$f.org_table"
db: "$f.db"
catalog: "$f.catalog"
def: "$f.def"
length: $f.length
max_length: $f.max_length
name_length: $f.name_length
org_name_length: $f.org_name_length
table_length: $f.table_length
org_table_length: $f.org_table_length
db_length: $f.db_length
catalog_length: $f.catalog_length
def_length: $f.def_length
flags: $f.flags
decimals: $f.decimals
charsetnr: $f.charsetnr
type: ${f.type_.str()}
}
'
}
pub fn (r Result) free() {
mysql_free_result(r.result)
}

16
vlib/mysql/utils.v Normal file
View File

@ -0,0 +1,16 @@
module mysql
fn get_error_msg(conn &MYSQL) string {
return string(mysql_error(conn))
}
fn get_errno(conn &MYSQL) int {
return mysql_errno(conn)
}
fn resolve_nil_str(ptr byteptr) string {
if isnil(ptr) {
return ''
}
return string(ptr)
}