@@ -54,12 +54,10 @@ def affected_rows_from_results_or_handle(raw_result, handle)
5454 end
5555
5656 def internal_exec_sql_query ( sql , conn )
57- handle = internal_raw_execute ( sql , conn )
57+ handle = internal_raw_execute ( sql , conn , as : :array )
5858 results = handle_to_names_and_values ( handle , ar_result : true )
5959
6060 [ results , affected_rows_from_results_or_handle ( results , handle ) ]
61- ensure
62- finish_statement_handle ( handle )
6361 end
6462
6563 def exec_delete ( sql , name = nil , binds = [ ] )
@@ -241,16 +239,19 @@ def execute_procedure(proc_name, *variables)
241239 with_raw_connection do |conn |
242240 result = internal_raw_execute ( sql , conn )
243241 verified!
244- options = { as : :hash , cache_rows : true , timezone : ActiveRecord . default_timezone || :utc }
245242
246- result . each ( options ) do |row |
247- r = row . with_indifferent_access
248- yield ( r ) if block_given?
243+ if block_given?
244+ result . rows . flatten . each do |row |
245+ r = row . with_indifferent_access
246+ yield ( r )
247+ end
249248 end
250249
251- result = result . each . map { |row | row . is_a? ( Hash ) ? row . with_indifferent_access : row }
252250 notification_payload [ :row_count ] = result . count
253- result
251+
252+ # existing code heavily depends on receiving an array here, and #rows is an array
253+ # e.g. #flatten is not something Enumerable provides, which is what TinyTds::Result implements
254+ result . rows
254255 end
255256 end
256257 end
@@ -498,20 +499,14 @@ def identity_columns(table_name)
498499 # === SQLServer Specific (Selecting) ============================ #
499500
500501 def _raw_select ( sql , conn )
501- handle = internal_raw_execute ( sql , conn )
502- handle_to_names_and_values ( handle , fetch : :rows )
503- ensure
504- finish_statement_handle ( handle )
502+ handle = internal_raw_execute ( sql , conn , as : :array )
503+ handle_to_names_and_values ( handle )
505504 end
506505
507- def handle_to_names_and_values ( handle , options = { } )
508- query_options = { } . tap do |qo |
509- qo [ :timezone ] = ActiveRecord . default_timezone || :utc
510- qo [ :as ] = ( options [ :ar_result ] || options [ :fetch ] == :rows ) ? :array : :hash
511- end
512- results = handle . each ( query_options )
506+ def handle_to_names_and_values ( handle , ar_result : false )
507+ results = handle . rows
513508
514- if options [ : ar_result]
509+ if ar_result
515510 columns = handle . fields
516511 columns = columns . last if columns . any? && columns . all? { |e | e . is_a? ( Array ) } # If query returns multiple result sets, only return the columns of the last one.
517512 columns = columns . map ( &:downcase ) if lowercase_schema_reflection
@@ -522,14 +517,8 @@ def handle_to_names_and_values(handle, options = {})
522517 end
523518 end
524519
525- def finish_statement_handle ( handle )
526- handle &.cancel
527- handle
528- end
529-
530- def internal_raw_execute ( sql , raw_connection , perform_do : false )
531- result = raw_connection . execute ( sql )
532- perform_do ? result . do : result
520+ def internal_raw_execute ( sql , raw_connection , perform_do : false , as : :hash , timezone : ActiveRecord . default_timezone || :utc )
521+ perform_do ? raw_connection . do ( sql ) : raw_connection . execute ( sql , as :, timezone :)
533522 end
534523
535524 # === SQLServer Specific (insert_all / upsert_all support) ===================== #
0 commit comments