aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-02-03 15:43:16 -0800
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-02-03 15:43:16 -0800
commit6c73e3dbc714a9752a66a6da51e7e41f372797b3 (patch)
tree55571262b97fc7a91114af78d04f231b1c3e3892 /lib
parent9c4403f67a8f2ac40fc0c964d8c24ecc6c6d7f27 (diff)
downloadrails-6c73e3dbc714a9752a66a6da51e7e41f372797b3.tar.gz
rails-6c73e3dbc714a9752a66a6da51e7e41f372797b3.tar.bz2
rails-6c73e3dbc714a9752a66a6da51e7e41f372797b3.zip
i know it doesn't work but need to anchor here...
Diffstat (limited to 'lib')
-rw-r--r--lib/active_relation/primitives.rb2
-rw-r--r--lib/active_relation/primitives/aggregation.rb35
-rw-r--r--lib/active_relation/primitives/attribute.rb14
-rw-r--r--lib/active_relation/relations.rb3
-rw-r--r--lib/active_relation/relations/aggregation.rb27
-rw-r--r--lib/active_relation/relations/alias.rb12
-rw-r--r--lib/active_relation/relations/compound.rb19
-rw-r--r--lib/active_relation/relations/group.rb21
-rw-r--r--lib/active_relation/relations/join.rb18
-rw-r--r--lib/active_relation/relations/projection.rb1
-rw-r--r--lib/active_relation/relations/relation.rb46
-rw-r--r--lib/active_relation/relations/rename.rb40
-rw-r--r--lib/active_relation/relations/schmoin.rb43
-rw-r--r--lib/active_relation/relations/table.rb12
14 files changed, 129 insertions, 164 deletions
diff --git a/lib/active_relation/primitives.rb b/lib/active_relation/primitives.rb
index 5d35f42564..2ac157297e 100644
--- a/lib/active_relation/primitives.rb
+++ b/lib/active_relation/primitives.rb
@@ -1,3 +1,3 @@
require 'active_relation/primitives/attribute'
-require 'active_relation/primitives/aggregation'
+require 'active_relation/primitives/expression'
diff --git a/lib/active_relation/primitives/aggregation.rb b/lib/active_relation/primitives/aggregation.rb
deleted file mode 100644
index 51ceee6e66..0000000000
--- a/lib/active_relation/primitives/aggregation.rb
+++ /dev/null
@@ -1,35 +0,0 @@
-module ActiveRelation
- class Aggregation
- include Sql::Quoting
-
- attr_reader :attribute, :function_sql, :alias
- delegate :relation, :to => :attribute
-
- def initialize(attribute, function_sql, aliaz = nil)
- @attribute, @function_sql, @alias = attribute, function_sql, aliaz
- end
-
- module Transformations
- def substitute(new_relation)
- Aggregation.new(attribute.substitute(new_relation), function_sql, @alias)
- end
-
- def as(aliaz)
- Aggregation.new(attribute, function_sql, aliaz)
- end
-
- def to_attribute
- Attribute.new(relation, @alias)
- end
- end
- include Transformations
-
- def to_sql(strategy = nil)
- "#{function_sql}(#{attribute.to_sql})" + (@alias ? " AS #{quote_column_name(@alias)}" : '')
- end
-
- def ==(other)
- self.class == other.class and attribute == other.attribute and function_sql == other.function_sql
- end
- end
-end \ No newline at end of file
diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb
index 3b63bf985b..8d40a4141f 100644
--- a/lib/active_relation/primitives/attribute.rb
+++ b/lib/active_relation/primitives/attribute.rb
@@ -60,28 +60,28 @@ module ActiveRelation
end
include Predications
- module Aggregations
+ module Expressions
def count
- Aggregation.new(self, "COUNT")
+ Expression.new(self, "COUNT")
end
def sum
- Aggregation.new(self, "SUM")
+ Expression.new(self, "SUM")
end
def maximum
- Aggregation.new(self, "MAX")
+ Expression.new(self, "MAX")
end
def minimum
- Aggregation.new(self, "MIN")
+ Expression.new(self, "MIN")
end
def average
- Aggregation.new(self, "AVG")
+ Expression.new(self, "AVG")
end
end
- include Aggregations
+ include Expressions
def to_sql(strategy = Sql::Predicate.new)
strategy.attribute relation.name, name, self.alias
diff --git a/lib/active_relation/relations.rb b/lib/active_relation/relations.rb
index c9b9de93b2..27dd229f3f 100644
--- a/lib/active_relation/relations.rb
+++ b/lib/active_relation/relations.rb
@@ -2,8 +2,7 @@ require 'active_relation/relations/relation'
require 'active_relation/relations/compound'
require 'active_relation/relations/table'
require 'active_relation/relations/join'
-require 'active_relation/relations/schmoin'
-require 'active_relation/relations/group'
+require 'active_relation/relations/aggregation'
require 'active_relation/relations/projection'
require 'active_relation/relations/selection'
require 'active_relation/relations/order'
diff --git a/lib/active_relation/relations/aggregation.rb b/lib/active_relation/relations/aggregation.rb
new file mode 100644
index 0000000000..161a748567
--- /dev/null
+++ b/lib/active_relation/relations/aggregation.rb
@@ -0,0 +1,27 @@
+module ActiveRelation
+ class Aggregation < Compound
+ attr_reader :expressions, :groupings
+ alias_method :attributes, :expressions
+
+ def initialize(relation, options)
+ @relation, @expressions, @groupings = relation, options[:expressions], options[:groupings]
+ end
+
+ def ==(other)
+ relation == other.relation and groupings == other.groupings and expressions == other.expressions
+ end
+
+ def qualify
+ Aggregation.new(relation.qualify, :expressions => expressions.collect(&:qualify), :groupings => groupings.collect(&:qualify))
+ end
+
+ protected
+ def aggregation?
+ true
+ end
+
+ def attribute_for_expression(expression)
+ expression.relation == self ? expression : (e = @expressions.detect { |e| e == expression }) && e.substitute(self)
+ end
+ end
+end \ No newline at end of file
diff --git a/lib/active_relation/relations/alias.rb b/lib/active_relation/relations/alias.rb
index 26b582e98a..5460413b25 100644
--- a/lib/active_relation/relations/alias.rb
+++ b/lib/active_relation/relations/alias.rb
@@ -1,25 +1,13 @@
module ActiveRelation
class Alias < Compound
attr_reader :alias
- alias_method :name, :alias
def initialize(relation, aliaz)
@relation, @alias = relation, aliaz
end
- def attributes
- relation.attributes.collect { |a| a.substitute(self) }
- end
-
def ==(other)
relation == other.relation and @alias == other.alias
end
-
- protected
- def attribute(name)
- if unaliased_attribute = relation[name]
- unaliased_attribute.substitute(self)
- end
- end
end
end \ No newline at end of file
diff --git a/lib/active_relation/relations/compound.rb b/lib/active_relation/relations/compound.rb
index 332147523e..7c4a7e707b 100644
--- a/lib/active_relation/relations/compound.rb
+++ b/lib/active_relation/relations/compound.rb
@@ -1,8 +1,25 @@
module ActiveRelation
class Compound < Relation
attr_reader :relation
- delegate :projections, :attributes, :attribute, :joins, :selects, :orders, :groupings, :table_sql, :inserts, :limit,
+ delegate :joins, :selects, :orders, :groupings, :table_sql, :inserts, :limit,
:offset, :name, :alias, :aggregation?,
:to => :relation
+
+ def attributes
+ relation.attributes.collect { |a| a.substitute(self) }
+ end
+
+ protected
+ def attribute_for_name(name)
+ (a = relation[name]) && a.substitute(self)
+ end
+
+ def attribute_for_attribute(attribute)
+ attribute.relation == self ? attribute : (a = relation[attribute]) && a.substitute(self)
+ end
+
+ def attribute_for_expression(expression)
+ expression.relation == self ? expression : (a = relation[expression]) && a.substitute(self)
+ end
end
end \ No newline at end of file
diff --git a/lib/active_relation/relations/group.rb b/lib/active_relation/relations/group.rb
deleted file mode 100644
index 31de6f4bd8..0000000000
--- a/lib/active_relation/relations/group.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-module ActiveRelation
- class Group < Compound
- attr_reader :groupings
-
- def initialize(relation, *groupings)
- @relation, @groupings = relation, groupings
- end
-
- def ==(other)
- relation == other.relation and groupings == other.groupings
- end
-
- def qualify
- Group.new(relation.qualify, *groupings.collect(&:qualify))
- end
-
- def aggregation?
- true
- end
- end
-end \ No newline at end of file
diff --git a/lib/active_relation/relations/join.rb b/lib/active_relation/relations/join.rb
index 37ed558e9f..dfc9992f0b 100644
--- a/lib/active_relation/relations/join.rb
+++ b/lib/active_relation/relations/join.rb
@@ -17,7 +17,10 @@ module ActiveRelation
end
def attributes
- projections.map(&:to_attribute)
+ [
+ relation1.aggregation?? relation1.attributes.collect(&:to_attribute) : relation1.attributes,
+ relation2.aggregation?? relation2.attributes.collect(&:to_attribute) : relation2.attributes,
+ ].flatten
end
protected
@@ -32,16 +35,13 @@ module ActiveRelation
].compact.flatten
end
- def projections
- [
- relation1.aggregation?? relation1.attributes : relation1.send(:projections),
- relation2.aggregation?? relation2.attributes : relation2.send(:projections),
- ].flatten
- end
-
- def attribute(name)
+ def attribute_for_name(name)
relation1[name] || relation2[name]
end
+
+ def attribute_for_attribute(attribute)
+ relation1[attribute] || relation2[attribute]
+ end
def table_sql
relation1.aggregation?? relation1.to_sql(Sql::Aggregation.new) : relation1.send(:table_sql)
diff --git a/lib/active_relation/relations/projection.rb b/lib/active_relation/relations/projection.rb
index 9651acd021..78698603f0 100644
--- a/lib/active_relation/relations/projection.rb
+++ b/lib/active_relation/relations/projection.rb
@@ -1,6 +1,7 @@
module ActiveRelation
class Projection < Compound
attr_reader :projections
+ alias_method :attributes, :projections
def initialize(relation, *projections)
@relation, @projections = relation, projections
diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb
index d09ee058ef..838697a2ac 100644
--- a/lib/active_relation/relations/relation.rb
+++ b/lib/active_relation/relations/relation.rb
@@ -26,10 +26,14 @@ module ActiveRelation
def [](index)
case index
- when Symbol
- attribute(index)
+ when Symbol, String
+ attribute_for_name(index)
when ::Range
Range.new(self, index)
+ when Attribute
+ attribute_for_attribute(index)
+ when Expression
+ attribute_for_expression(index)
end
end
@@ -65,8 +69,8 @@ module ActiveRelation
Deletion.new(self)
end
- def group(*attributes)
- Group.new(self, *attributes)
+ def aggregate(*expressions)
+ AggregateOperation.new(self, expressions)
end
JoinOperation = Struct.new(:join_sql, :relation1, :relation2) do
@@ -74,6 +78,12 @@ module ActiveRelation
Join.new(join_sql, relation1, relation2, *predicates)
end
end
+
+ AggregateOperation = Struct.new(:relation, :expressions) do
+ def group(*groupings)
+ Aggregation.new(relation, :expressions => expressions, :groupings => groupings)
+ end
+ end
end
include Operations
@@ -83,7 +93,7 @@ module ActiveRelation
def to_sql(strategy = Sql::Select.new)
strategy.select [
- "SELECT #{projections.collect{ |a| a.to_sql(Sql::Projection.new) }.join(', ')}",
+ "SELECT #{attributes.collect{ |a| a.to_sql(Sql::Projection.new) }.join(', ')}",
"FROM #{table_sql}",
(joins unless joins.blank?),
("WHERE #{selects.collect{|s| s.to_sql(Sql::Predicate.new)}.join("\n\tAND ")}" unless selects.blank?),
@@ -99,15 +109,23 @@ module ActiveRelation
def connection
ActiveRecord::Base.connection
end
+
+ def attribute_for_attribute(attribute)
+ self == attribute.relation ? attribute : nil
+ end
+
+ def attribute_for_expression(expression)
+ nil
+ end
- def projections; [] end
- def selects; [] end
- def orders; [] end
- def inserts; [] end
- def groupings; [] end
- def joins; nil end
- def limit; nil end
- def offset; nil end
- def alias; nil end
+ def attributes; [] end
+ def selects; [] end
+ def orders; [] end
+ def inserts; [] end
+ def groupings; [] end
+ def joins; nil end
+ def limit; nil end
+ def offset; nil end
+ def alias; nil end
end
end \ No newline at end of file
diff --git a/lib/active_relation/relations/rename.rb b/lib/active_relation/relations/rename.rb
index 94e5edcd47..8942ffbe29 100644
--- a/lib/active_relation/relations/rename.rb
+++ b/lib/active_relation/relations/rename.rb
@@ -1,40 +1,52 @@
module ActiveRelation
class Rename < Compound
- attr_reader :autonym, :pseudonym
+ attr_reader :attribute, :pseudonym
def initialize(relation, pseudonyms)
- @autonym, @pseudonym = pseudonyms.shift
+ @attribute, @pseudonym = pseudonyms.shift
@relation = pseudonyms.empty?? relation : Rename.new(relation, pseudonyms)
end
def ==(other)
- relation == other.relation and autonym == other.autonym and pseudonym == other.pseudonym
+ self.class == other.class and relation == other.relation and attribute == other.attribute and pseudonym == other.pseudonym
end
def qualify
- Rename.new(relation.qualify, autonym.qualify => self.pseudonym)
+ Rename.new(relation.qualify, attribute.qualify => pseudonym)
end
def attributes
- projections.collect(&:to_attribute)
+ relation.attributes.collect(&method(:substitute))
end
protected
- def projections
- relation.send(:projections).collect(&method(:substitute))
- end
-
- def attribute(name)
+ def attribute_for_name(name)
case
- when name == pseudonym then autonym.as(pseudonym)
- when relation[name] == autonym then nil
- else relation[name]
+ when referring_by_autonym?(name) then nil
+ when referring_by_pseudonym?(name) then attribute.as(pseudonym).substitute(self)
+ else (a = relation[name]) && a.substitute(self)
end
end
+
+ def attribute_for_attribute(attribute)
+ attribute.relation == self ? attribute : substitute(relation[attribute])
+ end
+
+ def attribute_for_expression(expression)
+ expression.relation == self ? expression : substitute(relation[expression])
+ end
private
def substitute(attribute)
- attribute == autonym ? attribute.as(pseudonym) : attribute
+ (relation[attribute] == relation[self.attribute] ? attribute.as(pseudonym) : attribute).substitute(self) if attribute
+ end
+
+ def referring_by_autonym?(name)
+ relation[name] == relation[attribute]
+ end
+
+ def referring_by_pseudonym?(name)
+ name == pseudonym
end
end
end \ No newline at end of file
diff --git a/lib/active_relation/relations/schmoin.rb b/lib/active_relation/relations/schmoin.rb
deleted file mode 100644
index 6d0cb6f171..0000000000
--- a/lib/active_relation/relations/schmoin.rb
+++ /dev/null
@@ -1,43 +0,0 @@
-module ActiveRelation
- class Schmoin < Relation
- attr_reader :join_sql, :relation1, :relation2, :predicates
- delegate :table_sql, :to => :relation1
-
- def initialize(join_sql, relation1, relation2, *predicates)
- @join_sql, @relation1, @relation2, @predicates = join_sql, relation1, relation2, predicates
- end
-
- def ==(other)
- predicates == other.predicates and
- ((relation1 == other.relation1 and relation2 == other.relation2) or
- (relation2 == other.relation1 and relation1 == other.relation2))
- end
-
- def qualify
- Schmoin.new(join_sql, relation1.qualify, relation2.qualify, *predicates.collect(&:qualify))
- end
-
- protected
- def joins
- [relation1.joins, relation2.joins, join].compact.join(" ")
- end
-
- def selects
- relation1.send(:selects) + relation2.send(:selects)
- end
-
- # this is magick!!!
- def projections
- relation1.projections + relation2.attributes
- end
-
- def attribute(name)
- relation1[name] || relation2[name]
- end
-
- private
- def join
- "#{join_sql} #{relation2.to_sql(Sql::Aggregation.new)} ON #{predicates.collect { |p| p.to_sql(Sql::Predicate.new) }.join(' AND ')}"
- end
- end
-end \ No newline at end of file
diff --git a/lib/active_relation/relations/table.rb b/lib/active_relation/relations/table.rb
index 637273f949..ffc9077076 100644
--- a/lib/active_relation/relations/table.rb
+++ b/lib/active_relation/relations/table.rb
@@ -13,14 +13,16 @@ module ActiveRelation
def qualify
Rename.new self, qualifications
end
-
- protected
- alias_method :projections, :attributes
- def attribute(name)
+ def inspect
+ "<Table: #{name}>"
+ end
+
+ protected
+ def attribute_for_name(name)
attributes_by_name[name.to_s]
end
-
+
def table_sql
"#{quote_table_name(name)}"
end