From 16dd889922be3cc441c216a41f28491ab7e9564c Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 23 Jun 2020 13:59:00 +0200 Subject: [PATCH] orm: test true/false --- vlib/orm/orm_test.v | 19 +++++++++++++++++-- vlib/v/gen/sql.v | 4 ++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/vlib/orm/orm_test.v b/vlib/orm/orm_test.v index 8b737ff5dd..5fc481c58c 100644 --- a/vlib/orm/orm_test.v +++ b/vlib/orm/orm_test.v @@ -15,18 +15,19 @@ struct User { id int age int name string + is_customer bool } fn test_orm_sqlite() { db := sqlite.connect(':memory:') or { panic(err) } 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' 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) values ('Kate')") + db.exec("insert into User (name, age, is_customer) values ('Kate', 30, 1)") nr_all_users := sql db { select count from User } @@ -108,6 +109,20 @@ fn test_orm_sqlite() { assert x.age == 30 assert x.id == 4 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' + } diff --git a/vlib/v/gen/sql.v b/vlib/v/gen/sql.v index 2f342c6957..52fd707836 100644 --- a/vlib/v/gen/sql.v +++ b/vlib/v/gen/sql.v @@ -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);') 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_ROW) ;') // another row 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` // for left sides just add a string, for right sides, generate the bindings if g.sql_side == .left { - println("sql gen left $expr.name") + //println("sql gen left $expr.name") g.write(expr.name) } else { g.inc_sql_i()