mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
adding count nodes
This commit is contained in:
parent
a3b8ef8375
commit
2579fec805
5 changed files with 39 additions and 0 deletions
|
@ -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'
|
||||
|
|
12
lib/arel/nodes/count.rb
Normal file
12
lib/arel/nodes/count.rb
Normal file
|
@ -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
|
|
@ -1,6 +1,9 @@
|
|||
module Arel
|
||||
module Nodes
|
||||
class SqlLiteral < String
|
||||
def count distinct = false
|
||||
Count.new [self], distinct
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
19
spec/arel/nodes/sql_literal_spec.rb
Normal file
19
spec/arel/nodes/sql_literal_spec.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue