mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
Added docs and the single param version
This commit is contained in:
26
main.v
26
main.v
@@ -1,26 +0,0 @@
|
||||
module main
|
||||
|
||||
import db.sqlite
|
||||
|
||||
fn main() {
|
||||
db := sqlite.connect('testing.db') or { panic(err) }
|
||||
|
||||
println(db.exec_none('CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
password TEXT NOT NULL)'))
|
||||
println(db.exec_none('CREATE TABLE IF NOT EXISTS sessions (
|
||||
id INTEGER PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL,
|
||||
session_token TEXT NOT NULL UNIQUE,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id))'))
|
||||
|
||||
println(db.exec_none('INSERT INTO users (username, password) VALUES ("jackson", "password")'))
|
||||
println(db.exec_none('INSERT INTO SESSIONS (user_id, session_token) VALUES ("1", "testing_token")'))
|
||||
|
||||
println(db.exec_param_many('SELECT * from users WHERE username = ?', ['jackson']) or {
|
||||
panic(err)
|
||||
})
|
||||
println(db.exec_param_many('SELECT * from sessions LEFT JOIN users on users.id = sessions.user_id WHERE username = ?',
|
||||
['jackson']) or { panic(err) })
|
||||
}
|
||||
@@ -285,13 +285,9 @@ pub fn (db &DB) exec_none(query string) int {
|
||||
return code
|
||||
}
|
||||
|
||||
// TODO pub fn (db &DB) exec_param(query string, param string) []Row {
|
||||
// exec_param_many executes a query with parameters provided as ?,
|
||||
// and returns either an error on failure, or the full result set on success
|
||||
pub fn (db &DB) exec_param_many(query string, params []string) ![]Row {
|
||||
println(query)
|
||||
println('')
|
||||
println(params)
|
||||
println('')
|
||||
|
||||
mut stmt := &C.sqlite3_stmt(unsafe { nil })
|
||||
defer {
|
||||
C.sqlite3_finalize(stmt)
|
||||
@@ -299,11 +295,9 @@ pub fn (db &DB) exec_param_many(query string, params []string) ![]Row {
|
||||
|
||||
mut code := C.sqlite3_prepare_v2(db.conn, &char(query.str), -1, &stmt, 0)
|
||||
if code != 0 {
|
||||
println('prepare failed')
|
||||
return &SQLError{
|
||||
msg: unsafe {
|
||||
cstring_to_vstring(&char(C.sqlite3_errstr(code))) +
|
||||
' Placeholder "?" cannot be used to select a table'
|
||||
cstring_to_vstring(&char(C.sqlite3_errstr(code)))
|
||||
}
|
||||
code: code
|
||||
}
|
||||
@@ -312,7 +306,6 @@ pub fn (db &DB) exec_param_many(query string, params []string) ![]Row {
|
||||
for i, param in params {
|
||||
code = C.sqlite3_bind_text(stmt, i + 1, voidptr(param.str), param.len, 0)
|
||||
if code != 0 {
|
||||
println('bind failed at index=${i}')
|
||||
return &SQLError{
|
||||
msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) }
|
||||
code: code
|
||||
@@ -325,7 +318,7 @@ pub fn (db &DB) exec_param_many(query string, params []string) ![]Row {
|
||||
mut rows := []Row{}
|
||||
for {
|
||||
res = C.sqlite3_step(stmt)
|
||||
if res != 100 {
|
||||
if res != sqlite.sqlite_row {
|
||||
break
|
||||
}
|
||||
mut row := Row{}
|
||||
@@ -334,8 +327,7 @@ pub fn (db &DB) exec_param_many(query string, params []string) ![]Row {
|
||||
if val == &u8(0) {
|
||||
row.vals << ''
|
||||
} else {
|
||||
row.vals << unsafe { cstring_to_vstring(C.sqlite3_column_name(stmt, i)) } + ': ' +
|
||||
unsafe { tos_clone(val) }
|
||||
row.vals << unsafe { tos_clone(val) }
|
||||
}
|
||||
}
|
||||
rows << row
|
||||
@@ -344,6 +336,12 @@ pub fn (db &DB) exec_param_many(query string, params []string) ![]Row {
|
||||
return rows
|
||||
}
|
||||
|
||||
// exec_param executes a query with one parameter provided as a ?,
|
||||
// and returns either an error on failure, or the full result set on success
|
||||
pub fn (db &DB) exec_param(query string, param string) ![]Row {
|
||||
return db.exec_param_many(query, [param])
|
||||
}
|
||||
|
||||
// create_table issues a "create table if not exists" command to the db.
|
||||
// It creates the table named 'table_name', with columns generated from 'columns' array.
|
||||
// The default columns type will be TEXT.
|
||||
|
||||
@@ -44,17 +44,21 @@ fn test_sqlite() {
|
||||
assert db.last_insert_rowid() == 2
|
||||
db.exec("insert into users (name) values ('Kate')")
|
||||
assert db.last_insert_rowid() == 3
|
||||
db.exec_param('insert into users (name) values (?)', 'Tom')!
|
||||
assert db.last_insert_rowid() == 4
|
||||
nr_users := db.q_int('select count(*) from users')
|
||||
assert nr_users == 3
|
||||
assert nr_users == 4
|
||||
name := db.q_string('select name from users where id = 1')
|
||||
assert name == 'Sam'
|
||||
username := db.exec_param('select name from users where id = ?', '1')!
|
||||
assert username[0].vals[0] == 'Sam'
|
||||
|
||||
// this insert will be rejected due to duplicated id
|
||||
db.exec("insert into users (id,name) values (1,'Sam')")
|
||||
assert db.get_affected_rows_count() == 0
|
||||
|
||||
users, mut code := db.exec('select * from users')
|
||||
assert users.len == 3
|
||||
assert users.len == 4
|
||||
assert code == 101
|
||||
code = db.exec_none('vacuum')
|
||||
assert code == 101
|
||||
@@ -67,6 +71,8 @@ fn test_sqlite() {
|
||||
|
||||
db.exec("update users set name='Peter1' where name='Peter'")
|
||||
assert db.get_affected_rows_count() == 1
|
||||
db.exec_param_many('update users set name=? where name=?', ['Peter', 'Peter1'])!
|
||||
assert db.get_affected_rows_count() == 1
|
||||
|
||||
db.exec("delete from users where name='qqqq'")
|
||||
assert db.get_affected_rows_count() == 0
|
||||
|
||||
Reference in New Issue
Block a user