pg, sqlite, mysql: run vfmt (#6650)

pull/6655/head
Lukas Neubert 2020-10-19 20:11:04 +02:00 committed by GitHub
parent 37d6503437
commit f37e936321
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 160 additions and 130 deletions

View File

@ -1,17 +1,21 @@
module mysql
struct C.MYSQL
struct C.MYSQL_RES
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 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
@ -19,45 +23,74 @@ struct C.MYSQL_FIELD {
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 */
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 &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_real_query(mysql &C.MYSQL, q byteptr, len u32) int
fn C.mysql_select_db(mysql &C.MYSQL, db byteptr) int
fn C.mysql_change_user(mysql &C.MYSQL, user byteptr, password byteptr, db byteptr) bool
fn C.mysql_affected_rows(mysql &C.MYSQL) u64
fn C.mysql_options(mysql &C.MYSQL, option int, arg voidptr) int
fn C.mysql_get_option(mysql &C.MYSQL, option int, arg voidptr) int
fn C.mysql_list_tables(mysql &C.MYSQL, wild byteptr) &C.MYSQL_RES
fn C.mysql_num_fields(res &C.MYSQL_RES) int
fn C.mysql_num_rows(res &C.MYSQL_RES) u64
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 &C.MYSQL) &C.MYSQL_RES
fn C.mysql_fetch_row(res &C.MYSQL_RES) &byteptr
fn C.mysql_fetch_fields(res &C.MYSQL_RES) &C.MYSQL_FIELD
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)
/* INFO & VERSION */
// INFO & VERSION
fn C.mysql_info(mysql &C.MYSQL) byteptr
fn C.mysql_get_host_info(mysql &C.MYSQL) byteptr
fn C.mysql_get_server_info(mysql &C.MYSQL) byteptr
fn C.mysql_get_server_version(mysql &C.MYSQL) u64
fn C.mysql_get_client_version() u64
fn C.mysql_get_client_info() byteptr
/* DEBUG & ERROR INFO */
// DEBUG & ERROR INFO
fn C.mysql_error(mysql &C.MYSQL) byteptr
fn C.mysql_errno(mysql &C.MYSQL) int
fn C.mysql_dump_debug_info(mysql &C.MYSQL) int
fn C.mysql_debug(debug byteptr)

View File

