提交 69cb942d 编写于 作者: D David Heinemeier Hansson

Changed the interface on AbstractAdapter to require that adapters return the...

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
上级 88192b9b
*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 @@ 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
......
......@@ -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:
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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' (
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册