Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ debug.log
.DS_Store
pkg/
doc/
db/
*.gem
.bundle
Gemfile.lock
Expand Down
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
AllCops:
TargetRubyVersion: 2.5

Style/StringLiterals:
EnforcedStyle: double_quotes
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ group :guard do
gem "guard-minitest"
gem "terminal-notifier-guard" if RbConfig::CONFIG["host_os"] =~ /darwin/
end

group :rubocop do
gem "rubocop", require: false
end
17 changes: 9 additions & 8 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
# frozen_string_literal: true

require_relative 'test/support/paths_sqlserver'
require_relative "test/support/paths_sqlserver"

clearing :on
notification :terminal_notifier if defined?(TerminalNotifier)
ignore %r{debug\.log}

ar_lib = File.join ARTest::SQLServer.root_activerecord, 'lib'
ar_test = File.join ARTest::SQLServer.root_activerecord, 'test'
ar_lib = File.join ARTest::SQLServer.root_activerecord, "lib"
ar_test = File.join ARTest::SQLServer.root_activerecord, "test"

guard :minitest, {
all_on_start: false,
autorun: false,
include: ['lib', 'test', ar_lib, ar_test],
test_folders: ['test'],
include: ["lib", "test", ar_lib, ar_test],
test_folders: ["test"],
test_file_patterns: ["*_test.rb", "*_test_sqlserver.rb"]
} do
# Our project watchers.
if ENV['TEST_FILES']
ENV['TEST_FILES'].split(',').map(&:strip).each do |file|
if ENV["TEST_FILES"]
ENV["TEST_FILES"].split(",").map(&:strip).each do |file|
watch(%r{.*}) { file }
end
else
watch(%r{^test/cases/\w+_test_sqlserver\.rb$})
watch(%r{^test/cases/coerced_tests\.rb$}) { "test/cases/coerced_tests.rb" }
watch(%r{^lib/active_record/connection_adapters/sqlserver/([^/]+)\.rb$}) { |m| "test/cases/#{m[1]}_test_sqlserver.rb" }
watch(%r{^test/cases/helper_sqlserver\.rb$}) { 'test' }
watch(%r{^test/cases/helper_sqlserver\.rb$}) { "test" }
end
end
24 changes: 12 additions & 12 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rake/testtask'
require_relative 'test/support/paths_sqlserver'
require_relative 'test/support/rake_helpers'
require "bundler/gem_tasks"
require "rake/testtask"
require_relative "test/support/paths_sqlserver"
require_relative "test/support/rake_helpers"

task test: ['test:dblib']
task test: ["test:dblib"]
task default: [:test]

namespace :test do
Expand All @@ -15,25 +15,25 @@ namespace :test do
Rake::TestTask.new(mode) do |t|
t.libs = ARTest::SQLServer.test_load_paths
t.test_files = test_files
t.warning = !!ENV['WARNING']
t.warning = !!ENV["WARNING"]
t.verbose = false
end

end

task 'dblib:env' do
ENV['ARCONN'] = 'dblib'
task "dblib:env" do
ENV["ARCONN"] = "dblib"
end

end

task 'test:dblib' => 'test:dblib:env'
task "test:dblib" => "test:dblib:env"

namespace :profile do
['dblib'].each do |mode|
["dblib"].each do |mode|
namespace mode.to_sym do
Dir.glob('test/profile/*_profile_case.rb').sort.each do |test_file|
profile_case = File.basename(test_file).sub('_profile_case.rb', '')
Dir.glob("test/profile/*_profile_case.rb").sort.each do |test_file|
profile_case = File.basename(test_file).sub("_profile_case.rb", "")
Rake::TestTask.new(profile_case) do |t|
t.libs = ARTest::SQLServer.test_load_paths
t.test_files = [test_file]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'active_record/attribute_methods'
require "active_record/attribute_methods"

module ActiveRecord
module ConnectionAdapters
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'active_record/relation'
require 'active_record/version'
require "active_record/relation"
require "active_record/version"

module ActiveRecord
module ConnectionAdapters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module SQLServer
module CoreExt
module Explain

SQLSERVER_STATEMENT_PREFIX = 'EXEC sp_executesql '
SQLSERVER_STATEMENT_PREFIX = "EXEC sp_executesql "
SQLSERVER_STATEMENT_REGEXP = /N'(.+)', N'(.+)', (.+)/

def exec_explain(queries)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'active_record/relation'
require 'active_record/version'
require "active_record/relation"
require "active_record/version"

module ActiveRecord
module ConnectionAdapters
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'active_record/relation'
require 'active_record/version'
require "active_record/relation"
require "active_record/version"

