Skip to content

Commit 5a7a3a4

Browse files
committed
Standard.rb automated fixes
1 parent 01239aa commit 5a7a3a4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1088
-970
lines changed

Gemfile

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
77
gemspec
88

99
gem "bcrypt"
10-
gem "pg", ">= 0.18.0"
10+
gem "pg", ">= 0.18.0"
1111
gem "sqlite3", ">= 1.6.6"
1212
gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]
1313
gem "benchmark-ips"
@@ -28,13 +28,13 @@ else
2828
require "yaml"
2929

3030
spec = eval(File.read("activerecord-sqlserver-adapter.gemspec"))
31-
ver = spec.dependencies.detect { |d| d.name == "activerecord" }.requirement.requirements.first.last.version
31+
ver = spec.dependencies.detect { |d| d.name == "activerecord" }.requirement.requirements.first.last.version
3232
major, minor, _tiny, pre = ver.split(".")
3333

3434
if pre
3535
ver
3636
else
37-
uri = URI.parse("https://rubygems.org/api/v1/versions/activerecord.yaml")
37+
uri = URI.parse("https://rubygems.org/api/v1/versions/activerecord.yaml")
3838
http = Net::HTTP.new(uri.host, uri.port)
3939
http.use_ssl = true
4040
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
@@ -68,9 +68,7 @@ end
6868
group :guard do
6969
gem "guard"
7070
gem "guard-minitest"
71-
gem "terminal-notifier-guard" if RbConfig::CONFIG["host_os"] =~ /darwin/
71+
gem "terminal-notifier-guard" if /darwin/.match?(RbConfig::CONFIG["host_os"])
7272
end
7373

74-
group :rubocop do
75-
gem "rubocop", require: false
76-
end
74+
gem "standard", require: false

Guardfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ clearing :on
66
notification :terminal_notifier if defined?(TerminalNotifier)
77
ignore %r{debug\.log}
88

9-
ar_lib = File.join ARTest::SQLServer.root_activerecord, "lib"
9+
ar_lib = File.join ARTest::SQLServer.root_activerecord, "lib"
1010
ar_test = File.join ARTest::SQLServer.root_activerecord, "test"
1111

