Class: ActiveRecord::ConnectionAdapters::Mysql2Column
- Inherits:
-
Column
- Object
- Column
- ActiveRecord::ConnectionAdapters::Mysql2Column
- Defined in:
- lib/active_record/connection_adapters/mysql2_adapter.rb
Constant Summary collapse
- BOOL =
"tinyint(1)"
Instance Method Summary collapse
- #extract_default(default) ⇒ Object
- #has_default? ⇒ Boolean
-
#klass ⇒ Object
Returns the Ruby class that corresponds to the abstract data type.
- #type_cast(value) ⇒ Object
- #type_cast_code(var_name) ⇒ Object
Instance Method Details
#extract_default(default) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 26 def extract_default(default) if sql_type =~ /blob/i || type == :text if default.blank? return null ? nil : '' else raise ArgumentError, "#{type} columns cannot have a default value: #{default.inspect}" end elsif missing_default_forged_as_empty_string?(default) nil else super end end |
#has_default? ⇒ Boolean
40 41 42 43 |
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 40 def has_default? return false if sql_type =~ /blob/i || type == :text #mysql forbids defaults on blob and text columns super end |
#klass ⇒ Object
Returns the Ruby class that corresponds to the abstract data type.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 46 def klass case type when :integer then Fixnum when :float then Float when :decimal then BigDecimal when :datetime then Time when :date then Date when :timestamp then Time when :time then Time when :text, :string then String when :binary then String when :boolean then Object end end |
#type_cast(value) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 61 def type_cast(value) return nil if value.nil? case type when :string then value when :text then value when :integer then value.to_i rescue value ? 1 : 0 when :float then value.to_f # returns self if it's already a Float when :decimal then self.class.value_to_decimal(value) when :datetime, :timestamp then value.class == Time ? value : self.class.string_to_time(value) when :time then value.class == Time ? value : self.class.string_to_dummy_time(value) when :date then value.class == Date ? value : self.class.string_to_date(value) when :binary then value when :boolean then self.class.value_to_boolean(value) else value end end |
#type_cast_code(var_name) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 78 def type_cast_code(var_name) case type when :string then nil when :text then nil when :integer then "#{var_name}.to_i rescue #{var_name} ? 1 : 0" when :float then "#{var_name}.to_f" when :decimal then "#{self.class.name}.value_to_decimal(#{var_name})" when :datetime, :timestamp then "#{var_name}.class == Time ? #{var_name} : #{self.class.name}.string_to_time(#{var_name})" when :time then "#{var_name}.class == Time ? #{var_name} : #{self.class.name}.string_to_dummy_time(#{var_name})" when :date then "#{var_name}.class == Date ? #{var_name} : #{self.class.name}.string_to_date(#{var_name})" when :binary then nil when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})" else nil end end |