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

orm: test true/false

This commit is contained in:
Alexander Medvednikov 2020-06-23 13:59:00 +02:00
parent 076d0208cd
commit 16dd889922
2 changed files with 19 additions and 4 deletions

View File

@ -15,18 +15,19 @@ struct User {
id int id int
age int age int
name string name string
is_customer bool
} }
fn test_orm_sqlite() { fn test_orm_sqlite() {
db := sqlite.connect(':memory:') or { panic(err) } db := sqlite.connect(':memory:') or { panic(err) }
db.exec("drop table if exists User") db.exec("drop table if exists User")
db.exec("create table User (id integer primary key, age int default 0, name text default '');") db.exec("create table User (id integer primary key, age int default 0, name text default '', is_customer int default 0);")
name := 'Peter' name := 'Peter'
db.exec("insert into User (name, age) values ('Sam', 29)") db.exec("insert into User (name, age) values ('Sam', 29)")
db.exec("insert into User (name, age) values ('Peter', 31)") db.exec("insert into User (name, age) values ('Peter', 31)")
db.exec("insert into User (name) values ('Kate')") db.exec("insert into User (name, age, is_customer) values ('Kate', 30, 1)")
nr_all_users := sql db { nr_all_users := sql db {
select count from User select count from User
} }
@ -108,6 +109,20 @@ fn test_orm_sqlite() {
assert x.age == 30 assert x.age == 30
assert x.id == 4 assert x.id == 4
assert x.name == 'New user' assert x.name == 'New user'
//
kate := sql db {
select from User where id == 3
}
println(kate)
assert kate.is_customer == true
//
customer := sql db {
select from User where is_customer == true limit 1
}
println(customer)
assert customer.is_customer == true
assert customer.name == 'Kate'
} }

View File

@ -137,7 +137,7 @@ fn (mut g Gen) sql_select_expr(node ast.SqlExpr) {
// //
g.writeln('int _step_res$tmp = sqlite3_step($g.sql_stmt_name);') g.writeln('int _step_res$tmp = sqlite3_step($g.sql_stmt_name);')
if node.is_array { if node.is_array {
g.writeln('\tprintf("step res=%d\\n", _step_res$tmp);') //g.writeln('\tprintf("step res=%d\\n", _step_res$tmp);')
g.writeln('\tif (_step_res$tmp == SQLITE_DONE) break;') g.writeln('\tif (_step_res$tmp == SQLITE_DONE) break;')
g.writeln('\tif (_step_res$tmp == SQLITE_ROW) ;') // another row g.writeln('\tif (_step_res$tmp == SQLITE_ROW) ;') // another row
g.writeln('\telse if (_step_res$tmp != SQLITE_OK) break;') g.writeln('\telse if (_step_res$tmp != SQLITE_OK) break;')
@ -210,7 +210,7 @@ fn (mut g Gen) expr_to_sql(expr ast.Expr) {
// `name == user_name` => `name == ?1` // `name == user_name` => `name == ?1`
// for left sides just add a string, for right sides, generate the bindings // for left sides just add a string, for right sides, generate the bindings
if g.sql_side == .left { if g.sql_side == .left {
println("sql gen left $expr.name") //println("sql gen left $expr.name")
g.write(expr.name) g.write(expr.name)
} else { } else {
g.inc_sql_i() g.inc_sql_i()