1212
guard :minitest, {
@@ -24,7 +24,7 @@ guard :minitest, {
2424
else
2525
watch(%r{^test/cases/\w+_test_sqlserver\.rb$})
2626
watch(%r{^test/cases/coerced_tests\.rb$}) { "test/cases/coerced_tests.rb" }
27-
watch(%r{^lib/active_record/connection_adapters/sqlserver/([^/]+)\.rb$}) { |m| "test/cases/#{m[1]}_test_sqlserver.rb" }
27+
watch(%r{^lib/active_record/connection_adapters/sqlserver/([^/]+)\.rb$}) { |m| "test/cases/#{m[1]}_test_sqlserver.rb" }
2828
watch(%r{^test/cases/helper_sqlserver\.rb$}) { "test" }
2929
end
3030
end

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ task default: [:test]
1111
namespace :test do
1212
ENV["ARCONN"] = "sqlserver"
1313

14-
%w(dblib).each do |mode|
14+
%w[dblib].each do |mode|
1515
Rake::TestTask.new(mode) do |t|
1616
t.libs = ARTest::SQLServer.test_load_paths
1717
t.test_files = test_files

activerecord-sqlserver-adapter.gemspec

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,27 @@
33
version = File.read(File.expand_path("VERSION", __dir__)).strip
44

55
Gem::Specification.new do |spec|
6-
spec.name = "activerecord-sqlserver-adapter"
7-
spec.platform = Gem::Platform::RUBY
8-
spec.version = version
6+
spec.name = "activerecord-sqlserver-adapter"
7+
spec.platform = Gem::Platform::RUBY
8+
spec.version = version
99

1010
spec.required_ruby_version = ">= 3.2.0"
1111

12-
spec.license = "MIT"
13-
spec.authors = ["Ken Collins", "Anna Carey", "Will Bond", "Murray Steele", "Shawn Balestracci", "Joe Rafaniello", "Tom Ward", "Aidan Haran"]
14-
15-
spec.homepage = "http://github.com/rails-sqlserver/activerecord-sqlserver-adapter"
16-
spec.summary = "ActiveRecord SQL Server Adapter."
17-
spec.description = "ActiveRecord SQL Server Adapter. SQL Server 2012 and upward."
12+
spec.license = "MIT"
13+
spec.authors = ["Ken Collins", "Anna Carey", "Will Bond", "Murray Steele", "Shawn Balestracci", "Joe Rafaniello", "Tom Ward", "Aidan Haran"]
14+
15+
spec.homepage = "http://github.com/rails-sqlserver/activerecord-sqlserver-adapter"
16+
spec.summary = "ActiveRecord SQL Server Adapter."
17+
spec.description = "ActiveRecord SQL Server Adapter. SQL Server 2012 and upward."
1818

19-
spec.metadata = {
19+
spec.metadata = {
2020
"bug_tracker_uri" => "https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues",
2121
"changelog_uri" => "https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/v#{version}/CHANGELOG.md",
22-
"source_code_uri" => "https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/v#{version}",
22+
"source_code_uri" => "https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/v#{version}"
2323
}
2424

25-
spec.files = `git ls-files -z`.split("\x0")
26-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
27-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
25+
spec.files = `git ls-files -z`.split("\x0")
26+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
2827
spec.require_paths = ["lib"]
2928

3029
spec.add_dependency "activerecord", "~> 8.1.0.alpha"

lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ def unprepare_sqlserver_statement(sql, binds)
3232
executesql = executesql.match(SQLSERVER_STATEMENT_REGEXP).to_a[1]
3333

3434
binds.each_with_index do |bind, index|
35-
36-
value = if bind.is_a?(::ActiveModel::Attribute) then
35+
value = if bind.is_a?(::ActiveModel::Attribute)
3736
connection.quote(bind.value_for_database)
3837
else
3938
connection.quote(bind)

lib/active_record/connection_adapters/sqlserver/core_ext/finder_methods.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ def construct_relation_for_exists(conditions)
2424
def _construct_relation_for_exists(conditions)
2525
conditions = sanitize_forbidden_attributes(conditions)
2626

27-
if distinct_value && offset_value
27+
relation = if distinct_value && offset_value
2828
# Start of monkey-patch
2929
if select_values.present?
30-
relation = order(*select_values).limit!(1)
30+
order(*select_values).limit!(1)
3131
else
32-
relation = except(:order).limit!(1)
32+
except(:order).limit!(1)
3333
end
3434
# End of monkey-patch
3535
else
36-
relation = except(:select, :distinct, :order)._select!(Arel.sql(::ActiveRecord::FinderMethods::ONE_AS_ONE, retryable: true)).limit!(1)
36+
except(:select, :distinct, :order)._select!(Arel.sql(::ActiveRecord::FinderMethods::ONE_AS_ONE, retryable: true)).limit!(1)
3737
end
3838

3939
case conditions

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

lib/active_record/connection_adapters/sqlserver/database_tasks.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,19 @@ def collation
3232
private
3333

3434
def create_database_options(options = {})
35-
keys = [:collate]
35+
keys = [:collate]
3636
copts = @connection_parameters
37-
options = {
37+
{
3838
collate: copts[:collation]
3939
}.merge(options.symbolize_keys).select { |_, v|
4040
v.present?
4141
}.slice(*keys).map { |k, v|
4242
"#{k.to_s.upcase} #{v}"
4343
}.join(" ")
44-
options
4544
end
4645

4746
def create_database_edition_options(options = {})
48-
keys = [:maxsize, :edition, :service_objective]
47+
keys = [:maxsize, :edition, :service_objective]
4948
copts = @connection_parameters
5049
edition_options = {
5150
maxsize: copts[:azure_maxsize],

0 commit comments

Comments
 (0)