mirror of
https://github.com/lloeki/rebel.git
synced 2025-12-06 10:04:39 +01:00
Add support for SELECT without FROM
This commit is contained in:
parent
818da9047a
commit
a3712ceefd
2 changed files with 10 additions and 1 deletions
|
|
@ -180,7 +180,8 @@ module Rebel::SQL
|
||||||
|
|
||||||
def select(*fields, from: nil, where: nil, inner: nil, left: nil, right: nil)
|
def select(*fields, from: nil, where: nil, inner: nil, left: nil, right: nil)
|
||||||
raw <<-SQL
|
raw <<-SQL
|
||||||
SELECT #{names(*fields)} FROM #{name(from)}
|
SELECT #{names(*fields)}
|
||||||
|
#{from?(from)}
|
||||||
#{inner?(inner)}
|
#{inner?(inner)}
|
||||||
#{left?(left)}
|
#{left?(left)}
|
||||||
#{right?(right)}
|
#{right?(right)}
|
||||||
|
|
@ -329,6 +330,10 @@ module Rebel::SQL
|
||||||
end.join(' AND ')
|
end.join(' AND ')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def from?(from)
|
||||||
|
from ? "FROM #{name(from)}" : nil
|
||||||
|
end
|
||||||
|
|
||||||
def where?(*clause)
|
def where?(*clause)
|
||||||
clause.any? ? "WHERE #{and_clause(*clause)}" : nil
|
clause.any? ? "WHERE #{and_clause(*clause)}" : nil
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -105,6 +105,10 @@ class TestRaw < Minitest::Test
|
||||||
assert_str_equal(Rebel::SQL.select(Rebel::SQL.raw('*'), from: Rebel::SQL.name(:foo)).gsub(/\s+/, ' ').strip, 'SELECT * FROM "foo"')
|
assert_str_equal(Rebel::SQL.select(Rebel::SQL.raw('*'), from: Rebel::SQL.name(:foo)).gsub(/\s+/, ' ').strip, 'SELECT * FROM "foo"')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_select_without_from
|
||||||
|
assert_str_equal(Rebel::SQL.select(Rebel::SQL.raw('1')).strip, 'SELECT 1')
|
||||||
|
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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue