aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_relation/primitives
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-02-03 22:10:38 -0800
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-02-03 22:10:38 -0800
commitb6c0de24fadc6b9e124a36aa35718edc027130de (patch)
tree8d965b22c97102ed269e47ad5882be8e71a068d9 /lib/active_relation/primitives
parent6c73e3dbc714a9752a66a6da51e7e41f372797b3 (diff)
downloadrails-b6c0de24fadc6b9e124a36aa35718edc027130de.tar.gz
rails-b6c0de24fadc6b9e124a36aa35718edc027130de.tar.bz2
rails-b6c0de24fadc6b9e124a36aa35718edc027130de.zip
this is very messy but it is finally close to feature-complete
Diffstat (limited to 'lib/active_relation/primitives')
-rw-r--r--lib/active_relation/primitives/attribute.rb29
-rw-r--r--lib/active_relation/primitives/expression.rb50
2 files changed, 71 insertions, 8 deletions
diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb
index 8d40a4141f..c17118f1e8 100644
--- a/lib/active_relation/primitives/attribute.rb
+++ b/lib/active_relation/primitives/attribute.rb
@@ -1,18 +1,18 @@
module ActiveRelation
class Attribute
- attr_reader :relation, :name, :alias
+ attr_reader :relation, :name, :alias, :ancestor
- def initialize(relation, name, aliaz = nil)
- @relation, @name, @alias = relation, name, aliaz
+ def initialize(relation, name, aliaz = nil, ancestor = nil)
+ @relation, @name, @alias, @ancestor = relation, name, aliaz, ancestor
end
module Transformations
def as(aliaz = nil)
- Attribute.new(relation, name, aliaz)
+ Attribute.new(relation, name, aliaz, self)
end
def substitute(new_relation)
- Attribute.new(new_relation, name, @alias)
+ relation == new_relation ? self : Attribute.new(new_relation, name, @alias, self)
end
def qualify
@@ -26,11 +26,19 @@ module ActiveRelation
include Transformations
def qualified_name
- "#{relation.name}.#{name}"
+ "#{prefix}.#{name}"
end
def ==(other)
- self.class == other.class and relation == other.relation and name == other.name and @alias == other.alias
+ self.class == other.class and relation == other.relation and name == other.name and @alias == other.alias and ancestor == other.ancestor
+ end
+
+ def =~(other)
+ !(history & other.history).empty?
+ end
+
+ def history
+ [self] + (ancestor ? [ancestor, ancestor.history].flatten : [])
end
module Predications
@@ -84,7 +92,12 @@ module ActiveRelation
include Expressions
def to_sql(strategy = Sql::Predicate.new)
- strategy.attribute relation.name, name, self.alias
+ strategy.attribute prefix, name, self.alias
+ end
+
+ private
+ def prefix
+ relation.prefix_for(self)
end
end
end \ No newline at end of file
diff --git a/lib/active_relation/primitives/expression.rb b/lib/active_relation/primitives/expression.rb
new file mode 100644
index 0000000000..47658c49da
--- /dev/null
+++ b/lib/active_relation/primitives/expression.rb
@@ -0,0 +1,50 @@
+module ActiveRelation
+ class Expression
+ include Sql::Quoting
+
+ attr_reader :attribute, :function_sql, :alias, :ancestor
+ delegate :relation, :to => :attribute
+
+ def initialize(attribute, function_sql, aliaz = nil, ancestor = nil)
+ @attribute, @function_sql, @alias, @ancestor = attribute, function_sql, aliaz, ancestor
+ end
+
+ module Transformations
+ def substitute(new_relation)
+ Expression.new(attribute.substitute(new_relation), function_sql, @alias, self)
+ end
+
+ def as(aliaz)
+ # key line -- note self
+ Expression.new(attribute, function_sql, aliaz, self)
+ end
+
+ def to_attribute
+ # key line -- note self
+ Attribute.new(relation, @alias, nil, self)
+ 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 and ancestor == other.ancestor and @alias == other.alias
+ end
+ alias_method :eql?, :==
+
+ def hash
+ attribute.hash + function_sql.hash
+ end
+
+ def =~(other)
+ !(history & other.history).empty?
+ end
+
+ def history
+ [self] + (ancestor ? [ancestor, ancestor.history].flatten : [])
+ end
+ end
+end \ No newline at end of file