module ActiveRecord
module ConnectionAdapters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def execute(sql, name = nil)
end
end

def exec_query(sql, name = 'SQL', binds = [], prepare: false)
def exec_query(sql, name = "SQL", binds = [], prepare: false)
if preventing_writes? && write_query?(sql)
raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
end
Expand All @@ -44,17 +44,17 @@ def exec_insert(sql, name = nil, binds = [], pk = nil, _sequence_name = nil)
end

def exec_delete(sql, name, binds)
sql = sql.dup << '; SELECT @@ROWCOUNT AS AffectedRows'
sql = sql.dup << "; SELECT @@ROWCOUNT AS AffectedRows"
super(sql, name, binds).rows.first.first
end

def exec_update(sql, name, binds)
sql = sql.dup << '; SELECT @@ROWCOUNT AS AffectedRows'
sql = sql.dup << "; SELECT @@ROWCOUNT AS AffectedRows"
super(sql, name, binds).rows.first.first
end

def begin_db_transaction
do_execute 'BEGIN TRANSACTION'
do_execute "BEGIN TRANSACTION"
end

def transaction_isolation_levels
Expand All @@ -71,11 +71,11 @@ def set_transaction_isolation_level(isolation_level)
end

def commit_db_transaction
do_execute 'COMMIT TRANSACTION'
do_execute "COMMIT TRANSACTION"
end

def exec_rollback_db_transaction
do_execute 'IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION'
do_execute "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION"
end

include Savepoints
Expand Down Expand Up @@ -159,9 +159,9 @@ def execute_procedure(proc_name, *variables)
variables.first.map { |k, v| "@#{k} = #{quote(v)}" }
else
variables.map { |v| quote(v) }
end.join(', ')
end.join(", ")
sql = "EXEC #{proc_name} #{vars}".strip
name = 'Execute Procedure'
name = "Execute Procedure"
log(sql, name) do
case @connection_options[:mode]
when :dblib
Expand Down Expand Up @@ -192,14 +192,14 @@ def use_database(database = nil)

def user_options
return {} if sqlserver_azure?
rows = select_rows('DBCC USEROPTIONS WITH NO_INFOMSGS', 'SCHEMA')
rows = select_rows("DBCC USEROPTIONS WITH NO_INFOMSGS", "SCHEMA")
rows = rows.first if rows.size == 2 && rows.last.empty?
rows.reduce(HashWithIndifferentAccess.new) do |values, row|
if row.instance_of? Hash
set_option = row.values[0].gsub(/\s+/, '_')
set_option = row.values[0].gsub(/\s+/, "_")
user_value = row.values[1]
elsif row.instance_of? Array
set_option = row[0].gsub(/\s+/, '_')
set_option = row[0].gsub(/\s+/, "_")
user_value = row[1]
end
values[set_option] = user_value
Expand All @@ -209,9 +209,9 @@ def user_options

def user_options_dateformat
if sqlserver_azure?
select_value 'SELECT [dateformat] FROM [sys].[syslanguages] WHERE [langid] = @@LANGID', 'SCHEMA'
select_value "SELECT [dateformat] FROM [sys].[syslanguages] WHERE [langid] = @@LANGID", "SCHEMA"
else
user_options['dateformat']
user_options["dateformat"]
end
end

Expand All @@ -226,26 +226,26 @@ def user_options_isolation_level
WHEN 5 THEN 'SNAPSHOT' END AS [isolation_level]
FROM [sys].[dm_exec_sessions]
WHERE [session_id] = @@SPID).squish
select_value sql, 'SCHEMA'
select_value sql, "SCHEMA"
else
user_options['isolation_level']
user_options["isolation_level"]
end
end

def user_options_language
if sqlserver_azure?
select_value 'SELECT @@LANGUAGE AS [language]', 'SCHEMA'
select_value "SELECT @@LANGUAGE AS [language]", "SCHEMA"
else
user_options['language']
user_options["language"]
end
end

def newid_function
select_value 'SELECT NEWID()'
select_value "SELECT NEWID()"
end

def newsequentialid_function
select_value 'SELECT NEWSEQUENTIALID()'
select_value "SELECT NEWSEQUENTIALID()"
end


Expand All @@ -263,7 +263,7 @@ def sql_for_insert(sql, pk, binds)
exclude_output_inserted = exclude_output_inserted_table_name?(table_name, sql)

