This commit is contained in:
Thomas Kienlen 2017-06-02 15:14:26 +00:00 committed by GitHub
commit 81fd37a1cf
3 changed files with 68 additions and 4 deletions

View file

@ -13,13 +13,15 @@ module Rebel::SQL
exec(Rebel::SQL.drop_table(table_name)) exec(Rebel::SQL.drop_table(table_name))
end end
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, order: nil, limit: nil)
exec(Rebel::SQL.select(*fields, exec(Rebel::SQL.select(*fields,
from: from, from: from,
where: where, where: where,
inner: inner, inner: inner,
left: left, left: left,
right: right)) right: right,
order: order,
limit: limit))
end end
def insert_into(table_name, *rows) def insert_into(table_name, *rows)
@ -50,6 +52,10 @@ module Rebel::SQL
Rebel::SQL.outer_join(table, on: on) Rebel::SQL.outer_join(table, on: on)
end end
def by(*clause)
Rebel::SQL.by(clause)
end
class Raw < String class Raw < String
def wants_parens! def wants_parens!
@wants_parens = true @wants_parens = true
@ -141,6 +147,18 @@ module Rebel::SQL
def like(n) def like(n)
Raw.new("#{self} LIKE #{Rebel::SQL.value(n)}") Raw.new("#{self} LIKE #{Rebel::SQL.value(n)}")
end end
def asc
Raw.new("#{self} ASC")
end
def desc
Raw.new("#{self} DESC")
end
# def by(*clause)
# Raw.new(Rebel::SQL.list(self, Rebel::SQL.names(*clause)))
# end
end end
@identifier_quote = '"' @identifier_quote = '"'
@ -178,13 +196,15 @@ module Rebel::SQL
SQL SQL
end end
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, order: nil, limit: nil)
<<-SQL <<-SQL
SELECT #{names(*fields)} FROM #{name(from)} SELECT #{names(*fields)} FROM #{name(from)}
#{inner?(inner)} #{inner?(inner)}
#{left?(left)} #{left?(left)}
#{right?(right)} #{right?(right)}
#{where?(where)}; #{where?(where)}
#{order?(order)}
#{limit?(limit)};
SQL SQL
end end
@ -255,6 +275,11 @@ module Rebel::SQL
raw(right? outer_join(table, on: on)) raw(right? outer_join(table, on: on))
end end
def by(clause)
raw("BY #{names(*clause)}")
end
## Support ## Support
def name(name) def name(name)
@ -344,5 +369,13 @@ module Rebel::SQL
def right?(join) def right?(join)
join ? "RIGHT #{join}" : nil join ? "RIGHT #{join}" : nil
end end
def order?(clause)
clause ? "ORDER #{clause}" : nil
end
def limit?(count)
count ? "LIMIT #{count}" : nil
end
end end
end end

View file

@ -46,4 +46,21 @@ class TestExec < Minitest::Test
insert_into :foo, id: 1, col: 'whatevs' insert_into :foo, id: 1, col: 'whatevs'
assert_equal(select('*', from: :foo), [[1, 'whatevs']]) assert_equal(select('*', from: :foo), [[1, 'whatevs']])
end end
def test_limit
create_table :foo, id: 'INT', col: 'VARCHAR(255)'
insert_into :foo, id: 1, col: 'whatevs'
insert_into :foo, id: 2, col: 'something else'
assert_equal(select('*', from: :foo, limit: 1), [[1, 'whatevs']])
end
def test_order_by
create_table :foo, id: 'INT', col: 'VARCHAR(255)', value: 'VARCHAR(255)'
insert_into :foo, id: 1, value: '2', col: 'whatevs'
insert_into :foo, id: 2, value: '2', col: 'something'
insert_into :foo, id: 3, value: '1', col: 'else'
assert_equal(select(:id, from: :foo, order: by(:id).desc), [[3], [2], [1]])
assert_equal(select(:id, from: :foo, order: by(:value, :id).asc), [[3], [1], [2]])
# assert_equal(select(:id, from: :foo, order: by(:value).asc.by(:id).desc), [[3], [2], [1]])
end
end end

View file

@ -96,4 +96,18 @@ class TestRaw < Minitest::Test
assert_str_equal(Rebel::SQL.value(DateTime.new(2016, 12, 31, 23, 59, 59)), "'2016-12-31T23:59:59+00:00'") assert_str_equal(Rebel::SQL.value(DateTime.new(2016, 12, 31, 23, 59, 59)), "'2016-12-31T23:59:59+00:00'")
assert_str_equal(Rebel::SQL.value(nil), 'NULL') assert_str_equal(Rebel::SQL.value(nil), 'NULL')
end end
def test_asc
assert_str_equal(Rebel::SQL.raw("'FOO'").asc, "'FOO' ASC")
end
def test_desc
assert_str_equal(Rebel::SQL.raw("'FOO'").desc, "'FOO' DESC")
end
def test_by
assert_str_equal(Rebel::SQL.by(:foo), 'BY "foo"')
assert_str_equal(Rebel::SQL.by(:foo).desc, 'BY "foo" DESC')
# assert_str_equal(Rebel::SQL.by(:foo).desc.by(:bar).asc, 'BY "foo" DESC, "bar" ASC')
end
end end