diff --git a/doc/TODO b/doc/TODO index d7dd75d62e..0f688bc258 100644 --- a/doc/TODO +++ b/doc/TODO @@ -1,4 +1,19 @@ todo: +- joining with LIMIT is like aggregations!! + +users.delete().where( + addresses.c.user_id== + select([users.c.id]). + where(users.c.name=='jack') + ) + + SELECT id, name, + (select count(*) FROM addresses WHERE + user_id=users.id) + FROM users + + SELECT users.*, (SELECT count(id) FROM addresses WHERE + addresses.user_id=users.id) FROM users - and/or w/ predicates - blocks for all operations - result sets to attr correlation too diff --git a/lib/arel/primitives/attribute.rb b/lib/arel/primitives/attribute.rb index dffe24d121..30885dd129 100644 --- a/lib/arel/primitives/attribute.rb +++ b/lib/arel/primitives/attribute.rb @@ -75,7 +75,7 @@ module Arel end def find_correlate_in(relation) - relation[self] + relation[self] || self end def descends_from?(other) diff --git a/lib/arel/relations/operations/group.rb b/lib/arel/relations/operations/group.rb index 253c4215b6..04fd9fea62 100644 --- a/lib/arel/relations/operations/group.rb +++ b/lib/arel/relations/operations/group.rb @@ -5,7 +5,7 @@ module Arel def initialize(relation, *groupings, &block) @relation = relation - @groupings = (groupings + (block_given?? [yield(self)] : [])).collect { |g| g.bind(relation) } + @groupings = (groupings + (block_given?? [yield(relatoin)] : [])).collect { |g| g.bind(relation) } end def externalizable? diff --git a/lib/arel/relations/operations/join.rb b/lib/arel/relations/operations/join.rb index 8025db095e..a72030abf9 100644 --- a/lib/arel/relations/operations/join.rb +++ b/lib/arel/relations/operations/join.rb @@ -31,6 +31,7 @@ module Arel end def wheres + # TESTME bind to self? relation1.externalize.wheres end diff --git a/lib/arel/relations/operations/order.rb b/lib/arel/relations/operations/order.rb index 82924806e2..05af3fde4d 100644 --- a/lib/arel/relations/operations/order.rb +++ b/lib/arel/relations/operations/order.rb @@ -5,7 +5,7 @@ module Arel def initialize(relation, *orderings, &block) @relation = relation - @orderings = (orderings + (block_given?? [yield(self)] : [])).collect { |o| o.bind(relation) } + @orderings = (orderings + (block_given?? [yield(relation)] : [])).collect { |o| o.bind(relation) } end # TESTME diff --git a/lib/arel/relations/operations/project.rb b/lib/arel/relations/operations/project.rb index c92a9df5a5..d7835edda4 100644 --- a/lib/arel/relations/operations/project.rb +++ b/lib/arel/relations/operations/project.rb @@ -5,7 +5,7 @@ module Arel def initialize(relation, *projections, &block) @relation = relation - @projections = (projections + (block_given?? [yield(self)] : [])).collect { |p| p.bind(relation) } + @projections = (projections + (block_given?? [yield(relation)] : [])).collect { |p| p.bind(relation) } end def attributes diff --git a/lib/arel/relations/operations/where.rb b/lib/arel/relations/operations/where.rb index 9acb8ae3c6..8882f36104 100644 --- a/lib/arel/relations/operations/where.rb +++ b/lib/arel/relations/operations/where.rb @@ -4,7 +4,7 @@ module Arel deriving :== def initialize(relation, *predicates, &block) - predicate = block_given?? yield(self) : predicates.shift + predicate = block_given?? yield(relation) : predicates.shift @relation = predicates.empty?? relation : Where.new(relation, *predicates) @predicate = predicate.bind(@relation) end diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index 1e2ac9f2be..a2d8bba6ea 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -39,6 +39,10 @@ module Arel self end + def root + self + end + def christener @christener ||= Sql::Christener.new end diff --git a/lib/arel/relations/utilities.rb b/lib/arel/relations/utilities.rb index 02c2e0a952..454d455359 100644 --- a/lib/arel/relations/utilities.rb +++ b/lib/arel/relations/utilities.rb @@ -1,5 +1,5 @@ require 'arel/relations/utilities/compound' require 'arel/relations/utilities/recursion' require 'arel/relations/utilities/nil' -require 'arel/relations/utilities/aggregation' +require 'arel/relations/utilities/externalization' require 'arel/relations/utilities/recursion' \ No newline at end of file diff --git a/lib/arel/relations/utilities/aggregation.rb b/lib/arel/relations/utilities/externalization.rb similarity index 97% rename from lib/arel/relations/utilities/aggregation.rb rename to lib/arel/relations/utilities/externalization.rb index bdc7650a20..4e9139a78a 100644 --- a/lib/arel/relations/utilities/aggregation.rb +++ b/lib/arel/relations/utilities/externalization.rb @@ -16,6 +16,7 @@ module Arel @attributes ||= relation.attributes.collect(&:to_attribute).collect { |a| a.bind(self) } end + # REMOVEME def name relation.name + '_external' end diff --git a/spec/arel/integration/joins/with_aggregations_spec.rb b/spec/arel/integration/joins/with_aggregations_spec.rb index d429806094..42248add07 100644 --- a/spec/arel/integration/joins/with_aggregations_spec.rb +++ b/spec/arel/integration/joins/with_aggregations_spec.rb @@ -16,6 +16,7 @@ module Arel end describe '#to_sql' do + # CLEANUP it '' do @relation1.join(@relation2.take(3)).on(@predicate).to_sql.should be_like(" SELECT `users`.`id`, `users`.`name`, `photos_external`.`id`, `photos_external`.`user_id`, `photos_external`.`camera_id`