diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/nodes.rb | 1 | ||||
-rw-r--r-- | lib/arel/nodes/group.rb | 11 | ||||
-rw-r--r-- | lib/arel/nodes/select_core.rb | 8 | ||||
-rw-r--r-- | lib/arel/select_manager.rb | 7 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 7 |
5 files changed, 30 insertions, 4 deletions
diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb index 6dcd86867a..bf7ae830d0 100644 --- a/lib/arel/nodes.rb +++ b/lib/arel/nodes.rb @@ -14,6 +14,7 @@ require 'arel/nodes/delete_statement' require 'arel/nodes/unqualified_column' require 'arel/nodes/table_alias' require 'arel/nodes/join' +require 'arel/nodes/group' require 'arel/nodes/inner_join' require 'arel/nodes/outer_join' require 'arel/nodes/string_join' diff --git a/lib/arel/nodes/group.rb b/lib/arel/nodes/group.rb new file mode 100644 index 0000000000..57a7c579da --- /dev/null +++ b/lib/arel/nodes/group.rb @@ -0,0 +1,11 @@ +module Arel + module Nodes + class Group + attr_accessor :expr + + def initialize expr + @expr = expr + end + end + end +end diff --git a/lib/arel/nodes/select_core.rb b/lib/arel/nodes/select_core.rb index 5e27c7c414..6e85968b31 100644 --- a/lib/arel/nodes/select_core.rb +++ b/lib/arel/nodes/select_core.rb @@ -1,19 +1,21 @@ module Arel module Nodes class SelectCore - attr_reader :froms, :projections, :wheres + attr_reader :froms, :projections, :wheres, :groups def initialize @froms = [] @projections = [] @wheres = [] + @groups = [] end def initialize_copy other super - @froms = @froms.clone + @froms = @froms.clone @projections = @projections.clone - @wheres = @wheres.clone + @wheres = @wheres.clone + @group = @groups.clone end end end diff --git a/lib/arel/select_manager.rb b/lib/arel/select_manager.rb index c9214b20a2..3ac6a5cbc8 100644 --- a/lib/arel/select_manager.rb +++ b/lib/arel/select_manager.rb @@ -13,6 +13,13 @@ module Arel self end + def group *columns + columns.each do |column| + @ctx.groups.push Nodes::Group.new column + end + self + end + def from table @ctx.froms << table self diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 4ac38fb13e..55b3259e81 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -54,10 +54,15 @@ module Arel [ "SELECT #{o.projections.map { |x| visit x }.join ', '}", ("FROM #{o.froms.map { |x| visit x }.join ', ' }" unless o.froms.empty?), - ("WHERE #{o.wheres.map { |x| visit x }.join ' AND ' }" unless o.wheres.empty?) + ("WHERE #{o.wheres.map { |x| visit x }.join ' AND ' }" unless o.wheres.empty?), + ("GROUP BY #{o.groups.map { |x| visit x }.join ', ' }" unless o.groups.empty?) ].compact.join ' ' end + def visit_Arel_Nodes_Group o + visit o.expr + end + def visit_Arel_Nodes_Count o "COUNT(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| visit x }.join(', ')})" end |