1
0
Fork 0
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:
David Heinemeier Hansson 2004-12-19 16:21:55 +00:00
parent 88192b9b1b
commit 69cb942d9b
8 changed files with 39 additions and 14 deletions

View file

@ -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]

View file

@ -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

View file

@ -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:

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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' (