orm: offset

pull/5532/head
Alexander Medvednikov 2020-06-27 16:22:41 +02:00
parent f073ffa4ad
commit f990a0b3d3
2 changed files with 14 additions and 0 deletions

View File

@ -136,6 +136,10 @@ fn (mut g Gen) sql_select_expr(node ast.SqlExpr) {
g.write(' limit ')
g.expr_to_sql(node.limit_expr)
}
if node.has_offset {
g.write(' offset ')
g.expr_to_sql(node.offset_expr)
}
g.writeln('"));')
// Dump all sql parameters generated by our custom expr handler
binds := g.sql_buf.str()

View File

@ -40,6 +40,8 @@ fn (mut p Parser) sql_expr() ast.Expr {
}
mut has_limit := false
mut limit_expr := ast.Expr{}
mut has_offset := false
mut offset_expr := ast.Expr{}
if p.tok.kind == .name && p.tok.lit == 'limit' {
// `limit 1` means that a single object is returned
p.check_name() // `limit`
@ -50,6 +52,12 @@ fn (mut p Parser) sql_expr() ast.Expr {
}
limit_expr = p.expr(0)
}
if p.tok.kind == .name && p.tok.lit == 'offset' {
// `limit 1` means that a single object is returned
p.check_name() // `limit`
has_offset = true
offset_expr = p.expr(0)
}
if !query_one && !is_count {
// return an array
typ = table.new_type(p.table.find_or_register_array(table_type, 1, p.mod))
@ -69,6 +77,8 @@ fn (mut p Parser) sql_expr() ast.Expr {
has_where: has_where
has_limit: has_limit
limit_expr: limit_expr
has_offset: has_offset
offset_expr: offset_expr
is_array: !query_one
pos: pos
}