Methods
A
B
C
E
L
N
P
Q
R
S
T
Classes and Modules
Constants
LOST_CONNECTION_ERROR_MESSAGES = [ "Server shutdown in progress", "Broken pipe", "Lost connection to MySQL server during query", "MySQL server has gone away" ]
QUOTED_FALSE = '1', '0'
NATIVE_DATABASE_TYPES = { :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY", :string => { :name => "varchar", :limit => 255 }, :text => { :name => "text" }, :integer => { :name => "int", :limit => 4 }, :float => { :name => "float" }, :decimal => { :name => "decimal" }, :datetime => { :name => "datetime" }, :timestamp => { :name => "datetime" }, :time => { :name => "time" }, :date => { :name => "date" }, :binary => { :name => "blob" }, :boolean => { :name => "tinyint", :limit => 1 } }
Class Public methods
emulate_booleans

By default, the MysqlAdapter will consider all columns of type tinyint(1) as boolean. If you wish to disable this emulation (which was the default behavior in versions 0.13.1 and earlier) you can add the following line to your application.rb file:

  ActiveRecord::ConnectionAdapters::Mysql[2]Adapter.emulate_booleans = false
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 100
100:       class_attribute :emulate_booleans
new(connection, logger, connection_options, config)

FIXME: Make the first parameter more similar for the two adapters

     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 131
131:       def initialize(connection, logger, connection_options, config)
132:         super(connection, logger)
133:         @connection_options, @config = connection_options, config
134:         @quoted_column_names, @quoted_table_names = {}, {}
135: 
136:         if config.fetch(:prepared_statements) { true }
137:           @visitor = Arel::Visitors::MySQL.new self
138:         else
139:           @visitor = BindSubstitution.new self
140:         end
141:       end
Instance Public methods
add_column(table_name, column_name, type, options = {})
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 458
458:       def add_column(table_name, column_name, type, options = {})
459:         execute("ALTER TABLE #{quote_table_name(table_name)} #{add_column_sql(table_name, column_name, type, options)}")
460:       end
add_column_position!(sql, options)
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 510
510:       def add_column_position!(sql, options)
511:         if options[:first]
512:           sql << " FIRST"
513:         elsif options[:after]
514:           sql << " AFTER #{quote_column_name(options[:after])}"
515:         end
516:       end
begin_db_transaction()
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 265
265:       def begin_db_transaction
266:         execute "BEGIN"
267:       rescue Exception
268:         # Transactions aren't supported
269:       end
case_insensitive_comparison(table, attribute, column, value)
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 547
547:       def case_insensitive_comparison(table, attribute, column, value)
548:         if column.case_sensitive?
549:           super
550:         else
551:           table[attribute].eq(value)
552:         end
553:       end
case_sensitive_modifier(node)
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 543
543:       def case_sensitive_modifier(node)
544:         Arel::Nodes::Bin.new(node)
545:       end
change_column_default(table_name, column_name, default)
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 462
462:       def change_column_default(table_name, column_name, default)
463:         column = column_for(table_name, column_name)
464:         change_column table_name, column_name, column.sql_type, :default => default
465:       end
change_column_null(table_name, column_name, null, default = nil)
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 467
467:       def change_column_null(table_name, column_name, null, default = nil)
468:         column = column_for(table_name, column_name)
469: 
470:         unless null || default.nil?
471:           execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
472:         end
473: 
474:         change_column table_name, column_name, column.sql_type, :null => null
475:       end
charset()

Returns the database character set.

     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 367
367:       def charset
368:         show_variable 'character_set_database'
369:       end
collation()

Returns the database collation strategy.

     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 372
372:       def collation
373:         show_variable 'collation_database'
374:       end
create_database(name, options = {})

Create a new MySQL database with optional :charset and :collation. Charset defaults to utf8.

Example:

  create_database 'charset_test', :charset => 'latin1', :collation => 'latin1_bin'
  create_database 'matt_development'
  create_database 'matt_development', :charset => :big5
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 346
346:       def create_database(name, options = {})
347:         if options[:collation]
348:           execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
349:         else
350:           execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
351:         end
352:       end
create_savepoint()
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 283
283:       def create_savepoint
284:         execute("SAVEPOINT #{current_savepoint_name}")
285:       end
current_database()
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 362
362:       def current_database
363:         select_value 'SELECT DATABASE() as db'
364:       end
execute(sql, name = nil)

Executes the SQL statement in the context of this connection.

     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 239
239:       def execute(sql, name = nil)
240:         if name == :skip_logging
241:           @connection.query(sql)
242:         else
243:           log(sql, name) { @connection.query(sql) }
244:         end
245:       rescue ActiveRecord::StatementInvalid => exception
246:         if exception.message.split(":").first =~ /Packets out of order/
247:           raise ActiveRecord::StatementInvalid, "'Packets out of order' error was received from the database. Please update your mysql bindings (gem install mysql) and read http://dev.mysql.com/doc/mysql/en/password-hashing.html for more information. If you're on Windows, use the Instant Rails installer to get the updated mysql bindings."
248:         else
249:           raise
250:         end
251:       end
limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 555
555:       def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
556:         where_sql
557:       end
native_database_types()
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 171
171:       def native_database_types
172:         NATIVE_DATABASE_TYPES
173:       end
pk_and_sequence_for(table)

Returns a table’s primary key and belonging sequence.

     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 525
525:       def pk_and_sequence_for(table)
526:         execute_and_free("SHOW CREATE TABLE #{quote_table_name(table)}", 'SCHEMA') do |result|
527:           create_table = each_hash(result).first[:"Create Table"]
528:           if create_table.to_s =~ /PRIMARY KEY\s+(?:USING\s+\w+\s+)?\((.+)\)/
529:             keys = $1.split(",").map { |key| key.gsub(/[`"]/, "") }
530:             keys.length == 1 ? [keys.first, nil] : nil
531:           else
532:             nil
533:           end
534:         end
535:       end
primary_key(table)

Returns just a table’s primary key

     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 538
538:       def primary_key(table)
539:         pk_and_sequence = pk_and_sequence_for(table)
540:         pk_and_sequence && pk_and_sequence.first
541:       end
quote(value, column = nil)

QUOTING ==================================================

     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 196
196:       def quote(value, column = nil)
197:         if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
198:           s = column.class.string_to_binary(value).unpack("H*")[0]
199:           "x'#{s}'"
200:         elsif value.kind_of?(BigDecimal)
201:           value.to_s("F")
202:         else
203:           super
204:         end
205:       end
quoted_false()
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 219
219:       def quoted_false
220:         QUOTED_FALSE
221:       end
quoted_true()
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 215
215:       def quoted_true
216:         QUOTED_TRUE
217:       end
recreate_database(name, options = {})

Drops the database specified on the name attribute and creates it again using the provided options.

     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 334
334:       def recreate_database(name, options = {})
335:         drop_database(name)
336:         create_database(name, options)
337:       end
release_savepoint()
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 291
291:       def release_savepoint
292:         execute("RELEASE SAVEPOINT #{current_savepoint_name}")
293:       end
rename_table(table_name, new_name)

Renames a table.

Example:

  rename_table('octopuses', 'octopi')
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 454
454:       def rename_table(table_name, new_name)
455:         execute "RENAME TABLE #{quote_table_name(table_name)} TO #{quote_table_name(new_name)}"
456:       end
rollback_to_savepoint()
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 287
287:       def rollback_to_savepoint
288:         execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
289:       end
show_variable(name)

SHOW VARIABLES LIKE ‘name’

     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 519
519:       def show_variable(name)
520:         variables = select_all("SHOW VARIABLES LIKE '#{name}'")
521:         variables.first['Value'] unless variables.empty?
522:       end
supports_index_sort_order?()

Technically MySQL allows to create indexes with the sort order syntax but at the moment (5.5) it doesn’t yet implement them

     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 167
167:       def supports_index_sort_order?
168:         true
169:       end
supports_migrations?()

Returns true, since this connection adapter supports migrations.

     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 148
148:       def supports_migrations?
149:         true
150:       end
supports_primary_key?()
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 152
152:       def supports_primary_key?
153:         true
154:       end
supports_savepoints?()

Returns true, since this connection adapter supports savepoints.

     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 157
157:       def supports_savepoints?
158:         true
159:       end
table_exists?(name)
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 386
386:       def table_exists?(name)
387:         return false unless name
388:         return true if tables(nil, nil, name).any?
389: 
390:         name          = name.to_s
391:         schema, table = name.split('.', 2)
392: 
393:         unless table # A table was provided without a schema
394:           table  = schema
395:           schema = nil
396:         end
397: 
398:         tables(nil, schema, table).any?
399:       end
type_to_sql(type, limit = nil, precision = nil, scale = nil)

Maps logical Rails types to MySQL-specific data types.

     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 486
486:       def type_to_sql(type, limit = nil, precision = nil, scale = nil)
487:         case type.to_s
488:         when 'integer'
489:           case limit
490:           when 1; 'tinyint'
491:           when 2; 'smallint'
492:           when 3; 'mediumint'
493:           when nil, 4, 11; 'int(11)'  # compatibility with MySQL default
494:           when 5..8; 'bigint'
495:           else raise(ActiveRecordError, "No integer type has byte size #{limit}")
496:           end
497:         when 'text'
498:           case limit
499:           when 0..0xff;               'tinytext'
500:           when nil, 0x100..0xffff;    'text'
501:           when 0x10000..0xffffff;     'mediumtext'
502:           when 0x1000000..0xffffffff; 'longtext'
503:           else raise(ActiveRecordError, "No text type has character length #{limit}")
504:           end
505:         else
506:           super
507:         end
508:       end
Instance Protected methods
add_column_sql(table_name, column_name, type, options = {})
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 597
597:       def add_column_sql(table_name, column_name, type, options = {})
598:         add_column_sql = "ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
599:         add_column_options!(add_column_sql, options)
600:         add_column_position!(add_column_sql, options)
601:         add_column_sql
602:       end
add_index_length(option_strings, column_names, options = {})
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 561
561:       def add_index_length(option_strings, column_names, options = {})
562:         if options.is_a?(Hash) && length = options[:length]
563:           case length
564:           when Hash
565:             column_names.each {|name| option_strings[name] += "(#{length[name]})" if length.has_key?(name) && length[name].present?}
566:           when Fixnum
567:             column_names.each {|name| option_strings[name] += "(#{length})"}
568:           end
569:         end
570: 
571:         return option_strings
572:       end
add_index_sql(table_name, column_name, options = {})
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 642
642:       def add_index_sql(table_name, column_name, options = {})
643:         index_name, index_type, index_columns = add_index_options(table_name, column_name, options)
644:         "ADD #{index_type} INDEX #{index_name} (#{index_columns})"
645:       end
add_timestamps_sql(table_name)
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 652
652:       def add_timestamps_sql(table_name)
653:         [add_column_sql(table_name, :created_at, :datetime), add_column_sql(table_name, :updated_at, :datetime)]
654:       end
change_column_sql(table_name, column_name, type, options = {})
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 604
604:       def change_column_sql(table_name, column_name, type, options = {})
605:         column = column_for(table_name, column_name)
606: 
607:         unless options_include_default?(options)
608:           options[:default] = column.default
609:         end
610: 
611:         unless options.has_key?(:null)
612:           options[:null] = column.null
613:         end
614: 
615:         change_column_sql = "CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
616:         add_column_options!(change_column_sql, options)
617:         add_column_position!(change_column_sql, options)
618:         change_column_sql
619:       end
quoted_columns_for_index(column_names, options = {})
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 574
574:       def quoted_columns_for_index(column_names, options = {})
575:         option_strings = Hash[column_names.map {|name| [name, '']}]
576: 
577:         # add index length
578:         option_strings = add_index_length(option_strings, column_names, options)
579: 
580:         # add index sort order
581:         option_strings = add_index_sort_order(option_strings, column_names, options)
582: 
583:         column_names.map {|name| quote_column_name(name) + option_strings[name]}
584:       end
remove_column_sql(table_name, *column_names)
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 637
637:       def remove_column_sql(table_name, *column_names)
638:         columns_for_remove(table_name, *column_names).map {|column_name| "DROP #{column_name}" }
639:       end
remove_index_sql(table_name, options = {})
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 647
647:       def remove_index_sql(table_name, options = {})
648:         index_name = index_name_for_remove(table_name, options)
649:         "DROP INDEX #{index_name}"
650:       end
remove_timestamps_sql(table_name)
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 656
656:       def remove_timestamps_sql(table_name)
657:         [remove_column_sql(table_name, :updated_at), remove_column_sql(table_name, :created_at)]
658:       end
rename_column_sql(table_name, column_name, new_column_name)
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 621
621:       def rename_column_sql(table_name, column_name, new_column_name)
622:         options = {}
623: 
624:         if column = columns(table_name).find { |c| c.name == column_name.to_s }
625:           options[:default] = column.default
626:           options[:null] = column.null
627:         else
628:           raise ActiveRecordError, "No such column: #{table_name}.#{column_name}"
629:         end
630: 
631:         current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"]
632:         rename_column_sql = "CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}"
633:         add_column_options!(rename_column_sql, options)
634:         rename_column_sql
635:       end
translate_exception(exception, message)
     # File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 586
586:       def translate_exception(exception, message)
587:         case error_number(exception)
588:         when 1062
589:           RecordNotUnique.new(message, exception)
590:         when 1452
591:           InvalidForeignKey.new(message, exception)
592:         else
593:           super
594:         end
595:       end