Skip to content

Commit e46e495

Browse files
committed
customize case comparision methods to improvie performace
1 parent 6791ca5 commit e46e495

File tree

6 files changed

+48
-2
lines changed

6 files changed

+48
-2
lines changed

activerecord-sqlserver-adapter.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ Gem::Specification.new do |spec|
2424
spec.add_development_dependency 'minitest-spec-rails'
2525
spec.add_development_dependency 'mocha'
2626
spec.add_development_dependency 'nokogiri'
27-
spec.add_development_dependency 'pry'
27+
spec.add_development_dependency 'pry-byebug'
2828
spec.add_development_dependency 'rake'
2929
end

bin/console

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
# Run a Ruby REPL.
3+
4+
set -e
5+
6+
cd $(dirname "$0")/..
7+
PRY_PATH=$(which pry)
8+
echo $PRY_PATH
9+
10+
if [ -x $PRY_PATH ]
11+
then
12+
exec bundle exec $PRY_PATH -Ilib -r activerecord-sqlserver-adapter -r console
13+
else
14+
red='\033[0;31m'
15+
NC='\033[0m'
16+
echo "${red}Pry was not found or not executable. Make sure \`which pry\` returns an executable.${NC}"
17+
18+
fi

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ def case_sensitive_modifier(node, table_attribute)
8787
Arel::Nodes::Bin.new(node)
8888
end
8989

90+
def case_sensitive_comparison(table, attribute, column, value)
91+
if column.case_sensitive?
92+
table[attribute].eq(value)
93+
else
94+
super
95+
end
96+
end
97+
98+
def case_insensitive_comparison(table, attribute, column, value)
99+
if column.case_sensitive?
100+
super
101+
else
102+
table[attribute].eq(value)
103+
end
104+
end
105+
90106
# === SQLServer Specific ======================================== #
91107

92108
def execute_procedure(proc_name, *variables)

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def indexes(table_name, name = nil)
4545
def columns(table_name, _name = nil)
4646
return [] if table_name.blank?
4747
column_definitions(table_name).map do |ci|
48-
sqlserver_options = ci.slice :ordinal_position, :is_primary, :is_identity, :default_function, :table_name
48+
sqlserver_options = ci.slice :ordinal_position, :is_primary, :is_identity, :default_function, :table_name, :collation
4949
cast_type = lookup_cast_type(ci[:type])
5050
new_column ci[:name], ci[:default_value], cast_type, ci[:type], ci[:null], sqlserver_options
5151
end
@@ -233,6 +233,7 @@ def column_definitions(table_name)
233233
columns.NUMERIC_SCALE AS numeric_scale,
234234
columns.NUMERIC_PRECISION AS numeric_precision,
235235
columns.DATETIME_PRECISION AS datetime_precision,
236+
columns.COLLATION_NAME AS collation,
236237
columns.ordinal_position,
237238
CASE
238239
WHEN columns.DATA_TYPE IN ('nchar','nvarchar','char','varchar') THEN columns.CHARACTER_MAXIMUM_LENGTH

lib/active_record/connection_adapters/sqlserver_column.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ def is_real?
4040
@sql_type =~ /real/i
4141
end
4242

43+
def collation
44+
@sqlserver_options[:collation]
45+
end
46+
47+
def case_sensitive?
48+
collation && !collation.match(/_CI/)
49+
end
50+
4351
end
4452
end
4553
end

lib/console.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Pry.config.prompt = lambda do |context, nesting, pry|
2+
"[sqlserver] #{context} > "
3+
end

0 commit comments

Comments
 (0)