diff options
-rw-r--r-- | lib/arel/engine.rb | 6 | ||||
-rw-r--r-- | lib/arel/primitives/value.rb | 8 | ||||
-rw-r--r-- | lib/arel/relations/relation.rb | 4 | ||||
-rw-r--r-- | lib/arel/relations/writes/delete.rb | 6 | ||||
-rw-r--r-- | lib/arel/relations/writes/update.rb | 12 | ||||
-rw-r--r-- | spec/arel/unit/relations/relation_spec.rb | 57 | ||||
-rw-r--r-- | spec/arel/unit/relations/update_spec.rb | 32 |
7 files changed, 53 insertions, 72 deletions
diff --git a/lib/arel/engine.rb b/lib/arel/engine.rb index b0b7b4e955..5a5ef06878 100644 --- a/lib/arel/engine.rb +++ b/lib/arel/engine.rb @@ -6,13 +6,13 @@ module Arel def initialize(ar = nil) @ar = ar end - + def connection @ar.connection end - + def method_missing(method, *args, &block) @ar.connection.send(method, *args, &block) end end -end
\ No newline at end of file +end diff --git a/lib/arel/primitives/value.rb b/lib/arel/primitives/value.rb index 809af7c206..74baa06e5b 100644 --- a/lib/arel/primitives/value.rb +++ b/lib/arel/primitives/value.rb @@ -3,8 +3,8 @@ module Arel attributes :value, :relation deriving :initialize, :== delegate :inclusion_predicate_sql, :equality_predicate_sql, :to => :value - - + + def to_sql(formatter = Sql::WhereCondition.new(relation)) formatter.value value end @@ -12,9 +12,9 @@ module Arel def format(object) object.to_sql(Sql::Value.new(relation)) end - + def bind(relation) Value.new(value, relation) end end -end
\ No newline at end of file +end diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index d9ba9a108b..50110c7416 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -99,11 +99,11 @@ module Arel end def update(assignments) - session.update Update.new(self, assignments); self + session.update Update.new(self, assignments) end def delete - session.delete Deletion.new(self); self + session.delete Deletion.new(self) end end include Writable diff --git a/lib/arel/relations/writes/delete.rb b/lib/arel/relations/writes/delete.rb index 318a299b8b..b1ff3bef27 100644 --- a/lib/arel/relations/writes/delete.rb +++ b/lib/arel/relations/writes/delete.rb @@ -11,9 +11,9 @@ module Arel ("LIMIT #{taken}" unless taken.blank? ), ].compact.join("\n") end - - def call(connection = engine.connection) + + def call(connection = engine) connection.delete(to_sql) end end -end
\ No newline at end of file +end diff --git a/lib/arel/relations/writes/update.rb b/lib/arel/relations/writes/update.rb index 720b9d697d..3b43152ad6 100644 --- a/lib/arel/relations/writes/update.rb +++ b/lib/arel/relations/writes/update.rb @@ -2,7 +2,7 @@ module Arel class Update < Compound attributes :relation, :assignments deriving :== - + def initialize(relation, assignments) @relation, @assignments = relation, assignments.bind(relation) end @@ -11,15 +11,15 @@ module Arel [ "UPDATE #{table_sql} SET", assignments.collect do |attribute, value| - "#{value.format(attribute)} = #{attribute.format(value)}" + "#{engine.quote_column_name(attribute.name)} = #{attribute.format(value)}" end.join(",\n"), - ("WHERE #{wheres.collect(&:to_sql).join('\n\tAND ')}" unless wheres.blank? ), + ("WHERE #{wheres.map(&:to_sql).join('\n\tAND ')}" unless wheres.blank? ), ("LIMIT #{taken}" unless taken.blank? ) ].join("\n") end - - def call(connection = engine.connection) + + def call(connection = engine) connection.update(to_sql) end end -end
\ No newline at end of file +end diff --git a/spec/arel/unit/relations/relation_spec.rb b/spec/arel/unit/relations/relation_spec.rb index 77a787b840..a3bfa67353 100644 --- a/spec/arel/unit/relations/relation_spec.rb +++ b/spec/arel/unit/relations/relation_spec.rb @@ -7,14 +7,14 @@ module Arel @attribute1 = @relation[:id] @attribute2 = @relation[:name] end - + describe '[]' do describe 'when given an', Attribute do it "return the attribute congruent to the provided attribute" do @relation[@attribute1].should == @attribute1 end end - + describe 'when given a', Symbol, String do it "returns the attribute with the same name, if it exists" do @relation[:id].should == @attribute1 @@ -23,13 +23,13 @@ module Arel end end end - + describe Relation::Operable do describe 'joins' do before do @predicate = @relation[:id].eq(@relation[:id]) end - + describe '#join' do describe 'when given a relation' do it "manufactures an inner join operation between those two relations" do @@ -37,13 +37,13 @@ module Arel should == Join.new("INNER JOIN", @relation, @relation, @predicate) end end - + describe "when given a string" do it "manufactures a join operation with the string passed through" do - @relation.join(arbitrary_string = "ASDF").should == Join.new(arbitrary_string, @relation) + @relation.join(arbitrary_string = "ASDF").should == Join.new(arbitrary_string, @relation) end end - + describe "when given something blank" do it "returns self" do @relation.join.should == @relation @@ -64,7 +64,7 @@ module Arel @relation.project(@attribute1, @attribute2). \ should == Project.new(@relation, @attribute1, @attribute2) end - + describe "when given blank attributes" do it "returns self" do @relation.project.should == @relation @@ -97,36 +97,36 @@ module Arel end end end - + describe '#order' do it "manufactures an order relation" do @relation.order(@attribute1, @attribute2).should == Order.new(@relation, @attribute1, @attribute2) end - + describe 'when given a blank ordering' do it 'returns self' do @relation.order.should == @relation end end end - + describe '#take' do it "manufactures a take relation" do @relation.take(5).should == Take.new(@relation, 5) end - + describe 'when given a blank number of items' do it 'returns self' do @relation.take.should == @relation end end end - + describe '#skip' do it "manufactures a skip relation" do @relation.skip(4).should == Skip.new(@relation, 4) end - + describe 'when given a blank number of items' do it 'returns self' do @relation.skip.should == @relation @@ -138,24 +138,15 @@ module Arel it 'manufactures a group relation' do @relation.group(@attribute1, @attribute2).should == Group.new(@relation, @attribute1, @attribute2) end - + describe 'when given blank groupings' do it 'returns self' do @relation.group.should == @relation end end end - - describe Relation::Operable::Writable do - describe '#delete' do - it 'manufactures a deletion relation' do - Session.start do - mock(Session.new).delete(Deletion.new(@relation)) - @relation.delete.should == @relation - end - end - end + describe Relation::Operable::Writable do describe '#insert' do it 'manufactures an insertion relation' do Session.start do @@ -165,26 +156,16 @@ module Arel end end end - - describe '#update' do - it 'manufactures an update relation' do - Session.start do - assignments = {@relation[:name] => Value.new('bob', @relation)} - mock(Session.new).update(Update.new(@relation, assignments)) - @relation.update(assignments).should == @relation - end - end - end end end - + describe Relation::Enumerable do it "implements enumerable" do @relation.collect.should == @relation.session.read(@relation) @relation.first.should == @relation.session.read(@relation).first end end - + describe '#call' do it 'executes a select_all on the connection' do mock(connection = Object.new).execute(@relation.to_sql) { [] } @@ -192,4 +173,4 @@ module Arel end end end -end
\ No newline at end of file +end diff --git a/spec/arel/unit/relations/update_spec.rb b/spec/arel/unit/relations/update_spec.rb index 08c6da7901..b67369251f 100644 --- a/spec/arel/unit/relations/update_spec.rb +++ b/spec/arel/unit/relations/update_spec.rb @@ -5,32 +5,32 @@ module Arel before do @relation = Table.new(:users) end - + describe '#to_sql' do 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`.`id` = 1, `users`.`name` = 'nick' + SET `id` = 1, `name` = 'nick' ") end - + it "manufactures sql updating attributes when given a ranged relation" do Update.new(@relation.take(1), @relation[:name] => "nick").to_sql.should be_like(" UPDATE `users` - SET `users`.`name` = 'nick' + SET `name` = 'nick' LIMIT 1 ") end - + describe 'when given values whose types correspond to the types of the attributes' do before do @update = Update.new(@relation, @relation[:name] => "nick") end - + it 'manufactures sql updating attributes' do @update.to_sql.should be_like(" UPDATE `users` - SET `users`.`name` = 'nick' + SET `name` = 'nick' ") end end @@ -39,15 +39,15 @@ module Arel before do @update = Update.new(@relation, @relation[:id] => '1-asdf') end - + it 'manufactures sql updating attributes' do @update.to_sql.should be_like(" UPDATE `users` - SET `users`.`id` = 1 + SET `id` = 1 ") end end - + describe 'when the relation is a where' do before do @update = Update.new( @@ -55,27 +55,27 @@ module Arel @relation[:name] => "nick" ) end - + it 'manufactures sql updating a where relation' do @update.to_sql.should be_like(" UPDATE `users` - SET `users`.`name` = 'nick' + SET `name` = 'nick' WHERE `users`.`id` = 1 ") end end end - + describe '#call' do before do @update = Update.new(@relation, @relation[:name] => "nick") end - + it 'executes an update on the connection' do mock(connection = Object.new).update(@update.to_sql) @update.call(connection) end end - + end -end
\ No newline at end of file +end |