diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-19 16:21:55 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-19 16:21:55 +0000 |
commit | 69cb942d9b72d9a18f8d00c5887f2532bdda0a0f (patch) | |
tree | 309f72ad492f052ed7f8cf57b067bd618f59da60 /activerecord/lib | |
parent | 88192b9b1be2bd0729073c899df42a789f264993 (diff) | |
download | rails-69cb942d9b72d9a18f8d00c5887f2532bdda0a0f.tar.gz rails-69cb942d9b72d9a18f8d00c5887f2532bdda0a0f.tar.bz2 rails-69cb942d9b72d9a18f8d00c5887f2532bdda0a0f.zip |
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
Diffstat (limited to 'activerecord/lib')
5 files changed, 28 insertions, 11 deletions
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 |