aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG4
-rwxr-xr-xactiverecord/lib/active_record/base.rb6
-rwxr-xr-xactiverecord/lib/active_record/connection_adapters/abstract_adapter.rb4
-rwxr-xr-xactiverecord/lib/active_record/connection_adapters/mysql_adapter.rb8
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb9
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb12
-rwxr-xr-xactiverecord/test/base_test.rb8
-rw-r--r--activerecord/test/fixtures/db_definitions/sqlite.sql2
8 files changed, 39 insertions, 14 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 173ea25f11..b8e3e0f4ba 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 38002ad2a0..58a1ac5787 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -343,13 +343,13 @@ module ActiveRecord #:nodoc:
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 66b5962b18..b521a0fdf8 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 @@ module ActiveRecord
# 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 55c15c6823..f9c38470aa 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 @@ module ActiveRecord
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 fb54642d3a..359cb067a2 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 @@ module ActiveRecord
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 8a77cb0ce7..604fc960aa 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 @@ module ActiveRecord
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 b80f4ff6ef..c2a105f85d 100755
--- a/activerecord/test/base_test.rb
+++ b/activerecord/test/base_test.rb
@@ -260,11 +260,15 @@ class BasicsTest < Test::Unit::TestCase
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 1e9aed1e1b..9bab9d9a15 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' (