From 16730fdbd0ab630320aba225314aa6a1a3b450fe Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Tue, 13 May 2008 16:53:36 -0700 Subject: fixed defect in alias --- doc/TODO | 15 +++++++++------ lib/arel/primitives/attribute.rb | 8 ++++++++ lib/arel/relations/aggregation.rb | 2 +- lib/arel/relations/alias.rb | 5 +---- lib/arel/relations/compound.rb | 2 +- lib/arel/relations/join.rb | 8 +++++--- lib/arel/relations/projection.rb | 2 +- lib/arel/relations/recursion.rb | 2 +- lib/arel/relations/relation.rb | 10 +--------- lib/arel/sessions/session.rb | 8 -------- lib/arel/sql/christener.rb | 6 +++--- spec/arel/unit/relations/alias_spec.rb | 11 ++++++++++- spec/arel/unit/relations/insertion_spec.rb | 2 +- spec/arel/unit/relations/update_spec.rb | 2 +- spec/arel/unit/session/session_spec.rb | 10 +--------- 15 files changed, 44 insertions(+), 49 deletions(-) diff --git a/doc/TODO b/doc/TODO index 752579d229..00a8ce4e4d 100644 --- a/doc/TODO +++ b/doc/TODO @@ -1,11 +1,9 @@ todo: -- Explicitly model recursive structural decomposition / polymorphism -- Explicitly model the namer/externalizer using interpreter jargon -- All Sql Strategies should be accumulations with the top-level relation? -- lock - - SELECT suchandsuch FOR UPDATE +- instance methodify externalize +- test: find_attribute_given_attribute and all @attribute ||= everywhere and memoization of table class. - rename select to where - and/or w/ predicates +- blocks for joins - cache expiry on write - rewrite of arecord querycache test in light of this - scoped writes @@ -65,6 +63,9 @@ done: - fix complex joining cases: - active record query adapter - anonymous table names +- Explicitly model recursive structural decomposition / polymorphism +- Explicitly model the namer/externalizer using interpreter jargon +- All Sql Strategies should be accumulations with the top-level relation? icebox: - #bind in Attribute and Expression should be doing a descend? @@ -76,4 +77,6 @@ icebox: - rename the tion (Selection) classes so that words that don't end in tion don't seem inconsistent - consider this code from has_many: # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */ - @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } \ No newline at end of file + @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" } +- lock + - SELECT suchandsuch FOR UPDATE diff --git a/lib/arel/primitives/attribute.rb b/lib/arel/primitives/attribute.rb index 39de11be26..5f8618ddfc 100644 --- a/lib/arel/primitives/attribute.rb +++ b/lib/arel/primitives/attribute.rb @@ -46,6 +46,14 @@ module Arel end module Transformations + def self.included(klass) + alias_method :eql?, :== + end + + def hash + name.hash + history.size.hash + end + def as(aliaz = nil) Attribute.new(relation, name, :alias => aliaz, :ancestor => self) end diff --git a/lib/arel/relations/aggregation.rb b/lib/arel/relations/aggregation.rb index 7a56834125..955a71c0b0 100644 --- a/lib/arel/relations/aggregation.rb +++ b/lib/arel/relations/aggregation.rb @@ -16,6 +16,6 @@ Aggregation = Struct.new(:relation) do end def attributes - relation.attributes.collect(&:to_attribute) + @attributes ||= relation.attributes.collect(&:to_attribute) end end \ No newline at end of file diff --git a/lib/arel/relations/alias.rb b/lib/arel/relations/alias.rb index 08be02e862..d14a51f67a 100644 --- a/lib/arel/relations/alias.rb +++ b/lib/arel/relations/alias.rb @@ -1,13 +1,10 @@ module Arel class Alias < Compound include Recursion::BaseCase + alias_method :==, :equal? def initialize(relation) @relation = relation end - - def ==(other) - equal? other - end end end \ No newline at end of file diff --git a/lib/arel/relations/compound.rb b/lib/arel/relations/compound.rb index 663711760c..649d11e8b1 100644 --- a/lib/arel/relations/compound.rb +++ b/lib/arel/relations/compound.rb @@ -3,7 +3,7 @@ module Arel attr_reader :relation hash_on :relation delegate :joins, :selects, :orders, :groupings, :inserts, :taken, - :skipped, :name, :alias, :aggregation?, :column_for, + :skipped, :name, :aggregation?, :column_for, :engine, :table, :relation_for, :table_sql, :to => :relation diff --git a/lib/arel/relations/join.rb b/lib/arel/relations/join.rb index b3452968e4..b7c5c3f39b 100644 --- a/lib/arel/relations/join.rb +++ b/lib/arel/relations/join.rb @@ -27,7 +27,7 @@ module Arel end def attributes - (externalize(relation1).attributes + + @attributes ||= (externalize(relation1).attributes + externalize(relation2).attributes).collect { |a| a.bind(self) } end @@ -44,9 +44,11 @@ module Arel end def relation_for(attribute) - [externalize(relation1).relation_for(attribute), externalize(relation2).relation_for(attribute)].max do |r1, r2| + [ + externalize(relation1).relation_for(attribute), + externalize(relation2).relation_for(attribute) + ].max do |r1, r2| a1, a2 = r1 && r1[attribute], r2 && r2[attribute] - attribute / a1 <=> attribute / a2 end end diff --git a/lib/arel/relations/projection.rb b/lib/arel/relations/projection.rb index f09d4f894b..8a08cda70c 100644 --- a/lib/arel/relations/projection.rb +++ b/lib/arel/relations/projection.rb @@ -7,7 +7,7 @@ module Arel end def attributes - projections.collect { |p| p.bind(self) } + @attributes ||= projections.collect { |p| p.bind(self) } end def ==(other) diff --git a/lib/arel/relations/recursion.rb b/lib/arel/relations/recursion.rb index 90976c702b..2c6024dc30 100644 --- a/lib/arel/relations/recursion.rb +++ b/lib/arel/relations/recursion.rb @@ -6,7 +6,7 @@ module Arel end def relation_for(attribute) - self + self[attribute] and self end def table_sql(formatter = Sql::TableReference.new(self)) diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index db7d746c79..c576033938 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -139,20 +139,12 @@ module Arel attributes.detect { |a| a.named?(name) } end - # TESTME - added relation_for(x)[x] because of AR + # TESTME - added original_attribute because of AR def find_attribute_matching_attribute(attribute) attributes.select { |a| a.match?(attribute) }.max do |a1, a2| (attribute / a1.original_attribute) <=> (attribute / a2.original_attribute) end end - - def find_attribute_matching_attribute_with_memoization(attribute) - @attribute_for_attribute ||= Hash.new do |h, a| - h[a] = find_attribute_matching_attribute_without_memoization(a) - end - @attribute_for_attribute[attribute] - end - alias_method_chain :find_attribute_matching_attribute, :memoization end include AttributeAccessable diff --git a/lib/arel/sessions/session.rb b/lib/arel/sessions/session.rb index becf23b8b6..9c61d0cba1 100644 --- a/lib/arel/sessions/session.rb +++ b/lib/arel/sessions/session.rb @@ -44,13 +44,5 @@ module Arel end end include CRUD - - module Transactions - end - include Transactions - - module UnitOfWork - end - include UnitOfWork end end \ No newline at end of file diff --git a/lib/arel/sql/christener.rb b/lib/arel/sql/christener.rb index 894f030342..26e1acab58 100644 --- a/lib/arel/sql/christener.rb +++ b/lib/arel/sql/christener.rb @@ -3,9 +3,9 @@ module Arel class Christener 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]}" : '') + @relation_names ||= Hash.new do |hash, relation| + @used_names[name = relation.name] += 1 + hash[relation] = name + (@used_names[name] > 1 ? "_#{@used_names[name]}" : '') end @relation_names[relation] end diff --git a/spec/arel/unit/relations/alias_spec.rb b/spec/arel/unit/relations/alias_spec.rb index 25dbf70668..85850b5e1a 100644 --- a/spec/arel/unit/relations/alias_spec.rb +++ b/spec/arel/unit/relations/alias_spec.rb @@ -7,10 +7,19 @@ module Arel end describe '==' do - it "returns the alias" do + it "obtains if the objects are the same" do 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 \ No newline at end of file diff --git a/spec/arel/unit/relations/insertion_spec.rb b/spec/arel/unit/relations/insertion_spec.rb index 0667aa665e..10b70a2036 100644 --- a/spec/arel/unit/relations/insertion_spec.rb +++ b/spec/arel/unit/relations/insertion_spec.rb @@ -24,7 +24,7 @@ module Arel @insertion.to_sql.should be_like(" INSERT INTO `users` - (`users`.`name`, `users`.`id`) VALUES ('nick', 1) + (`users`.`id`, `users`.`name`) VALUES (1, 'nick') ") end diff --git a/spec/arel/unit/relations/update_spec.rb b/spec/arel/unit/relations/update_spec.rb index 976e88dddc..f411781392 100644 --- a/spec/arel/unit/relations/update_spec.rb +++ b/spec/arel/unit/relations/update_spec.rb @@ -10,7 +10,7 @@ module Arel it "manufactures sql updating attributes when given multiple attributes" do Update.new(@relation, @relation[:id] => 1, @relation[:name] => "nick").to_sql.should be_like(" UPDATE `users` - SET `users`.`name` = 'nick', `users`.`id` = 1 + SET `users`.`id` = 1, `users`.`name` = 'nick' ") end diff --git a/spec/arel/unit/session/session_spec.rb b/spec/arel/unit/session/session_spec.rb index c2eb9a4555..fbb2b7791b 100644 --- a/spec/arel/unit/session/session_spec.rb +++ b/spec/arel/unit/session/session_spec.rb @@ -73,20 +73,12 @@ module Arel end end - describe Session::Transactions do + describe 'Transactions' do describe '#begin' do end describe '#end' do end end - - describe Session::UnitOfWork do - describe '#flush' do - end - - describe '#clear' do - end - end end end \ No newline at end of file -- cgit v1.2.3