@@ -19,9 +19,9 @@ def raw_execute(sql, name, async: false, allow_retry: false, materialize_transac
1919 log ( sql , name , async : async ) do
2020 with_raw_connection ( allow_retry : allow_retry , materialize_transactions : materialize_transactions ) do |conn |
2121 result = if id_insert_table_name = query_requires_identity_insert? ( sql )
22- with_identity_insert_enabled ( id_insert_table_name , conn ) { _execute ( sql , conn , perform_do : true ) }
22+ with_identity_insert_enabled ( id_insert_table_name , conn ) { internal_raw_execute ( sql , conn , perform_do : true ) }
2323 else
24- _execute ( sql , conn , perform_do : true )
24+ internal_raw_execute ( sql , conn , perform_do : true )
2525 end
2626 end
2727 end
@@ -49,11 +49,11 @@ def internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: fa
4949 # TODO: Look into refactoring this.
5050 if id_insert_table_name = query_requires_identity_insert? ( sql )
5151 with_identity_insert_enabled ( id_insert_table_name , conn ) do
52- handle = _execute ( sql , conn )
52+ handle = internal_raw_execute ( sql , conn )
5353 result = handle_to_names_and_values ( handle , options )
5454 end
5555 else
56- handle = _execute ( sql , conn )
56+ handle = internal_raw_execute ( sql , conn )
5757 result = handle_to_names_and_values ( handle , options )
5858 end
5959 ensure
@@ -176,7 +176,7 @@ def execute_procedure(proc_name, *variables)
176176
177177 log ( sql , "Execute Procedure" ) do
178178 with_raw_connection do |conn |
179- result = _execute ( sql , conn )
179+ result = internal_raw_execute ( sql , conn )
180180 options = { as : :hash , cache_rows : true , timezone : ActiveRecord . default_timezone || :utc }
181181
182182 result . each ( options ) do |row |
@@ -305,28 +305,13 @@ def sql_for_insert(sql, pk, binds, returning)
305305 # === SQLServer Specific ======================================== #
306306
307307 def set_identity_insert ( table_name , conn , enable )
308- _execute ( "SET IDENTITY_INSERT #{ table_name } #{ enable ? 'ON' : 'OFF' } " , conn , perform_do : true )
308+ internal_raw_execute ( "SET IDENTITY_INSERT #{ table_name } #{ enable ? 'ON' : 'OFF' } " , conn , perform_do : true )
309309 rescue Exception
310310 raise ActiveRecordError , "IDENTITY_INSERT could not be turned #{ enable ? 'ON' : 'OFF' } for table #{ table_name } "
311311 end
312312
313313 # === SQLServer Specific (Executing) ============================ #
314314
315- # TODO: Adapter should be refactored to use `with_raw_connection` to translate exceptions.
316- def sp_executesql ( sql , name , binds , options = { } )
317- options [ :ar_result ] = true if options [ :fetch ] != :rows
318-
319- unless without_prepared_statement? ( binds )
320- types , params = sp_executesql_types_and_parameters ( binds )
321- sql = sp_executesql_sql ( sql , types , params , name )
322- end
323-
324- raw_select sql , name , binds , options
325- rescue => original_exception
326- translated_exception = translate_exception_class ( original_exception , sql , binds )
327- raise translated_exception
328- end
329-
330315 def sp_executesql_types_and_parameters ( binds )
331316 types , params = [ ] , [ ]
332317 binds . each_with_index do |attr , index |
@@ -377,13 +362,6 @@ def sp_executesql_sql(sql, types, params, name)
377362 sql . freeze
378363 end
379364
380- def raw_connection_do ( sql )
381- result = ensure_established_connection! { dblib_execute ( sql ) }
382- result . do
383- ensure
384- @update_sql = false
385- end
386-
387365 # === SQLServer Specific (Identity Inserts) ===================== #
388366
389367 def use_output_inserted?
@@ -422,21 +400,14 @@ def identity_columns(table_name)
422400
423401 # === SQLServer Specific (Selecting) ============================ #
424402
425- def raw_select ( sql , name = "SQL" , binds = [ ] , options = { } )
426- log ( sql , name , binds , async : options [ :async ] ) { _raw_select ( sql , options ) }
427- end
403+ def _raw_select ( sql , conn , options = { } )
404+ handle = internal_raw_execute ( sql , conn )
428405
429- def _raw_select ( sql , options = { } )
430- handle = raw_connection_run ( sql )
431406 handle_to_names_and_values ( handle , options )
432407 ensure
433408 finish_statement_handle ( handle )
434409 end
435410
436- def raw_connection_run ( sql )
437- ensure_established_connection! { dblib_execute ( sql ) }
438- end
439-
440411 def handle_to_names_and_values ( handle , options = { } )
441412 query_options = { } . tap do |qo |
442413 qo [ :timezone ] = ActiveRecord . default_timezone || :utc
@@ -453,33 +424,16 @@ def finish_statement_handle(handle)
453424 handle
454425 end
455426
456- # TODO: Rename
457- def _execute ( sql , conn , perform_do : false )
427+ # TinyTDS returns false instead of raising an exception if connection fails.
428+ # Getting around this by raising an exception ourselves while PR
429+ # https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
430+ def internal_raw_execute ( sql , conn , perform_do : false )
458431 result = conn . execute ( sql ) . tap do |_result |
459- # TinyTDS returns false instead of raising an exception if connection fails.
460- # Getting around this by raising an exception ourselves while PR
461- # https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
462432 raise TinyTds ::Error , "failed to execute statement" if _result . is_a? ( FalseClass )
463433 end
464434
465435 perform_do ? result . do : result
466436 end
467-
468- # TODO: Remove
469- def dblib_execute ( sql )
470- @raw_connection . execute ( sql ) . tap do |result |
471- # TinyTDS returns false instead of raising an exception if connection fails.
472- # Getting around this by raising an exception ourselves while this PR
473- # https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
474- raise TinyTds ::Error , "failed to execute statement" if result . is_a? ( FalseClass )
475- end
476- end
477-
478- def ensure_established_connection!
479- raise TinyTds ::Error , 'SQL Server client is not connected' unless @raw_connection
480-
481- yield
482- end
483437 end
484438 end
485439 end
0 commit comments