Skip to content
Browse files

Add multiple results set support with #execute_procedure for :dblib m…

…ode.
  • Loading branch information...
1 parent 8cbdac4 commit 40ef67694220cec980ec888e0a0fb1bb2b569b48 @metaskills metaskills committed
View
2 CHANGELOG
@@ -1,5 +1,7 @@
* master *
+* Add multiple results set support with #execute_procedure for :dblib mode. [Ken Collins]
+
* Simplify encoding support. [Ken Collins]
* Add binary timestamp datatype handling. [Erik Bryn]
View
25 lib/active_record/connection_adapters/sqlserver/database_statements.rb
@@ -72,12 +72,17 @@ def execute_procedure(proc_name, *variables)
vars = variables.map{ |v| quote(v) }.join(', ')
sql = "EXEC #{proc_name} #{vars}".strip
name = 'Execute Procedure'
- results = []
- case @connection_options[:mode]
- when :dblib
- results << select(sql, name).map { |r| r.with_indifferent_access }
- when :odbc
- log(sql, name) do
+ log(sql, name) do
+ case @connection_options[:mode]
+ when :dblib
+ result = @connection.execute(sql)
+ result.each(:as => :hash, :cache_rows => true) do |row|
+ r = row.with_indifferent_access
+ yield(r) if block_given?
+ end
+ result.each
+ when :odbc
+ results = []
raw_connection_run(sql) do |handle|
get_rows = lambda {
rows = handle_to_names_and_values handle, :fetch => :all
@@ -89,11 +94,13 @@ def execute_procedure(proc_name, *variables)
get_rows.call
end
end
+ results.many? ? results : results.first
+ when :adonet
+ results = []
+ results << select(sql, name).map { |r| r.with_indifferent_access }
+ results.many? ? results : results.first
end
- when :adonet
- results << select(sql, name).map { |r| r.with_indifferent_access }
end
- results.many? ? results : results.first
end
def use_database(database=nil)
View
32 test/cases/execute_procedure_test_sqlserver.rb
@@ -9,34 +9,26 @@ def setup
should 'execute a simple procedure' do
tables = @klass.execute_procedure :sp_tables
assert_instance_of Array, tables
- assert_instance_of HashWithIndifferentAccess, tables.first
+ assert tables.first.respond_to?(:keys)
end
should 'take parameter arguments' do
tables = @klass.execute_procedure :sp_tables, 'sql_server_chronics'
table_info = tables.first
assert_equal 1, tables.size
- assert_equal (ENV['ARUNIT_DB_NAME'] || 'activerecord_unittest'), table_info[:TABLE_QUALIFIER], "Table Info: #{table_info.inspect}"
- assert_equal 'TABLE', table_info[:TABLE_TYPE], "Table Info: #{table_info.inspect}"
+ assert_equal (ENV['ARUNIT_DB_NAME'] || 'activerecord_unittest'), table_info['TABLE_QUALIFIER'], "Table Info: #{table_info.inspect}"
+ assert_equal 'TABLE', table_info['TABLE_TYPE'], "Table Info: #{table_info.inspect}"
end
- if connection_mode_odbc?
-
- should 'allow multiple result sets to be returned' do
- results1, results2 = @klass.execute_procedure('sp_helpconstraint','accounts')
- assert_instance_of Array, results1
- assert_instance_of HashWithIndifferentAccess, results1.first
- assert results1.first['Object Name']
- assert_instance_of Array, results2
- assert_instance_of HashWithIndifferentAccess, results2.first
- assert results2.first['constraint_name']
- assert results2.first['constraint_type']
- end
-
- else
-
- should 'allow multiple result sets to be returned'
-
+ should 'allow multiple result sets to be returned' do
+ results1, results2 = @klass.execute_procedure('sp_helpconstraint','accounts')
+ assert_instance_of Array, results1
+ assert results1.first.respond_to?(:keys)
+ assert results1.first['Object Name']
+ assert_instance_of Array, results2
+ assert results2.first.respond_to?(:keys)
+ assert results2.first['constraint_name']
+ assert results2.first['constraint_type']
end

0 comments on commit 40ef676

Please sign in to comment.
Something went wrong with that request. Please try again.