orm: `limit 1` test

pull/5460/head
Alexander Medvednikov 2020-06-22 16:58:43 +02:00
parent deb09d95b0
commit e3f00ff28b
2 changed files with 16 additions and 10 deletions

View File

@ -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

View File

@ -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`