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 module mysql
struct C.MYSQL struct C.MYSQL {
struct C.MYSQL_RES }
struct C.MYSQL_RES {
}
struct C.MYSQL_FIELD { struct C.MYSQL_FIELD {
name byteptr /* Name of column */ name byteptr // Name of column
org_name byteptr /* Original column name, if an alias */ org_name byteptr // Original column name, if an alias
table byteptr /* Table of column if column was a field */ table byteptr // Table of column if column was a field
org_table byteptr /* Org table name, if table was an alias */ org_table byteptr // Org table name, if table was an alias
db byteptr /* Database for table */ db byteptr // Database for table
catalog byteptr /* Catalog for table */ catalog byteptr // Catalog for table
def byteptr /* Default value (set by mysql_list_fields) */ def byteptr // Default value (set by mysql_list_fields)
length int /* Width of column (create length) */ length int // Width of column (create length)
max_length int /* Max width for selected set */ max_length int // Max width for selected set
name_length u32 name_length u32
org_name_length u32 org_name_length u32
table_length u32 table_length u32
@ -19,45 +23,74 @@ struct C.MYSQL_FIELD {
db_length u32 db_length u32
catalog_length u32 catalog_length u32
def_length u32 def_length u32
flags u32 /* Div flags */ flags u32 // Div flags
decimals u32 /* Number of decimals in field */ decimals u32 // Number of decimals in field
charsetnr u32 /* Character set */ charsetnr u32 // Character set
@type int /* Type of field. See mysql_com.h for types */ @type int // Type of field. See mysql_com.h for types
} }
fn C.mysql_init(mysql &C.MYSQL) &C.MYSQL 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_real_query(mysql &C.MYSQL, q byteptr, len u32) 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_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_change_user(mysql &C.MYSQL, user byteptr, password byteptr, db byteptr) bool
fn C.mysql_affected_rows(mysql &C.MYSQL) u64 fn C.mysql_affected_rows(mysql &C.MYSQL) u64
fn C.mysql_options(mysql &C.MYSQL, option int, arg voidptr) int 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_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_list_tables(mysql &C.MYSQL, wild byteptr) &C.MYSQL_RES
fn C.mysql_num_fields(res &C.MYSQL_RES) int fn C.mysql_num_fields(res &C.MYSQL_RES) int
fn C.mysql_num_rows(res &C.MYSQL_RES) u64 fn C.mysql_num_rows(res &C.MYSQL_RES) u64
fn C.mysql_autocommit(mysql MYSQL, mode bool) fn C.mysql_autocommit(mysql MYSQL, mode bool)
fn C.mysql_refresh(mysql MYSQL, options u32) int fn C.mysql_refresh(mysql MYSQL, options u32) int
fn C.mysql_reset_connection(mysql MYSQL) int fn C.mysql_reset_connection(mysql MYSQL) int
fn C.mysql_ping(mysql MYSQL) int fn C.mysql_ping(mysql MYSQL) 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_fetch_fields(res &C.MYSQL_RES) &C.MYSQL_FIELD fn C.mysql_fetch_fields(res &C.MYSQL_RES) &C.MYSQL_FIELD
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_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)
/* INFO & VERSION */ // INFO & VERSION
fn C.mysql_info(mysql &C.MYSQL) byteptr fn C.mysql_info(mysql &C.MYSQL) byteptr
fn C.mysql_get_host_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_info(mysql &C.MYSQL) byteptr
fn C.mysql_get_server_version(mysql &C.MYSQL) u64 fn C.mysql_get_server_version(mysql &C.MYSQL) u64
fn C.mysql_get_client_version() u64 fn C.mysql_get_client_version() u64
fn C.mysql_get_client_info() byteptr fn C.mysql_get_client_info() byteptr
/* DEBUG & ERROR INFO */ // DEBUG & ERROR INFO
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_errno(mysql &C.MYSQL) int
fn C.mysql_dump_debug_info(mysql &C.MYSQL) int fn C.mysql_dump_debug_info(mysql &C.MYSQL) int
fn C.mysql_debug(debug byteptr) fn C.mysql_debug(debug byteptr)

View File

