From e3f00ff28bbee7e7bad9e09aa56856697d9d8e86 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 22 Jun 2020 16:58:43 +0200 Subject: [PATCH] orm: `limit 1` test --- vlib/orm/orm_test.v | 7 ++++++- vlib/v/parser/sql.v | 19 ++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/vlib/orm/orm_test.v b/vlib/orm/orm_test.v index 60d722960d..8b737ff5dd 100644 --- a/vlib/orm/orm_test.v +++ b/vlib/orm/orm_test.v @@ -54,10 +54,15 @@ fn test_orm_sqlite() { } assert nr_peters3 == 1 peters := sql db { - select from User where name == name // limit 1 + select from User where name == name } assert peters.len == 1 assert peters[0].name == 'Peter' + one_peter := sql db { + select from User where name == name limit 1 + } + assert one_peter.name == 'Peter' + assert one_peter.id == 2 // user := sql db { select from User where id == 1 diff --git a/vlib/v/parser/sql.v b/vlib/v/parser/sql.v index 65b76e5831..78ef5c786d 100644 --- a/vlib/v/parser/sql.v +++ b/vlib/v/parser/sql.v @@ -31,8 +31,7 @@ fn (mut p Parser) sql_expr() ast.Expr { sym := p.table.get_type_symbol(table_type) table_name := sym.name mut where_expr := ast.Expr{} - has_where := p.tok.kind == .name && - p.tok.lit == 'where' + has_where := p.tok.kind == .name && p.tok.lit == 'where' mut query_one := false // one object is returned, not an array if has_where { p.next() @@ -43,18 +42,11 @@ fn (mut p Parser) sql_expr() ast.Expr { if e.op == .eq && e.left is ast.Ident { ident := e.left as ast.Ident if ident.name == 'id' { - // TODO optional query_one = true - typ = table_type - // typ = table_type.set_flag(.optional) } } } } - if !query_one && !is_count { - // return an array - typ = table.new_type(p.table.find_or_register_array(table_type, 1, p.mod)) - } if p.tok.kind ==.name && p.tok.lit == 'limit' { // `limit 1` means that a single object is returned p.check_name() // `limit` @@ -63,6 +55,15 @@ fn (mut p Parser) sql_expr() ast.Expr { } p.next() } + if !query_one && !is_count { + // return an array + typ = table.new_type(p.table.find_or_register_array(table_type, 1, p.mod)) + } else if !is_count { + // return a single object + // TODO optional + // typ = table_type.set_flag(.optional) + typ = table_type + } p.check(.rcbr) // ///////// // Register this type's fields as variables so they can be used in `where`