@@ -15,15 +15,15 @@ def write_query?(sql) # :nodoc:
15
15
16
16
def perform_query ( raw_connection , sql , binds , type_casted_binds , prepare :, notification_payload :, batch :)
17
17
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 )
20
20
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
27
27
28
28
verified!
29
29
notification_payload [ :affected_rows ] = affected_rows
@@ -41,7 +41,7 @@ def cast_result(raw_result)
41
41
42
42
# Returns the affected rows from results.
43
43
def affected_rows ( raw_result )
44
- raw_result &.first &.fetch ( ' AffectedRows' , nil )
44
+ raw_result &.first &.fetch ( " AffectedRows" , nil )
45
45
end
46
46
47
47
# Returns the affected rows from results or handle.
@@ -66,19 +66,19 @@ def internal_exec_sql_query(sql, conn)
66
66
handle = internal_raw_execute ( sql , conn )
67
67
results = handle_to_names_and_values ( handle , ar_result : true )
68
68
69
- return results , affected_rows_from_results_or_handle ( results , handle )
69
+ [ results , affected_rows_from_results_or_handle ( results , handle ) ]
70
70
ensure
71
71
finish_statement_handle ( handle )
72
72
end
73
73
74
74
def exec_delete ( sql , name = nil , binds = [ ] )
75
75
sql = sql . dup << "; SELECT @@ROWCOUNT AS AffectedRows"
76
- super ( sql , name , binds )
76
+ super
77
77
end
78
78
79
79
def exec_update ( sql , name = nil , binds = [ ] )
80
80
sql = sql . dup << "; SELECT @@ROWCOUNT AS AffectedRows"
81
- super ( sql , name , binds )
81
+ super
82
82
end
83
83
84
84
def begin_db_transaction
@@ -155,14 +155,14 @@ def default_insert_value(column)
155
155
private :default_insert_value
156
156
157
157
def build_insert_sql ( insert ) # :nodoc:
158
- sql = + "INSERT #{ insert . into } "
158
+ sql = "INSERT #{ insert . into } "
159
159
160
160
if returning = insert . send ( :insert_all ) . returning
161
161
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
166
166
sql << " OUTPUT #{ returning_sql } "
167
167
end
168
168
@@ -174,17 +174,17 @@ def build_insert_sql(insert) # :nodoc:
174
174
175
175
def execute_procedure ( proc_name , *variables )
176
176
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 ( ", " )
181
181
sql = "EXEC #{ proc_name } #{ vars } " . strip
182
182
183
183
log ( sql , "Execute Procedure" ) do |notification_payload |
184
184
with_raw_connection do |conn |
185
185
result = internal_raw_execute ( sql , conn )
186
186
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 }
188
188
189
189
result . each ( options ) do |row |
190
190
r = row . with_indifferent_access
@@ -218,7 +218,7 @@ def user_options
218
218
219
219
rows = select_rows ( "DBCC USEROPTIONS WITH NO_INFOMSGS" , "SCHEMA" )
220
220
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 |
222
222
if row . instance_of? Hash
223
223
set_option = row . values [ 0 ] . gsub ( /\s +/ , "_" )
224
224
user_value = row . values [ 1 ]
@@ -227,7 +227,6 @@ def user_options
227
227
user_value = row [ 1 ]
228
228
end
229
229
values [ set_option ] = user_value
230
- values
231
230
end
232
231
end
233
232
@@ -281,45 +280,45 @@ def sql_for_insert(sql, pk, binds, returning)
281
280
end
282
281
283
282
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
313
312
314
313
[ sql , binds ]
315
314
end
316
315
317
316
# === SQLServer Specific ======================================== #
318
317
319
318
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 )
321
320
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 } "
323
322
end
324
323
325
324
# === SQLServer Specific (Executing) ============================ #
@@ -350,9 +349,9 @@ def sp_executesql_sql_type(attr)
350
349
value = active_model_attribute? ( attr ) ? attr . value_for_database : attr
351
350
352
351
if value . is_a? ( Numeric )
353
- value > 2_147_483_647 ? "bigint" . freeze : "int" . freeze
352
+ ( value > 2_147_483_647 ) ? "bigint" : "int"
354
353
else
355
- "nvarchar(max)" . freeze
354
+ "nvarchar(max)"
356
355
end
357
356
end
358
357
@@ -418,7 +417,7 @@ def query_requires_identity_insert?(sql)
418
417
raw_table_name = get_raw_table_name ( sql )
419
418
id_column = identity_columns ( raw_table_name ) . first
420
419
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
422
421
end
423
422
424
423
def insert_sql? ( sql )
0 commit comments