Add support for LIMIT and OFFSET

This commit is contained in:
Loic Nageleisen 2017-11-21 11:19:48 +01:00
parent adfa38baea
commit 6c4b435ae6
2 changed files with 18 additions and 3 deletions

View file

@ -13,14 +13,16 @@ module Rebel::SQL
exec(Rebel::SQL.drop_table(table_name)) exec(Rebel::SQL.drop_table(table_name))
end end
def select(*fields, distinct: distinct, from: nil, where: nil, inner: nil, left: nil, right: nil) def select(*fields, distinct: distinct, from: nil, where: nil, inner: nil, left: nil, right: nil, limit: nil, offset: nil)
exec(Rebel::SQL.select(*fields, exec(Rebel::SQL.select(*fields,
distinct: distinct, distinct: distinct,
from: from, from: from,
where: where, where: where,
inner: inner, inner: inner,
left: left, left: left,
right: right)) right: right,
limit: limit,
offset: offset))
end end
def insert_into(table_name, *rows) def insert_into(table_name, *rows)
@ -179,7 +181,7 @@ module Rebel::SQL
SQL SQL
end end
def select(*fields, distinct: nil, from: nil, where: nil, inner: nil, left: nil, right: nil) def select(*fields, distinct: nil, from: nil, where: nil, inner: nil, left: nil, right: nil, limit: nil, offset: nil)
raw <<-SQL raw <<-SQL
SELECT #{distinct ? "DISTINCT #{names(*distinct)}" : names(*fields)} SELECT #{distinct ? "DISTINCT #{names(*distinct)}" : names(*fields)}
#{from?(from)} #{from?(from)}
@ -187,6 +189,7 @@ module Rebel::SQL
#{left?(left)} #{left?(left)}
#{right?(right)} #{right?(right)}
#{where?(where)} #{where?(where)}
#{limit?(limit, offset)}
SQL SQL
end end
@ -350,5 +353,9 @@ module Rebel::SQL
def right?(join) def right?(join)
join ? "RIGHT #{join}" : nil join ? "RIGHT #{join}" : nil
end end
def limit?(limit, offset)
limit ? "LIMIT #{value(limit)}" << (offset ? " OFFSET #{offset}" : "") : nil
end
end end
end end

View file

@ -117,6 +117,14 @@ class TestRaw < Minitest::Test
assert_str_equal(Rebel::SQL.select(distinct: [:bar, :baz], from: :foo).gsub(/\s+/, ' ').strip, 'SELECT DISTINCT "bar", "baz" FROM "foo"') assert_str_equal(Rebel::SQL.select(distinct: [:bar, :baz], from: :foo).gsub(/\s+/, ' ').strip, 'SELECT DISTINCT "bar", "baz" FROM "foo"')
end end
def test_select_limit
assert_str_equal(Rebel::SQL.select(:bar, from: :foo, limit: 10).gsub(/\s+/, ' ').strip, 'SELECT "bar" FROM "foo" LIMIT 10')
end
def test_select_offset
assert_str_equal(Rebel::SQL.select(:bar, from: :foo, limit: 10, offset: 20).gsub(/\s+/, ' ').strip, 'SELECT "bar" FROM "foo" LIMIT 10 OFFSET 20')
end
def test_nested_select def test_nested_select
assert_str_equal(Rebel::SQL.select(Rebel::SQL.raw('*'), from: Rebel::SQL.name(:foo), where: Rebel::SQL.name(:bar).in(Rebel::SQL.select(Rebel::SQL.name(:bar), from: Rebel::SQL.name(:foo)))).gsub(/\s+/, ' ').strip, 'SELECT * FROM "foo" WHERE "bar" IN ( SELECT "bar" FROM "foo" )') assert_str_equal(Rebel::SQL.select(Rebel::SQL.raw('*'), from: Rebel::SQL.name(:foo), where: Rebel::SQL.name(:bar).in(Rebel::SQL.select(Rebel::SQL.name(:bar), from: Rebel::SQL.name(:foo)))).gsub(/\s+/, ' ').strip, 'SELECT * FROM "foo" WHERE "bar" IN ( SELECT "bar" FROM "foo" )')
end end