aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/visitors/to_sql.rb
diff options
context:
space:
mode:
authorVipul A M <vipulnsward@gmail.com>2014-10-24 12:01:26 +0530
committerVipul A M <vipulnsward@gmail.com>2016-04-11 22:13:15 +0530
commit549c06dc6d1432dcd5994a117716818f96f989af (patch)
tree29f9f72de873f9a08766ade5bab3ff40fb6cebeb /lib/arel/visitors/to_sql.rb
parent7a5faf5fee02accd6d96963444ad5c386613b91f (diff)
downloadrails-549c06dc6d1432dcd5994a117716818f96f989af.tar.gz
rails-549c06dc6d1432dcd5994a117716818f96f989af.tar.bz2
rails-549c06dc6d1432dcd5994a117716818f96f989af.zip
DRY up visit_Arel_Nodes_SelectCore and extract nodes collection to collect_nodes_for, for collecting wheres, projections, groups, windows
Diffstat (limited to 'lib/arel/visitors/to_sql.rb')
-rw-r--r--lib/arel/visitors/to_sql.rb46
1 files changed, 13 insertions, 33 deletions
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb
index cab0ab7eca..1435f3e21d 100644
--- a/lib/arel/visitors/to_sql.rb
+++ b/lib/arel/visitors/to_sql.rb
@@ -243,53 +243,33 @@ module Arel
collector = maybe_visit o.set_quantifier, collector
- unless o.projections.empty?
- collector << SPACE
- len = o.projections.length - 1
- o.projections.each_with_index do |x, i|
- collector = visit(x, collector)
- collector << COMMA unless len == i
- end
- end
+ collect_nodes_for o.projections, collector, SPACE
if o.source && !o.source.empty?
collector << " FROM "
collector = visit o.source, collector
end
- unless o.wheres.empty?
- collector << WHERE
- len = o.wheres.length - 1
- o.wheres.each_with_index do |x, i|
- collector = visit(x, collector)
- collector << AND unless len == i
- end
- end
-
- unless o.groups.empty?
- collector << GROUP_BY
- len = o.groups.length - 1
- o.groups.each_with_index do |x, i|
- collector = visit(x, collector)
- collector << COMMA unless len == i
- end
- end
-
+ collect_nodes_for o.wheres, collector, WHERE, AND
+ collect_nodes_for o.groups, collector, GROUP_BY
unless o.havings.empty?
collector << " HAVING "
inject_join o.havings, collector, AND
end
+ collect_nodes_for o.windows, collector, WINDOW
+
+ collector
+ end
- unless o.windows.empty?
- collector << WINDOW
- len = o.windows.length - 1
- o.windows.each_with_index do |x, i|
+ def collect_nodes_for nodes, collector, spacer, connector = COMMA
+ unless nodes.empty?
+ collector << spacer
+ len = nodes.length - 1
+ nodes.each_with_index do |x, i|
collector = visit(x, collector)
- collector << COMMA unless len == i
+ collector << connector unless len == i
end
end
-
- collector
end
def visit_Arel_Nodes_Bin o, collector