mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
added aggregations
This commit is contained in:
parent
45d2557ae0
commit
a83efc5d5d
7 changed files with 100 additions and 13 deletions
|
@ -6,5 +6,6 @@ require 'activerecord'
|
|||
|
||||
require 'active_relation/sql_builder'
|
||||
require 'active_relation/extensions'
|
||||
require 'active_relation/predicates'
|
||||
require 'active_relation/relations'
|
||||
require 'active_relation/predicates'
|
||||
require 'active_relation/primitives'
|
3
lib/active_relation/primitives.rb
Normal file
3
lib/active_relation/primitives.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
require 'active_relation/primitives/attribute'
|
||||
require 'active_relation/primitives/aggregation'
|
||||
|
21
lib/active_relation/primitives/aggregation.rb
Normal file
21
lib/active_relation/primitives/aggregation.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
module ActiveRelation
|
||||
module Primitives
|
||||
class Aggregation
|
||||
include SqlBuilder
|
||||
|
||||
attr_reader :attribute, :function_sql
|
||||
|
||||
def initialize(attribute, function_sql)
|
||||
@attribute, @function_sql = attribute, function_sql
|
||||
end
|
||||
|
||||
def to_sql(options = {})
|
||||
"#{function_sql}(@attribute.to_sql)"
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
self.class == other.class and attribute == other.attribute and function_sql == other.function_sql
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,7 +1,7 @@
|
|||
module ActiveRelation
|
||||
module Primitives
|
||||
class Attribute
|
||||
include ::ActiveRelation::SqlBuilder
|
||||
include SqlBuilder
|
||||
|
||||
attr_reader :relation, :name, :alias
|
||||
|
||||
|
@ -10,7 +10,7 @@ module ActiveRelation
|
|||
end
|
||||
|
||||
def alias(aliaz = nil)
|
||||
aliaz ? ActiveRelation::Primitives::Attribute.new(relation, name, aliaz) : @alias
|
||||
aliaz ? Attribute.new(relation, name, aliaz) : @alias
|
||||
end
|
||||
|
||||
def qualified_name
|
||||
|
@ -25,32 +25,57 @@ module ActiveRelation
|
|||
relation == other.relation and name == other.name and @alias == other.alias
|
||||
end
|
||||
|
||||
module Predications
|
||||
module Predications
|
||||
include Predicates
|
||||
|
||||
def equals(other)
|
||||
Predicates::Equality.new(self, other)
|
||||
Equality.new(self, other)
|
||||
end
|
||||
|
||||
def less_than(other)
|
||||
Predicates::LessThan.new(self, other)
|
||||
LessThan.new(self, other)
|
||||
end
|
||||
|
||||
def less_than_or_equal_to(other)
|
||||
Predicates::LessThanOrEqualTo.new(self, other)
|
||||
LessThanOrEqualTo.new(self, other)
|
||||
end
|
||||
|
||||
def greater_than(other)
|
||||
Predicates::GreaterThan.new(self, other)
|
||||
GreaterThan.new(self, other)
|
||||
end
|
||||
|
||||
def greater_than_or_equal_to(other)
|
||||
Predicates::GreaterThanOrEqualTo.new(self, other)
|
||||
GreaterThanOrEqualTo.new(self, other)
|
||||
end
|
||||
|
||||
def matches(regexp)
|
||||
Predicates::Match.new(self, regexp)
|
||||
Match.new(self, regexp)
|
||||
end
|
||||
end
|
||||
include Predications
|
||||
|
||||
module Aggregations
|
||||
def count
|
||||
Aggregation.new(self, "COUNT")
|
||||
end
|
||||
|
||||
def sum
|
||||
Aggregation.new(self, "SUM")
|
||||
end
|
||||
|
||||
def maximum
|
||||
Aggregation.new(self, "MAX")
|
||||
end
|
||||
|
||||
def minimum
|
||||
Aggregation.new(self, "MIN")
|
||||
end
|
||||
|
||||
def average
|
||||
Aggregation.new(self, "AVG")
|
||||
end
|
||||
end
|
||||
include Aggregations
|
||||
|
||||
def to_sql(options = {})
|
||||
"#{quote_table_name(relation.table)}.#{quote_column_name(name)}" + (options[:use_alias] && self.alias ? " AS #{self.alias.to_s.to_sql}" : "")
|
|
@ -2,7 +2,6 @@ require 'active_relation/relations/base'
|
|||
require 'active_relation/relations/compound'
|
||||
require 'active_relation/relations/table'
|
||||
require 'active_relation/relations/join'
|
||||
require 'active_relation/relations/attribute'
|
||||
require 'active_relation/relations/projection'
|
||||
require 'active_relation/relations/selection'
|
||||
require 'active_relation/relations/order'
|
||||
|
|
|
@ -23,7 +23,7 @@ module ActiveRelation
|
|||
private
|
||||
def attributes_by_name
|
||||
@attributes_by_name ||= connection.columns(table, "#{table} Columns").inject({}) do |attributes_by_name, column|
|
||||
attributes_by_name.merge(column.name => ActiveRelation::Primitives::Attribute.new(self, column.name.to_sym))
|
||||
attributes_by_name.merge(column.name => Primitives::Attribute.new(self, column.name.to_sym))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -56,7 +56,7 @@ describe ActiveRelation::Primitives::Attribute do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'greater_than' do
|
||||
describe '#greater_than' do
|
||||
it "manufactures a greater-than predicate" do
|
||||
@attribute1.greater_than(@attribute2).should == ActiveRelation::Predicates::GreaterThan.new(@attribute1, @attribute2)
|
||||
end
|
||||
|
@ -74,4 +74,42 @@ describe ActiveRelation::Primitives::Attribute do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'aggregations' do
|
||||
before do
|
||||
@attribute1 = ActiveRelation::Primitives::Attribute.new(@relation1, :name)
|
||||
end
|
||||
|
||||
describe '#count' do
|
||||
it "manufactures a count aggregation" do
|
||||
@attribute1.count.should == ActiveRelation::Primitives::Aggregation.new(@attribute1, "COUNT")
|
||||
end
|
||||
end
|
||||
|
||||
describe '#sum' do
|
||||
it "manufactures a sum aggregation" do
|
||||
@attribute1.sum.should == ActiveRelation::Primitives::Aggregation.new(@attribute1, "SUM")
|
||||
end
|
||||
end
|
||||
|
||||
describe '#maximum' do
|
||||
it "manufactures a maximum aggregation" do
|
||||
@attribute1.maximum.should == ActiveRelation::Primitives::Aggregation.new(@attribute1, "MAX")
|
||||
end
|
||||
end
|
||||
|
||||
describe '#minimum' do
|
||||
it "manufactures a minimum aggregation" do
|
||||
@attribute1.minimum.should == ActiveRelation::Primitives::Aggregation.new(@attribute1, "MIN")
|
||||
end
|
||||
end
|
||||
|
||||
describe '#average' do
|
||||
it "manufactures an average aggregation" do
|
||||
@attribute1.average.should == ActiveRelation::Primitives::Aggregation.new(@attribute1, "AVG")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue