Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9558: sqlserver_affected_rows.patch

File sqlserver_affected_rows.patch, 2.2 kB (added by lawrence, 1 year ago)
  • lib/active_record/connection_adapters/sqlserver_adapter.rb

    old new  
    304304        super || select_value("SELECT @@IDENTITY AS Ident") 
    305305      end 
    306306 
    307       def update_sql(sql, name = nil) 
    308         execute(sql, name) do |handle| 
    309           handle.rows 
    310         end || select_value("SELECT @@ROWCOUNT AS AffectedRows") 
     307      def update_sql(sql, name = nil)           
     308        autoCommiting = @connection["AutoCommit"] 
     309        begin 
     310          begin_db_transaction if autoCommiting 
     311          execute(sql, name) 
     312          affectedRows = select_value("SELECT @@ROWCOUNT AS AffectedRows") 
     313          commit_db_transaction if autoCommiting 
     314          affectedRows 
     315        rescue 
     316          rollback_db_transaction if autoCommiting 
     317          raise 
     318        end                     
    311319      end 
    312320 
    313321      def execute(sql, name = nil) 
  • test/affected_rows_test_sqlserver.rb

    old new  
     1require 'abstract_unit' 
     2require 'fixtures/topic' 
     3require 'fixtures/reply' 
     4 
     5class AffectedRowsTestSqlserver < Test::Unit::TestCase 
     6  self.use_transactional_fixtures = false 
     7  fixtures :topics 
     8 
     9  def setup 
     10    @first, @second = Topic.find(1, 2).sort_by { |t| t.id } 
     11  end 
     12 
     13  def test_affected_rows 
     14    assert Topic.connection.instance_variable_get("@connection")["AutoCommit"] 
     15 
     16    topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } } 
     17    updated = Topic.update(topic_data.keys, topic_data.values) 
     18 
     19    assert_equal 2, updated.size 
     20    assert_equal "1 updated", Topic.find(1).content 
     21    assert_equal "2 updated", Topic.find(2).content 
     22   
     23    assert_equal 2, Topic.delete_all         
     24  end 
     25   
     26  def test_update_sql_statement_invalid 
     27    assert_raise(ActiveRecord::StatementInvalid) { Topic.connection.update_sql("UPDATE XXX") } 
     28  end 
     29end