aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/arel/nodes.rb1
-rw-r--r--lib/arel/nodes/binary.rb2
-rw-r--r--lib/arel/nodes/select_statement.rb14
-rw-r--r--lib/arel/nodes/with.rb17
-rw-r--r--lib/arel/select_manager.rb29
-rw-r--r--lib/arel/visitors/to_sql.rb18
6 files changed, 75 insertions, 6 deletions
diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb
index 64f2cc13fd..cbd10c31e0 100644
--- a/lib/arel/nodes.rb
+++ b/lib/arel/nodes.rb
@@ -9,6 +9,7 @@ require 'arel/nodes/update_statement'
# unary
require 'arel/nodes/unary'
require 'arel/nodes/unqualified_column'
+require 'arel/nodes/with'
# binary
require 'arel/nodes/binary'
diff --git a/lib/arel/nodes/binary.rb b/lib/arel/nodes/binary.rb
index 0d02554199..e72228f11c 100644
--- a/lib/arel/nodes/binary.rb
+++ b/lib/arel/nodes/binary.rb
@@ -29,6 +29,8 @@ module Arel
NotEqual
NotIn
Or
+ Union
+ UnionAll
}.each do |name|
const_set name, Class.new(Binary)
end
diff --git a/lib/arel/nodes/select_statement.rb b/lib/arel/nodes/select_statement.rb
index c9a0cde4e0..cd74dd8e28 100644
--- a/lib/arel/nodes/select_statement.rb
+++ b/lib/arel/nodes/select_statement.rb
@@ -2,15 +2,17 @@ module Arel
module Nodes
class SelectStatement < Arel::Nodes::Node
attr_reader :cores
- attr_accessor :limit, :orders, :lock, :offset
+ attr_accessor :limit, :orders, :lock, :offset, :with, :with_recursive
def initialize cores = [SelectCore.new]
#puts caller
- @cores = cores
- @orders = []
- @limit = nil
- @lock = nil
- @offset = nil
+ @cores = cores
+ @orders = []
+ @limit = nil
+ @lock = nil
+ @offset = nil
+ @with = nil
+ @with_recursive = nil
end
def initialize_copy other
diff --git a/lib/arel/nodes/with.rb b/lib/arel/nodes/with.rb
new file mode 100644
index 0000000000..ce1644b60e
--- /dev/null
+++ b/lib/arel/nodes/with.rb
@@ -0,0 +1,17 @@
+module Arel
+ module Nodes
+ class With < Arel::Nodes::Unary
+ attr_reader :children
+ alias value children
+ alias expr children
+
+ def initialize *children
+ @children = children
+ end
+
+ end
+
+ class WithRecursive < With; end
+ end
+end
+
diff --git a/lib/arel/select_manager.rb b/lib/arel/select_manager.rb
index b4103144fe..4f510d096b 100644
--- a/lib/arel/select_manager.rb
+++ b/lib/arel/select_manager.rb
@@ -134,6 +134,35 @@ module Arel
Nodes::SqlLiteral.new viz.accept @ctx
end
+ def union operation, other = nil
+ if operation.is_a? Symbol
+ if operation === :all
+ node_class = Nodes::UnionAll
+ else
+ raise "Only supported UNION operation is :all"
+ end
+ else
+ other = operation
+ node_class = Nodes::Union
+ end
+
+ node_class.new self.ast, other.ast
+ end
+
+ def with *subqueries
+ if subqueries.first.is_a? Symbol
+ if subqueries.shift == :recursive
+ node_class = Nodes::WithRecursive
+ else
+ raise "Only supported WITH modifier is :recursive"
+ end
+ else
+ node_class = Nodes::With
+ end
+ @ast.with = node_class.new(*subqueries)
+ end
+
+
def take limit
@ast.limit = Nodes::Limit.new(limit)
@ctx.top = Nodes::Top.new(limit)
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb
index d5534384f8..f3c8710068 100644
--- a/lib/arel/visitors/to_sql.rb
+++ b/lib/arel/visitors/to_sql.rb
@@ -129,6 +129,8 @@ eowarn
def visit_Arel_Nodes_SelectStatement o
[
+ (visit(o.with) if o.with),
+ (visit(o.with_recursive) if o.with_recursive),
o.cores.map { |x| visit_Arel_Nodes_SelectCore x }.join,
("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?),
(visit(o.limit) if o.limit),
@@ -149,6 +151,22 @@ eowarn
].compact.join ' '
end
+ def visit_Arel_Nodes_With o
+ "WITH #{visit o.children}"
+ end
+
+ def visit_Arel_Nodes_WithRecursive o
+ "WITH RECURSIVE #{visit o.children}"
+ end
+
+ def visit_Arel_Nodes_Union o
+ "( #{visit o.left} UNION #{visit o.right} )"
+ end
+
+ def visit_Arel_Nodes_UnionAll o
+ "( #{visit o.left} UNION ALL #{visit o.right} )"
+ end
+
def visit_Arel_Nodes_Having o
"HAVING #{visit o.expr}"
end