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 /var/run/mysqld/mysqld.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"

Public Class methods

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 163
163:       def initialize(connection, logger, connection_options, config)
164:         super(connection, logger)
165:         @connection_options, @config = connection_options, config
166: 
167:         connect
168:       end

Public Instance methods

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 244
244:       def active?
245:         if @connection.respond_to?(:stat)
246:           @connection.stat
247:         else
248:           @connection.query 'select 1'
249:         end
250: 
251:         # mysql-ruby doesn't raise an exception when stat fails.
252:         if @connection.respond_to?(:errno)
253:           @connection.errno.zero?
254:         else
255:           true
256:         end
257:       rescue Mysql::Error
258:         false
259:       end

Returns the database character set.

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 376
376:       def charset
377:         show_variable 'character_set_database'
378:       end

Returns the database collation strategy.

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 381
381:       def collation
382:         show_variable 'collation_database'
383:       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 359
359:       def create_database(name, options = {})
360:         if options[:collation]
361:           execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
362:         else
363:           execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
364:         end
365:       end

[Source]

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

[Source]

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

[Source]

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

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 198
198:       def quote(value, column = nil)
199:         if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
200:           s = column.class.string_to_binary(value).unpack("H*")[0]
201:           "x'#{s}'"
202:         elsif value.kind_of?(BigDecimal)
203:           "'#{value.to_s("F")}'"
204:         else
205:           super
206:         end
207:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 225
225:       def quoted_false
226:         "0"
227:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 221
221:       def quoted_true
222:         "1"
223:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 261
261:       def reconnect!
262:         disconnect!
263:         connect
264:       end

[Source]

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

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 273
273:       def select_rows(sql, name = nil)
274:         @connection.query_with_result = true
275:         result = execute(sql, name)
276:         rows = []
277:         result.each { |row| rows << row }
278:         result.free
279:         rows
280:       end

SHOW VARIABLES LIKE ‘name‘

[Source]

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

[Validate]