diff options
-rw-r--r-- | lib/arel/nodes.rb | 1 | ||||
-rw-r--r-- | lib/arel/nodes/count.rb | 12 | ||||
-rw-r--r-- | lib/arel/nodes/sql_literal.rb | 3 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 4 | ||||
-rw-r--r-- | spec/arel/nodes/sql_literal_spec.rb | 19 |
5 files changed, 39 insertions, 0 deletions
diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb index 67568b5fd4..98fc045690 100644 --- a/lib/arel/nodes.rb +++ b/lib/arel/nodes.rb @@ -3,6 +3,7 @@ require 'arel/nodes/equality' require 'arel/nodes/or' require 'arel/nodes/in' +require 'arel/nodes/count' require 'arel/nodes/sql_literal' require 'arel/nodes/select_core' require 'arel/nodes/select_statement' diff --git a/lib/arel/nodes/count.rb b/lib/arel/nodes/count.rb new file mode 100644 index 0000000000..b7c4b60948 --- /dev/null +++ b/lib/arel/nodes/count.rb @@ -0,0 +1,12 @@ +module Arel + module Nodes + class Count + attr_accessor :expressions, :distinct + + def initialize expr, distinct = false + @expressions = expr + @distinct = distinct + end + end + end +end diff --git a/lib/arel/nodes/sql_literal.rb b/lib/arel/nodes/sql_literal.rb index 53ff376071..526d088449 100644 --- a/lib/arel/nodes/sql_literal.rb +++ b/lib/arel/nodes/sql_literal.rb @@ -1,6 +1,9 @@ module Arel module Nodes class SqlLiteral < String + def count distinct = false + Count.new [self], distinct + end end end end diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 55ed53177e..05c33ddb8b 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -58,6 +58,10 @@ module Arel ].compact.join ' ' end + def visit_Arel_Nodes_Count o + "COUNT(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| visit x }.join(', ')})" + end + def visit_Arel_Nodes_TableAlias o "#{visit o.relation} #{quote_table_name o.name}" end diff --git a/spec/arel/nodes/sql_literal_spec.rb b/spec/arel/nodes/sql_literal_spec.rb new file mode 100644 index 0000000000..390eb708e1 --- /dev/null +++ b/spec/arel/nodes/sql_literal_spec.rb @@ -0,0 +1,19 @@ +module Arel + module Nodes + describe 'sql literal' do + describe 'count' do + it 'makes a count node' do + node = SqlLiteral.new('*').count + viz = Visitors::ToSql.new Table.engine + viz.accept(node).should be_like %{ COUNT(*) } + end + + it 'makes a distinct node' do + node = SqlLiteral.new('*').count true + viz = Visitors::ToSql.new Table.engine + viz.accept(node).should be_like %{ COUNT(DISTINCT *) } + end + end + end + end +end |