Skip to content

Commit b0ea27b

Browse files
committed
Adding type_cast_code and removing casting for boolean.
Adding type_cast_code revealed that the boolean casting was naive at best and the super version from Column was much better. So I removed it as it just seems unnecessary when there was a better alternative.
1 parent 7f66791 commit b0ea27b

File tree

1 file changed

+51
-41
lines changed

1 file changed

+51
-41
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -77,60 +77,70 @@ def simplified_type(field_type)
7777
def type_cast(value)
7878
return nil if value.nil?
7979
case type
80-
when :datetime then cast_to_datetime(value)
81-
when :timestamp then cast_to_time(value)
82-
when :time then cast_to_time(value)
83-
when :date then cast_to_datetime(value)
84-
when :boolean then value == true or (value =~ /^t(rue)?$/i) == 0 or value.to_s == '1'
80+
when :datetime then self.class.cast_to_datetime(value)
81+
when :timestamp then self.class.cast_to_time(value)
82+
when :time then self.class.cast_to_time(value)
83+
when :date then self.class.cast_to_datetime(value)
8584
else super
8685
end
8786
end
8887

89-
def cast_to_time(value)
90-
return value if value.is_a?(Time)
91-
time_array = ParseDate.parsedate(value)
92-
Time.time_with_datetime_fallback(Base.default_timezone, *time_array) rescue nil
93-
#Time.send(Base.default_timezone, *time_array) rescue nil
88+
def type_cast_code(var_name)
89+
case type
90+
when :datetime then "#{self.class.name}.cast_to_datetime(#{var_name})"
91+
when :timestamp then "#{self.class.name}.cast_to_time(#{var_name})"
92+
when :time then "#{self.class.name}.cast_to_time(#{var_name})"
93+
when :date then "#{self.class.name}.cast_to_datetime(#{var_name})"
94+
else super
95+
end
9496
end
95-
96-
def cast_to_datetime(value)
97-
return value.to_time if value.is_a?(DBI::Timestamp)
97+
98+
class << self
99+
def cast_to_time(value)
100+
return value if value.is_a?(Time)
101+
time_array = ParseDate.parsedate(value)
102+
Time.time_with_datetime_fallback(Base.default_timezone, *time_array) rescue nil
103+
#Time.send(Base.default_timezone, *time_array) rescue nil
104+
end
98105

99-
if value.is_a?(Time)
100-
if value.year != 0 and value.month != 0 and value.day != 0
101-
return value
102-
else
103-
return Time.mktime(2000, 1, 1, value.hour, value.min, value.sec) rescue nil
106+
def cast_to_datetime(value)
107+
return value.to_time if value.is_a?(DBI::Timestamp)
108+
109+
if value.is_a?(Time)
110+
if value.year != 0 and value.month != 0 and value.day != 0
111+
return value
112+
else
113+
return Time.mktime(2000, 1, 1, value.hour, value.min, value.sec) rescue nil
114+
end
104115
end
105-
end
106116

107-
if value.is_a?(DateTime)
108-
return Time.time_with_datetime_fallback(Base.default_timezone, value.year, value.mon, value.day, value.hour, value.min, value.sec)
117+
if value.is_a?(DateTime)
118+
return Time.time_with_datetime_fallback(Base.default_timezone, value.year, value.mon, value.day, value.hour, value.min, value.sec)
119+
end
120+
121+
return cast_to_time(value) if value.is_a?(Date) or value.is_a?(String) rescue nil
122+
value
109123
end
110124

111-
return cast_to_time(value) if value.is_a?(Date) or value.is_a?(String) rescue nil
112-
value
113-
end
114-
115-
# TODO: Find less hack way to convert DateTime objects into Times
116-
117-
def self.string_to_time(value)
118-
if value.is_a?(DateTime)
119-
return Time.time_with_datetime_fallback(Base.default_timezone, value.year, value.mon, value.day, value.hour, value.min, value.sec)
120-
else
121-
super
125+
# TODO: Find less hack way to convert DateTime objects into Times
126+
def string_to_time(value)
127+
if value.is_a?(DateTime)
128+
return Time.time_with_datetime_fallback(Base.default_timezone, value.year, value.mon, value.day, value.hour, value.min, value.sec)
129+
else
130+
super
131+
end
122132
end
123-
end
124133

125-
# These methods will only allow the adapter to insert binary data with a length of 7K or less
126-
# because of a SQL Server statement length policy.
127-
def self.string_to_binary(value)
128-
Base64.encode64(value)
129-
end
134+
# These methods will only allow the adapter to insert binary data with a length of 7K or less
135+
# because of a SQL Server statement length policy.
136+
def string_to_binary(value)
137+
Base64.encode64(value)
138+
end
130139

131-
def self.binary_to_string(value)
132-
Base64.decode64(value)
133-
end
140+
def binary_to_string(value)
141+
Base64.decode64(value)
142+
end
143+
end
134144
end
135145

136146
# In ADO mode, this adapter will ONLY work on Windows systems,

0 commit comments

Comments
 (0)