Skip to content

Commit 5e9dc66

Browse files
ebrynmetaskills
authored andcommitted
Removed DBI dependency
1 parent e81eb75 commit 5e9dc66

File tree

5 files changed

+73
-169
lines changed

5 files changed

+73
-169
lines changed

activerecord-sqlserver-adapter.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = "activerecord-sqlserver-adapter"
3-
s.version = "2.3.1"
3+
s.version = "2.3.2.mmi"
44
s.date = "2009-09-10"
55
s.summary = "SQL Server 2000, 2005 and 2008 Adapter For Rails."
66
s.email = "ken@metaskills.net"
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
2121
"lib/activerecord-sqlserver-adapter.rb",
2222
"lib/active_record/connection_adapters/sqlserver_adapter.rb",
2323
"lib/active_record/connection_adapters/sqlserver_adapter/core_ext/active_record.rb",
24-
"lib/active_record/connection_adapters/sqlserver_adapter/core_ext/dbi.rb" ]
24+
"lib/active_record/connection_adapters/sqlserver_adapter/core_ext/odbc.rb" ]
2525
s.test_files = [
2626
"test/cases/aaaa_create_tables_test_sqlserver.rb",
2727
"test/cases/adapter_test_sqlserver.rb",

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'active_record/connection_adapters/abstract_adapter'
2-
require_library_or_gem 'dbi' unless defined?(DBI)
3-
require 'active_record/connection_adapters/sqlserver_adapter/core_ext/dbi'
2+
require_library_or_gem 'odbc' unless defined?(ODBC)
3+
require File.dirname(__FILE__)+'/sqlserver_adapter/core_ext/odbc'
44
require 'active_record/connection_adapters/sqlserver_adapter/core_ext/active_record'
55
require 'base64'
66

@@ -10,20 +10,11 @@ class Base
1010

1111
def self.sqlserver_connection(config) #:nodoc:
1212
config.symbolize_keys!
13-
mode = config[:mode] ? config[:mode].to_s.upcase : 'ADO'
1413
username = config[:username] ? config[:username].to_s : 'sa'
1514
password = config[:password] ? config[:password].to_s : ''
16-
if mode == "ODBC"
17-
raise ArgumentError, "Missing DSN. Argument ':dsn' must be set in order for this adapter to work." unless config.has_key?(:dsn)
18-
dsn = config[:dsn]
19-
driver_url = "DBI:ODBC:#{dsn}"
20-
else
21-
raise ArgumentError, "Missing Database. Argument ':database' must be set in order for this adapter to work." unless config.has_key?(:database)
22-
database = config[:database]
23-
host = config[:host] ? config[:host].to_s : 'localhost'
24-
driver_url = "DBI:ADO:Provider=SQLOLEDB;Data Source=#{host};Initial Catalog=#{database};User ID=#{username};Password=#{password};"
25-
end
26-
ConnectionAdapters::SQLServerAdapter.new(logger, [driver_url, username, password])
15+
raise ArgumentError, "Missing DSN. Argument ':dsn' must be set in order for this adapter to work." unless config.has_key?(:dsn)
16+
dsn = config[:dsn]
17+
ConnectionAdapters::SQLServerAdapter.new(logger, [dsn, username, password])
2718
end
2819

2920
protected
@@ -147,53 +138,28 @@ def simplified_datetime
147138

148139
end #SQLServerColumn
149140

150-
# In ADO mode, this adapter will ONLY work on Windows systems, since it relies on
151-
# Win32OLE, which, to my knowledge, is only available on Windows.
152-
#
153-
# This mode also relies on the ADO support in the DBI module. If you are using the
154-
# one-click installer of Ruby, then you already have DBI installed, but the ADO module
155-
# is *NOT* installed. You will need to get the latest source distribution of Ruby-DBI
156-
# from http://ruby-dbi.sourceforge.net/ unzip it, and copy the file from
157-
# <tt>src/lib/dbd_ado/ADO.rb</tt> to <tt>X:/Ruby/lib/ruby/site_ruby/1.8/DBD/ADO/ADO.rb</tt>
158-
#
159-
# You will more than likely need to create the ADO directory. Once you've installed
160-
# that file, you are ready to go.
161-
#
162141
# In ODBC mode, the adapter requires the ODBC support in the DBI module which requires
163142
# the Ruby ODBC module. Ruby ODBC 0.996 was used in development and testing,
164143
# and it is available at http://www.ch-werner.de/rubyodbc/
165144
#
166145
# Options:
167146
#
168-
# * <tt>:mode</tt> -- ADO or ODBC. Defaults to ADO.
169147
# * <tt>:username</tt> -- Defaults to sa.
170148
# * <tt>:password</tt> -- Defaults to empty string.
171-
# * <tt>:windows_auth</tt> -- Defaults to "User ID=#{username};Password=#{password}"
172-
#
173-
# ADO specific options:
174-
#
175-
# * <tt>:host</tt> -- Defaults to localhost.
176-
# * <tt>:database</tt> -- The name of the database. No default, must be provided.
177-
# * <tt>:windows_auth</tt> -- Use windows authentication instead of username/password.
178-
#
179-
# ODBC specific options:
180-
#
181149
# * <tt>:dsn</tt> -- Defaults to nothing.
182150
#
183151
class SQLServerAdapter < AbstractAdapter
184152

185153
ADAPTER_NAME = 'SQLServer'.freeze
186-
VERSION = '2.3'.freeze
154+
VERSION = '2.4-mmi'.freeze
187155
DATABASE_VERSION_REGEXP = /Microsoft SQL Server\s+(\d{4})/
188156
SUPPORTED_VERSIONS = [2000,2005,2008].freeze
189157
LIMITABLE_TYPES = ['string','integer','float','char','nchar','varchar','nvarchar'].freeze
190158

191-
LOST_CONNECTION_EXCEPTIONS = [DBI::DatabaseError, DBI::InterfaceError]
159+
LOST_CONNECTION_EXCEPTIONS = [ODBC::Error] #[DBI::DatabaseError, DBI::InterfaceError]
192160
LOST_CONNECTION_MESSAGES = [
193-
'Communication link failure',
194-
'Read from the server failed',
195-
'Write to the server failed',
196-
'Database connection was already closed']
161+
'Invalid handle'
162+
]
197163

