Permalink
Browse files

Resolved ticket #210.

 * Made DataObjectsAdapter#update do nothing when no properties
   are updated.
  • Loading branch information...
1 parent eeb1702 commit e6e61fcc9021a63c638ef86d41fc13c3cd52fc6c Martin Kihlgren committed Apr 17, 2008
Showing with 27 additions and 14 deletions.
  1. +18 −14 lib/data_mapper/adapters/data_objects_adapter.rb
  2. +9 −0 spec/unit/adapters/data_objects_adapter_spec.rb
@@ -122,20 +122,24 @@ def read(repository, resource, key)
def update(repository, resource)
properties = resource.dirty_attributes
-
- sql = update_statement(resource.class, properties)
- values = properties.map { |property| resource.instance_variable_get(property.instance_variable_name) }
- parameters = (values + resource.key)
- DataMapper.logger.debug { "UPDATE: #{sql} PARAMETERS: #{parameters.inspect}" }
-
- connection = create_connection
- command = connection.create_command(sql)
-
- affected_rows = command.execute_non_query(*parameters).to_i
-
- close_connection(connection)
-
- affected_rows == 1
+
+ if properties.empty?
+ return false
+ else
+ sql = update_statement(resource.class, properties)
+ values = properties.map { |property| resource.instance_variable_get(property.instance_variable_name) }
+ parameters = (values + resource.key)
+ DataMapper.logger.debug { "UPDATE: #{sql} PARAMETERS: #{parameters.inspect}" }
+
+ connection = create_connection
+ command = connection.create_command(sql)
+
+ affected_rows = command.execute_non_query(*parameters).to_i
+
+ close_connection(connection)
+
+ affected_rows == 1
+ end
end
def delete(repository, resource)
@@ -176,6 +176,15 @@ class LittleBox
end
end
+ describe "#update" do
+ it 'should not try to update if there are no dirty attributes' do
+ repository = mock("repository")
+ resource = mock("resource")
+ resource.stub!(:dirty_attributes).and_return({})
+ @adapter.update(repository, resource).should == false
+ end
+ end
+
describe "#create_statement_with_returning" do
it 'should generate a SQL statement for all fields' do

0 comments on commit e6e61fc

Please sign in to comment.