Class: ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/activerecord-mysql-unsigned/active_record/v3/connection_adapters/abstract_mysql_adapter.rb,
lib/activerecord-mysql-unsigned/active_record/v4/connection_adapters/abstract_mysql_adapter.rb

Direct Known Subclasses

Mysql2Adapter

Instance Method Summary collapse

Instance Method Details

#add_column_sql(table_name, column_name, type, options = {}) ⇒ Object



45
46
47
48
49
50
# File 'lib/activerecord-mysql-unsigned/active_record/v3/connection_adapters/abstract_mysql_adapter.rb', line 45

def add_column_sql(table_name, column_name, type, options = {})
  add_column_sql = "ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale], options[:unsigned], options[:auto_increment])}"
  add_column_options!(add_column_sql, options)
  add_column_position!(add_column_sql, options)
  add_column_sql
end

#change_column_sql(table_name, column_name, type, options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/activerecord-mysql-unsigned/active_record/v3/connection_adapters/abstract_mysql_adapter.rb', line 52

def change_column_sql(table_name, column_name, type, options = {})
  column = column_for(table_name, column_name)

  unless type.to_sym == :primary_key
    unless options_include_default?(options)
      options[:default] = column.default
    end
    unless options.has_key?(:null)
      options[:null] = column.null
    end
  end

  change_column_sql = "CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale], options[:unsigned], options[:auto_increment])}"
  add_column_options!(change_column_sql, options)
  add_column_position!(change_column_sql, options)
  change_column_sql
end

#type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = false) ⇒ Object

Maps logical Rails types to MySQL-specific data types.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/activerecord-mysql-unsigned/active_record/v3/connection_adapters/abstract_mysql_adapter.rb', line 12

def type_to_sql(type, limit = nil, precision = nil, scale = nil, unsigned = false, auto_increment = false)
  case type.to_s
  when 'integer'
    case limit
    when 1
      'tinyint' + (unsigned ? ' unsigned' : '') + (auto_increment ? ' AUTO_INCREMENT' : '')
    when 2
      'smallint' + (unsigned ? ' unsigned' : '') + (auto_increment ? ' AUTO_INCREMENT' : '')
    when 3
      'mediumint' + (unsigned ? ' unsigned' : '') + (auto_increment ? ' AUTO_INCREMENT' : '')
    when nil, 4, 11 # compatibility with MySQL default
      if unsigned
        'int(10) unsigned' + (auto_increment ? ' AUTO_INCREMENT' : '')
      else
        'int(10)'
      end
    when 5..8
      'bigint' + (unsigned ? ' unsigned' : '')
    else raise(ActiveRecordError, "No integer type has byte size #{limit}")
    end
  when 'text'
    case limit
    when 0..0xff;               'tinytext'
    when nil, 0x100..0xffff;    'text'
    when 0x10000..0xffffff;     'mediumtext'
    when 0x1000000..0xffffffff; 'longtext'
    else raise(ActiveRecordError, "No text type has character length #{limit}")
    end
  else
    super
  end
end