From f37e936321115a1939d5a832c76d0f3fc2e0bf7a Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Mon, 19 Oct 2020 20:11:04 +0200 Subject: [PATCH] pg, sqlite, mysql: run vfmt (#6650) --- vlib/mysql/_cdefs.c.v | 67 +++++++++++++++++++++++++++++---------- vlib/mysql/consts.v | 28 ++++++++-------- vlib/mysql/enums.v | 66 +++++++++++++++++++------------------- vlib/mysql/mysql.v | 47 ++++++++++----------------- vlib/mysql/result.v | 14 ++++---- vlib/mysql/utils.v | 8 +++-- vlib/pg/pg.v | 4 +-- vlib/sqlite/sqlite.v | 44 +++++++++++++++---------- vlib/sqlite/sqlite_test.v | 12 +++---- 9 files changed, 160 insertions(+), 130 deletions(-) diff --git a/vlib/mysql/_cdefs.c.v b/vlib/mysql/_cdefs.c.v index 1583a4f9e2..240262445a 100644 --- a/vlib/mysql/_cdefs.c.v +++ b/vlib/mysql/_cdefs.c.v @@ -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) diff --git a/vlib/mysql/consts.v b/vlib/mysql/consts.v index dbf34adbeb..121f3bc7ab 100644 --- a/vlib/mysql/consts.v +++ b/vlib/mysql/consts.v @@ -1,24 +1,24 @@ module mysql -/* MYSQL CONNECT FLAGS */ +// 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_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 + client_ssl = C.CLIENT_SSL + 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) diff --git a/vlib/mysql/enums.v b/vlib/mysql/enums.v index 56988246cb..a720cc18bc 100644 --- a/vlib/mysql/enums.v +++ b/vlib/mysql/enums.v @@ -21,7 +21,7 @@ pub enum FieldType { type_timestamp2 type_datetime2 type_time2 - type_json = 245 + type_json = 245 type_newdecimal type_enum type_set @@ -36,37 +36,37 @@ pub enum FieldType { 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' } + 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' } } } diff --git a/vlib/mysql/mysql.v b/vlib/mysql/mysql.v index 8d40069465..a9c22eb644 100644 --- a/vlib/mysql/mysql.v +++ b/vlib/mysql/mysql.v @@ -3,14 +3,13 @@ module mysql #flag -lmysqlclient #flag linux -I/usr/include/mysql #include - // TODO: Documentation pub struct Connection { mut: conn &C.MYSQL = C.mysql_init(0) pub mut: - host string = '127.0.0.1' - port u32 = 3306 + host string = '127.0.0.1' + port u32 = 3306 username string password string dbname string @@ -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) @@ -100,7 +91,7 @@ pub fn (conn Connection) tables(wildcard string) ?[]string { if isnil(cres) { return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn)) } - res := Result{cres} + res := Result{cres} mut tables := []string{} for row in res.rows() { tables << row.vals[0] @@ -111,12 +102,11 @@ pub fn (conn Connection) tables(wildcard string) ?[]string { // escape_string creates a legal SQL string for use in an SQL statement. 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() } + 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()} } // set_option is used to set extra connect options and affect behavior for a connection. @@ -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)) @@ -175,12 +164,12 @@ pub fn (conn Connection) info() string { // get_host_info returns a string describing the connection. pub fn (conn Connection) get_host_info() string { - return unsafe { C.mysql_get_host_info(conn.conn).vstring() } + return unsafe {C.mysql_get_host_info(conn.conn).vstring()} } // get_server_info returns the server version number as a string. pub fn (conn Connection) get_server_info() string { - return unsafe { C.mysql_get_server_info(conn.conn).vstring() } + return unsafe {C.mysql_get_server_info(conn.conn).vstring()} } // get_server_version returns the server version number as an integer. @@ -188,11 +177,10 @@ 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() } + return unsafe {C.mysql_get_client_info().vstring()} } // get_client_version returns client version information as an integer. @@ -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 { diff --git a/vlib/mysql/result.v b/vlib/mysql/result.v index 4ddeeaac23..98f77aa67d 100644 --- a/vlib/mysql/result.v +++ b/vlib/mysql/result.v @@ -30,11 +30,11 @@ pub fn (r Result) rows() []Row { nr_cols := r.n_fields() for rr := r.fetch_row(); rr; rr = r.fetch_row() { mut row := Row{} - for i in 0..nr_cols { + for i in 0 .. nr_cols { if rr[i] == 0 { row.vals << '' } else { - row.vals << mystring( byteptr(rr[i]) ) + row.vals << mystring(byteptr(rr[i])) } } rows << row @@ -47,9 +47,9 @@ pub fn (r Result) maps() []map[string]string { mut array_map := []map[string]string{} rows := r.rows() fields := r.fields() - for i in 0..rows.len { - mut map_val := map[string]string - for j in 0..fields.len { + for i in 0 .. rows.len { + mut map_val := map[string]string{} + for j in 0 .. fields.len { map_val[fields[j].name] = rows[i].vals[j] } array_map << map_val @@ -62,7 +62,7 @@ pub fn (r Result) fields() []Field { mut fields := []Field{} nr_cols := r.n_fields() orig_fields := C.mysql_fetch_fields(r.result) - for i in 0..nr_cols { + for i in 0 .. nr_cols { fields << Field{ name: mystring(orig_fields[i].name) org_name: mystring(orig_fields[i].org_name) @@ -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() } ' } diff --git a/vlib/mysql/utils.v b/vlib/mysql/utils.v index eee5d91e4e..5b0a4dd1b1 100644 --- a/vlib/mysql/utils.v +++ b/vlib/mysql/utils.v @@ -2,7 +2,7 @@ module mysql // get_error_msg returns error message from MySQL instance. fn get_error_msg(conn &C.MYSQL) string { - return unsafe { C.mysql_error(conn).vstring() } + return unsafe {C.mysql_error(conn).vstring()} } // get_errno returns error number from MySQL instance. @@ -12,8 +12,10 @@ 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 '' } - return unsafe { ptr.vstring() } + if isnil(ptr) { + return '' + } + return unsafe {ptr.vstring()} } [inline] diff --git a/vlib/pg/pg.v b/vlib/pg/pg.v index 5131ecfd3d..56cffcc926 100644 --- a/vlib/pg/pg.v +++ b/vlib/pg/pg.v @@ -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) diff --git a/vlib/sqlite/sqlite.v b/vlib/sqlite/sqlite.v index f0eb91e77c..1b8107db2d 100644 --- a/vlib/sqlite/sqlite.v +++ b/vlib/sqlite/sqlite.v @@ -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 - +// #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,10 +112,9 @@ 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) { +pub fn (db DB) exec(query string) ([]Row, int) { stmt := &C.sqlite3_stmt(0) C.sqlite3_prepare_v2(db.conn, query.str, -1, &stmt, 0) nr_cols := C.sqlite3_column_count(stmt) @@ -112,7 +124,7 @@ pub fn (db DB) exec(query string) ([]Row,int) { res = C.sqlite3_step(stmt) // Result Code SQLITE_ROW; Another row is available if res != 100 { - //C.puts(C.sqlite3_errstr(res)) + // C.puts(C.sqlite3_errstr(res)) break } mut row := Row{} @@ -122,13 +134,13 @@ pub fn (db DB) exec(query string) ([]Row,int) { } rows << row } - return rows,res + return rows, res } // Execute a query, handle error code // Return the first row from the resulting table pub fn (db DB) exec_one(query string) ?Row { - rows,code := db.exec(query) + rows, code := db.exec(query) if rows.len == 0 || code != 101 { return error('SQL Error: Rows #$rows.len Return code $code') } @@ -138,14 +150,14 @@ pub fn (db DB) exec_one(query string) ?Row { // In case you don't expect any result, but still want an error code // e.g. INSERT INTO ... VALUES (...) pub fn (db DB) exec_none(query string) int { - _,code := db.exec(query) + _, code := db.exec(query) return code } -/* TODO +/* +TODO pub fn (db DB) exec_param(query string, param string) []Row { } */ - pub fn (db DB) insert(x T) { } diff --git a/vlib/sqlite/sqlite_test.v b/vlib/sqlite/sqlite_test.v index a82673d586..021510d579 100644 --- a/vlib/sqlite/sqlite_test.v +++ b/vlib/sqlite/sqlite_test.v @@ -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) }