@@ -15,15 +15,15 @@ def write_query?(sql) # :nodoc:
1515
1616 def perform_query ( raw_connection , sql , binds , type_casted_binds , prepare :, notification_payload :, batch :)
1717 result , affected_rows = if id_insert_table_name = query_requires_identity_insert? ( sql )
18- # If the table name is a view, we need to get the base table name for enabling identity insert.
19- id_insert_table_name = view_table_name ( id_insert_table_name ) if view_exists? ( id_insert_table_name )
18+ # If the table name is a view, we need to get the base table name for enabling identity insert.
19+ id_insert_table_name = view_table_name ( id_insert_table_name ) if view_exists? ( id_insert_table_name )
2020
21- with_identity_insert_enabled ( id_insert_table_name , raw_connection ) do
22- internal_exec_sql_query ( sql , raw_connection )
23- end
24- else
25- internal_exec_sql_query ( sql , raw_connection )
26- end
21+ with_identity_insert_enabled ( id_insert_table_name , raw_connection ) do
22+ internal_exec_sql_query ( sql , raw_connection )
23+ end
24+ else
25+ internal_exec_sql_query ( sql , raw_connection )
26+ end
2727
2828 verified!
2929 notification_payload [ :affected_rows ] = affected_rows
@@ -41,7 +41,7 @@ def cast_result(raw_result)
4141
4242 # Returns the affected rows from results.
4343 def affected_rows ( raw_result )
44- raw_result &.first &.fetch ( ' AffectedRows' , nil )
44+ raw_result &.first &.fetch ( " AffectedRows" , nil )
4545 end
4646
4747 # Returns the affected rows from results or handle.
@@ -66,19 +66,19 @@ def internal_exec_sql_query(sql, conn)
6666 handle = internal_raw_execute ( sql , conn )
6767 results = handle_to_names_and_values ( handle , ar_result : true )
6868
69- return results , affected_rows_from_results_or_handle ( results , handle )
69+ [ results , affected_rows_from_results_or_handle ( results , handle ) ]
7070 ensure
7171 finish_statement_handle ( handle )
7272 end
7373
7474 def exec_delete ( sql , name = nil , binds = [ ] )
7575 sql = sql . dup << "; SELECT @@ROWCOUNT AS AffectedRows"
76- super ( sql , name , binds )
76+ super
7777 end
7878
7979 def exec_update ( sql , name = nil , binds = [ ] )
8080 sql = sql . dup << "; SELECT @@ROWCOUNT AS AffectedRows"
81- super ( sql , name , binds )
81+ super
8282 end
8383
8484 def begin_db_transaction
@@ -155,14 +155,14 @@ def default_insert_value(column)
155155 private :default_insert_value
156156
157157 def build_insert_sql ( insert ) # :nodoc:
158- sql = + "INSERT #{ insert . into } "
158+ sql = "INSERT #{ insert . into } "
159159
160160 if returning = insert . send ( :insert_all ) . returning
161161 returning_sql = if returning . is_a? ( String )
162- returning
163- else
164- Array ( returning ) . map { |column | "INSERTED.#{ quote_column_name ( column ) } " } . join ( ", " )
165- end
162+ returning
163+ else
164+ Array ( returning ) . map { |column | "INSERTED.#{ quote_column_name ( column ) } " } . join ( ", " )
165+ end
166166 sql << " OUTPUT #{ returning_sql } "
167167 end
168168
@@ -174,17 +174,17 @@ def build_insert_sql(insert) # :nodoc:
174174
175175 def execute_procedure ( proc_name , *variables )
176176 vars = if variables . any? && variables . first . is_a? ( Hash )
177- variables . first . map { |k , v | "@#{ k } = #{ quote ( v ) } " }
178- else
179- variables . map { |v | quote ( v ) }
180- end . join ( ", " )
177+ variables . first . map { |k , v | "@#{ k } = #{ quote ( v ) } " }
178+ else
179+ variables . map { |v | quote ( v ) }
180+ end . join ( ", " )
181181 sql = "EXEC #{ proc_name } #{ vars } " . strip
182182
183183 log ( sql , "Execute Procedure" ) do |notification_payload |
184184 with_raw_connection do |conn |
185185 result = internal_raw_execute ( sql , conn )
186186 verified!
187- options = { as : :hash , cache_rows : true , timezone : ActiveRecord . default_timezone || :utc }
187+ options = { as : :hash , cache_rows : true , timezone : ActiveRecord . default_timezone || :utc }
188188
189189 result . each ( options ) do |row |
190190 r = row . with_indifferent_access
@@ -218,7 +218,7 @@ def user_options
218218
219219 rows = select_rows ( "DBCC USEROPTIONS WITH NO_INFOMSGS" , "SCHEMA" )
220220 rows = rows . first if rows . size == 2 && rows . last . empty?
221- rows . reduce ( HashWithIndifferentAccess . new ) do |values , row |
221+ rows . each_with_object ( HashWithIndifferentAccess . new ) do |row , values |
222222 if row . instance_of? Hash
223223 set_option = row . values [ 0 ] . gsub ( /\s +/ , "_" )
224224 user_value = row . values [ 1 ]
@@ -227,7 +227,6 @@ def user_options
227227 user_value = row [ 1 ]
228228 end
229229 values [ set_option ] = user_value
230- values
231230 end
232231 end
233232
@@ -281,45 +280,45 @@ def sql_for_insert(sql, pk, binds, returning)
281280 end
282281
283282 sql = if pk && use_output_inserted? && !database_prefix_remote_server?
284- table_name ||= get_table_name ( sql )
285- exclude_output_inserted = exclude_output_inserted_table_name? ( table_name , sql )
286-
287- if exclude_output_inserted
288- pk_and_types = Array ( pk ) . map do |subkey |
289- {
290- quoted : SQLServer ::Utils . extract_identifiers ( subkey ) . quoted ,
291- id_sql_type : exclude_output_inserted_id_sql_type ( subkey , exclude_output_inserted )
292- }
293- end
294-
295- <<~SQL . squish
296- DECLARE @ssaIdInsertTable table (#{ pk_and_types . map { |pk_and_type | "#{ pk_and_type [ :quoted ] } #{ pk_and_type [ :id_sql_type ] } " } . join ( ", " ) } );
297- #{ sql . dup . insert sql . index ( / (DEFAULT )?VALUES/i ) , " OUTPUT #{ pk_and_types . map { |pk_and_type | "INSERTED.#{ pk_and_type [ :quoted ] } " } . join ( ", " ) } INTO @ssaIdInsertTable" }
298- 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
299- SQL
300- else
301- returning_columns = returning || Array ( pk )
302-
303- if returning_columns . any?
304- returning_columns_statements = returning_columns . map { |c | " INSERTED.#{ SQLServer ::Utils . extract_identifiers ( c ) . quoted } " }
305- sql . dup . insert sql . index ( / (DEFAULT )?VALUES/i ) , " OUTPUT" + returning_columns_statements . join ( "," )
306- else
307- sql
308- end
309- end
310- else
311- "#{ sql } ; SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident"
312- end
283+ table_name ||= get_table_name ( sql )
284+ exclude_output_inserted = exclude_output_inserted_table_name? ( table_name , sql )
285+
286+ if exclude_output_inserted
287+ pk_and_types = Array ( pk ) . map do |subkey |
288+ {
289+ quoted : SQLServer ::Utils . extract_identifiers ( subkey ) . quoted ,
290+ id_sql_type : exclude_output_inserted_id_sql_type ( subkey , exclude_output_inserted )
291+ }
292+ end
293+
294+ <<~SQL . squish
295+ DECLARE @ssaIdInsertTable table (#{ pk_and_types . map { |pk_and_type | "#{ pk_and_type [ :quoted ] } #{ pk_and_type [ :id_sql_type ] } " } . join ( ", " ) } );
296+ #{ sql . dup . insert sql . index ( / (DEFAULT )?VALUES/i ) , " OUTPUT #{ pk_and_types . map { |pk_and_type | "INSERTED.#{ pk_and_type [ :quoted ] } " } . join ( ", " ) } INTO @ssaIdInsertTable" }
297+ 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
298+ SQL
299+ else
300+ returning_columns = returning || Array ( pk )
301+
302+ if returning_columns . any?
303+ returning_columns_statements = returning_columns . map { |c | " INSERTED.#{ SQLServer ::Utils . extract_identifiers ( c ) . quoted } " }
304+ sql . dup . insert sql . index ( / (DEFAULT )?VALUES/i ) , " OUTPUT" + returning_columns_statements . join ( "," )
305+ else
306+ sql
307+ end
308+ end
309+ else
310+ "#{ sql } ; SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident"
311+ end
313312
314313 [ sql , binds ]
315314 end
316315
317316 # === SQLServer Specific ======================================== #
318317
319318 def set_identity_insert ( table_name , conn , enable )
320- internal_raw_execute ( "SET IDENTITY_INSERT #{ table_name } #{ enable ? 'ON' : ' OFF' } " , conn , perform_do : true )
319+ internal_raw_execute ( "SET IDENTITY_INSERT #{ table_name } #{ enable ? "ON" : " OFF" } " , conn , perform_do : true )
321320 rescue Exception
322- raise ActiveRecordError , "IDENTITY_INSERT could not be turned #{ enable ? 'ON' : ' OFF' } for table #{ table_name } "
321+ raise ActiveRecordError , "IDENTITY_INSERT could not be turned #{ enable ? "ON" : " OFF" } for table #{ table_name } "
323322 end
324323
325324 # === SQLServer Specific (Executing) ============================ #
@@ -350,9 +349,9 @@ def sp_executesql_sql_type(attr)
350349 value = active_model_attribute? ( attr ) ? attr . value_for_database : attr
351350
352351 if value . is_a? ( Numeric )
353- value > 2_147_483_647 ? "bigint" . freeze : "int" . freeze
352+ ( value > 2_147_483_647 ) ? "bigint" : "int"
354353 else
355- "nvarchar(max)" . freeze
354+ "nvarchar(max)"
356355 end
357356 end
358357
@@ -418,7 +417,7 @@ def query_requires_identity_insert?(sql)
418417 raw_table_name = get_raw_table_name ( sql )
419418 id_column = identity_columns ( raw_table_name ) . first
420419
421- 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
420+ ( 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
422421 end
423422
424423 def insert_sql? ( sql )
0 commit comments