198164
cattr_accessor :native_text_database_type, :native_binary_database_type, :native_string_database_type,
199165
:log_info_schema_queries, :enable_default_unicode_types, :auto_connect
@@ -349,7 +315,7 @@ def disable_referential_integrity(&block)
349315
# CONNECTION MANAGEMENT ====================================#
350316

351317
def active?
352-
raw_connection.execute("SELECT 1").finish
318+
raw_connection.run("SELECT 1").drop
353319
true
354320
rescue *LOST_CONNECTION_EXCEPTIONS
355321
false
@@ -366,7 +332,7 @@ def disconnect!
366332
end
367333

368334
def finish_statement_handle(handle)
369-
handle.finish if handle && handle.respond_to?(:finish) && !handle.finished?
335+
handle.drop if handle && handle.respond_to?(:drop) && !handle.finished?
370336
handle
371337
end
372338

@@ -771,15 +737,15 @@ def remove_database_connections_and_rollback(name)
771737
# CONNECTION MANAGEMENT ====================================#
772738

773739
def connect
774-
driver_url, username, password = @connection_options
775-
@connection = DBI.connect(driver_url, username, password)
740+
dsn, username, password = @connection_options
741+
@connection = ODBC.connect(dsn, username, password)
776742
configure_connection
777743
rescue
778744
raise unless @auto_connecting
779745
end
780746

781747
def configure_connection
782-
raw_connection['AutoCommit'] = true
748+
# raw_connection['AutoCommit'] = true
783749
end
784750

785751
def with_auto_reconnect
@@ -839,9 +805,9 @@ def info_schema_query
839805
def raw_execute(sql, name = nil, &block)
840806
log(sql, name) do
841807
if block_given?
842-
with_auto_reconnect { raw_connection.execute(sql) { |handle| yield(handle) } }
808+
with_auto_reconnect { raw_connection.run_block(sql) { |handle| yield(handle) } }
843809
else
844-
with_auto_reconnect { raw_connection.execute(sql) }
810+
with_auto_reconnect { raw_connection.run(sql) }
845811
end
846812
end
847813
end
@@ -861,12 +827,12 @@ def do_execute(sql,name=nil)
861827

