aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-01-12 23:30:12 -0800
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-01-12 23:30:12 -0800
commit7c7044085617a66abb3f9bd37dc7c072701f03c7 (patch)
tree9b220bc7e359d593da61cdccf979e3668c9836cf /lib
parent0cba6cde9958969933da79626d00464f7d07b22f (diff)
downloadrails-7c7044085617a66abb3f9bd37dc7c072701f03c7.tar.gz
rails-7c7044085617a66abb3f9bd37dc7c072701f03c7.tar.bz2
rails-7c7044085617a66abb3f9bd37dc7c072701f03c7.zip
aliasing of relations
Diffstat (limited to 'lib')
-rw-r--r--lib/active_relation/primitives/attribute.rb6
-rw-r--r--lib/active_relation/relations.rb3
-rw-r--r--lib/active_relation/relations/alias.rb19
-rw-r--r--lib/active_relation/relations/base.rb10
-rw-r--r--lib/active_relation/relations/compound.rb2
-rw-r--r--lib/active_relation/relations/rename.rb4
6 files changed, 35 insertions, 9 deletions
diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb
index 65ae12cf36..b13f9befe1 100644
--- a/lib/active_relation/primitives/attribute.rb
+++ b/lib/active_relation/primitives/attribute.rb
@@ -9,8 +9,8 @@ module ActiveRelation
@relation, @name, @alias = relation, name, aliaz
end
- def alias(aliaz = nil)
- aliaz ? Attribute.new(relation, name, aliaz) : @alias
+ def as(aliaz = nil)
+ Attribute.new(relation, name, aliaz)
end
def qualified_name
@@ -18,7 +18,7 @@ module ActiveRelation
end
def qualify
- self.alias(qualified_name)
+ self.as(qualified_name)
end
def ==(other)
diff --git a/lib/active_relation/relations.rb b/lib/active_relation/relations.rb
index a25e82f607..1eb58d3b42 100644
--- a/lib/active_relation/relations.rb
+++ b/lib/active_relation/relations.rb
@@ -8,4 +8,5 @@ require 'active_relation/relations/order'
require 'active_relation/relations/range'
require 'active_relation/relations/rename'
require 'active_relation/relations/deletion'
-require 'active_relation/relations/insertion' \ No newline at end of file
+require 'active_relation/relations/insertion'
+require 'active_relation/relations/alias' \ No newline at end of file
diff --git a/lib/active_relation/relations/alias.rb b/lib/active_relation/relations/alias.rb
new file mode 100644
index 0000000000..97950ea5b3
--- /dev/null
+++ b/lib/active_relation/relations/alias.rb
@@ -0,0 +1,19 @@
+module ActiveRelation
+ module Relations
+ class Alias < Compound
+ attr_reader :alias
+
+ def initialize(relation, aliaz)
+ @relation, @alias = relation, aliaz
+ end
+
+ def ==(other)
+ relation == other.relation and self.alias == other.alias
+ end
+
+ def to_sql(options = {})
+ super + " AS #{@alias}"
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/lib/active_relation/relations/base.rb b/lib/active_relation/relations/base.rb
index 960735f07f..b4654f8f4b 100644
--- a/lib/active_relation/relations/base.rb
+++ b/lib/active_relation/relations/base.rb
@@ -45,6 +45,10 @@ module ActiveRelation
def project(*attributes)
Projection.new(self, *attributes)
end
+
+ def as(aliaz)
+ Alias.new(self, aliaz)
+ end
def order(*attributes)
Order.new(self, *attributes)
@@ -75,8 +79,8 @@ module ActiveRelation
end
def to_sql(options = {})
- [
- "SELECT #{attributes.collect{ |a| a.to_sql(:use_alias => true) }.join(', ')}",
+ sql = [
+ "SELECT #{attributes.collect{ |a| a.to_sql(:use_alias => true, :use_parens => true) }.join(', ')}",
"FROM #{quote_table_name(table)}",
(joins.to_sql(:quote => false) unless joins.blank?),
("WHERE #{selects.collect{|s| s.to_sql(:quote => false)}.join("\n\tAND ")}" unless selects.blank?),
@@ -84,6 +88,7 @@ module ActiveRelation
("LIMIT #{limit.to_sql}" unless limit.blank?),
("OFFSET #{offset.to_sql}" unless offset.blank?)
].compact.join("\n")
+ options[:use_parens] ? "(#{sql})" : sql
end
alias_method :to_s, :to_sql
@@ -95,6 +100,7 @@ module ActiveRelation
def joins; nil end
def limit; nil end
def offset; nil end
+ def alias; nil 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 442224a011..26ee92b365 100644
--- a/lib/active_relation/relations/compound.rb
+++ b/lib/active_relation/relations/compound.rb
@@ -3,7 +3,7 @@ module ActiveRelation
class Compound < Base
attr_reader :relation
- delegate :attributes, :attribute, :joins, :selects, :orders, :table, :inserts, :limit, :offset, :to => :relation
+ delegate :attributes, :attribute, :joins, :selects, :orders, :table, :inserts, :limit, :offset, :alias, :to => :relation
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 c7b99c2127..cff042dbc6 100644
--- a/lib/active_relation/relations/rename.rb
+++ b/lib/active_relation/relations/rename.rb
@@ -23,7 +23,7 @@ module ActiveRelation
protected
def attribute(name)
case
- when name == self.alias then schmattribute.alias(self.alias)
+ when name == self.alias then schmattribute.as(self.alias)
when relation[name] == schmattribute then nil
else relation[name]
end
@@ -31,7 +31,7 @@ module ActiveRelation
private
def substitute(a)
- a == schmattribute ? a.alias(self.alias) : a
+ a == schmattribute ? a.as(self.alias) : a
end
end
end