Skip to content

Commit cb11728

Browse files
committed
Merge branch 'master' of git://github.com/h-lame/rails-sqlserver-adapter
2 parents fce645d + 90d7844 commit cb11728

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
# Modifications (Numerous fixes as maintainer): Ryan Tomayko <rtomayko@gmail.com>
2222
# Date: Up to July 2006
2323

24-
# Current maintainer: Tom Ward <tom@popdog.net>
24+
# Previous maintainer: Tom Ward <tom@popdog.net>
25+
#
26+
27+
# Current maintainer: Shawn Balestracci <shawn@vegantech.com>
2528

2629
module ActiveRecord
2730
class Base
@@ -166,13 +169,13 @@ def cast_to_datetime(value)
166169

167170
def cast_to_time(value)
168171
return value if value.is_a?(Time)
169-
time_hash = Date._parse(string)
172+
time_hash = Date._parse(value)
170173
time_hash[:sec_fraction] = 0 # REVISIT: microseconds(time_hash)
171174
new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction)) rescue nil
172175
end
173176

174177
# TODO: Find less hack way to convert DateTime objects into Times
175-
def self.string_to_time(value)
178+
def string_to_time(value)
176179
if value.is_a?(DateTime)
177180
return new_time(value.year, value.mon, value.day, value.hour, value.min, value.sec)
178181
else
@@ -182,11 +185,11 @@ def self.string_to_time(value)
182185

183186
# These methods will only allow the adapter to insert binary data with a length of 7K or less
184187
# because of a SQL Server statement length policy.
185-
def self.string_to_binary(value)
188+
def string_to_binary(value)
186189
Base64.encode64(value)
187190
end
188191

189-
def self.binary_to_string(value)
192+
def binary_to_string(value)
190193
Base64.decode64(value)
191194
end
192195

@@ -196,8 +199,8 @@ def new_time(year, mon, mday, hour, min, sec, microsec = 0)
196199
return nil if year.nil? || year == 0
197200
Time.time_with_datetime_fallback(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
198201
end
199-
end
200-
end
202+
end #class << self
203+
end #SQLServerColumn
201204

202205
# In ADO mode, this adapter will ONLY work on Windows systems,
203206
# since it relies on Win32OLE, which, to my knowledge, is only
@@ -333,8 +336,10 @@ def select_rows(sql, name = nil)
333336

334337
def columns(table_name, name = nil)
335338
return [] if table_name.blank?
336-
table_name = table_name.to_s.split('.')[-1]
339+
table_names = table_name.to_s.split('.')
340+
table_name = table_names[-1]
337341
table_name = table_name.gsub(/[\[\]]/, '')
342+
db_name = "#{table_names[0]}." if table_names.length==3
338343
sql = %{
339344
SELECT
340345
columns.COLUMN_NAME as name,
@@ -358,7 +363,7 @@ def columns(table_name, name = nil)
358363
WHEN COLUMNPROPERTY(OBJECT_ID(columns.TABLE_NAME), columns.COLUMN_NAME, 'IsIdentity') = 0 THEN NULL
359364
ELSE 1
360365
END is_identity
361-
FROM INFORMATION_SCHEMA.COLUMNS columns
366+
FROM #{db_name}INFORMATION_SCHEMA.COLUMNS columns
362367
LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS primary_key_constraints ON (
363368
primary_key_constraints.table_name = columns.table_name
364369
AND primary_key_constraints.constraint_type = 'PRIMARY KEY'
@@ -370,7 +375,6 @@ def columns(table_name, name = nil)
370375
WHERE columns.TABLE_NAME = '#{table_name}'
371376
ORDER BY columns.COLUMN_NAME
372377
}
373-
# ORDER BY columns.ordinal_position
374378
result = select(sql, name, true)
375379
result.collect do |column_info|
376380
# Remove brackets and outer quotes (if quoted) of default value returned by db, i.e:
@@ -463,6 +467,16 @@ def quote_string(string)
463467
string.gsub(/\'/, "''")
464468
end
465469

470+
def quote_table_name(name)
471+
name_split_on_dots = name.to_s.split('.')
472+
if name_split_on_dots.length == 3
473+
"[#{name_split_on_dots[0]}].[#{name_split_on_dots[1]}].[#{name_split_on_dots[2]}]"
474+
else
475+
super(name)
476+
end
477+
478+
end
479+
466480
def quote_column_name(name)
467481
"[#{name}]"
468482
end
@@ -774,7 +788,7 @@ def repair_special_columns(sql)
774788

775789
def get_utf8_columns(table_name)
776790
utf8 = []
777-
@table_columns ||= []
791+
@table_columns ||= {}
778792
@table_columns[table_name] ||= columns(table_name)
779793
@table_columns[table_name].each do |col|
780794
utf8 << col.name if col.is_utf8
@@ -791,7 +805,7 @@ def set_utf8_values!(sql)
791805
elsif sql =~ /^\s*INSERT/i
792806
# TODO This code should be simplified
793807
# Get columns and values, split them into arrays, and store the original_values for when we need to replace them
794-
columns_and_values = sql.scan(/\((.*?)\)/).flatten
808+
columns_and_values = sql.scan(/\((.*?)\)/m).flatten
795809
columns = columns_and_values.first.split(',')
796810
values = columns_and_values[1].split(',')
797811
original_values = values.dup
@@ -809,3 +823,4 @@ def set_utf8_values!(sql)
809823
end #class SQLServerAdapter < AbstractAdapter
810824
end #module ConnectionAdapters
811825
end #module ActiveRecord
826+

rails-sqlserver-adapter.gemspec

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
Gem::Specification.new do |s|
22
s.name = %q{activerecord-sqlserver-adapter}
3-
s.version = "1.0.1"
3+
s.version = "1.0.2"
44

55
s.specification_version = 2 if s.respond_to? :specification_version=
66

77
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8-
s.authors = ["Tom Ward"]
9-
s.date = %q{2008-06-03}
10-
s.email = %q{tom@popdog.net}
8+
s.authors = ["Shawn Balestracci"]
9+
s.date = %q{2008-09-27}
10+
s.email = %q{shawn@vegantech.com}
1111
s.files = ["lib/active_record/connection_adapters/sqlserver_adapter.rb"]
12-
s.homepage = %q{http://wiki.rubyonrails.org/rails/pages/SQL+Server}
12+
s.homepage = %q{http://vegantech.lighthouseapp.com/projects/17542-activerecord-sqlserver-adapter}
1313
s.require_paths = ["lib"]
1414
s.rubyforge_project = %q{activerecord}
1515
s.rubygems_version = %q{1.1.1}
1616
s.summary = %q{SQL Server adapter for Active Record}
1717

1818
s.add_dependency(%q<activerecord>, [">= 1.15.5.7843"])
19-
end
19+
end

0 commit comments

Comments
 (0)