mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add stored procedure test in mysql2
This commit is contained in:
parent
d9e74ace9f
commit
d39b6f77fc
6 changed files with 65 additions and 17 deletions
|
@ -126,7 +126,9 @@ module ActiveRecord
|
|||
# Returns an array of arrays containing the field values.
|
||||
# Order is the same as that returned by +columns+.
|
||||
def select_rows(sql, name = nil, binds = [])
|
||||
execute(sql, name).to_a
|
||||
result = execute(sql, name)
|
||||
@connection.next_result while @connection.more_results?
|
||||
result.to_a
|
||||
end
|
||||
|
||||
# Executes the SQL statement in the context of this connection.
|
||||
|
@ -142,6 +144,7 @@ module ActiveRecord
|
|||
|
||||
def exec_query(sql, name = 'SQL', binds = [])
|
||||
result = execute(sql, name)
|
||||
@connection.next_result while @connection.more_results?
|
||||
ActiveRecord::Result.new(result.fields, result.to_a)
|
||||
end
|
||||
|
||||
|
|
|
@ -118,15 +118,6 @@ class MysqlConnectionTest < ActiveRecord::MysqlTestCase
|
|||
end
|
||||
end
|
||||
|
||||
# Test that MySQL allows multiple results for stored procedures
|
||||
if defined?(Mysql) && Mysql.const_defined?(:CLIENT_MULTI_RESULTS)
|
||||
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
|
||||
|
||||
def test_mysql_connection_collation_is_configured
|
||||
assert_equal 'utf8_unicode_ci', @connection.show_variable('collation_connection')
|
||||
assert_equal 'utf8_general_ci', ARUnit2Model.connection.show_variable('collation_connection')
|
||||
|
|
|
@ -1,15 +1,29 @@
|
|||
require "cases/helper"
|
||||
require 'models/topic'
|
||||
require 'models/reply'
|
||||
|
||||
class StoredProcedureTest < ActiveRecord::MysqlTestCase
|
||||
class MysqlStoredProcedureTest < ActiveRecord::MysqlTestCase
|
||||
fixtures :topics
|
||||
|
||||
def setup
|
||||
@connection = ActiveRecord::Base.connection
|
||||
end
|
||||
|
||||
# Test that MySQL allows multiple results for stored procedures
|
||||
if defined?(Mysql) && Mysql.const_defined?(:CLIENT_MULTI_RESULTS)
|
||||
#
|
||||
# In MySQL 5.6, CLIENT_MULTI_RESULTS is enabled by default.
|
||||
# http://dev.mysql.com/doc/refman/5.6/en/call.html
|
||||
if ActiveRecord::Base.connection.version >= '5.6.0' || Mysql.const_defined?(:CLIENT_MULTI_RESULTS)
|
||||
def test_multi_results
|
||||
rows = @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
|
||||
|
||||
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'"
|
||||
topics = Topic.find_by_sql 'CALL topics(3);'
|
||||
assert_equal 3, topics.size
|
||||
assert @connection.active?, "Bad connection use by 'MysqlAdapter.select'"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
29
activerecord/test/cases/adapters/mysql2/sp_test.rb
Normal file
29
activerecord/test/cases/adapters/mysql2/sp_test.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
require "cases/helper"
|
||||
require 'models/topic'
|
||||
require 'models/reply'
|
||||
|
||||
class Mysql2StoredProcedureTest < ActiveRecord::Mysql2TestCase
|
||||
fixtures :topics
|
||||
|
||||
def setup
|
||||
@connection = ActiveRecord::Base.connection
|
||||
end
|
||||
|
||||
# Test that MySQL allows multiple results for stored procedures
|
||||
#
|
||||
# In MySQL 5.6, CLIENT_MULTI_RESULTS is enabled by default.
|
||||
# http://dev.mysql.com/doc/refman/5.6/en/call.html
|
||||
if ActiveRecord::Base.connection.version >= '5.6.0'
|
||||
def test_multi_results
|
||||
rows = @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 'Mysql2Adapter.select_rows'"
|
||||
end
|
||||
|
||||
def test_multi_results_from_find_by_sql
|
||||
topics = Topic.find_by_sql 'CALL topics(3);'
|
||||
assert_equal 3, topics.size
|
||||
assert @connection.active?, "Bad connection use by 'Mysql2Adapter.select'"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -38,6 +38,17 @@ CREATE PROCEDURE ten() SQL SECURITY INVOKER
|
|||
BEGIN
|
||||
select 10;
|
||||
END
|
||||
SQL
|
||||
|
||||
ActiveRecord::Base.connection.execute <<-SQL
|
||||
DROP PROCEDURE IF EXISTS topics;
|
||||
SQL
|
||||
|
||||
ActiveRecord::Base.connection.execute <<-SQL
|
||||
CREATE PROCEDURE topics(IN num INT) SQL SECURITY INVOKER
|
||||
BEGIN
|
||||
select * from topics limit num;
|
||||
END
|
||||
SQL
|
||||
|
||||
ActiveRecord::Base.connection.drop_table "enum_tests", if_exists: true
|
||||
|
|
|
@ -45,9 +45,9 @@ DROP PROCEDURE IF EXISTS topics;
|
|||
SQL
|
||||
|
||||
ActiveRecord::Base.connection.execute <<-SQL
|
||||
CREATE PROCEDURE topics() SQL SECURITY INVOKER
|
||||
CREATE PROCEDURE topics(IN num INT) SQL SECURITY INVOKER
|
||||
BEGIN
|
||||
select * from topics limit 1;
|
||||
select * from topics limit num;
|
||||
END
|
||||
SQL
|
||||
|
||||
|
|
Loading…
Reference in a new issue