@@ -14,6 +14,8 @@ def write_query?(sql) # :nodoc:
14
14
end
15
15
16
16
def raw_execute ( sql , name , async : false , allow_retry : false , materialize_transactions : true )
17
+ # puts "raw_execute: #{sql}" if $AIDO
18
+
17
19
log ( sql , name , async : async ) do |notification_payload |
18
20
with_raw_connection ( allow_retry : allow_retry , materialize_transactions : materialize_transactions ) do |conn |
19
21
result = if id_insert_table_name = query_requires_identity_insert? ( sql )
@@ -312,8 +314,11 @@ def sql_for_insert(sql, pk, binds, returning)
312
314
# === SQLServer Specific ======================================== #
313
315
314
316
def set_identity_insert ( table_name , conn , enable )
317
+ puts "set_identity_insert: #{ table_name } #{ enable } "
318
+
315
319
internal_raw_execute ( "SET IDENTITY_INSERT #{ table_name } #{ enable ? 'ON' : 'OFF' } " , conn , perform_do : true )
316
320
rescue Exception
321
+ puts "IDENTITY_INSERT could not be turned #{ enable ? 'ON' : 'OFF' } for table #{ table_name } "
317
322
raise ActiveRecordError , "IDENTITY_INSERT could not be turned #{ enable ? 'ON' : 'OFF' } for table #{ table_name } "
318
323
end
319
324
@@ -404,12 +409,37 @@ def exclude_output_inserted_id_sql_type(pk, exclude_output_inserted)
404
409
end
405
410
406
411
def query_requires_identity_insert? ( sql )
412
+ # puts "query_requires_identity_insert?: #{sql}" if $AIDO
413
+
407
414
return false unless insert_sql? ( sql )
408
415
409
416
raw_table_name = get_raw_table_name ( sql )
417
+
418
+ # binding.pry
419
+ # if $AIDO
420
+ #
421
+ # puts "xxxx"
422
+ # return true
423
+ # end
424
+
425
+
426
+
427
+
410
428
id_column = identity_columns ( raw_table_name ) . first
411
429
412
- id_column && sql =~ /^\s *(INSERT|EXEC sp_executesql N'INSERT)[^(]+\( [^)]*\b (#{ id_column . name } )\b ,?[^)]*\) /i ? SQLServer ::Utils . extract_identifiers ( raw_table_name ) . quoted : false
430
+
431
+ if id_column && sql =~ /^\s *(INSERT|EXEC sp_executesql N'INSERT)[^(]+\( [^)]*\b (#{ id_column . name } )\b ,?[^)]*\) /i
432
+
433
+ # puts "xxx: raw_table_name=#{raw_table_name}, id_column=#{id_column.name}" if $AIDO
434
+ # puts "xxx: quoted=#{SQLServer::Utils.extract_identifiers(raw_table_name).quoted}" if $AIDO
435
+
436
+ SQLServer ::Utils . extract_identifiers ( raw_table_name ) . quoted
437
+ else
438
+ false
439
+
440
+ end
441
+
442
+ # id_column && sql =~ /^\s*(INSERT|EXEC sp_executesql N'INSERT)[^(]+\([^)]*\b(#{id_column.name})\b,?[^)]*\)/i ? SQLServer::Utils.extract_identifiers(raw_table_name).quoted : false
413
443
end
414
444
415
445
def insert_sql? ( sql )
@@ -453,6 +483,8 @@ def finish_statement_handle(handle)
453
483
# Getting around this by raising an exception ourselves while PR
454
484
# https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
455
485
def internal_raw_execute ( sql , conn , perform_do : false )
486
+ # puts "internal_raw_execute: #{sql}" if $AIDO
487
+
456
488
result = conn . execute ( sql ) . tap do |_result |
457
489
raise TinyTds ::Error , "failed to execute statement" if _result . is_a? ( FalseClass )
458
490
end
0 commit comments