Skip to content

Commit 785f2ba

Browse files
committed
Standardrb
1 parent a7b1adb commit 785f2ba

File tree

3 files changed

+64
-63
lines changed

3 files changed

+64
-63
lines changed

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
2222
id_insert_table_name = query_requires_identity_insert?(sql)
2323

2424
result, affected_rows = if id_insert_table_name
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
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
3131

3232
verified!
3333
notification_payload[:affected_rows] = affected_rows
@@ -231,10 +231,10 @@ def merge_insert_values_list(insert:, insert_all:)
231231

232232
def execute_procedure(proc_name, *variables)
233233
vars = if variables.any? && variables.first.is_a?(Hash)
234-
variables.first.map { |k, v| "@#{k} = #{quote(v)}" }
235-
else
236-
variables.map { |v| quote(v) }
237-
end.join(", ")
234+
variables.first.map { |k, v| "@#{k} = #{quote(v)}" }
235+
else
236+
variables.map { |v| quote(v) }
237+
end.join(", ")
238238
sql = "EXEC #{proc_name} #{vars}".strip
239239

240240
log(sql, "Execute Procedure") do |notification_payload|
@@ -340,35 +340,35 @@ def sql_for_insert(sql, pk, binds, returning)
340340
end
341341

342342
sql = if pk && use_output_inserted? && !database_prefix_remote_server?
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
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
355355
DECLARE @ssaIdInsertTable table (#{pk_and_types.map { |pk_and_type| "#{pk_and_type[:quoted]} #{pk_and_type[:id_sql_type]}" }.join(", ")});
356356
#{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/i), " OUTPUT #{pk_and_types.map { |pk_and_type| "INSERTED.#{pk_and_type[:quoted]}" }.join(", ")} INTO @ssaIdInsertTable"}
357357
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
358358
SQL
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
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
372372

373373
[sql, binds]
374374
end
@@ -537,16 +537,16 @@ def build_sql_for_returning(insert:, insert_all:)
537537
return "" unless insert_all.returning
538538

539539
returning_values_sql = if insert_all.returning.is_a?(String)
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
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
550550

551551
" OUTPUT #{returning_values_sql}"
552552
end

test/cases/coerced_tests.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def test_belongs_to_with_primary_key_joins_on_correct_column_coerced
261261
def test_belongs_to_coerced
262262
client = Client.find(3)
263263
first_firm = companies(:first_firm)
264-
assert_queries_and_values_match(/FETCH NEXT @3 ROWS ONLY/, ['Firm', 'Agency', 1, 1]) do
264+
assert_queries_and_values_match(/FETCH NEXT @3 ROWS ONLY/, ["Firm", "Agency", 1, 1]) do
265265
assert_equal first_firm, client.firm
266266
assert_equal first_firm.name, client.firm.name
267267
end
@@ -397,11 +397,11 @@ def test_select_avg_with_group_by_as_virtual_attribute_with_ar_coerced
397397
rails_core = companies(:rails_core)
398398

399399
account = Account
400-
.select(:firm_id, "AVG(CAST(credit_limit AS DECIMAL)) AS avg_credit_limit")
401-
.where(firm: rails_core)
402-
.group(:firm_id)
403-
.order(:firm_id)
404-
.take!
400+
.select(:firm_id, "AVG(CAST(credit_limit AS DECIMAL)) AS avg_credit_limit")
401+
.where(firm: rails_core)
402+
.group(:firm_id)
403+
.order(:firm_id)
404+
.take!
405405

406406
# id was not selected, so it should be nil
407407
# (cannot select id because it wasn't used in the GROUP BY clause)
@@ -448,11 +448,11 @@ def test_select_avg_with_joins_and_group_by_as_virtual_attribute_with_ar_coerced
448448
rails_core = companies(:rails_core)
449449

450450
firm = DependentFirm
451-
.select("companies.*", "AVG(CAST(accounts.credit_limit AS DECIMAL)) AS avg_credit_limit")
452-
.where(id: rails_core)
453-
.joins(:account)
454-
.group(:id, :type, :firm_id, :firm_name, :name, :client_of, :rating, :account_id, :description, :status)
455-
.take!
451+
.select("companies.*", "AVG(CAST(accounts.credit_limit AS DECIMAL)) AS avg_credit_limit")
452+
.where(id: rails_core)
453+
.joins(:account)
454+
.group(:id, :type, :firm_id, :firm_name, :name, :client_of, :rating, :account_id, :description, :status)
455+
.take!
456456

457457
# all the DependentFirm attributes should be present
458458
assert_equal rails_core, firm
@@ -997,7 +997,7 @@ class FinderTest < ActiveRecord::TestCase
997997

998998
# We have implicit ordering, via FETCH.
999999
coerce_tests! %r{doesn't have implicit ordering},
1000-
:test_find_doesnt_have_implicit_ordering
1000+
:test_find_doesnt_have_implicit_ordering
10011001

10021002
# Assert SQL Server limit implementation
10031003
coerce_tests! :test_take_and_first_and_last_with_integer_should_use_sql_limit
@@ -1154,7 +1154,8 @@ def test_implicit_order_column_prepends_query_constraints_coerced
11541154
quoted_color = Regexp.escape(c.quote_table_name("clothing_items.color"))
11551155
quoted_descrption = Regexp.escape(c.quote_table_name("clothing_items.description"))
11561156

1157-
assert_queries_match(/ORDER BY #{quoted_descrption} ASC, #{quoted_type} ASC, #{quoted_color} ASC OFFSET 0 ROWS FETCH NEXT @(\d) ROWS ONLY/i) do assert_kind_of ClothingItem, ClothingItem.first
1157+
assert_queries_match(/ORDER BY #{quoted_descrption} ASC, #{quoted_type} ASC, #{quoted_color} ASC OFFSET 0 ROWS FETCH NEXT @(\d) ROWS ONLY/i) do
1158+
assert_kind_of ClothingItem, ClothingItem.first
11581159
end
11591160
ensure
11601161
ClothingItem.implicit_order_column = nil
@@ -1481,7 +1482,7 @@ def test_reorder_with_first_coerced
14811482
def test_multiple_where_and_having_clauses_coerced
14821483
post = Post.first
14831484
having_then_where = Post.having(id: post.id).where(title: post.title)
1484-
.having(id: post.id).where(title: post.title).group(:id).select(:id)
1485+
.having(id: post.id).where(title: post.title).group(:id).select(:id)
14851486

14861487
assert_equal [post], having_then_where
14871488
end
@@ -2486,7 +2487,7 @@ def test_upsert_all_updates_using_provided_sql_coerced
24862487
ELSE 1
24872488
END
24882489
SQL
2489-
)
2490+
)
24902491
)
24912492

24922493
assert_equal "published", Book.find(1).status

test/support/query_assertions.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def assert_queries_count(count = nil, include_schema: false, &block)
2222
end
2323
end
2424

25-
def assert_queries_and_values_match(match, bound_values=[], count: nil, &block)
25+
def assert_queries_and_values_match(match, bound_values = [], count: nil, &block)
2626
ActiveRecord::Base.lease_connection.materialize_transactions
2727

2828
counter = ActiveRecord::Assertions::QueryAssertions::SQLCounter.new
@@ -35,9 +35,9 @@ def assert_queries_and_values_match(match, bound_values=[], count: nil, &block)
3535
end
3636

3737
if count
38-
assert_equal count, matched_queries.size, "#{matched_queries.size} instead of #{count} queries were executed.#{count.log.empty? ? '' : "\nQueries:\n#{counter.log.join("\n")}"}"
38+
assert_equal count, matched_queries.size, "#{matched_queries.size} instead of #{count} queries were executed.#{"\nQueries:\n#{counter.log.join("\n")}" unless count.log.empty?}"
3939
else
40-
assert_operator matched_queries.size, :>=, 1, "1 or more queries expected, but none were executed.#{counter.log.empty? ? '' : "\nQueries:\n#{counter.log.join("\n")}"}"
40+
assert_operator matched_queries.size, :>=, 1, "1 or more queries expected, but none were executed.#{"\nQueries:\n#{counter.log.join("\n")}" unless counter.log.empty?}"
4141
end
4242

4343
result

0 commit comments

Comments
 (0)