From ef75a6e3e4a70a843cdfc00b145fc31efbd11a4c Mon Sep 17 00:00:00 2001 From: Jeff Lawson Date: Sun, 1 Aug 2010 11:41:03 +0100 Subject: [PATCH 1/2] Bug Fix -- clean up connection after stored procedure [#3151 state:resolved] --- .../connection_adapters/mysql_adapter.rb | 2 ++ .../test/cases/adapters/mysql/connection_test.rb | 1 + activerecord/test/cases/adapters/mysql/sp_test.rb | 15 +++++++++++++++ activerecord/test/schema/mysql_specific_schema.rb | 11 +++++++++++ 4 files changed, 29 insertions(+), 0 deletions(-) create mode 100644 activerecord/test/cases/adapters/mysql/sp_test.rb diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index b403443..a523c11 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -275,6 +275,7 @@ module ActiveRecord rows = [] result.each { |row| rows << row } result.free + @connection.more_results && @connection.next_result # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped rows end @@ -616,6 +617,7 @@ module ActiveRecord result = execute(sql, name) rows = [] result.each_hash { |row| rows << row } + @connection.more_results && @connection.next_result # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped result.free rows end diff --git a/activerecord/test/cases/adapters/mysql/connection_test.rb b/activerecord/test/cases/adapters/mysql/connection_test.rb index 8e4842a..ddec85b 100644 --- a/activerecord/test/cases/adapters/mysql/connection_test.rb +++ b/activerecord/test/cases/adapters/mysql/connection_test.rb @@ -48,6 +48,7 @@ class MysqlConnectionTest < ActiveRecord::TestCase def test_multi_results rows = ActiveRecord::Base.connection.select_rows('CALL ten();') assert_equal 10, rows[0][0].to_i, "ten() did not return 10 as expected: #{rows.inspect}" + assert @connection.active?, "Bad connection use by 'MysqlAdapter.select_rows'" end end diff --git a/activerecord/test/cases/adapters/mysql/sp_test.rb b/activerecord/test/cases/adapters/mysql/sp_test.rb new file mode 100644 index 0000000..3ca2917 --- /dev/null +++ b/activerecord/test/cases/adapters/mysql/sp_test.rb @@ -0,0 +1,15 @@ +require "cases/helper" +require 'models/topic' + +class StoredProcedureTest < ActiveRecord::TestCase + fixtures :topics + + # Test that MySQL allows multiple results for stored procedures + if Mysql.const_defined?(:CLIENT_MULTI_RESULTS) + def test_multi_results_from_find_by_sql + topics = Topic.find_by_sql 'CALL topics();' + assert_equal 1, topics.size + assert ActiveRecord::Base.connection.active?, "Bad connection use by 'MysqlAdapter.select'" + end + end +end diff --git a/activerecord/test/schema/mysql_specific_schema.rb b/activerecord/test/schema/mysql_specific_schema.rb index c78d99f..30e1c5a 100644 --- a/activerecord/test/schema/mysql_specific_schema.rb +++ b/activerecord/test/schema/mysql_specific_schema.rb @@ -21,4 +21,15 @@ BEGIN END SQL + ActiveRecord::Base.connection.execute <<-SQL +DROP PROCEDURE IF EXISTS topics; +SQL + + ActiveRecord::Base.connection.execute <<-SQL +CREATE PROCEDURE topics() SQL SECURITY INVOKER +BEGIN + select * from topics limit 1; +END +SQL + end -- 1.7.2 From cd76ff5bd5b80cfb2214d2f1196ceda34f3cf6cb Mon Sep 17 00:00:00 2001 From: Jeff Lawson Date: Sun, 1 Aug 2010 12:30:58 +0100 Subject: [PATCH 2/2] Bug Fix -- clean up connection after stored procedure [#3151 state:resolved] --- .../connection_adapters/mysql_adapter.rb | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index a523c11..ef86590 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -617,8 +617,8 @@ module ActiveRecord result = execute(sql, name) rows = [] result.each_hash { |row| rows << row } - @connection.more_results && @connection.next_result # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped result.free + @connection.more_results && @connection.next_result # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped rows end -- 1.7.2