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

orm: add support for ordering (#5624)

This commit is contained in:
Louis Schmieder 2020-07-02 19:29:22 +02:00 committed by GitHub
parent 6d788654e1
commit b89cbf3224
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 1 deletions

View File

@ -197,6 +197,11 @@ fn test_orm_sqlite() {
} }
assert z.len == 2 assert z.len == 2
assert z[0].id == 3 assert z[0].id == 3
oldest := sql db {
select from User order age desc limit 1
}
assert oldest.age == 34
} }
fn test_orm_pg() { fn test_orm_pg() {

View File

@ -846,6 +846,9 @@ pub:
has_where bool has_where bool
has_offset bool has_offset bool
offset_expr Expr offset_expr Expr
has_order bool
order_expr Expr
has_desc bool
is_array bool is_array bool
table_type table.Type table_type table.Type
pos token.Position pos token.Position
@ -934,6 +937,7 @@ pub fn (expr Expr) position() token.Position {
} }
// ast.None { } // ast.None { }
PrefixExpr { PrefixExpr {
return expr.pos return expr.pos
} }
// ast.ParExpr { } // ast.ParExpr { }

View File

@ -2808,6 +2808,9 @@ fn (mut c Checker) sql_expr(mut node ast.SqlExpr) table.Type {
if node.has_limit { if node.has_limit {
c.expr(node.limit_expr) c.expr(node.limit_expr)
} }
if node.has_order {
c.expr(node.order_expr)
}
c.expr(node.db_expr) c.expr(node.db_expr)
return node.typ return node.typ
} }

View File

@ -133,7 +133,16 @@ fn (mut g Gen) sql_select_expr(node ast.SqlExpr) {
if node.has_where && node.where_expr is ast.InfixExpr { if node.has_where && node.where_expr is ast.InfixExpr {
g.expr_to_sql(node.where_expr) g.expr_to_sql(node.where_expr)
} }
g.write(' ORDER BY id ') if node.has_order {
g.write(' ORDER BY ')
g.sql_side = .left
g.expr_to_sql(node.order_expr)
if node.has_desc {
g.write(' DESC ')
}
} else {
g.write(' ORDER BY id ')
}
if node.has_limit { if node.has_limit {
g.write(' LIMIT ') g.write(' LIMIT ')
g.expr_to_sql(node.limit_expr) g.expr_to_sql(node.limit_expr)

View File

@ -42,6 +42,18 @@ fn (mut p Parser) sql_expr() ast.Expr {
mut limit_expr := ast.Expr{} mut limit_expr := ast.Expr{}
mut has_offset := false mut has_offset := false
mut offset_expr := ast.Expr{} mut offset_expr := ast.Expr{}
mut has_order := false
mut order_expr := ast.Expr{}
mut has_desc := false
if p.tok.kind == .name && p.tok.lit == 'order' {
p.check_name() // `order`
has_order = true
order_expr = p.expr(0)
if p.tok.kind == .name && p.tok.lit == 'desc' {
p.check_name() // `desc`
has_desc = true
}
}
if p.tok.kind == .name && p.tok.lit == 'limit' { if p.tok.kind == .name && p.tok.lit == 'limit' {
// `limit 1` means that a single object is returned // `limit 1` means that a single object is returned
p.check_name() // `limit` p.check_name() // `limit`
@ -77,6 +89,9 @@ fn (mut p Parser) sql_expr() ast.Expr {
limit_expr: limit_expr limit_expr: limit_expr
has_offset: has_offset has_offset: has_offset
offset_expr: offset_expr offset_expr: offset_expr
has_order: has_order
order_expr: order_expr
has_desc: has_desc
is_array: !query_one is_array: !query_one
pos: pos pos: pos
} }