aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-01-12 20:18:52 -0800
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-01-12 20:18:52 -0800
commita83efc5d5d94d50589a80bdd27ddf5b83ed39810 (patch)
treed85ad7f670b2c4a7e8af62d96d8f95ecfebfbafa /lib
parent45d2557ae097da1e5dd3dc532444233b4fed0b31 (diff)
downloadrails-a83efc5d5d94d50589a80bdd27ddf5b83ed39810.tar.gz
rails-a83efc5d5d94d50589a80bdd27ddf5b83ed39810.tar.bz2
rails-a83efc5d5d94d50589a80bdd27ddf5b83ed39810.zip
added aggregations
Diffstat (limited to 'lib')
-rw-r--r--lib/active_relation.rb3
-rw-r--r--lib/active_relation/primitives.rb3
-rw-r--r--lib/active_relation/primitives/aggregation.rb21
-rw-r--r--lib/active_relation/primitives/attribute.rb (renamed from lib/active_relation/relations/attribute.rb)43
-rw-r--r--lib/active_relation/relations.rb1
-rw-r--r--lib/active_relation/relations/table.rb2
6 files changed, 61 insertions, 12 deletions
diff --git a/lib/active_relation.rb b/lib/active_relation.rb
index cdac17facb..0b55ed74d0 100644
--- a/lib/active_relation.rb
+++ b/lib/active_relation.rb
@@ -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' \ No newline at end of file
+require 'active_relation/primitives' \ No newline at end of file
diff --git a/lib/active_relation/primitives.rb b/lib/active_relation/primitives.rb
new file mode 100644
index 0000000000..5d35f42564
--- /dev/null
+++ b/lib/active_relation/primitives.rb
@@ -0,0 +1,3 @@
+require 'active_relation/primitives/attribute'
+require 'active_relation/primitives/aggregation'
+
diff --git a/lib/active_relation/primitives/aggregation.rb b/lib/active_relation/primitives/aggregation.rb
new file mode 100644
index 0000000000..20fdcf46c5
--- /dev/null
+++ b/lib/active_relation/primitives/aggregation.rb
@@ -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 \ No newline at end of file
diff --git a/lib/active_relation/relations/attribute.rb b/lib/active_relation/primitives/attribute.rb
index 2fe702cb14..65ae12cf36 100644
--- a/lib/active_relation/relations/attribute.rb
+++ b/lib/active_relation/primitives/attribute.rb
@@ -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}" : "")
diff --git a/lib/active_relation/relations.rb b/lib/active_relation/relations.rb
index f7d1bc207a..a25e82f607 100644
--- a/lib/active_relation/relations.rb
+++ b/lib/active_relation/relations.rb
@@ -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'
diff --git a/lib/active_relation/relations/table.rb b/lib/active_relation/relations/table.rb
index 38f540cc52..d642851687 100644
--- a/lib/active_relation/relations/table.rb
+++ b/lib/active_relation/relations/table.rb
@@ -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