if exclude_output_inserted
id_sql_type = exclude_output_inserted.is_a?(TrueClass) ? 'bigint' : exclude_output_inserted
id_sql_type = exclude_output_inserted.is_a?(TrueClass) ? "bigint" : exclude_output_inserted
<<~SQL.squish
DECLARE @ssaIdInsertTable table (#{quoted_pk} #{id_sql_type});
#{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk} INTO @ssaIdInsertTable"}
Expand All @@ -288,7 +288,7 @@ def set_identity_insert(table_name, enable = true)

# === SQLServer Specific (Executing) ============================ #

def do_execute(sql, name = 'SQL')
def do_execute(sql, name = "SQL")
materialize_transactions

log(sql, name) { raw_connection_do(sql) }
Expand Down Expand Up @@ -318,9 +318,9 @@ def sp_executesql_sql_type(attr)
return attr.type.sqlserver_type if attr.type.respond_to?(:sqlserver_type)
case value = attr.value_for_database
when Numeric
value > 2_147_483_647 ? 'bigint'.freeze : 'int'.freeze
value > 2_147_483_647 ? "bigint".freeze : "int".freeze
else
'nvarchar(max)'.freeze
"nvarchar(max)".freeze
end
end

Expand All @@ -335,14 +335,14 @@ def sp_executesql_sql_param(attr)
end

def sp_executesql_sql(sql, types, params, name)
if name == 'EXPLAIN'
if name == "EXPLAIN"
params.each.with_index do |param, index|
substitute_at_finder = /(@#{index})(?=(?:[^']|'[^']*')*$)/ # Finds unquoted @n values.
sql = sql.sub substitute_at_finder, param.to_s
end
else
types = quote(types.join(', '))
params = params.map.with_index{ |p, i| "@#{i} = #{p}" }.join(', ') # Only p is needed, but with @i helps explain regexp.
types = quote(types.join(", "))
params = params.map.with_index{ |p, i| "@#{i} = #{p}" }.join(", ") # Only p is needed, but with @i helps explain regexp.
sql = "EXEC sp_executesql #{quote(sql)}"
sql += ", #{types}, #{params}" unless params.empty?
end
Expand All @@ -357,7 +357,7 @@ def raw_connection_do(sql)
# TinyTDS returns false instead of raising an exception if connection fails.
# Getting around this by raising an exception ourselves while this PR
# https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
raise TinyTds::Error, 'failed to execute statement' if result.is_a?(FalseClass)
raise TinyTds::Error, "failed to execute statement" if result.is_a?(FalseClass)

result.do
end
Expand Down Expand Up @@ -407,7 +407,7 @@ def identity_columns(table_name)

# === SQLServer Specific (Selecting) ============================ #

def raw_select(sql, name = 'SQL', binds = [], options = {})
def raw_select(sql, name = "SQL", binds = [], options = {})
log(sql, name, binds) { _raw_select(sql, options) }
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def drop_database(database)
end

def current_database
select_value 'SELECT DB_NAME()'
select_value "SELECT DB_NAME()"
end

def charset
Expand All @@ -41,7 +41,7 @@ def create_database_options(options={})
v.present?
}.slice(*keys).map { |k,v|
"#{k.to_s.upcase} #{v}"
}.join(' ')
}.join(" ")
options
end

Expand All @@ -56,7 +56,7 @@ def create_database_edition_options(options={})
v.present?
}.slice(*keys).map { |k,v|
"#{k.to_s.upcase} = #{v}"
}.join(', ')
}.join(", ")
edition_options = "( #{edition_options} )" if edition_options.present?
edition_options
end
Expand Down
6 changes: 3 additions & 3 deletions lib/active_record/connection_adapters/sqlserver/quoting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module ConnectionAdapters
module SQLServer
module Quoting

QUOTED_TRUE = '1'.freeze
QUOTED_FALSE = '0'.freeze
QUOTED_STRING_PREFIX = 'N'.freeze
QUOTED_TRUE = "1".freeze
QUOTED_FALSE = "0".freeze
QUOTED_STRING_PREFIX = "N".freeze

def fetch_type_metadata(sql_type, sqlserver_options = {})
cast_type = lookup_cast_type(sql_type)
Expand Down
10 changes: 5 additions & 5 deletions lib/active_record/connection_adapters/sqlserver/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ module SQLServer
class SchemaDumper < ConnectionAdapters::SchemaDumper

SQLSEVER_NO_LIMIT_TYPES = [
'text',
'ntext',
'varchar(max)',
'nvarchar(max)',
'varbinary(max)'
"text",
"ntext",
"varchar(max)",
"nvarchar(max)",
"varbinary(max)"
].freeze

private
Expand Down
Loading