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.
  • :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

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'

Public Class methods

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 181
181:       def initialize(connection, logger, connection_options, config)
182:         super(connection, logger)
183:         @connection_options, @config = connection_options, config
184:         @quoted_column_names, @quoted_table_names = {}, {}
185:         connect
186:       end

Public Instance methods

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

[Source]

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

Returns the database character set.

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 395
395:       def charset
396:         show_variable 'character_set_database'
397:       end

Returns the database collation strategy.

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 400
400:       def collation
401:         show_variable 'collation_database'
402:       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 378
378:       def create_database(name, options = {})
379:         if options[:collation]
380:           execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
381:         else
382:           execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
383:         end
384:       end

[Source]

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

[Source]

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

[Source]

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

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

[Source]

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 243
243:       def quoted_false
244:         QUOTED_FALSE
245:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 239
239:       def quoted_true
240:         QUOTED_TRUE
241:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 279
279:       def reconnect!
280:         disconnect!
281:         connect
282:       end

[Source]

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

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 291
291:       def select_rows(sql, name = nil)
292:         @connection.query_with_result = true
293:         result = execute(sql, name)
294:         rows = []
295:         result.each { |row| rows << row }
296:         result.free
297:         rows
298:       end

SHOW VARIABLES LIKE ‘name‘

[Source]

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

Maps logical Rails types to MySQL-specific data types.

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 470
470:       def type_to_sql(type, limit = nil, precision = nil, scale = nil)
471:         return super unless type.to_s == 'integer'
472: 
473:         case limit
474:         when 0..3
475:           "smallint(#{limit})"
476:         when 4..8
477:           "int(#{limit})"
478:         when 9..20
479:           "bigint(#{limit})"
480:         else
481:           'int(11)'
482:         end
483:       end

[Validate]