aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/arel/attributes/attribute.rb16
-rw-r--r--lib/arel/nodes.rb2
-rw-r--r--lib/arel/nodes/between.rb6
-rw-r--r--lib/arel/nodes/less_than.rb6
-rw-r--r--lib/arel/visitors/to_sql.rb8
5 files changed, 35 insertions, 3 deletions
diff --git a/lib/arel/attributes/attribute.rb b/lib/arel/attributes/attribute.rb
index 6bb880d211..e1f7fd81d2 100644
--- a/lib/arel/attributes/attribute.rb
+++ b/lib/arel/attributes/attribute.rb
@@ -12,10 +12,20 @@ module Arel
end
def in other
- if Arel::SelectManager === other
- other = other.to_a.map { |x| x.id }
+ case other
+ when Arel::SelectManager
+ Nodes::In.new self, other.to_a.map { |x| x.id }
+ when Range
+ if other.exclude_end?
+ left = Nodes::GreaterThanOrEqual.new(self, other.min)
+ right = Nodes::LessThan.new(self, other.max + 1)
+ Nodes::And.new left, right
+ else
+ Nodes::Between.new(self, Nodes::And.new(other.min, other.max))
+ end
+ else
+ Nodes::In.new self, other
end
- Nodes::In.new self, other
end
def gteq right
diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb
index ac4bba57c2..fade666ff7 100644
--- a/lib/arel/nodes.rb
+++ b/lib/arel/nodes.rb
@@ -1,12 +1,14 @@
require 'arel/nodes/node'
require 'arel/nodes/binary'
require 'arel/nodes/equality'
+require 'arel/nodes/between'
require 'arel/nodes/not_equal'
require 'arel/nodes/assignment'
require 'arel/nodes/or'
require 'arel/nodes/and'
require 'arel/nodes/greater_than'
require 'arel/nodes/greater_than_or_equal'
+require 'arel/nodes/less_than'
require 'arel/nodes/in'
require 'arel/nodes/lock'
diff --git a/lib/arel/nodes/between.rb b/lib/arel/nodes/between.rb
new file mode 100644
index 0000000000..2e7596cdae
--- /dev/null
+++ b/lib/arel/nodes/between.rb
@@ -0,0 +1,6 @@
+module Arel
+ module Nodes
+ class Between < Arel::Nodes::Binary
+ end
+ end
+end
diff --git a/lib/arel/nodes/less_than.rb b/lib/arel/nodes/less_than.rb
new file mode 100644
index 0000000000..cfaf716c42
--- /dev/null
+++ b/lib/arel/nodes/less_than.rb
@@ -0,0 +1,6 @@
+module Arel
+ module Nodes
+ class LessThan < Arel::Nodes::Binary
+ end
+ end
+end
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb
index 5a92e27149..d6a9a3697f 100644
--- a/lib/arel/visitors/to_sql.rb
+++ b/lib/arel/visitors/to_sql.rb
@@ -122,6 +122,10 @@ module Arel
"#{visit o.relation} #{quote_table_name o.name}"
end
+ def visit_Arel_Nodes_Between o
+ "#{visit o.left} BETWEEN #{visit o.right}"
+ end
+
def visit_Arel_Nodes_GreaterThanOrEqual o
"#{visit o.left} >= #{visit o.right}"
end
@@ -130,6 +134,10 @@ module Arel
"#{visit o.left} > #{visit o.right}"
end
+ def visit_Arel_Nodes_LessThan o
+ "#{visit o.left} < #{visit o.right}"
+ end
+
def visit_Arel_Nodes_StringJoin o
"#{visit o.left} #{visit o.right}"
end