diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 173ea25f111d43c47d8c4ab540e30e8ae6b6d0aa..b8e3e0f4ba2862390dab994b161343cdd24ff61e 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,9 @@ *SVN* +* Added that Base.update_all and Base.delete_all return an integer of the number of affected rows #341 + +* Changed the interface on AbstractAdapter to require that adapters return the number of affected rows on delete and update operations. + * Added that query benchmarking will only happen if its going to be logged anyway #344 * Added higher_item and lower_item as public methods for acts_as_list #342 [Tobias Luetke] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 38002ad2a062a1689ae1dd31515d4cbff29975ba..58a1ac5787c615522522f65cceecf782b21b6034 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -343,13 +343,13 @@ def destroy(id) find(id).destroy end - # Updates all records with the SET-part of an SQL update statement in +updates+. A subset of the records can be selected - # by specifying +conditions+. Example: + # Updates all records with the SET-part of an SQL update statement in +updates+ and returns an integer with the number of rows updates. + # A subset of the records can be selected by specifying +conditions+. Example: # Billing.update_all "category = 'authorized', approved = 1", "author = 'David'" def update_all(updates, conditions = nil) sql = "UPDATE #{table_name} SET #{updates} " add_conditions!(sql, conditions) - connection.update(sql, "#{name} Update") + return connection.update(sql, "#{name} Update") end # Destroys the objects for all the records that matches the +condition+ by instantiating each object and calling diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 66b5962b183cbeaea8883303ae6193cdaf1b717e..b521a0fdf8a7539142ffb5e253cbe0d367e6987f 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -285,10 +285,10 @@ def columns(table_name, name = nil) end # Returns the last auto-generated ID from the affected table. def insert(sql, name = nil, pk = nil, id_value = nil) end - # Executes the update statement. + # Executes the update statement and returns the number of rows affected. def update(sql, name = nil) end - # Executes the delete statement. + # Executes the delete statement and returns the number of rows affected. def delete(sql, name = nil) end def reset_runtime # :nodoc: diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 55c15c6823ac28366dff7782287f3b3589f329ae..f9c38470aa7fac59e6138c1a21a4d48c04579c5b 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -67,8 +67,12 @@ def execute(sql, name = nil) log(sql, name, @connection) { |connection| connection.query(sql) } end - alias_method :update, :execute - alias_method :delete, :execute + def update(sql, name = nil) + execute(sql, name) + @connection.affected_rows + end + + alias_method :delete, :update def begin_db_transaction begin diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index fb54642d3a68d791c7661e1a3d8cb8298c8d4b41..359cb067a295cbc83bb0ab116dbfee1c710d4252 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -64,8 +64,13 @@ def execute(sql, name = nil) log(sql, name, @connection) { |connection| connection.query(sql) } end - alias_method :update, :execute - alias_method :delete, :execute + def update(sql, name = nil) + result = nil + log(sql, name, @connection) { |connection| result = connection.exec(sql) } + result.cmdtuples + end + + alias_method :delete, :update def begin_db_transaction() execute "BEGIN" end def commit_db_transaction() execute "COMMIT" end diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index 8a77cb0ce7737ba7f1bcc4ff442b18d632b9f382..604fc960aa4861421a9bdaab137e700a2e215080 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -62,8 +62,16 @@ def execute(sql, name = nil) end end - alias_method :update, :execute - alias_method :delete, :execute + def update(sql, name = nil) + execute(sql, name) + @connection.changes + end + + def delete(sql, name = nil) + sql += " WHERE 1=1" unless sql =~ /WHERE/i + execute(sql, name) + @connection.changes + end def begin_db_transaction() execute "BEGIN" end def commit_db_transaction() execute "COMMIT" end diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index b80f4ff6eff24031b0ddd382ef73365c253be727..c2a105f85d24fc152912b13b3ba7ba80fe9011c1 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -260,11 +260,15 @@ def test_decrement_counter end def test_update_all - Topic.update_all "content = 'bulk updated!'" + assert_equal 2, Topic.update_all("content = 'bulk updated!'") assert_equal "bulk updated!", Topic.find(1).content assert_equal "bulk updated!", Topic.find(2).content end - + + def test_delete_all + assert_equal 2, Topic.delete_all + end + def test_update_by_condition Topic.update_all "content = 'bulk updated!'", "approved = 1" assert_equal "Have a nice day", Topic.find(1).content diff --git a/activerecord/test/fixtures/db_definitions/sqlite.sql b/activerecord/test/fixtures/db_definitions/sqlite.sql index 1e9aed1e1b2f11d1da1e6d5b2c1f7130b9fb23d6..9bab9d9a15fb2022b8a74bde7a28b92e816e252b 100644 --- a/activerecord/test/fixtures/db_definitions/sqlite.sql +++ b/activerecord/test/fixtures/db_definitions/sqlite.sql @@ -33,7 +33,7 @@ CREATE TABLE 'topics' ( CREATE TABLE 'developers' ( 'id' INTEGER PRIMARY KEY NOT NULL, 'name' TEXT DEFAULT NULL, - 'salary' INTEGER 70000 + 'salary' INTEGER DEFAULT 70000 ); CREATE TABLE 'projects' (