Module: ArJdbc::MySQL::Column

Included in:
ActiveRecord::ConnectionAdapters::MysqlColumn
Defined in:
lib/arjdbc/mysql/adapter.rb

Instance Method Summary collapse

Instance Method Details

#extract_default(default) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/arjdbc/mysql/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

#extract_limit(sql_type) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/arjdbc/mysql/adapter.rb', line 55

def extract_limit(sql_type)
  case sql_type
  when /blob|text/i
    case sql_type
    when /tiny/i
      255
    when /medium/i
      16777215
    when /long/i
      2147483647 # mysql only allows 2^31-1, not 2^32-1, somewhat inconsistently with the tiny/medium/normal cases
    else
      super # we could return 65535 here, but we leave it undecorated by default
    end
  when /^bigint/i;    8
  when /^int/i;       4
  when /^mediumint/i; 3
  when /^smallint/i;  2
  when /^tinyint/i;   1
  when /^enum\((.+)\)/i # 255
    $1.split(',').map{ |enum| enum.strip.length - 2 }.max
  when /^(bool|date|float|int|time)/i
    nil
  else
    super
  end
end

#has_default?Boolean

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/arjdbc/mysql/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

#missing_default_forged_as_empty_string?(default) ⇒ Boolean

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 ”.

Returns:

  • (Boolean)


89
90
91
# File 'lib/arjdbc/mysql/adapter.rb', line 89

def missing_default_forged_as_empty_string?(default)
  type != :string && !null && default == ''
end

#simplified_type(field_type) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/arjdbc/mysql/adapter.rb', line 45

def simplified_type(field_type)
  case field_type
  when /tinyint\(1\)|bit/i then :boolean
  when /enum/i             then :string
  when /year/i             then :integer
  else
    super
  end
end