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
2629module 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
811825end #module ActiveRecord
826+
0 commit comments