Column
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 23
23: def extract_default(default)
24: if sql_type =~ /blob/ || type == :text
25: if default.blank?
26: return null ? nil : ''
27: else
28: raise ArgumentError, "#{type} columns cannot have a default value: #{default.inspect}"
29: end
30: elsif missing_default_forged_as_empty_string?(default)
31: nil
32: else
33: super
34: end
35: end
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 37
37: def has_default?
38: return false if sql_type =~ /blob/ || type == :text #mysql forbids defaults on blob and text columns
39: super
40: end
Returns the Ruby class that corresponds to the abstract data type.
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 43
43: def klass
44: case type
45: when :integer then Fixnum
46: when :float then Float
47: when :decimal then BigDecimal
48: when :datetime then Time
49: when :date then Date
50: when :timestamp then Time
51: when :time then Time
52: when :text, :string then String
53: when :binary then String
54: when :boolean then Object
55: end
56: end
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 58
58: def type_cast(value)
59: return nil if value.nil?
60: case type
61: when :string then value
62: when :text then value
63: when :integer then value.to_i rescue value ? 1 : 0
64: when :float then value.to_f # returns self if it's already a Float
65: when :decimal then self.class.value_to_decimal(value)
66: when :datetime, :timestamp then value.class == Time ? value : self.class.string_to_time(value)
67: when :time then value.class == Time ? value : self.class.string_to_dummy_time(value)
68: when :date then value.class == Date ? value : self.class.string_to_date(value)
69: when :binary then value
70: when :boolean then self.class.value_to_boolean(value)
71: else value
72: end
73: end
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 75
75: def type_cast_code(var_name)
76: case type
77: when :string then nil
78: when :text then nil
79: when :integer then "#{var_name}.to_i rescue #{var_name} ? 1 : 0"
80: when :float then "#{var_name}.to_f"
81: when :decimal then "#{self.class.name}.value_to_decimal(#{var_name})"
82: when :datetime, :timestamp then "#{var_name}.class == Time ? #{var_name} : #{self.class.name}.string_to_time(#{var_name})"
83: when :time then "#{var_name}.class == Time ? #{var_name} : #{self.class.name}.string_to_dummy_time(#{var_name})"
84: when :date then "#{var_name}.class == Date ? #{var_name} : #{self.class.name}.string_to_date(#{var_name})"
85: when :binary then nil
86: when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})"
87: else nil
88: end
89: end
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 100
100: def extract_limit(sql_type)
101: case sql_type
102: when /blob|text/
103: case sql_type
104: when /tiny/
105: 255
106: when /medium/
107: 16777215
108: when /long/
109: 2147483647 # mysql only allows 2^31-1, not 2^32-1, somewhat inconsistently with the tiny/medium/normal cases
110: else
111: super # we could return 65535 here, but we leave it undecorated by default
112: end
113: when /^bigint/; 8
114: when /^int/; 4
115: when /^mediumint/; 3
116: when /^smallint/; 2
117: when /^tinyint/; 1
118: else
119: super
120: end
121: end
MySQL misreports NOT NULL column default when none is given. We can’t detect this for columns which may have a legitimate ’’ default (string) but we can for others (integer, datetime, boolean, and the rest).
Test whether the column has default ’’, is not null, and is not a type allowing default ’’.
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 130
130: def missing_default_forged_as_empty_string?(default)
131: type != :string && !null && default == ''
132: end
# File lib/active_record/connection_adapters/mysql2_adapter.rb, line 92
92: def simplified_type(field_type)
93: return :boolean if Mysql2Adapter.emulate_booleans && field_type.downcase.index(BOOL)
94: return :string if field_type =~ /enum/ or field_type =~ /set/
95: return :integer if field_type =~ /year/
96: return :binary if field_type =~ /bit/
97: super
98: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.