1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

rename refactor of circle

This commit is contained in:
Nick Kallen 2008-05-18 16:11:08 -07:00
parent 7a068384b7
commit 32ad530b82
12 changed files with 36 additions and 51 deletions

View file

@ -3,7 +3,7 @@ class Object
Arel::Value.new(self, relation)
end
def circle(relation)
def find_correlate_in(relation)
bind(relation)
end

View file

@ -17,7 +17,7 @@ module Arel
end
def bind(relation)
self.class.new(operand1.circle(relation), operand2.circle(relation))
self.class.new(operand1.find_correlate_in(relation), operand2.find_correlate_in(relation))
end
def to_sql(formatter = nil)

View file

@ -45,7 +45,7 @@ module Arel
@original_attribute ||= history.detect { |a| !a.join? }
end
def circle(relation)
def find_correlate_in(relation)
relation[self]
end

View file

@ -29,10 +29,8 @@ module Arel
Expression.new(attribute, function_sql, aliaz, self)
end
# FIXME
def bind(new_relation)
# new_relation == relation ? self : Expression.new(attribute.bind(new_relation), function_sql, @alias, self)
self
new_relation == relation ? self : Expression.new(attribute.bind(new_relation), function_sql, @alias, self)
end
def to_attribute

View file

@ -24,9 +24,5 @@ module Arel
def bind(relation)
Value.new(value, relation)
end
def circle(relation)
bind(relation)
end
end
end

View file

@ -20,8 +20,8 @@ module Arel
"#{expression.function_sql}(#{expression.attribute.to_sql(self)})" + (expression.alias ? " AS #{quote_column_name(expression.alias)}" : '')
end
def select(select_sql, name)
"(#{select_sql}) AS #{quote_table_name(name.to_s)}"
def select(select_sql, table)
"(#{select_sql}) AS #{quote_table_name(table)}"
end
def value(value)
@ -67,24 +67,24 @@ module Arel
quote(value, column)
end
def select(select_sql, name)
def select(select_sql, table)
"(#{select_sql})"
end
end
class SelectStatement < Formatter
def select(select_sql, name)
def select(select_sql, table)
select_sql
end
end
class TableReference < Formatter
def select(select_sql, name)
"(#{select_sql}) AS #{quote_table_name(name)}"
def select(select_sql, table)
"(#{select_sql}) AS #{quote_table_name(table)}"
end
def table(table)
quote_table_name(table.name) + (table.name != name_for(table) ? " AS " + engine.quote_table_name(name_for(table)) : '')
quote_table_name(table.name) + (table.name != name_for(table) ? " AS " + quote_table_name(name_for(table)) : '')
end
end

View file

@ -98,6 +98,17 @@ module Arel
.should disambiguate_attributes(@relation1[:id], @relation2[:id])
end
describe 'when both relations are compound and only one is an alias' do
it 'disambiguates the relation that serves as the ancestor to the attribute' do
compound1 = @relation1.select(@predicate)
compound2 = compound1.alias
compound1 \
.join(compound2) \
.on(@predicate) \
.should disambiguate_attributes(compound1[:id], compound2[:id])
end
end
describe 'when the left relation is extremely compound' do
it 'disambiguates the relation that serves as the ancestor to the attribute' do
@relation1 \
@ -107,13 +118,6 @@ module Arel
.on(@predicate) \
.should disambiguate_attributes(@relation1[:id], @relation2[:id])
end
it '' do
r0 = @relation1.select(@predicate)
r1 = r0.alias
r = r0.join(r1).on(@predicate)
r.should disambiguate_attributes(r0[:id], r1[:id])
end
end
describe 'when the right relation is extremely compound' do

View file

