diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-09-07 16:37:11 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-09-07 16:37:11 -0700 |
commit | 5ab502a755d0031d229278b6f14123c45623dd04 (patch) | |
tree | 3c2948cda53c1c33d69b8d81542ce2d407adc512 | |
parent | cea486ac810a92ef4f96ef84e3a412fd7a4f6925 (diff) | |
download | rails-5ab502a755d0031d229278b6f14123c45623dd04.tar.gz rails-5ab502a755d0031d229278b6f14123c45623dd04.tar.bz2 rails-5ab502a755d0031d229278b6f14123c45623dd04.zip |
adding "as" and to_sql to count nodes
-rw-r--r-- | lib/arel/attributes/attribute.rb | 6 | ||||
-rw-r--r-- | lib/arel/nodes/count.rb | 13 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 4 | ||||
-rw-r--r-- | spec/arel/attributes/attribute_spec.rb | 7 | ||||
-rw-r--r-- | spec/arel/nodes/count_spec.rb | 12 |
5 files changed, 40 insertions, 2 deletions
diff --git a/lib/arel/attributes/attribute.rb b/lib/arel/attributes/attribute.rb index ff17d6136c..6087dc8e52 100644 --- a/lib/arel/attributes/attribute.rb +++ b/lib/arel/attributes/attribute.rb @@ -8,6 +8,10 @@ module Arel def in other Nodes::In.new self, other end + + def count + Nodes::Count.new [self] + end end class String < Attribute; end @@ -17,4 +21,6 @@ module Arel class Float < Attribute; end class Integer < Attribute; end end + + Attribute = Attributes::Attribute end diff --git a/lib/arel/nodes/count.rb b/lib/arel/nodes/count.rb index b7c4b60948..1222a791bb 100644 --- a/lib/arel/nodes/count.rb +++ b/lib/arel/nodes/count.rb @@ -1,11 +1,22 @@ module Arel module Nodes class Count - attr_accessor :expressions, :distinct + attr_accessor :expressions, :distinct, :alias def initialize expr, distinct = false @expressions = expr @distinct = distinct + @alias = nil + end + + def as aliaz + self.alias = SqlLiteral.new(aliaz) + self + end + + def to_sql + viz = Visitors::ToSql.new Table.engine + viz.accept self end end end diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 55b3259e81..38cb81717d 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -64,7 +64,9 @@ module Arel end def visit_Arel_Nodes_Count o - "COUNT(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| visit x }.join(', ')})" + "COUNT(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| + visit x + }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}" end def visit_Arel_Nodes_TableAlias o diff --git a/spec/arel/attributes/attribute_spec.rb b/spec/arel/attributes/attribute_spec.rb index e6e6483e64..f9eb56391e 100644 --- a/spec/arel/attributes/attribute_spec.rb +++ b/spec/arel/attributes/attribute_spec.rb @@ -3,6 +3,13 @@ require 'spec_helper' module Arel module Attributes describe 'attribute' do + describe '#count' do + it 'should return a count node' do + relation = Table.new(:users) + relation[:id].count.should be_kind_of Nodes::Count + end + end + describe '#eq' do it 'should return an equality node' do attribute = Attribute.new nil, nil, nil diff --git a/spec/arel/nodes/count_spec.rb b/spec/arel/nodes/count_spec.rb new file mode 100644 index 0000000000..7013a16429 --- /dev/null +++ b/spec/arel/nodes/count_spec.rb @@ -0,0 +1,12 @@ +require 'spec_helper' + +describe Arel::Nodes::Count do + describe "as" do + it 'should alias the count' do + table = Arel::Table.new :users + table[:id].count.as('foo').to_sql.should be_like %{ + COUNT("users"."id") AS foo + } + end + end +end |