Module: ArJdbc::MySQL::Column
- Included in:
- ActiveRecord::ConnectionAdapters::MysqlColumn
- Defined in:
- lib/arjdbc/mysql/adapter.rb
Instance Method Summary collapse
- #extract_default(default) ⇒ Object
- #extract_limit(sql_type) ⇒ Object
- #has_default? ⇒ Boolean
-
#missing_default_forged_as_empty_string?(default) ⇒ Boolean
MySQL misreports NOT NULL column default when none is given.
- #simplified_type(field_type) ⇒ Object
Instance Method Details
#extract_default(default) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/arjdbc/mysql/adapter.rb', line 18 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
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/arjdbc/mysql/adapter.rb', line 43 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 nil # 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 /^(datetime|timestamp)/ nil else super end end |
#has_default? ⇒ Boolean
32 33 34 35 |
# File 'lib/arjdbc/mysql/adapter.rb', line 32 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 ”.
75 76 77 |
# File 'lib/arjdbc/mysql/adapter.rb', line 75 def missing_default_forged_as_empty_string?(default) type != :string && !null && default == '' end |
#simplified_type(field_type) ⇒ Object
37 38 39 40 41 |
# File 'lib/arjdbc/mysql/adapter.rb', line 37 def simplified_type(field_type) return :boolean if field_type =~ /tinyint\(1\)|bit/i return :string if field_type =~ /enum/i super end |