Class: ActiveRecord::ConnectionAdapters::Mysql2Column

Inherits:
Column
  • Object
show all
Defined in:
lib/active_record/connection_adapters/mysql2_adapter.rb

Constant Summary collapse

BOOL =
"tinyint(1)"

Instance Method Summary collapse

Instance Method Details

#extract_default(default) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 23

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

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 37

def has_default?
  return false if sql_type =~ /blob/i || type == :text #mysql forbids defaults on blob and text columns
  super
end

#klassObject

Returns the Ruby class that corresponds to the abstract data type.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 43

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 58

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 75

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