mirror of
https://github.com/lloeki/rebel.git
synced 2025-12-06 10:04:39 +01:00
select: order_by limit
This commit is contained in:
parent
179cba86b6
commit
4ce508862d
2 changed files with 49 additions and 4 deletions
|
|
@ -13,13 +13,15 @@ module Rebel::SQL
|
|||
exec(Rebel::SQL.drop_table(table_name))
|
||||
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,
|
||||
from: from,
|
||||
where: where,
|
||||
inner: inner,
|
||||
left: left,
|
||||
right: right))
|
||||
right: right,
|
||||
order_by: order_by,
|
||||
limit: limit))
|
||||
end
|
||||
|
||||
def insert_into(table_name, *rows)
|
||||
|
|
@ -178,13 +180,15 @@ module Rebel::SQL
|
|||
SQL
|
||||
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
|
||||
SELECT #{names(*fields)} FROM #{name(from)}
|
||||
#{inner?(inner)}
|
||||
#{left?(left)}
|
||||
#{right?(right)}
|
||||
#{where?(where)};
|
||||
#{where?(where)}
|
||||
#{order_by?(order_by)}
|
||||
#{limit?(limit)};
|
||||
SQL
|
||||
end
|
||||
|
||||
|
|
@ -304,10 +308,26 @@ module Rebel::SQL
|
|||
"#{name_or_value(l)} = #{name_or_value(r)}"
|
||||
end
|
||||
|
||||
def order(l, r)
|
||||
"#{name(l)} #{value(raw(r.to_s))}"
|
||||
end
|
||||
|
||||
def assign_clause(clause)
|
||||
list(clause.map { |k, v| equal(k, v) })
|
||||
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)
|
||||
case right
|
||||
when Array
|
||||
|
|
@ -344,5 +364,13 @@ module Rebel::SQL
|
|||
def right?(join)
|
||||
join ? "RIGHT #{join}" : nil
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue