Class ActiveRecord::ConnectionAdapters::MysqlAdapter
In: vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
Parent: AbstractAdapter

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.

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 environment.rb file:

  ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans = false

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 197
197:       def initialize(connection, logger, connection_options, config)
198:         super(connection, logger)
199:         @connection_options, @config = connection_options, config
200:         @quoted_column_names, @quoted_table_names = {}, {}
201:         connect
202:       end

Public Instance methods

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 265
265:       def active?
266:         if @connection.respond_to?(:stat)
267:           @connection.stat
268:         else
269:           @connection.query 'select 1'
270:         end
271: 
272:         # mysql-ruby doesn't raise an exception when stat fails.
273:         if @connection.respond_to?(:errno)
274:           @connection.errno.zero?
275:         else
276:           true
277:         end
278:       rescue Mysql::Error
279:         false
280:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 530
530:       def case_sensitive_equality_operator
531:         "= BINARY"
532:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 460
460:       def change_column_null(table_name, column_name, null, default = nil)
461:         column = column_for(table_name, column_name)
462: 
463:         unless null || default.nil?
464:           execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
465:         end
466: 
467:         change_column table_name, column_name, column.sql_type, :null => null
468:       end

Returns the database character set.

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 406
406:       def charset
407:         show_variable 'character_set_database'
408:       end

Returns the database collation strategy.

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 411
411:       def collation
412:         show_variable 'collation_database'
413:       end

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 389
389:       def create_database(name, options = {})
390:         if options[:collation]
391:           execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
392:         else
393:           execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
394:         end
395:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 401
401:       def current_database
402:         select_value 'SELECT DATABASE() as db'
403:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 287
287:       def disconnect!
288:         @connection.close rescue nil
289:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 421
421:       def drop_table(table_name, options = {})
422:         super(table_name, options)
423:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 534
534:       def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
535:         where_sql
536:       end

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 219
219:       def quote(value, column = nil)
220:         if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
221:           s = column.class.string_to_binary(value).unpack("H*")[0]
222:           "x'#{s}'"
223:         elsif value.kind_of?(BigDecimal)
224:           value.to_s("F")
225:         else
226:           super
227:         end
228:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 246
246:       def quoted_false
247:         QUOTED_FALSE
248:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 242
242:       def quoted_true
243:         QUOTED_TRUE
244:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 282
282:       def reconnect!
283:         disconnect!
284:         connect
285:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 451
451:       def rename_table(table_name, new_name)
452:         execute "RENAME TABLE #{quote_table_name(table_name)} TO #{quote_table_name(new_name)}"
453:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 291
291:       def reset!
292:         if @connection.respond_to?(:change_user)
293:           # See http://bugs.mysql.com/bug.php?id=33540 -- the workaround way to
294:           # reset the connection is to change the user to the same user.
295:           @connection.change_user(@config[:username], @config[:password], @config[:database])
296:           configure_connection
297:         end
298:       end

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 302
302:       def select_rows(sql, name = nil)
303:         @connection.query_with_result = true
304:         result = execute(sql, name)
305:         rows = []
306:         result.each { |row| rows << row }
307:         result.free
308:         rows
309:       end

SHOW VARIABLES LIKE ‘name‘

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 516
516:       def show_variable(name)
517:         variables = select_all("SHOW VARIABLES LIKE '#{name}'")
518:         variables.first['Value'] unless variables.empty?
519:       end

Maps logical Rails types to MySQL-specific data types.

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 501
501:       def type_to_sql(type, limit = nil, precision = nil, scale = nil)
502:         return super unless type.to_s == 'integer'
503: 
504:         case limit
505:         when 1; 'tinyint'
506:         when 2; 'smallint'
507:         when 3; 'mediumint'
508:         when nil, 4, 11; 'int(11)'  # compatibility with MySQL default
509:         when 5..8; 'bigint'
510:         else raise(ActiveRecordError, "No integer type has byte size #{limit}")
511:         end
512:       end

[Validate]