862828
def raw_select(sql, name = nil)
863829
handle = raw_execute(sql,name)
864-
fields = handle.column_names
830+
fields = handle.columns(true).map{|c|c.name}
865831
results = handle_as_array(handle)
866832
rows = results.inject([]) do |rows,row|
867833
row.each_with_index do |value, i|
868834
# DEPRECATED in DBI 0.4.0 and above. Remove when 0.2.2 and lower is no longer supported.
869-
if value.is_a? DBI::Timestamp
835+
if value.is_a? ODBC::TimeStamp
870836
row[i] = value.to_sqlserver_string
871837
end
872838
end

lib/active_record/connection_adapters/sqlserver_adapter/core_ext/dbi.rb

Lines changed: 0 additions & 106 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
module ActiveRecord
3+
module ConnectionAdapters
4+
module SQLServerCoreExtensions
5+
module ODBC
6+
module TimeStamp
7+
def to_sqlserver_string
8+
date, time, nanoseconds = to_s.split(' ')
9+
"#{date} #{time}.#{sprintf("%03d",nanoseconds.to_i/1000000)}"
10+
end
11+
end
12+
13+
module Statement
14+
def finished?
15+
begin
16+
connected?
17+
false
18+
rescue ::ODBC::Error => e
19+
true
20+
end
21+
end
22+
23+
end
24+
25+
module Database
26+
def run_block(*args)
27+
yield sth = run(*args)
28+
sth.drop
29+
end
30+
end
31+
end
32+
end
33+
end
34+
end
35+
36+
if defined?(ODBC::TimeStamp)
37+
ODBC::TimeStamp.send :include, ActiveRecord::ConnectionAdapters::SQLServerCoreExtensions::ODBC::TimeStamp
38+
end
39+
if defined?(ODBC::Statement)
40+
ODBC::Statement.send :include, ActiveRecord::ConnectionAdapters::SQLServerCoreExtensions::ODBC::Statement
41+
end
42+
if defined?(ODBC::Database)
43+
ODBC::Database.send :include, ActiveRecord::ConnectionAdapters::SQLServerCoreExtensions::ODBC::Database
44+
end

test/cases/connection_test_sqlserver.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ def setup
1212
end
1313

1414

15-
should 'return finished DBI statment handle from #execute without block' do
15+
should 'return finished ODBC statment handle from #execute without block' do
1616
handle = @connection.execute('SELECT * FROM [topics]')
17-
assert_instance_of DBI::StatementHandle, handle
17+
assert_instance_of ODBC::Statement, handle
1818
assert handle.finished?
1919
end
2020

21-
should 'finish DBI statment handle from #execute with block' do
21+
should 'finish ODBC statment handle from #execute with block' do
2222
assert_all_statements_used_are_closed do
2323
@connection.execute('SELECT * FROM [topics]') { }
2424
end
2525
end
2626

27-
should 'return an unfinished DBI statement handler from #raw_execute' do
27+
should 'return an unfinished ODBC statement handler from #raw_execute' do
2828
handle = @connection.send(:raw_execute,'SELECT * FROM [topics]')
29-
assert_instance_of DBI::StatementHandle, handle
29+
assert_instance_of ODBC::Statement, handle
3030
assert !handle.finished?
3131
end
3232

@@ -37,7 +37,7 @@ def setup
3737
end
3838

3939
should 'affect rows' do
40-
assert Topic.connection.instance_variable_get("@connection")["AutoCommit"]
40+
# assert Topic.connection.instance_variable_get("@connection")["AutoCommit"]
4141
topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }
4242
updated = Topic.update(topic_data.keys, topic_data.values)
4343
assert_equal 2, updated.size
@@ -114,15 +114,15 @@ def setup
114114

115115
def assert_all_statements_used_are_closed(&block)
116116
existing_handles = []
117-
ObjectSpace.each_object(DBI::StatementHandle) {|handle| existing_handles << handle}
117+
ObjectSpace.each_object(ODBC::Statement) {|handle| existing_handles << handle}
118118
GC.disable
119119
yield
120120
used_handles = []
121-
ObjectSpace.each_object(DBI::StatementHandle) {|handle| used_handles << handle unless existing_handles.include? handle}
121+
ObjectSpace.each_object(ODBC::Statement) {|handle| used_handles << handle unless existing_handles.include? handle}
122122
assert_block "No statements were used within given block" do
123123
used_handles.size > 0
124124
end
125-
ObjectSpace.each_object(DBI::StatementHandle) do |handle|
125+
ObjectSpace.each_object(ODBC::Statement) do |handle|
126126
assert_block "Statement should have been closed within given block" do
127127
handle.finished?
128128
end

0 commit comments

Comments
 (0)