From 0066afe7fc3c4c8eee581877a136a014220af363 Mon Sep 17 00:00:00 2001 From: S-YOU Date: Sun, 18 Aug 2019 23:24:43 +0900 Subject: [PATCH] mysql module --- examples/.gitignore | 1 + examples/database/mysql.v | 11 +++++ vlib/mysql/mysql.v | 92 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 examples/database/mysql.v create mode 100644 vlib/mysql/mysql.v diff --git a/examples/.gitignore b/examples/.gitignore index 6347c26174..581f34c2ca 100644 --- a/examples/.gitignore +++ b/examples/.gitignore @@ -7,3 +7,4 @@ rune terminal_control nbody spectral +database/mysql diff --git a/examples/database/mysql.v b/examples/database/mysql.v new file mode 100644 index 0000000000..72ac398a99 --- /dev/null +++ b/examples/database/mysql.v @@ -0,0 +1,11 @@ +// import mysql + +// pub fn main() { +// conn := mysql.connect('localhost', 'root', '', 'mysql') +// res := conn.query('show tables') +// for row in res.rows() { +// println(row.vals.join(', ')) +// } +// res.free() +// conn.close() +// } diff --git a/vlib/mysql/mysql.v b/vlib/mysql/mysql.v new file mode 100644 index 0000000000..f33135d480 --- /dev/null +++ b/vlib/mysql/mysql.v @@ -0,0 +1,92 @@ +module mysql + +#flag -lmysqlclient +#include + +struct DB { + conn *C.MYSQL +} + +struct Result { + result *C.MYSQL_RES +} + +struct Row { +pub: + vals []string +} + +// C + +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 +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) + +// V + + +pub fn connect(server, user, passwd, dbname string) DB { + conn := C.mysql_init(0) + if isnil(conn) { + eprintln('mysql_init failed') + exit(1) + } + conn2 := C.mysql_real_connect(conn, server.str, user.str, passwd.str, dbname.str, 0, 0, 0) + if isnil(conn2) { + eprintln('mysql_real_connect failed') + exit(1) + } + return DB {conn: conn2} +} + +pub fn (db DB) query(q string) Result { + ret := C.mysql_query(db.conn, q.str) + if ret != 0 { + C.fprintf(stderr, '%s\n', mysql_error(db.conn)) + exit(1) + } + res := C.mysql_store_result(db.conn) + return Result {result: res} +} + +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 + } + return rows +} + +pub fn (r Result) free() { + C.mysql_free_result(r.result) +}