Skip to content

Commit d9fe27e

Browse files
committed
Tested ruby 1.9, ruby-odbc 0.9996, and DBI 0.4.1. Also added correct support for UTF-8 character encoding going in and out of the DB. See before gist http://gist.github.com/111709 and after gist http://gist.github.com/111719
1 parent d6be139 commit d9fe27e

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

CHANGELOG

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11

22
MASTER
33

4-
*
4+
* Tested ruby 1.9, ruby-odbc 0.9996, and DBI 0.4.1. Also added correct support for UTF-8 character
5+
encoding going in and out of the DB. See before gist http://gist.github.com/111709 and after gist
6+
http://gist.github.com/111719
57

68

79
* 2.2.16 * (April 21st, 2009)

README.rdoc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The SQL Server adapter for rails is back for ActiveRecord 2.2 and up! We are cur
66

77
== What's New
88

9+
* Fully tested under 1.9!!! Correctly encodes/decodes UTF-8 types in ruby 1.9 too.
910
* Now supports both rails 2.2 & 2.3!!!
1011
* An ActiveRecord::Base.execute_procedure method that can be used by classes.
1112
* Enabled support for DDL transactions.
@@ -119,11 +120,11 @@ It is our goal to match the adapter version with each version of rails. However
119120

120121
== Installation
121122

122-
First, you will need Ruby DBI and Ruby ODBC. To my knowledge the ADO DBD for DBI is no longer supported. The installation below is not a comprehensive walk thru on how to get all the required moving parts like FreeTDS installed and/or configured. It will also assume gem installations of both the dependent libraries and the adapter itself.
123+
First, you will need Ruby DBI and Ruby ODBC. If you are using the adapter under 1.9, then you need at least ruby-odbc version 0.9996. To my knowledge the ADO DBD for DBI is no longer supported. The installation below is not a comprehensive walk thru on how to get all the required moving parts like FreeTDS installed and/or configured. It will also assume gem installations of both the dependent libraries and the adapter itself.
123124

124-
It should be noted that this version of the adapter was developed using both the ancient 0.0.23 version of DBI up to the current stable release of 0.4.0. Because later versions of DBI will be changing many things, IT IS HIGHLY RECOMMENDED that you max your install to version 0.4.0 which the examples below show. For the time being we are not supporting DBI versions higher than 0.4.0. The good news is that if you were using a very old DBI with ADO, technically this adapter will still work for you, but be warned your path is getting old and may not be supported for long.
125+
It should be noted that this version of the adapter was developed using both the ancient 0.0.23 version of DBI up to the current stable release of 0.4.1. Note that DBI 0.4.1 is the minimal for ruby 1.9 compatibility. Because later versions of DBI will be changing many things, IT IS HIGHLY RECOMMENDED that you max your install to version 0.4.0 which the examples below show. For the time being we are not supporting DBI versions higher than 0.4.0. The good news is that if you were using a very old DBI with ADO, technically this adapter will still work for you, but be warned your path is getting old and may not be supported for long.
125126

126-
$ gem install dbi --version 0.4.0
127+
$ gem install dbi --version 0.4.1
127128
$ gem install dbd-odbc --version 0.2.4
128129
$ gem install rails-sqlserver-2000-2005-adapter -s http://gems.github.com
129130

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def initialize(name, default, sql_type = nil, null = true, sqlserver_options = {
4949

5050
class << self
5151

52+
def string_to_utf8_encoding(value)
53+
value.force_encoding('UTF-8') rescue value
54+
end
55+
5256
def string_to_binary(value)
5357
"0x#{value.unpack("H*")[0]}"
5458
end
@@ -59,6 +63,22 @@ def binary_to_string(value)
5963

6064
end
6165

66+
def type_cast(value)
67+
if value && type == :string && is_utf8?
68+
self.class.string_to_utf8_encoding(value)
69+
else
70+
super
71+
end
72+
end
73+
74+
def type_cast_code(var_name)
75+
if type == :string && is_utf8?
76+
"#{self.class.name}.string_to_utf8_encoding(#{var_name})"
77+
else
78+
super
79+
end
80+
end
81+
6282
def is_identity?
6383
@sqlserver_options[:is_identity]
6484
end

test/cases/sqlserver_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def sqlserver_2000? ; ActiveRecord::Base.connection.sqlserver_2000? ; end
9898
def sqlserver_2005? ; ActiveRecord::Base.connection.sqlserver_2005? ; end
9999
def active_record_2_point_2? ; ActiveRecord::VERSION::MAJOR == 2 && ActiveRecord::VERSION::MINOR == 2 ; end
100100
def active_record_2_point_3? ; ActiveRecord::VERSION::MAJOR == 2 && ActiveRecord::VERSION::MINOR == 3 ; end
101+
def ruby_19? ; RUBY_VERSION >= '1.9' ; end
101102
end
102103
def assert_sql(*patterns_to_match)
103104
$queries_executed = []
@@ -113,6 +114,7 @@ def sqlserver_2000? ; self.class.sqlserver_2000? ; end
113114
def sqlserver_2005? ; self.class.sqlserver_2005? ; end
114115
def active_record_2_point_2? ; self.class.active_record_2_point_2? ; end
115116
def active_record_2_point_3? ; self.class.active_record_2_point_3? ; end
117+
def ruby_19? ; self.class.ruby_19? ; end
116118
end
117119
end
118120

test/cases/unicode_test_sqlserver.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# encoding: utf-8
21
require 'cases/sqlserver_helper'
32

43
class UnicodeTestSqlserver < ActiveRecord::TestCase
@@ -30,13 +29,19 @@ class UnicodeTestSqlserver < ActiveRecord::TestCase
3029
context 'Testing unicode data' do
3130

3231
setup do
33-
@unicode_data = "一二34五六"
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?
3434
end
3535

3636
should 'insert into nvarchar field' do
3737
assert data = SqlServerUnicode.create!(:nvarchar => @unicode_data)
3838
assert_equal @unicode_data, data.reload.nvarchar
3939
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?
4045

4146
end
4247

0 commit comments

Comments
 (0)