Changeset 7771
- Timestamp:
- 10/07/07 06:27:39 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
r7769 r7771 20 20 @limit, @precision, @scale = extract_limit(sql_type), extract_precision(sql_type), extract_scale(sql_type) 21 21 @type = simplified_type(sql_type) 22 @default = type_cast(default)22 @default = extract_default(default) 23 23 24 24 @primary = nil … … 93 93 end 94 94 95 def extract_default(default) 96 type_cast(default) 97 end 98 95 99 class << self 96 100 # Used to convert from Strings to BLOBs … … 106 110 def string_to_date(string) 107 111 return string unless string.is_a?(String) 112 108 113 new_date *ParseDate.parsedate(string)[0..2] 109 114 end … … 112 117 return string unless string.is_a?(String) 113 118 return nil if string.empty? 119 114 120 time_hash = Date._parse(string) 115 121 time_hash[:sec_fraction] = microseconds(time_hash) 122 116 123 new_time *time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction) 117 124 end … … 152 159 153 160 def new_date(year, mon, mday) 154 Date.new(year, mon, mday) unless year == 0 161 if year && year != 0 162 Date.new(year, mon, mday) rescue nil 163 end 155 164 end 156 165 157 166 def new_time(year, mon, mday, hour, min, sec, microsec) 158 167 # Treat 0000-00-00 00:00:00 as nil. 159 return nil if year == 0168 return nil if year.nil? || year == 0 160 169 161 170 Time.send(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) … … 164 173 zone_offset = if Base.default_timezone == :local then DateTime.now.offset else 0 end 165 174 # Append zero calendar reform start to account for dates skipped by calendar reform 166 DateTime.new(year, mon, mday, hour, min, sec, zone_offset, 0) 175 DateTime.new(year, mon, mday, hour, min, sec, zone_offset, 0) rescue nil 167 176 end 168 177 end trunk/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
r7769 r7771 92 92 module ConnectionAdapters 93 93 class MysqlColumn < Column #:nodoc: 94 TYPES_DISALLOWING_DEFAULT = Set.new([:binary, :text])95 TYPES_ALLOWING_EMPTY_STRING_DEFAULT = Set.new([:string])96 97 94 module Format 98 95 DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/ … … 100 97 end 101 98 102 def initialize(name, default, sql_type = nil, null = true) 103 @original_default = default 104 super 105 @default = nil if no_default_allowed? || missing_default_forged_as_empty_string? 106 @default = '' if @original_default == '' && no_default_allowed? 99 def extract_default(default) 100 if type == :binary || type == :text 101 if default.blank? 102 default 103 else 104 raise ArgumentError, "#{type} columns cannot have a default value: #{default.inspect}" 105 end 106 elsif missing_default_forged_as_empty_string?(default) 107 nil 108 else 109 super 110 end 107 111 end 108 112 … … 115 119 new_date $1.to_i, $2.to_i, $3.to_i 116 120 else 117 new_date *ParseDate.parsedate(string)[0..2]121 super 118 122 end 119 123 end … … 126 130 new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, $7.to_i 127 131 else 128 time_hash = Date._parse(string) 129 new_time *(time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec) << microseconds(time_hash)) 132 super 130 133 end 131 134 end … … 146 149 # Test whether the column has default '', is not null, and is not 147 150 # a type allowing default ''. 148 def missing_default_forged_as_empty_string? 149 !null && @original_default == '' && !TYPES_ALLOWING_EMPTY_STRING_DEFAULT.include?(type) 150 end 151 152 # MySQL 5.0 does not allow text and binary columns to have defaults 153 def no_default_allowed? 154 TYPES_DISALLOWING_DEFAULT.include?(type) 151 def missing_default_forged_as_empty_string?(default) 152 type != :string && !null && default == '' 155 153 end 156 154 end