select: order_by limit

This commit is contained in:
Thomas Kienlen 2017-05-31 15:44:12 +02:00
parent 179cba86b6
commit 4ce508862d
2 changed files with 49 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_by: 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_by: order_by,
limit: limit))
end end
def insert_into(table_name, *rows) def insert_into(table_name, *rows)
@ -178,13 +180,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_by: 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_by?(order_by)}
#{limit?(limit)};
SQL SQL
end end
@ -304,10 +308,26 @@ module Rebel::SQL
"#{name_or_value(l)} = #{name_or_value(r)}" "#{name_or_value(l)} = #{name_or_value(r)}"
end end
def order(l, r)
"#{name(l)} #{value(raw(r.to_s))}"
end
def assign_clause(clause) def assign_clause(clause)
list(clause.map { |k, v| equal(k, v) }) list(clause.map { |k, v| equal(k, v) })
end end
def order_by_clause(clause)
case clause
when Symbol, String
order_by_clause(name(clause) => nil)
when Hash
list(clause.map { |k, v| order(k, v) })
when Array
list(clause.map { |c| order_by_clause(c) })
else raise NotImplementedError, clause.class
end
end
def clause_term(left, right) def clause_term(left, right)
case right case right
when Array when Array
@ -344,5 +364,13 @@ module Rebel::SQL
def right?(join) def right?(join)
join ? "RIGHT #{join}" : nil join ? "RIGHT #{join}" : nil
end end
def order_by?(*clause)
clause.any? ? "ORDER BY #{order_by_clause(*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: :col), [[3], [2], [1]])
assert_equal(select(:id, from: :foo, order_by: {id: :desc}), [[3], [2], [1]])
assert_equal(select(:id, from: :foo, order_by: [value: :asc, id: :asc]), [[3], [1], [2]])
end
end end