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

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

This commit is contained in:
Lukas Neubert
2020-10-19 20:11:04 +02:00
committed by GitHub
parent 37d6503437
commit f37e936321
9 changed files with 160 additions and 130 deletions

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
// #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<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)
}