mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Changed the interface on AbstractAdapter to require that adapters return the number of affected rows on delete and update operations. Added that Base.update_all and Base.delete_all return an integer of the number of affected rows #341
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@228 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
88192b9b1b
commit
69cb942d9b
8 changed files with 39 additions and 14 deletions
|
@ -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]
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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' (
|
||||
|
|
Loading…
Reference in a new issue