@ -1,24 +1,24 @@
module mysql module mysql
/* MYSQL CONNECT FLAGS */ // MYSQL CONNECT FLAGS
pub const ( pub const (
// CAN_HANDLE_EXPIRED_PASSWORDS = C.CAN_HANDLE_EXPIRED_PASSWORDS // CAN_HANDLE_EXPIRED_PASSWORDS = C.CAN_HANDLE_EXPIRED_PASSWORDS
client_compress = C.CLIENT_COMPRESS client_compress = C.CLIENT_COMPRESS
client_found_rows = C.CLIENT_FOUND_ROWS client_found_rows = C.CLIENT_FOUND_ROWS
client_ignore_sigpipe = C.CLIENT_IGNORE_SIGPIPE client_ignore_sigpipe = C.CLIENT_IGNORE_SIGPIPE
client_ignore_space = C.CLIENT_IGNORE_SPACE client_ignore_space = C.CLIENT_IGNORE_SPACE
client_interactive = C.CLIENT_INTERACTIVE client_interactive = C.CLIENT_INTERACTIVE
client_local_files = C.CLIENT_LOCAL_FILES client_local_files = C.CLIENT_LOCAL_FILES
client_multi_results = C.CLIENT_MULTI_RESULTS client_multi_results = C.CLIENT_MULTI_RESULTS
client_multi_statements = C.CLIENT_MULTI_STATEMENTS client_multi_statements = C.CLIENT_MULTI_STATEMENTS
client_no_schema = C.CLIENT_NO_SCHEMA client_no_schema = C.CLIENT_NO_SCHEMA
client_odbc = C.CLIENT_ODBC client_odbc = C.CLIENT_ODBC
// client_optional_resultset_metadata = C.CLIENT_OPTIONAL_RESULTSET_METADATA // client_optional_resultset_metadata = C.CLIENT_OPTIONAL_RESULTSET_METADATA
client_ssl = C.CLIENT_SSL client_ssl = C.CLIENT_SSL
client_remember_options = C.CLIENT_REMEMBER_OPTIONS client_remember_options = C.CLIENT_REMEMBER_OPTIONS
) )
/* MYSQL REFRESH FLAGS */ // MYSQL REFRESH FLAGS
pub const ( pub const (
refresh_grant = u32(C.REFRESH_GRANT) refresh_grant = u32(C.REFRESH_GRANT)
refresh_log = u32(C.REFRESH_LOG) refresh_log = u32(C.REFRESH_LOG)

View File

@ -21,7 +21,7 @@ pub enum FieldType {
type_timestamp2 type_timestamp2
type_datetime2 type_datetime2
type_time2 type_time2
type_json = 245 type_json = 245
type_newdecimal type_newdecimal
type_enum type_enum
type_set type_set
@ -36,37 +36,37 @@ pub enum FieldType {
pub fn (f FieldType) str() string { pub fn (f FieldType) str() string {
return match f { return match f {
0 { 'decimal' } 0 { 'decimal' }
1 { 'tiny' } 1 { 'tiny' }
2 { 'short' } 2 { 'short' }
3 { 'long' } 3 { 'long' }
4 { 'float' } 4 { 'float' }
5 { 'double' } 5 { 'double' }
6 { 'null' } 6 { 'null' }
7 { 'timestamp' } 7 { 'timestamp' }
8 { 'longlong' } 8 { 'longlong' }
9 { 'int24' } 9 { 'int24' }
10 { 'date' } 10 { 'date' }
11 { 'time' } 11 { 'time' }
12 { 'datetime' } 12 { 'datetime' }
13 { 'year' } 13 { 'year' }
14 { 'newdate' } 14 { 'newdate' }
15 { 'varchar' } 15 { 'varchar' }
16 { 'bit' } 16 { 'bit' }
17 { 'timestamp2' } 17 { 'timestamp2' }
18 { 'datetime2' } 18 { 'datetime2' }
19 { 'time2' } 19 { 'time2' }
245 { 'json' } 245 { 'json' }
246 { 'newdecimal' } 246 { 'newdecimal' }
247 { 'enum' } 247 { 'enum' }
248 { 'set' } 248 { 'set' }
249 { 'tiny_blob' } 249 { 'tiny_blob' }
250 { 'medium_blob' } 250 { 'medium_blob' }
251 { 'long_blob' } 251 { 'long_blob' }
252 { 'blob' } 252 { 'blob' }
253 { 'var_string' } 253 { 'var_string' }
254 { 'string' } 254 { 'string' }
255 { 'geometry' } 255 { 'geometry' }
else { 'unknown' } else { 'unknown' }
} }
} }

View File

@ -3,14 +3,13 @@ module mysql
#flag -lmysqlclient #flag -lmysqlclient
#flag linux -I/usr/include/mysql #flag linux -I/usr/include/mysql
#include <mysql.h> #include <mysql.h>
// TODO: Documentation // TODO: Documentation
pub struct Connection { pub struct Connection {
mut: mut:
conn &C.MYSQL = C.mysql_init(0) conn &C.MYSQL = C.mysql_init(0)
pub mut: pub mut:
host string = '127.0.0.1' host string = '127.0.0.1'
port u32 = 3306 port u32 = 3306
username string username string
password string password string
dbname string dbname string
@ -20,16 +19,8 @@ pub mut:
// connect connects to a MySQL server. // connect connects to a MySQL server.
pub fn (mut conn Connection) connect() ?bool { pub fn (mut conn Connection) connect() ?bool {
instance := C.mysql_init(conn.conn) instance := C.mysql_init(conn.conn)
conn.conn = C.mysql_real_connect( conn.conn = C.mysql_real_connect(conn.conn, conn.host.str, conn.username.str, conn.password.str,
conn.conn, conn.dbname.str, conn.port, 0, conn.flag)
conn.host.str,
conn.username.str,
conn.password.str,
conn.dbname.str,
conn.port,
0,
conn.flag
)
if isnil(conn.conn) { if isnil(conn.conn) {
return error_with_code(get_error_msg(instance), get_errno(instance)) 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. // change_user changes the user of the specified database connection.
// if desired, the empty string value can be passed to the `dbname` parameter // if desired, the empty string value can be passed to the `dbname` parameter
// resulting in only changing the user and not selecting a database. // 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 mut ret := true
if dbname != '' { if dbname != '' {
ret = C.mysql_change_user(conn.conn, username.str, password.str, dbname.str) 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) { if isnil(cres) {
return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn)) return error_with_code(get_error_msg(conn.conn), get_errno(conn.conn))
} }
res := Result{cres} res := Result{cres}
mut tables := []string{} mut tables := []string{}
for row in res.rows() { for row in res.rows() {
tables << row.vals[0] 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. // escape_string creates a legal SQL string for use in an SQL statement.
pub fn (conn Connection) escape_string(s string) string { pub fn (conn Connection) escape_string(s string) string {
len := C.strlen(s.str) len := C.strlen(s.str)
to := malloc(2 * len + 1) to := malloc(2 * len + 1)
quote := byte(39) // single quote quote := byte(39) // single quote
C.mysql_real_escape_string_quote(conn.conn, to, s.str, len, quote)
C.mysql_real_escape_string_quote(conn.conn, to, s.str, len, quote) return unsafe {to.vstring()}
return unsafe { to.vstring() }
} }
// set_option is used to set extra connect options and affect behavior for a connection. // 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) C.mysql_close(conn.conn)
} }
/* -------------------------- MYSQL INFO & VERSION -------------------------- */ // -------------------------- MYSQL INFO & VERSION --------------------------
// info returns information about the most recently executed query. // info returns information about the most recently executed query.
pub fn (conn Connection) info() string { pub fn (conn Connection) info() string {
return resolve_nil_str(C.mysql_info(conn.conn)) 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. // get_host_info returns a string describing the connection.
pub fn (conn Connection) get_host_info() string { 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. // get_server_info returns the server version number as a string.
pub fn (conn Connection) get_server_info() 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. // 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) return C.mysql_get_server_version(conn.conn)
} }
/* --------------------------------- CLIENT --------------------------------- */ // --------------------------------- CLIENT ---------------------------------
// get_client_info returns client version information as a string. // get_client_info returns client version information as a string.
pub fn get_client_info() 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. // 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() return C.mysql_get_client_version()
} }
/* ------------------------------- MYSQL DEBUG ------------------------------ */ // ------------------------------- MYSQL DEBUG ------------------------------
// dump_debug_info causes the server to write debug information to the log // dump_debug_info causes the server to write debug information to the log
pub fn (conn Connection) dump_debug_info() ?bool { pub fn (conn Connection) dump_debug_info() ?bool {
if C.mysql_dump_debug_info(conn.conn) != 0 { if C.mysql_dump_debug_info(conn.conn) != 0 {

View File

@ -30,11 +30,11 @@ pub fn (r Result) rows() []Row {
nr_cols := r.n_fields() nr_cols := r.n_fields()
for rr := r.fetch_row(); rr; rr = r.fetch_row() { for rr := r.fetch_row(); rr; rr = r.fetch_row() {
mut row := Row{} mut row := Row{}
for i in 0..nr_cols { for i in 0 .. nr_cols {
if rr[i] == 0 { if rr[i] == 0 {
row.vals << '' row.vals << ''
} else { } else {
row.vals << mystring( byteptr(rr[i]) ) row.vals << mystring(byteptr(rr[i]))
} }
} }
rows << row rows << row
@ -47,9 +47,9 @@ pub fn (r Result) maps() []map[string]string {
mut array_map := []map[string]string{} mut array_map := []map[string]string{}
rows := r.rows() rows := r.rows()
fields := r.fields() fields := r.fields()
for i in 0..rows.len { for i in 0 .. rows.len {
mut map_val := map[string]string mut map_val := map[string]string{}
for j in 0..fields.len { for j in 0 .. fields.len {
map_val[fields[j].name] = rows[i].vals[j] map_val[fields[j].name] = rows[i].vals[j]
} }
array_map << map_val array_map << map_val
@ -62,7 +62,7 @@ pub fn (r Result) fields() []Field {
mut fields := []Field{} mut fields := []Field{}
nr_cols := r.n_fields() nr_cols := r.n_fields()
orig_fields := C.mysql_fetch_fields(r.result) orig_fields := C.mysql_fetch_fields(r.result)
for i in 0..nr_cols { for i in 0 .. nr_cols {
fields << Field{ fields << Field{
name: mystring(orig_fields[i].name) name: mystring(orig_fields[i].name)
org_name: mystring(orig_fields[i].org_name) org_name: mystring(orig_fields[i].org_name)
@ -134,7 +134,7 @@ pub fn (f Field) str() string {
flags: $f.flags flags: $f.flags
decimals: $f.decimals decimals: $f.decimals
charsetnr: $f.charsetnr charsetnr: $f.charsetnr
type: ${f.type_.str()} type: $f.type_.str()
} }
' '
} }

View File

@ -2,7 +2,7 @@ module mysql
// get_error_msg returns error message from MySQL instance. // get_error_msg returns error message from MySQL instance.
fn get_error_msg(conn &C.MYSQL) string { 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. // 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. // resolve_nil_str returns empty string if passed value is a nil pointer.
fn resolve_nil_str(ptr byteptr) string { fn resolve_nil_str(ptr byteptr) string {
if isnil(ptr) { return '' } if isnil(ptr) {
return unsafe { ptr.vstring() } return ''
}
return unsafe {ptr.vstring()}
} }
[inline] [inline]

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{} mut param_vals := [2]byteptr{}
param_vals[0] = param.str param_vals[0] = param.str
param_vals[1] = param2.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') 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{} mut param_vals := [1]byteptr{}
param_vals[0] = param.str param_vals[0] = param.str
res := C.PQexecParams(db.conn, query.str, 1, 0, param_vals, 0, 0, 0) 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 darwin -lsqlite3
#flag linux -lsqlite3 #flag linux -lsqlite3
#flag solaris -lsqlite3 #flag solaris -lsqlite3
#flag freebsd -I/usr/local/include #flag freebsd -I/usr/local/include
#flag freebsd -Wl -L/usr/local/lib -lsqlite3 #flag freebsd -Wl -L/usr/local/lib -lsqlite3
#flag windows -I@VROOT/thirdparty/sqlite #flag windows -I@VROOT/thirdparty/sqlite
#flag windows -L@VROOT/thirdparty/sqlite #flag windows -L@VROOT/thirdparty/sqlite
#flag windows @VROOT/thirdparty/sqlite/sqlite3.o #flag windows @VROOT/thirdparty/sqlite/sqlite3.o
// #flag linux -I @VROOT/thirdparty/sqlite
//#flag linux -I @VROOT/thirdparty/sqlite // #flag @VROOT/thirdparty/sqlite/sqlite.c
//#flag @VROOT/thirdparty/sqlite/sqlite.c
#include "sqlite3.h" #include "sqlite3.h"
// //
struct C.sqlite3 {} struct C.sqlite3 {
struct C.sqlite3_stmt {} }
struct C.sqlite3_stmt {
}
// //
pub struct DB { pub struct DB {
mut: mut:
@ -32,22 +32,35 @@ pub struct Row {
pub mut: pub mut:
vals []string vals []string
} }
// //
fn C.sqlite3_open(charptr, &&C.sqlite3) int fn C.sqlite3_open(charptr, &&C.sqlite3) int
fn C.sqlite3_close(&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_prepare_v2(&C.sqlite3, charptr, int, &&sqlite3_stmt, &charptr) int
fn C.sqlite3_step(&C.sqlite3_stmt) int fn C.sqlite3_step(&C.sqlite3_stmt) int
fn C.sqlite3_finalize(&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_name(&C.sqlite3_stmt, int) charptr
fn C.sqlite3_column_text(&C.sqlite3_stmt, int) byteptr fn C.sqlite3_column_text(&C.sqlite3_stmt, int) byteptr
fn C.sqlite3_column_int(&C.sqlite3_stmt, int) int fn C.sqlite3_column_int(&C.sqlite3_stmt, int) int
fn C.sqlite3_column_int64(&C.sqlite3_stmt, int) int64 fn C.sqlite3_column_int64(&C.sqlite3_stmt, int) int64
fn C.sqlite3_column_double(&C.sqlite3_stmt, int) f64 fn C.sqlite3_column_double(&C.sqlite3_stmt, int) f64
fn C.sqlite3_column_count(&C.sqlite3_stmt) int fn C.sqlite3_column_count(&C.sqlite3_stmt) int
// //
fn C.sqlite3_errstr(int) charptr fn C.sqlite3_errstr(int) charptr
fn C.sqlite3_free(voidptr) fn C.sqlite3_free(voidptr)
// Opens the connection with a database. // Opens the connection with a database.
@ -99,10 +112,9 @@ pub fn (db DB) q_string(query string) string {
return res return res
} }
// Execute the query on db, return an array of all the results, alongside any result code. // Execute the query on db, return an array of all the results, alongside any result code.
// Result codes: https://www.sqlite.org/rescode.html // 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) stmt := &C.sqlite3_stmt(0)
C.sqlite3_prepare_v2(db.conn, query.str, -1, &stmt, 0) C.sqlite3_prepare_v2(db.conn, query.str, -1, &stmt, 0)
nr_cols := C.sqlite3_column_count(stmt) 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) res = C.sqlite3_step(stmt)
// Result Code SQLITE_ROW; Another row is available // Result Code SQLITE_ROW; Another row is available
if res != 100 { if res != 100 {
//C.puts(C.sqlite3_errstr(res)) // C.puts(C.sqlite3_errstr(res))
break break
} }
mut row := Row{} mut row := Row{}
@ -122,13 +134,13 @@ pub fn (db DB) exec(query string) ([]Row,int) {
} }
rows << row rows << row
} }
return rows,res return rows, res
} }
// Execute a query, handle error code // Execute a query, handle error code
// Return the first row from the resulting table // Return the first row from the resulting table
pub fn (db DB) exec_one(query string) ?Row { 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 { if rows.len == 0 || code != 101 {
return error('SQL Error: Rows #$rows.len Return code $code') 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 // In case you don't expect any result, but still want an error code
// e.g. INSERT INTO ... VALUES (...) // e.g. INSERT INTO ... VALUES (...)
pub fn (db DB) exec_none(query string) int { pub fn (db DB) exec_none(query string) int {
_,code := db.exec(query) _, code := db.exec(query)
return code return code
} }
/* TODO /*
TODO
pub fn (db DB) exec_param(query string, param string) []Row { pub fn (db DB) exec_param(query string, param string) []Row {
} }
*/ */
pub fn (db DB) insert<T>(x T) { pub fn (db DB) insert<T>(x T) {
} }

View File

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