@@ -14,15 +14,20 @@ def write_query?(sql) # :nodoc:
1414 end
1515
1616 def perform_query ( raw_connection , sql , binds , type_casted_binds , prepare :, notification_payload :, batch :)
17+ unless binds . nil? || binds . empty?
18+ types , params = sp_executesql_types_and_parameters ( binds )
19+ sql = sp_executesql_sql ( sql , types , params , notification_payload [ :name ] )
20+ end
21+
1722 id_insert_table_name = query_requires_identity_insert? ( sql )
1823
1924 result , affected_rows = if id_insert_table_name
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
25+ with_identity_insert_enabled ( id_insert_table_name , raw_connection ) do
26+ internal_exec_sql_query ( sql , raw_connection )
27+ end
28+ else
29+ internal_exec_sql_query ( sql , raw_connection )
30+ end
2631
2732 verified!
2833 notification_payload [ :affected_rows ] = affected_rows
@@ -48,15 +53,6 @@ def affected_rows_from_results_or_handle(raw_result, handle)
4853 affected_rows ( raw_result ) || handle . affected_rows
4954 end
5055
51- def raw_execute ( sql , name = nil , binds = [ ] , prepare : false , async : false , allow_retry : false , materialize_transactions : true , batch : false )
52- unless binds . nil? || binds . empty?
53- types , params = sp_executesql_types_and_parameters ( binds )
54- sql = sp_executesql_sql ( sql , types , params , name )
55- end
56-
57- super
58- end
59-
6056 def internal_exec_sql_query ( sql , conn )
6157 handle = internal_raw_execute ( sql , conn )
6258 results = handle_to_names_and_values ( handle , ar_result : true )
@@ -235,10 +231,10 @@ def merge_insert_values_list(insert:, insert_all:)
235231
236232 def execute_procedure ( proc_name , *variables )
237233 vars = if variables . any? && variables . first . is_a? ( Hash )
238- variables . first . map { |k , v | "@#{ k } = #{ quote ( v ) } " }
239- else
240- variables . map { |v | quote ( v ) }
241- end . join ( ", " )
234+ variables . first . map { |k , v | "@#{ k } = #{ quote ( v ) } " }
235+ else
236+ variables . map { |v | quote ( v ) }
237+ end . join ( ", " )
242238 sql = "EXEC #{ proc_name } #{ vars } " . strip
243239
244240 log ( sql , "Execute Procedure" ) do |notification_payload |
@@ -344,35 +340,35 @@ def sql_for_insert(sql, pk, binds, returning)
344340 end
345341
346342 sql = if pk && use_output_inserted? && !database_prefix_remote_server?
347- table_name ||= get_table_name ( sql )
348- exclude_output_inserted = exclude_output_inserted_table_name? ( table_name , sql )
349-
350- if exclude_output_inserted
351- pk_and_types = Array ( pk ) . map do |subkey |
352- {
353- quoted : SQLServer ::Utils . extract_identifiers ( subkey ) . quoted ,
354- id_sql_type : exclude_output_inserted_id_sql_type ( subkey , exclude_output_inserted )
355- }
356- end
357-
358- <<~SQL . squish
343+ table_name ||= get_table_name ( sql )
344+ exclude_output_inserted = exclude_output_inserted_table_name? ( table_name , sql )
345+
346+ if exclude_output_inserted
347+ pk_and_types = Array ( pk ) . map do |subkey |
348+ {
349+ quoted : SQLServer ::Utils . extract_identifiers ( subkey ) . quoted ,
350+ id_sql_type : exclude_output_inserted_id_sql_type ( subkey , exclude_output_inserted )
351+ }
352+ end
353+
354+ <<~SQL . squish
359355 DECLARE @ssaIdInsertTable table (#{ pk_and_types . map { |pk_and_type | "#{ pk_and_type [ :quoted ] } #{ pk_and_type [ :id_sql_type ] } " } . join ( ", " ) } );
360356 #{ sql . dup . insert sql . index ( / (DEFAULT )?VALUES/i ) , " OUTPUT #{ pk_and_types . map { |pk_and_type | "INSERTED.#{ pk_and_type [ :quoted ] } " } . join ( ", " ) } INTO @ssaIdInsertTable" }
361357 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
362358 SQL
363- else
364- returning_columns = returning || Array ( pk )
365-
366- if returning_columns . any?
367- returning_columns_statements = returning_columns . map { |c | " INSERTED.#{ SQLServer ::Utils . extract_identifiers ( c ) . quoted } " }
368- sql . dup . insert sql . index ( / (DEFAULT )?VALUES/i ) , " OUTPUT" + returning_columns_statements . join ( "," )
369- else
370- sql
371- end
372- end
373- else
374- "#{ sql } ; SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident"
375- end
359+ else
360+ returning_columns = returning || Array ( pk )
361+
362+ if returning_columns . any?
363+ returning_columns_statements = returning_columns . map { |c | " INSERTED.#{ SQLServer ::Utils . extract_identifiers ( c ) . quoted } " }
364+ sql . dup . insert sql . index ( / (DEFAULT )?VALUES/i ) , " OUTPUT" + returning_columns_statements . join ( "," )
365+ else
366+ sql
367+ end
368+ end
369+ else
370+ "#{ sql } ; SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident"
371+ end
376372
377373 [ sql , binds ]
378374 end
@@ -541,16 +537,16 @@ def build_sql_for_returning(insert:, insert_all:)
541537 return "" unless insert_all . returning
542538
543539 returning_values_sql = if insert_all . returning . is_a? ( String )
544- insert_all . returning
545- else
546- Array ( insert_all . returning ) . map do |attribute |
547- if insert . model . attribute_alias? ( attribute )
548- "INSERTED.#{ quote_column_name ( insert . model . attribute_alias ( attribute ) ) } AS #{ quote_column_name ( attribute ) } "
549- else
550- "INSERTED.#{ quote_column_name ( attribute ) } "
551- end
552- end . join ( "," )
553- end
540+ insert_all . returning
541+ else
542+ Array ( insert_all . returning ) . map do |attribute |
543+ if insert . model . attribute_alias? ( attribute )
544+ "INSERTED.#{ quote_column_name ( insert . model . attribute_alias ( attribute ) ) } AS #{ quote_column_name ( attribute ) } "
545+ else
546+ "INSERTED.#{ quote_column_name ( attribute ) } "
547+ end
548+ end . join ( "," )
549+ end
554550
555551 " OUTPUT #{ returning_values_sql } "
556552 end
0 commit comments