Skip to content

Commit 8cbdac4

Browse files
committed
Simplify encoding support.
1 parent 4743909 commit 8cbdac4

File tree

6 files changed

+22
-49
lines changed

6 files changed

+22
-49
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
* master *
22

3+
* Simplify encoding support. [Ken Collins]
4+
35
* Add binary timestamp datatype handling. [Erik Bryn]
46

7+
58
* 3.0.3
69

710
* Add TinyTDS/dblib connection mode. [Ken Collins]

lib/active_record/connection_adapters/sqlserver/quoting.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ def quote(value, column = nil)
1010
when String, ActiveSupport::Multibyte::Chars
1111
if column && column.type == :binary
1212
column.class.string_to_binary(value)
13-
elsif quote_value_as_utf8?(value) || column && column.respond_to?(:is_utf8?) && column.is_utf8?
14-
quoted_utf8_value(value)
13+
elsif value.is_utf8? || (column && column.type == :string)
14+
"N'#{quote_string(value)}'"
1515
else
1616
super
1717
end
@@ -48,14 +48,6 @@ def quoted_date(value)
4848
super
4949
end
5050
end
51-
52-
def quoted_utf8_value(value)
53-
"N'#{quote_string(value)}'"
54-
end
55-
56-
def quote_value_as_utf8?(value)
57-
value.is_utf8? || enable_default_unicode_types
58-
end
5951

6052
end
6153
end

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,38 +68,16 @@ def initialize(name, default, sql_type = nil, null = true, sqlserver_options = {
6868

6969
class << self
7070

71-
def string_to_utf8_encoding(value)
72-
value.force_encoding('UTF-8') rescue value
73-
end
74-
7571
def string_to_binary(value)
76-
value = value.dup.force_encoding(Encoding::BINARY) if value.respond_to?(:force_encoding)
7772
"0x#{value.unpack("H*")[0]}"
7873
end
7974

8075
def binary_to_string(value)
81-
value = value.dup.force_encoding(Encoding::BINARY) if value.respond_to?(:force_encoding)
8276
value =~ /[^[:xdigit:]]/ ? value : [value].pack('H*')
8377
end
8478

8579
end
8680

87-
def type_cast(value)
88-
if value && type == :string && is_utf8?
89-
self.class.string_to_utf8_encoding(value)
90-
else
91-
super
92-
end
93-
end
94-
95-
def type_cast_code(var_name)
96-
if type == :string && is_utf8?
97-
"#{self.class.name}.string_to_utf8_encoding(#{var_name})"
98-
else
99-
super
100-
end
101-
end
102-
10381
def is_identity?
10482
@sqlserver_options[:is_identity]
10583
end

test/cases/finder_test_sqlserver.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@ class FinderTest < ActiveRecord::TestCase
1111
include SqlserverCoercedTest
1212

1313
def test_coerced_string_sanitation
14-
assert_not_equal "'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1")
15-
if quote_values_as_utf8?
16-
assert_equal "N'something; select table'", ActiveRecord::Base.sanitize("something; select table")
17-
else
18-
assert_equal "'something; select table'", ActiveRecord::Base.sanitize("something; select table")
19-
end
14+
assert_not_equal "N'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1")
15+
assert_equal "N'something; select table'", ActiveRecord::Base.sanitize("something; select table")
2016
end
2117

2218
end

test/cases/sqlserver_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ class TestCase < ActiveSupport::TestCase
9595
class << self
9696
def connection_mode_dblib? ; ActiveRecord::Base.connection.instance_variable_get(:@connection_options)[:mode] == :dblib ; end
9797
def connection_mode_odbc? ; ActiveRecord::Base.connection.instance_variable_get(:@connection_options)[:mode] == :odbc ; end
98+
def connection_mode_adonet? ; ActiveRecord::Base.connection.instance_variable_get(:@connection_options)[:mode] == :adonet ; end
9899
def sqlserver_2005? ; ActiveRecord::Base.connection.sqlserver_2005? ; end
99100
def sqlserver_2008? ; ActiveRecord::Base.connection.sqlserver_2008? ; end
100101
def ruby_19? ; RUBY_VERSION >= '1.9' ; end
101-
def quote_values_as_utf8? ; ActiveRecord::Base.connection.quote_value_as_utf8?('') ; end
102102
end
103103
def assert_sql(*patterns_to_match)
104104
$queries_executed = []
@@ -112,10 +112,10 @@ def assert_sql(*patterns_to_match)
112112
end
113113
def connection_mode_dblib? ; self.class.connection_mode_dblib? ; end
114114
def connection_mode_odbc? ; self.class.connection_mode_odbc? ; end
115+
def connection_mode_adonet? ; self.class.connection_mode_adonet? ; end
115116
def sqlserver_2005? ; self.class.sqlserver_2005? ; end
116117
def sqlserver_2008? ; self.class.sqlserver_2008? ; end
117118
def ruby_19? ; self.class.ruby_19? ; end
118-
def quote_values_as_utf8? ; self.class.quote_values_as_utf8? ; end
119119
def with_enable_default_unicode_types?
120120
ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_default_unicode_types.is_a?(TrueClass)
121121
end

test/cases/unicode_test_sqlserver.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# encoding: UTF-8
12
require 'cases/sqlserver_helper'
23

34
class UnicodeTestSqlserver < ActiveRecord::TestCase
@@ -29,19 +30,22 @@ class UnicodeTestSqlserver < ActiveRecord::TestCase
2930
context 'Testing unicode data' do
3031

3132
setup do
32-
@unicode_data = "\344\270\200\344\272\21434\344\272\224\345\205\255"
33-
@encoded_unicode_data = "\344\270\200\344\272\21434\344\272\224\345\205\255".force_encoding('UTF-8') if ruby_19?
33+
@unicode_data = "\344\270\200\344\272\21434\344\272\224\345\205\255" # "一二34五六"
3434
end
3535

36-
should 'insert into nvarchar field' do
36+
should 'insert and retrieve unicode data' do
3737
assert data = SqlServerUnicode.create!(:nvarchar => @unicode_data)
38-
assert_equal @unicode_data, data.reload.nvarchar
38+
if connection_mode_dblib?
39+
assert_equal "一二34五六", data.reload.nvarchar
40+
elsif connection_mode_odbc?
41+
assert_equal "一二34五六", data.reload.nvarchar, 'perhaps you are not using the utf8 odbc that does this legwork'
42+
elsif connection_mode_adonet?
43+
assert_equal "一二34五六", data.reload.nvarchar
44+
else
45+
raise 'need to add a case for this'
46+
end
47+
assert_equal Encoding.find('UTF-8'), data.nvarchar.encoding if ruby_19?
3948
end
40-
41-
should 're-encode data on DB reads' do
42-
assert data = SqlServerUnicode.create!(:nvarchar => @unicode_data)
43-
assert_equal @encoded_unicode_data, data.reload.nvarchar
44-
end if ruby_19?
4549

4650
end
4751

0 commit comments

Comments
 (0)