aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-04-28 14:01:33 -0700
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-04-28 14:01:33 -0700
commit09b2406e829cee71e4543a75fc4fbe226157d35e (patch)
treeee8a230c3b9c1d4f31cef38defe269f6b8868859 /lib
parent3ae7a601e962468468eac1399ebdfd7113730310 (diff)
downloadrails-09b2406e829cee71e4543a75fc4fbe226157d35e.tar.gz
rails-09b2406e829cee71e4543a75fc4fbe226157d35e.tar.bz2
rails-09b2406e829cee71e4543a75fc4fbe226157d35e.zip
automatically aliasing tables
Diffstat (limited to 'lib')
-rw-r--r--lib/arel/primitives/attribute.rb10
-rw-r--r--lib/arel/relations/grouping.rb4
-rw-r--r--lib/arel/relations/join.rb48
-rw-r--r--lib/arel/relations/nil.rb4
-rw-r--r--lib/arel/relations/relation.rb6
-rw-r--r--lib/arel/relations/table.rb4
-rw-r--r--lib/arel/sql.rb16
7 files changed, 50 insertions, 42 deletions
diff --git a/lib/arel/primitives/attribute.rb b/lib/arel/primitives/attribute.rb
index 280fc9f439..797ebfd07b 100644
--- a/lib/arel/primitives/attribute.rb
+++ b/lib/arel/primitives/attribute.rb
@@ -30,10 +30,6 @@ module Arel
end
include Transformations
- def qualified_name
- "#{prefix}.#{name}"
- end
-
def column
relation.column_for(self)
end
@@ -122,7 +118,7 @@ module Arel
include Expressions
def to_sql(formatter = Sql::WhereCondition.new(engine))
- formatter.attribute prefix, name, self.alias
+ formatter.attribute relation.prefix_for(self), name, self.alias
end
def format(object)
@@ -133,9 +129,5 @@ module Arel
def formatter
Sql::Attribute.new(self)
end
-
- def prefix
- relation.prefix_for(self)
- end
end
end \ No newline at end of file
diff --git a/lib/arel/relations/grouping.rb b/lib/arel/relations/grouping.rb
index ccca600360..41af4ba0f6 100644
--- a/lib/arel/relations/grouping.rb
+++ b/lib/arel/relations/grouping.rb
@@ -15,5 +15,9 @@ module Arel
def aggregation?
true
end
+
+ def name
+ relation.name + '_aggregation'
+ end
end
end \ No newline at end of file
diff --git a/lib/arel/relations/join.rb b/lib/arel/relations/join.rb
index e5ca49c284..ce236d79d0 100644
--- a/lib/arel/relations/join.rb
+++ b/lib/arel/relations/join.rb
@@ -24,9 +24,13 @@ module Arel
end
def prefix_for(attribute)
- externalize([relation1[attribute], relation2[attribute]].select { |a| a =~ attribute }.min do |a1, a2|
+ name_for(relation_for(attribute))
+ end
+
+ def relation_for(attribute)
+ [relation1[attribute], relation2[attribute]].select { |a| a =~ attribute }.min do |a1, a2|
(attribute % a1).size <=> (attribute % a2).size
- end.relation).prefix_for(attribute)
+ end.relation.relation_for(attribute)
end
# TESTME: Not sure which scenario needs this method, was driven by failing tests in ActiveRecord
@@ -34,10 +38,10 @@ module Arel
(relation1[attribute] || relation2[attribute]).column
end
- def joins
+ def joins(formatter = Sql::TableReference.new(engine))
this_join = [
join_sql,
- externalize(relation2).table_sql,
+ externalize(relation2).table_sql(formatter),
("ON" unless predicates.blank?),
predicates.collect { |p| p.bind(self).to_sql }.join(' AND ')
].compact.join(" ")
@@ -45,29 +49,35 @@ module Arel
end
def selects
- externalize(relation1).selects + externalize(relation2).selects
+ (externalize(relation1).selects + externalize(relation2).selects).collect { |s| s.bind(self) }
end
- def table_sql
- externalize(relation1).table_sql
+ def table_sql(formatter = Sql::TableReference.new(engine))
+ externalize(relation1).table_sql(formatter)
+ end
+
+ def name_for(relation)
+ @used_names ||= Hash.new(0)
+ @relation_names ||= Hash.new do |h, k|
+ @used_names[k.name] += 1
+ h[k] = k.name + (@used_names[k.name] > 1 ? "_#{@used_names[k.name]}" : '')
+ end
+ @relation_names[relation]
end
private
def externalize(relation)
- Externalizer.new(relation)
+ Externalizer.new(self, relation)
end
- Externalizer = Struct.new(:relation) do
+ Externalizer = Struct.new(:christener, :relation) do
delegate :engine, :to => :relation
- def table_sql
- case
- when relation.aggregation?
- relation.to_sql(Sql::TableReference.new(engine))
- when relation.alias?
- relation.table_sql + ' AS ' + engine.quote_table_name(relation.alias.to_s)
+ def table_sql(formatter = Sql::TableReference.new(engine))
+ if relation.aggregation?
+ relation.to_sql(formatter) + ' AS ' + engine.quote_table_name(christener.name_for(relation))
else
- relation.table_sql
+ relation.table_sql(formatter) + (relation.name != christener.name_for(relation) ? " AS " + engine.quote_table_name(christener.name_for(relation)) : '')
end
end
@@ -78,12 +88,6 @@ module Arel
def attributes
relation.aggregation?? relation.attributes.collect(&:to_attribute) : relation.attributes
end
-
- def prefix_for(attribute)
- if relation[attribute]
- relation.alias?? relation.alias : relation.prefix_for(attribute)
- end
- end
end
end
end \ No newline at end of file
diff --git a/lib/arel/relations/nil.rb b/lib/arel/relations/nil.rb
index 3c1d413953..67eb2d3a62 100644
--- a/lib/arel/relations/nil.rb
+++ b/lib/arel/relations/nil.rb
@@ -1,7 +1,7 @@
module Arel
class Nil < Relation
- def table_sql; '' end
-
+ def table_sql(formatter = nil); '' end
+ def name; '' end
def to_s; '' end
def ==(other)
diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb
index 3bd40e8f6b..0a5bf2ff24 100644
--- a/lib/arel/relations/relation.rb
+++ b/lib/arel/relations/relation.rb
@@ -103,6 +103,10 @@ module Arel
def alias?
false
end
+
+ def relation_for(attribute)
+ self[attribute] and self
+ end
end
include Externalizable
@@ -116,7 +120,7 @@ module Arel
("GROUP BY #{groupings.collect(&:to_sql)}" unless groupings.blank? ),
("LIMIT #{taken}" unless taken.blank? ),
("OFFSET #{skipped}" unless skipped.blank? )
- ].compact.join("\n"), self.alias
+ ].compact.join("\n")
end
alias_method :to_s, :to_sql
diff --git a/lib/arel/relations/table.rb b/lib/arel/relations/table.rb
index b627558caf..7637a471bf 100644
--- a/lib/arel/relations/table.rb
+++ b/lib/arel/relations/table.rb
@@ -36,8 +36,8 @@ module Arel
@attributes = @columns = nil
end
- def table_sql
- engine.quote_table_name(name)
+ def table_sql(formatter = Sql::TableReference.new(engine))
+ formatter.table name
end
end
end \ No newline at end of file
diff --git a/lib/arel/sql.rb b/lib/arel/sql.rb
index b6d646c047..3cb8d13680 100644
--- a/lib/arel/sql.rb
+++ b/lib/arel/sql.rb
@@ -19,8 +19,8 @@ module Arel
"#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}" + (aliaz ? " AS #{quote(aliaz.to_s)}" : "")
end
- def select(select_sql, aliaz)
- "(#{select_sql})" + (aliaz ? " AS #{quote(aliaz)}" : "")
+ def select(select_sql)
+ "(#{select_sql})"
end
def value(value)
@@ -56,20 +56,24 @@ module Arel
quote(value, column)
end
- def select(select_sql, aliaz)
+ def select(select_sql)
"(#{select_sql})"
end
end
class SelectStatement < Formatter
- def select(select_sql, aliaz)
+ def select(select_sql)
select_sql
end
end
class TableReference < Formatter
- def select(select_sql, aliaz)
- "(#{select_sql}) AS #{quote_table_name(aliaz)}"
+ def select(select_sql)
+ "(#{select_sql})"
+ end
+
+ def table(name)
+ quote_table_name(name)
end
end