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

mysql: add mysql.escape_string()

This commit is contained in:
Robert Cristof 2019-12-13 10:12:42 -06:00 committed by Alexander Medvednikov
parent 056454dff5
commit ec36755407

View File

@ -1,5 +1,10 @@
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
#include <mysql.h> #include <mysql.h>
@ -28,6 +33,7 @@ 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_real_escape_string_quote(mysql &C.MYSQL, to byteptr, from byteptr, len u64, quote byte) u64
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 {
@ -51,6 +57,15 @@ pub fn (db DB) query(q string) ?Result {
return Result {result: res} 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) close() { pub fn (db DB) close() {
C.mysql_close(db.conn) C.mysql_close(db.conn)
} }
@ -91,3 +106,4 @@ fn get_error_msg(conn &C.MYSQL) string {
fn get_errno(conn &C.MYSQL) int { fn get_errno(conn &C.MYSQL) int {
return C.mysql_errno(conn) return C.mysql_errno(conn)
} }