@@ -19,9 +19,9 @@ def raw_execute(sql, name, async: false, allow_retry: false, materialize_transac
19
19
log ( sql , name , async : async ) do
20
20
with_raw_connection ( allow_retry : allow_retry , materialize_transactions : materialize_transactions ) do |conn |
21
21
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 ) }
23
23
else
24
- _execute ( sql , conn , perform_do : true )
24
+ internal_raw_execute ( sql , conn , perform_do : true )
25
25
end
26
26
end
27
27
end
@@ -49,11 +49,11 @@ def internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: fa
49
49
# TODO: Look into refactoring this.
50
50
if id_insert_table_name = query_requires_identity_insert? ( sql )
51
51
with_identity_insert_enabled ( id_insert_table_name , conn ) do
52
- handle = _execute ( sql , conn )
52
+ handle = internal_raw_execute ( sql , conn )
53
53
result = handle_to_names_and_values ( handle , options )
54
54
end
55
55
else
56
- handle = _execute ( sql , conn )
56
+ handle = internal_raw_execute ( sql , conn )
57
57
result = handle_to_names_and_values ( handle , options )
58
58
end
59
59
ensure
@@ -176,7 +176,7 @@ def execute_procedure(proc_name, *variables)
176
176
177
177
log ( sql , "Execute Procedure" ) do
178
178
with_raw_connection do |conn |
179
- result = _execute ( sql , conn )
179
+ result = internal_raw_execute ( sql , conn )
180
180
options = { as : :hash , cache_rows : true , timezone : ActiveRecord . default_timezone || :utc }
181
181
182
182
result . each ( options ) do |row |
@@ -305,28 +305,13 @@ def sql_for_insert(sql, pk, binds, returning)
305
305
# === SQLServer Specific ======================================== #
306
306
307
307
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 )
309
309
rescue Exception
310
310
raise ActiveRecordError , "IDENTITY_INSERT could not be turned #{ enable ? 'ON' : 'OFF' } for table #{ table_name } "
311
311
end
312
312
313
313
# === SQLServer Specific (Executing) ============================ #
314
314
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
-
330
315
def sp_executesql_types_and_parameters ( binds )
331
316
types , params = [ ] , [ ]
332
317
binds . each_with_index do |attr , index |
@@ -377,13 +362,6 @@ def sp_executesql_sql(sql, types, params, name)
377
362
sql . freeze
378
363
end
379
364
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
-
387
365
# === SQLServer Specific (Identity Inserts) ===================== #
388
366
389
367
def use_output_inserted?
@@ -422,21 +400,14 @@ def identity_columns(table_name)
422
400
423
401
# === SQLServer Specific (Selecting) ============================ #
424
402
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 )
428
405
429
- def _raw_select ( sql , options = { } )
430
- handle = raw_connection_run ( sql )
431
406
handle_to_names_and_values ( handle , options )
432
407
ensure
433
408
finish_statement_handle ( handle )
434
409
end
435
410
436
- def raw_connection_run ( sql )
437
- ensure_established_connection! { dblib_execute ( sql ) }
438
- end
439
-
440
411
def handle_to_names_and_values ( handle , options = { } )
441
412
query_options = { } . tap do |qo |
442
413
qo [ :timezone ] = ActiveRecord . default_timezone || :utc
@@ -453,33 +424,16 @@ def finish_statement_handle(handle)
453
424
handle
454
425
end
455
426
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 )
458
431
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.
462
432
raise TinyTds ::Error , "failed to execute statement" if _result . is_a? ( FalseClass )
463
433
end
464
434
465
435
perform_do ? result . do : result
466
436
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
483
437
end
484
438
end
485
439
end
0 commit comments