mirror of
https://github.com/lloeki/rebel.git
synced 2025-12-06 10:04:39 +01:00
review
This commit is contained in:
parent
4ce508862d
commit
52ff7ffd50
3 changed files with 44 additions and 25 deletions
|
|
@ -13,14 +13,14 @@ 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, order_by: nil, limit: 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_by: order_by,
|
order: order,
|
||||||
limit: limit))
|
limit: limit))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -52,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
|
||||||
|
|
@ -143,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 = '"'
|
||||||
|
|
@ -180,14 +196,14 @@ module Rebel::SQL
|
||||||
SQL
|
SQL
|
||||||
end
|
end
|
||||||
|
|
||||||
def select(*fields, from: nil, where: nil, inner: nil, left: nil, right: nil, order_by: nil, limit: 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_by?(order_by)}
|
#{order?(order)}
|
||||||
#{limit?(limit)};
|
#{limit?(limit)};
|
||||||
SQL
|
SQL
|
||||||
end
|
end
|
||||||
|
|
@ -259,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)
|
||||||
|
|
@ -308,26 +329,10 @@ 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
|
||||||
|
|
@ -365,8 +370,8 @@ module Rebel::SQL
|
||||||
join ? "RIGHT #{join}" : nil
|
join ? "RIGHT #{join}" : nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def order_by?(*clause)
|
def order?(clause)
|
||||||
clause.any? ? "ORDER BY #{order_by_clause(*clause)}" : nil
|
clause ? "ORDER #{clause}" : nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def limit?(count)
|
def limit?(count)
|
||||||
|
|
|
||||||
|
|
@ -59,8 +59,8 @@ class TestExec < Minitest::Test
|
||||||
insert_into :foo, id: 1, value: '2', col: 'whatevs'
|
insert_into :foo, id: 1, value: '2', col: 'whatevs'
|
||||||
insert_into :foo, id: 2, value: '2', col: 'something'
|
insert_into :foo, id: 2, value: '2', col: 'something'
|
||||||
insert_into :foo, id: 3, value: '1', col: 'else'
|
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: {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, id: :asc]), [[3], [1], [2]])
|
assert_equal(select(:id, from: :foo, order: by(:value).asc.by(:id).desc), [[3], [2], [1]])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue