The MySQL adapter will work with both Ruby/MySQL, which is a Ruby-based MySQL adapter that comes bundled with Active Record, and with the faster C-based MySQL/Ruby adapter (available both as a gem and from www.tmtm.org/en/mysql/ruby/).

Options:

  • :host - Defaults to "localhost".
  • :port - Defaults to 3306.
  • :socket - Defaults to "/tmp/mysql.sock".
  • :username - Defaults to "root"
  • :password - Defaults to nothing.
  • :database - The name of the database. No default, must be provided.
  • :encoding - (Optional) Sets the client encoding by executing "SET NAMES <encoding>" after connection.
  • :reconnect - Defaults to false (See MySQL documentation: dev.mysql.com/doc/refman/5.0/en/auto-reconnect.html).
  • :sslca - Necessary to use MySQL with an SSL connection.
  • :sslkey - Necessary to use MySQL with an SSL connection.
  • :sslcert - Necessary to use MySQL with an SSL connection.
  • :sslcapath - Necessary to use MySQL with an SSL connection.
  • :sslcipher - Necessary to use MySQL with an SSL connection.
Methods
Constants
ADAPTER_NAME = 'MySQL'.freeze
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'.freeze, '0'.freeze
NATIVE_DATABASE_TYPES = { :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY".freeze, :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 }
Public Class methods
new(connection, logger, connection_options, config)
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 201
201:       def initialize(connection, logger, connection_options, config)
202:         super(connection, logger)
203:         @connection_options, @config = connection_options, config
204:         @quoted_column_names, @quoted_table_names = {}, {}
205:         connect
206:       end
Public Instance methods
active?()

CONNECTION MANAGEMENT ====================================

     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 277
277:       def active?
278:         if @connection.respond_to?(:stat)
279:           @connection.stat
280:         else
281:           @connection.query 'select 1'
282:         end
283: 
284:         # mysql-ruby doesn't raise an exception when stat fails.
285:         if @connection.respond_to?(:errno)
286:           @connection.errno.zero?
287:         else
288:           true
289:         end
290:       rescue Mysql::Error
291:         false
292:       end
add_column(table_name, column_name, type, options = {})
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 487
487:       def add_column(table_name, column_name, type, options = {})
488:         add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
489:         add_column_options!(add_column_sql, options)
490:         add_column_position!(add_column_sql, options)
491:         execute(add_column_sql)
492:       end
add_column_position!(sql, options)
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 554
554:       def add_column_position!(sql, options)
555:         if options[:first]
556:           sql << " FIRST"
557:         elsif options[:after]
558:           sql << " AFTER #{quote_column_name(options[:after])}"
559:         end
560:       end
case_sensitive_equality_operator()
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 585
585:       def case_sensitive_equality_operator
586:         "= BINARY"
587:       end
change_column_null(table_name, column_name, null, default = nil)
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 499
499:       def change_column_null(table_name, column_name, null, default = nil)
500:         column = column_for(table_name, column_name)
501: 
502:         unless null || default.nil?
503:           execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
504:         end
505: 
506:         change_column table_name, column_name, column.sql_type, :null => null
507:       end
charset()

Returns the database character set.

     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 431
431:       def charset
432:         show_variable 'character_set_database'
433:       end
collation()

Returns the database collation strategy.

     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 436
436:       def collation
437:         show_variable 'collation_database'
438:       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/mysql_adapter.rb, line 414
414:       def create_database(name, options = {})
415:         if options[:collation]
416:           execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
417:         else
418:           execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
419:         end
420:       end
create_savepoint()
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 363
363:       def create_savepoint
364:         execute("SAVEPOINT #{current_savepoint_name}")
365:       end
current_database()
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 426
426:       def current_database
427:         select_value 'SELECT DATABASE() as db'
428:       end
disconnect!()
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 299
299:       def disconnect!
300:         @connection.close rescue nil
301:       end
drop_table(table_name, options = {})
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 448
448:       def drop_table(table_name, options = {})
449:         super(table_name, options)
450:       end
limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 589
589:       def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
590:         where_sql
591:       end
primary_key(table)

Returns just a table‘s primary key

     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 580
580:       def primary_key(table)
581:         pk_and_sequence = pk_and_sequence_for(table)
582:         pk_and_sequence && pk_and_sequence.first
583:       end
quote(value, column = nil)

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

     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 231
231:       def quote(value, column = nil)
232:         if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
233:           s = column.class.string_to_binary(value).unpack("H*")[0]
234:           "x'#{s}'"
235:         elsif value.kind_of?(BigDecimal)
236:           value.to_s("F")
237:         else
238:           super
239:         end
240:       end
quoted_false()
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 258
258:       def quoted_false
259:         QUOTED_FALSE
260:       end
quoted_true()
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 254
254:       def quoted_true
255:         QUOTED_TRUE
256:       end
reconnect!()
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 294
294:       def reconnect!
295:         disconnect!
296:         connect
297:       end
release_savepoint()
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 371
371:       def release_savepoint
372:         execute("RELEASE SAVEPOINT #{current_savepoint_name}")
373:       end
rename_table(table_name, new_name)
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 483
483:       def rename_table(table_name, new_name)
484:         execute "RENAME TABLE #{quote_table_name(table_name)} TO #{quote_table_name(new_name)}"
485:       end
reset!()
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 303
303:       def reset!
304:         if @connection.respond_to?(:change_user)
305:           # See http://bugs.mysql.com/bug.php?id=33540 -- the workaround way to
306:           # reset the connection is to change the user to the same user.
307:           @connection.change_user(@config[:username], @config[:password], @config[:database])
308:           configure_connection
309:         end
310:       end
rollback_to_savepoint()
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 367
367:       def rollback_to_savepoint
368:         execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
369:       end
select_rows(sql, name = nil)

DATABASE STATEMENTS ======================================

     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 314
314:       def select_rows(sql, name = nil)
315:         @connection.query_with_result = true
316:         result = execute(sql, name)
317:         rows = []
318:         result.each { |row| rows << row }
319:         result.free
320:         @connection.more_results && @connection.next_result    # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped
321:         rows
322:       end
show_variable(name)

SHOW VARIABLES LIKE ‘name‘

     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 563
563:       def show_variable(name)
564:         variables = select_all("SHOW VARIABLES LIKE '#{name}'")
565:         variables.first['Value'] unless variables.empty?
566:       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/mysql_adapter.rb, line 541
541:       def type_to_sql(type, limit = nil, precision = nil, scale = nil)
542:         return super unless type.to_s == 'integer'
543: 
544:         case limit
545:         when 1; 'tinyint'
546:         when 2; 'smallint'
547:         when 3; 'mediumint'
548:         when nil, 4, 11; 'int(11)'  # compatibility with MySQL default
549:         when 5..8; 'bigint'
550:         else raise(ActiveRecordError, "No integer type has byte size #{limit}")
551:         end
552:       end
Protected Instance methods
quoted_columns_for_index(column_names, options = {})
     # File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 594
594:         def quoted_columns_for_index(column_names, options = {})
595:           length = options[:length] if options.is_a?(Hash)
596: 
597:           quoted_column_names = case length
598:           when Hash
599:             column_names.map {|name| length[name] ? "#{quote_column_name(name)}(#{length[name]})" : quote_column_name(name) }
600:           when Fixnum
601:             column_names.map {|name| "#{quote_column_name(name)}(#{length})"}
602:           else
603:             column_names.map {|name| quote_column_name(name) }
604:           end
605:         end