@ -1,6 +1,6 @@
module mysql
/* MYSQL CONNECT FLAGS */
// MYSQL CONNECT FLAGS
pub const (
// CAN_HANDLE_EXPIRED_PASSWORDS = C.CAN_HANDLE_EXPIRED_PASSWORDS
client_compress = C.CLIENT_COMPRESS
@ -18,7 +18,7 @@ pub const (
client_remember_options = C.CLIENT_REMEMBER_OPTIONS
)
/* MYSQL REFRESH FLAGS */
// MYSQL REFRESH FLAGS
pub const (
refresh_grant = u32(C.REFRESH_GRANT)
refresh_log = u32(C.REFRESH_LOG)

View File

@ -3,7 +3,6 @@ module mysql
#flag -lmysqlclient
#flag linux -I/usr/include/mysql
#include <mysql.h>
// TODO: Documentation
pub struct Connection {
mut:
@ -20,16 +19,8 @@ pub mut:
// connect connects to a MySQL server.
pub fn (mut conn Connection) connect() ?bool {
instance := C.mysql_init(conn.conn)
conn.conn = C.mysql_real_connect(
conn.conn,
conn.host.str,
conn.username.str,
conn.password.str,
conn.dbname.str,
conn.port,
0,
conn.flag
)
conn.conn = C.mysql_real_connect(conn.conn, 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))
}
@ -69,7 +60,7 @@ pub fn (conn Connection) select_db(dbname string) ?bool {
// change_user changes the user of the specified database connection.
// if desired, the empty string value can be passed to the `dbname` parameter
// resulting in only changing the user and not selecting a database.
pub fn (conn Connection) change_user(username, password, dbname string) ?bool {
pub fn (conn Connection) change_user(username string, password string, dbname string) ?bool {
mut ret := true
if dbname != '' {
ret = C.mysql_change_user(conn.conn, username.str, password.str, dbname.str)
@ -114,7 +105,6 @@ pub fn (conn Connection) escape_string(s string) string {
len := C.strlen(s.str)
to := malloc(2 * len + 1)
quote := byte(39) // single quote
C.mysql_real_escape_string_quote(conn.conn, to, s.str, len, quote)
return unsafe {to.vstring()}
}
@ -166,8 +156,7 @@ pub fn (conn &Connection) close() {
C.mysql_close(conn.conn)
}
/* -------------------------- MYSQL INFO & VERSION -------------------------- */
// -------------------------- MYSQL INFO & VERSION --------------------------
// info returns information about the most recently executed query.
pub fn (conn Connection) info() string {
return resolve_nil_str(C.mysql_info(conn.conn))
@ -188,8 +177,7 @@ pub fn (conn Connection) get_server_version() u64 {
return C.mysql_get_server_version(conn.conn)
}
/* --------------------------------- CLIENT --------------------------------- */
// --------------------------------- CLIENT ---------------------------------
// get_client_info returns client version information as a string.
pub fn get_client_info() string {
return unsafe {C.mysql_get_client_info().vstring()}
@ -200,8 +188,7 @@ pub fn get_client_version() u64 {
return C.mysql_get_client_version()
}
/* ------------------------------- MYSQL DEBUG ------------------------------ */
// ------------------------------- MYSQL DEBUG ------------------------------
// dump_debug_info causes the server to write debug information to the log
pub fn (conn Connection) dump_debug_info() ?bool {
if C.mysql_dump_debug_info(conn.conn) != 0 {

View File

@ -48,7 +48,7 @@ pub fn (r Result) maps() []map[string]string {
rows := r.rows()
fields := r.fields()
for i in 0 .. rows.len {
mut map_val := map[string]string
mut map_val := map[string]string{}
for j in 0 .. fields.len {
map_val[fields[j].name] = rows[i].vals[j]
}
@ -134,7 +134,7 @@ pub fn (f Field) str() string {
flags: $f.flags
decimals: $f.decimals
charsetnr: $f.charsetnr
type: ${f.type_.str()}
type: $f.type_.str()
}
'
}

View File

@ -12,7 +12,9 @@ fn get_errno(conn &C.MYSQL) int {
// resolve_nil_str returns empty string if passed value is a nil pointer.
fn resolve_nil_str(ptr byteptr) string {
if isnil(ptr) { return '' }
if isnil(ptr) {
return ''
}
return unsafe {ptr.vstring()}
}

View File

@ -179,7 +179,7 @@ pub fn (db DB) exec_param_many(query string, params []string) []Row {
}
}
pub fn (db DB) exec_param2(query, param, param2 string) []Row {
pub fn (db DB) exec_param2(query string, param string, param2 string) []Row {
mut param_vals := [2]byteptr{}
param_vals[0] = param.str
param_vals[1] = param2.str
@ -187,7 +187,7 @@ pub fn (db DB) exec_param2(query, param, param2 string) []Row {
return db.handle_error_or_result(res, 'exec_param2')
}
pub fn (db DB) exec_param(query, param string) []Row {
pub fn (db DB) exec_param(query string, param string) []Row {
mut param_vals := [1]byteptr{}
param_vals[0] = param.str
res := C.PQexecParams(db.conn, query.str, 1, 0, param_vals, 0, 0, 0)

View File

@ -3,21 +3,21 @@ module sqlite
#flag darwin -lsqlite3
#flag linux -lsqlite3
#flag solaris -lsqlite3
#flag freebsd -I/usr/local/include
#flag freebsd -Wl -L/usr/local/lib -lsqlite3
#flag windows -I@VROOT/thirdparty/sqlite
#flag windows -L@VROOT/thirdparty/sqlite
#flag windows @VROOT/thirdparty/sqlite/sqlite3.o
// #flag linux -I @VROOT/thirdparty/sqlite
// #flag @VROOT/thirdparty/sqlite/sqlite.c
#include "sqlite3.h"
//
struct C.sqlite3 {}
struct C.sqlite3_stmt {}
struct C.sqlite3 {
}
struct C.sqlite3_stmt {
}
//
pub struct DB {
mut:
@ -32,22 +32,35 @@ pub struct Row {
pub mut:
vals []string
}
//
fn C.sqlite3_open(charptr, &&C.sqlite3) int
fn C.sqlite3_close(&C.sqlite3) int
//
fn C.sqlite3_prepare_v2(&C.sqlite3, charptr, int, &&sqlite3_stmt, &charptr) int
fn C.sqlite3_step(&C.sqlite3_stmt) int
fn C.sqlite3_finalize(&C.sqlite3_stmt) int
//
fn C.sqlite3_column_name(&C.sqlite3_stmt, int) charptr
fn C.sqlite3_column_text(&C.sqlite3_stmt, int) byteptr
fn C.sqlite3_column_int(&C.sqlite3_stmt, int) int
fn C.sqlite3_column_int64(&C.sqlite3_stmt, int) int64
fn C.sqlite3_column_double(&C.sqlite3_stmt, int) f64
fn C.sqlite3_column_count(&C.sqlite3_stmt) int
//
fn C.sqlite3_errstr(int) charptr
fn C.sqlite3_free(voidptr)
// Opens the connection with a database.
@ -99,7 +112,6 @@ pub fn (db DB) q_string(query string) string {
return res
}
// Execute the query on db, return an array of all the results, alongside any result code.
// Result codes: https://www.sqlite.org/rescode.html
pub fn (db DB) exec(query string) ([]Row, int) {
@ -142,10 +154,10 @@ pub fn (db DB) exec_none(query string) int {
return code
}
/* TODO
/*
TODO
pub fn (db DB) exec_param(query string, param string) []Row {
}
*/
pub fn (db DB) insert<T>(x T) {
}

View File

@ -4,27 +4,23 @@ fn test_sqlite() {
$if !linux {
return
}
db := sqlite.connect(':memory:') or { panic(err) }
db.exec("drop table if exists users")
db := sqlite.connect(':memory:') or {
panic(err)
}
db.exec('drop table if exists users')
db.exec("create table users (id integer primary key, name text default '');")
db.exec("insert into users (name) values ('Sam')")
db.exec("insert into users (name) values ('Peter')")
db.exec("insert into users (name) values ('Kate')")
nr_users := db.q_int('select count(*) from users')
assert nr_users == 3
name := db.q_string('select name from users where id = 1')
assert name == 'Sam'
users, mut code := db.exec('select * from users')
assert users.len == 3
assert code == 101
code = db.exec_none('vacuum')
assert code == 101
user := db.exec_one('select * from users where id = 3') or {
panic(err)
}