mirror of
https://github.com/lloeki/rebel.git
synced 2025-12-06 01:54:40 +01:00
operators
This commit is contained in:
parent
65a751e375
commit
814aa46592
2 changed files with 107 additions and 0 deletions
|
|
@ -66,6 +66,58 @@ module Rebel::SQL
|
|||
def on?(clause)
|
||||
clause ? on(clause) : self
|
||||
end
|
||||
|
||||
def and(clause)
|
||||
Raw.new("#{self} AND #{Rebel::SQL.and_clause(clause)}")
|
||||
end
|
||||
|
||||
def or(clause)
|
||||
Raw.new("#{self} OR #{Rebel::SQL.and_clause(clause)}")
|
||||
end
|
||||
|
||||
def eq(n)
|
||||
case n
|
||||
when nil
|
||||
Raw.new("#{self} IS NULL")
|
||||
else
|
||||
Raw.new("#{self} = #{Rebel::SQL.name_or_value(n)}")
|
||||
end
|
||||
end
|
||||
alias == eq
|
||||
|
||||
def ne(n)
|
||||
case n
|
||||
when nil
|
||||
Raw.new("#{self} IS NOT NULL")
|
||||
else
|
||||
Raw.new("#{self} != #{Rebel::SQL.name_or_value(n)}")
|
||||
end
|
||||
end
|
||||
alias != ne
|
||||
|
||||
def lt(n)
|
||||
Raw.new("#{self} < #{Rebel::SQL.name_or_value(n)}")
|
||||
end
|
||||
alias < lt
|
||||
|
||||
def gt(n)
|
||||
Raw.new("#{self} > #{Rebel::SQL.name_or_value(n)}")
|
||||
end
|
||||
alias > gt
|
||||
|
||||
def le(n)
|
||||
Raw.new("#{self} <= #{Rebel::SQL.name_or_value(n)}")
|
||||
end
|
||||
alias <= le
|
||||
|
||||
def ge(n)
|
||||
Raw.new("#{self} >= #{Rebel::SQL.name_or_value(n)}")
|
||||
end
|
||||
alias >= ge
|
||||
|
||||
def in(*v)
|
||||
Raw.new("#{self} IN (#{Rebel::SQL.values(*v)})")
|
||||
end
|
||||
end
|
||||
|
||||
class << self
|
||||
|
|
|
|||
55
test/test_raw.rb
Normal file
55
test/test_raw.rb
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
require 'minitest/autorun'
|
||||
require 'helper'
|
||||
require 'rebel'
|
||||
|
||||
class TestRaw < Minitest::Test
|
||||
def assert_str_equal(expected, actual)
|
||||
assert_equal(expected.to_s, actual.to_s)
|
||||
end
|
||||
|
||||
def test_and
|
||||
assert_str_equal(Rebel::SQL.name(:foo).eq(1).and(Rebel::SQL.name(:bar).eq(2)), '"foo" = 1 AND "bar" = 2')
|
||||
end
|
||||
|
||||
def test_or
|
||||
assert_str_equal(Rebel::SQL.name(:foo).eq(1).or(Rebel::SQL.name(:bar).eq(2)), '"foo" = 1 OR "bar" = 2')
|
||||
end
|
||||
|
||||
def test_eq
|
||||
assert_str_equal(Rebel::SQL.name(:foo).eq(nil), '"foo" IS NULL')
|
||||
assert_str_equal(Rebel::SQL.name(:foo) == nil, '"foo" IS NULL')
|
||||
assert_str_equal(Rebel::SQL.name(:foo).eq(Rebel::SQL.name(:bar)), '"foo" = "bar"')
|
||||
assert_str_equal(Rebel::SQL.name(:foo) == Rebel::SQL.name(:bar), '"foo" = "bar"')
|
||||
end
|
||||
|
||||
def test_ne
|
||||
assert_str_equal(Rebel::SQL.name(:foo).ne(Rebel::SQL.name(:bar)), '"foo" != "bar"')
|
||||
assert_str_equal(Rebel::SQL.name(:foo) != Rebel::SQL.name(:bar), '"foo" != "bar"')
|
||||
assert_str_equal(Rebel::SQL.name(:foo).ne(nil), '"foo" IS NOT NULL')
|
||||
assert_str_equal(Rebel::SQL.name(:foo) != nil, '"foo" IS NOT NULL')
|
||||
end
|
||||
|
||||
def test_lt
|
||||
assert_str_equal(Rebel::SQL.name(:foo).lt(Rebel::SQL.name(:bar)), '"foo" < "bar"')
|
||||
assert_str_equal(Rebel::SQL.name(:foo) < Rebel::SQL.name(:bar), '"foo" < "bar"')
|
||||
end
|
||||
|
||||
def test_gt
|
||||
assert_str_equal(Rebel::SQL.name(:foo).gt(Rebel::SQL.name(:bar)), '"foo" > "bar"')
|
||||
assert_str_equal(Rebel::SQL.name(:foo) > Rebel::SQL.name(:bar), '"foo" > "bar"')
|
||||
end
|
||||
|
||||
def test_le
|
||||
assert_str_equal(Rebel::SQL.name(:foo).le(Rebel::SQL.name(:bar)), '"foo" <= "bar"')
|
||||
assert_str_equal(Rebel::SQL.name(:foo) <= Rebel::SQL.name(:bar), '"foo" <= "bar"')
|
||||
end
|
||||
|
||||
def test_ge
|
||||
assert_str_equal(Rebel::SQL.name(:foo).ge(Rebel::SQL.name(:bar)), '"foo" >= "bar"')
|
||||
assert_str_equal(Rebel::SQL.name(:foo) >= Rebel::SQL.name(:bar), '"foo" >= "bar"')
|
||||
end
|
||||
|
||||
def test_in
|
||||
assert_str_equal(Rebel::SQL.name(:foo).in(1, 2, 3), '"foo" IN (1, 2, 3)')
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue