@@ -17,15 +17,12 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
17
17
id_insert_table_name = query_requires_identity_insert? ( sql )
18
18
19
19
result , affected_rows = if id_insert_table_name
20
- # If the table name is a view, we need to get the base table name for enabling identity insert.
21
- id_insert_table_name = view_table_name ( id_insert_table_name ) if view_exists? ( id_insert_table_name )
22
-
23
- with_identity_insert_enabled ( id_insert_table_name , raw_connection ) do
24
- internal_exec_sql_query ( sql , raw_connection )
25
- end
26
- else
27
- internal_exec_sql_query ( sql , raw_connection )
28
- end
20
+ with_identity_insert_enabled ( id_insert_table_name , raw_connection ) do
21
+ internal_exec_sql_query ( sql , raw_connection )
22
+ end
23
+ else
24
+ internal_exec_sql_query ( sql , raw_connection )
25
+ end
29
26
30
27
verified!
31
28
notification_payload [ :affected_rows ] = affected_rows
@@ -239,10 +236,10 @@ def merge_insert_values_list(insert:, insert_all:)
239
236
240
237
def execute_procedure ( proc_name , *variables )
241
238
vars = if variables . any? && variables . first . is_a? ( Hash )
242
- variables . first . map { |k , v | "@#{ k } = #{ quote ( v ) } " }
243
- else
244
- variables . map { |v | quote ( v ) }
245
- end . join ( ", " )
239
+ variables . first . map { |k , v | "@#{ k } = #{ quote ( v ) } " }
240
+ else
241
+ variables . map { |v | quote ( v ) }
242
+ end . join ( ", " )
246
243
sql = "EXEC #{ proc_name } #{ vars } " . strip
247
244
248
245
log ( sql , "Execute Procedure" ) do |notification_payload |
@@ -264,11 +261,14 @@ def execute_procedure(proc_name, *variables)
264
261
end
265
262
266
263
def with_identity_insert_enabled ( table_name , conn )
267
- table_name = quote_table_name ( table_name )
268
- set_identity_insert ( table_name , conn , true )
264
+ # If the table name is a view, we need to get the base table name for enabling identity insert.
265
+ table_name = view_table_name ( table_name ) if view_exists? ( table_name )
266
+ quoted_table_name = quote_table_name ( table_name )
267
+
268
+ set_identity_insert ( quoted_table_name , conn , true )
269
269
yield
270
270
ensure
271
- set_identity_insert ( table_name , conn , false )
271
+ set_identity_insert ( quoted_table_name , conn , false )
272
272
end
273
273
274
274
def use_database ( database = nil )
@@ -345,35 +345,35 @@ def sql_for_insert(sql, pk, binds, returning)
345
345
end
346
346
347
347
sql = if pk && use_output_inserted? && !database_prefix_remote_server?
348
- table_name ||= get_table_name ( sql )
349
- exclude_output_inserted = exclude_output_inserted_table_name? ( table_name , sql )
350
-
351
- if exclude_output_inserted
352
- pk_and_types = Array ( pk ) . map do |subkey |
353
- {
354
- quoted : SQLServer ::Utils . extract_identifiers ( subkey ) . quoted ,
355
- id_sql_type : exclude_output_inserted_id_sql_type ( subkey , exclude_output_inserted )
356
- }
357
- end
358
-
359
- <<~SQL . squish
348
+ table_name ||= get_table_name ( sql )
349
+ exclude_output_inserted = exclude_output_inserted_table_name? ( table_name , sql )
350
+
351
+ if exclude_output_inserted
352
+ pk_and_types = Array ( pk ) . map do |subkey |
353
+ {
354
+ quoted : SQLServer ::Utils . extract_identifiers ( subkey ) . quoted ,
355
+ id_sql_type : exclude_output_inserted_id_sql_type ( subkey , exclude_output_inserted )
356
+ }
357
+ end
358
+
359
+ <<~SQL . squish
360
360
DECLARE @ssaIdInsertTable table (#{ pk_and_types . map { |pk_and_type | "#{ pk_and_type [ :quoted ] } #{ pk_and_type [ :id_sql_type ] } " } . join ( ", " ) } );
361
361
#{ sql . dup . insert sql . index ( / (DEFAULT )?VALUES/i ) , " OUTPUT #{ pk_and_types . map { |pk_and_type | "INSERTED.#{ pk_and_type [ :quoted ] } " } . join ( ", " ) } INTO @ssaIdInsertTable" }
362
362
SELECT #{ pk_and_types . map { |pk_and_type | "CAST(#{ pk_and_type [ :quoted ] } AS #{ pk_and_type [ :id_sql_type ] } ) #{ pk_and_type [ :quoted ] } " } . join ( ", " ) } FROM @ssaIdInsertTable
363
363
SQL
364
- else
365
- returning_columns = returning || Array ( pk )
366
-
367
- if returning_columns . any?
368
- returning_columns_statements = returning_columns . map { |c | " INSERTED.#{ SQLServer ::Utils . extract_identifiers ( c ) . quoted } " }
369
- sql . dup . insert sql . index ( / (DEFAULT )?VALUES/i ) , " OUTPUT" + returning_columns_statements . join ( "," )
370
- else
371
- sql
372
- end
373
- end
374
- else
375
- "#{ sql } ; SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident"
376
- end
364
+ else
365
+ returning_columns = returning || Array ( pk )
366
+
367
+ if returning_columns . any?
368
+ returning_columns_statements = returning_columns . map { |c | " INSERTED.#{ SQLServer ::Utils . extract_identifiers ( c ) . quoted } " }
369
+ sql . dup . insert sql . index ( / (DEFAULT )?VALUES/i ) , " OUTPUT" + returning_columns_statements . join ( "," )
370
+ else
371
+ sql
372
+ end
373
+ end
374
+ else
375
+ "#{ sql } ; SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident"
376
+ end
377
377
378
378
[ sql , binds ]
379
379
end
@@ -542,16 +542,16 @@ def build_sql_for_returning(insert:, insert_all:)
542
542
return "" unless insert_all . returning
543
543
544
544
returning_values_sql = if insert_all . returning . is_a? ( String )
545
- insert_all . returning
546
- else
547
- Array ( insert_all . returning ) . map do |attribute |
548
- if insert . model . attribute_alias? ( attribute )
549
- "INSERTED.#{ quote_column_name ( insert . model . attribute_alias ( attribute ) ) } AS #{ quote_column_name ( attribute ) } "
550
- else
551
- "INSERTED.#{ quote_column_name ( attribute ) } "
552
- end
553
- end . join ( "," )
554
- end
545
+ insert_all . returning
546
+ else
547
+ Array ( insert_all . returning ) . map do |attribute |
548
+ if insert . model . attribute_alias? ( attribute )
549
+ "INSERTED.#{ quote_column_name ( insert . model . attribute_alias ( attribute ) ) } AS #{ quote_column_name ( attribute ) } "
550
+ else
551
+ "INSERTED.#{ quote_column_name ( attribute ) } "
552
+ end
553
+ end . join ( "," )
554
+ end
555
555
556
556
" OUTPUT #{ returning_values_sql } "
557
557
end
0 commit comments