mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
mysql: add error handling
This commit is contained in:
parent
33793a869f
commit
f806e0f9e5
@ -23,31 +23,29 @@ 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_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_query(mysql &C.MYSQL, q byteptr) int
|
||||||
fn C.mysql_error(mysql &C.MYSQL) byteptr
|
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_num_fields(res &C.MYSQL_RES) int
|
||||||
fn C.mysql_store_result(mysql &C.MYSQL) &C.MYSQL_RES
|
fn C.mysql_store_result(mysql &C.MYSQL) &C.MYSQL_RES
|
||||||
fn C.mysql_fetch_row(res &C.MYSQL_RES) &byteptr
|
fn C.mysql_fetch_row(res &C.MYSQL_RES) &byteptr
|
||||||
fn C.mysql_free_result(res &C.MYSQL_RES)
|
fn C.mysql_free_result(res &C.MYSQL_RES)
|
||||||
fn C.mysql_close(sock &C.MYSQL)
|
fn C.mysql_close(sock &C.MYSQL)
|
||||||
|
|
||||||
pub fn connect(server, user, passwd, dbname string) DB {
|
pub fn connect(server, user, passwd, dbname string) ?DB {
|
||||||
conn := C.mysql_init(0)
|
conn := C.mysql_init(0)
|
||||||
if isnil(conn) {
|
if isnil(conn) {
|
||||||
eprintln('mysql_init failed')
|
return error_with_code(get_error_msg(conn), get_errno(conn))
|
||||||
exit(1)
|
|
||||||
}
|
}
|
||||||
conn2 := C.mysql_real_connect(conn, server.str, user.str, passwd.str, dbname.str, 0, 0, 0)
|
conn2 := C.mysql_real_connect(conn, server.str, user.str, passwd.str, dbname.str, 0, 0, 0)
|
||||||
if isnil(conn2) {
|
if isnil(conn2) {
|
||||||
eprintln('mysql_real_connect failed')
|
return error_with_code(get_error_msg(conn), get_errno(conn))
|
||||||
exit(1)
|
|
||||||
}
|
}
|
||||||
return DB {conn: conn2}
|
return DB {conn: conn2}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (db DB) query(q string) Result {
|
pub fn (db DB) query(q string) ?Result {
|
||||||
ret := C.mysql_query(db.conn, q.str)
|
ret := C.mysql_query(db.conn, q.str)
|
||||||
if ret != 0 {
|
if ret != 0 {
|
||||||
C.fprintf(stderr, '%s\n', mysql_error(db.conn))
|
return error_with_code(get_error_msg(db.conn), get_errno(db.conn))
|
||||||
exit(1)
|
|
||||||
}
|
}
|
||||||
res := C.mysql_store_result(db.conn)
|
res := C.mysql_store_result(db.conn)
|
||||||
return Result {result: res}
|
return Result {result: res}
|
||||||
@ -85,3 +83,11 @@ pub fn (r Result) rows() []Row {
|
|||||||
pub fn (r Result) free() {
|
pub fn (r Result) free() {
|
||||||
C.mysql_free_result(r.result)
|
C.mysql_free_result(r.result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user