@ -15,16 +15,10 @@ module Arel
.project(@relation2[:user_id], @relation2[:id].count.as(:cnt)) \
end
describe '#attributes' do
it '' do
@relation1.join(@aggregation).on(@predicate)[@relation2[:user_id]].should_not be_nil
end
end
describe '#to_sql' do
describe 'with the aggregation on the right' do
it 'manufactures sql joining the left table to a derived table' do
Join.new("INNER JOIN", @relation1, @aggregation, @predicate).to_sql.should be_like("
@relation1.join(@aggregation).on(@predicate).to_sql.should be_like("
SELECT `users`.`id`, `users`.`name`, `photos_aggregation`.`user_id`, `photos_aggregation`.`cnt`
FROM `users`
INNER JOIN (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_aggregation`
@ -35,7 +29,7 @@ module Arel
describe 'with the aggregation on the left' do
it 'manufactures sql joining the right table to a derived table' do
Join.new("INNER JOIN", @aggregation, @relation1, @predicate).to_sql.should be_like("
@aggregation.join(@relation1).on(@predicate).to_sql.should be_like("
SELECT `photos_aggregation`.`user_id`, `photos_aggregation`.`cnt`, `users`.`id`, `users`.`name`
FROM (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_aggregation`
INNER JOIN `users`
@ -44,10 +38,16 @@ module Arel
end
end
describe 'with the aggregation on both sides' do
it '' do
@aggregation.join(@aggregation.alias).on(@predicate).to_sql.should == ''
end
end
describe 'when the aggration has a selection' do
describe 'with the aggregation on the left' do
it "manufactures sql keeping selects on the aggregation within the derived table" do
Join.new("INNER JOIN", @relation1, @aggregation.select(@aggregation[:user_id].eq(1)), @predicate).to_sql.should be_like("
@relation1.join(@aggregation.select(@aggregation[:user_id].eq(1))).on(@predicate).to_sql.should be_like("
SELECT `users`.`id`, `users`.`name`, `photos_aggregation`.`user_id`, `photos_aggregation`.`cnt`
FROM `users`
INNER JOIN (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` WHERE `photos`.`user_id` = 1 GROUP BY `photos`.`user_id`) AS `photos_aggregation`
@ -58,7 +58,7 @@ module Arel
describe 'with the aggregation on the right' do
it "manufactures sql keeping selects on the aggregation within the derived table" do
Join.new("INNER JOIN", @aggregation.select(@aggregation[:user_id].eq(1)), @relation1, @predicate).to_sql.should be_like("
@aggregation.select(@aggregation[:user_id].eq(1)).join(@relation1).on(@predicate).to_sql.should be_like("
SELECT `photos_aggregation`.`user_id`, `photos_aggregation`.`cnt`, `users`.`id`, `users`.`name`
FROM (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` WHERE `photos`.`user_id` = 1 GROUP BY `photos`.`user_id`) AS `photos_aggregation`
INNER JOIN `users`

View file

@ -72,7 +72,7 @@ module Arel
describe 'when an operand is a value' do
it "manufactures an expression with unmodified values" do
ConcreteBinary.new(@attribute1, "asdf").bind(@another_relation). \
should == ConcreteBinary.new(@attribute1.circle(@another_relation), "asdf".circle(@another_relation))
should == ConcreteBinary.new(@attribute1.find_correlate_in(@another_relation), "asdf".find_correlate_in(@another_relation))
end
end
end

View file

@ -14,7 +14,6 @@ module Arel
describe '#bind' do
it "manufactures an attribute with a rebound relation and self as the ancestor" do
pending
derived_relation = @relation.select(@relation[:id].eq(1))
@expression.bind(derived_relation).should == Expression.new(@attribute.bind(derived_relation), "COUNT", nil, @expression)
end

View file

@ -11,15 +11,6 @@ module Arel
Alias.new(@relation).should_not == Alias.new(@relation)
(aliaz = Alias.new(@relation)).should == aliaz
end
it '' do
@relation.select(@relation[:id].eq(1)).to_sql.should be_like("
SELECT `users`.`id`, `users`.`name`
FROM `users`
WHERE
`users`.`id` = 1
")
end
end
end
end

View file

@ -8,13 +8,10 @@ module Arel
end
describe '#initialize' do
before do
@another_predicate = @relation[:name].lt(2)
end
it "manufactures nested selection relations if multiple predicates are provided" do
Selection.new(@relation, @predicate, @another_predicate). \
should == Selection.new(Selection.new(@relation, @another_predicate), @predicate)
another_predicate = @relation[:name].lt(2)
Selection.new(@relation, @predicate, another_predicate). \
should == Selection.new(Selection.new(@relation, another_predicate), @predicate)
end
end