| Class | ActiveRecord::ConnectionAdapters::MysqlAdapter |
| In: |
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:
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
| LOST_CONNECTION_ERROR_MESSAGES | = | [ "Server shutdown in progress", "Broken pipe", "Lost connection to MySQL server during query", "MySQL server has gone away" |
# File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 164 def initialize(connection, logger, connection_options, config) super(connection, logger) @connection_options, @config = connection_options, config connect end
CONNECTION MANAGEMENT ====================================
# File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 229 def active? if @connection.respond_to?(:stat) @connection.stat else @connection.query 'select 1' end # mysql-ruby doesn't raise an exception when stat fails. if @connection.respond_to?(:errno) @connection.errno.zero? else true end rescue Mysql::Error false end
Returns the database character set.
# File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 361 def charset show_variable 'character_set_database' end
Returns the database collation strategy.
# File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 366 def collation show_variable 'collation_database' 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
# File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 344 def create_database(name, options = {}) if options[:collation] execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`" else execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`" end end
# File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 356 def current_database select_value 'SELECT DATABASE() as db' end
# File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 251 def disconnect! @connection.close rescue nil end
QUOTING ==================================================
# File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 199 def quote(value, column = nil) if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary) s = column.class.string_to_binary(value).unpack("H*")[0] "x'#{s}'" elsif value.kind_of?(BigDecimal) "'#{value.to_s("F")}'" else super end end
# File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 222 def quoted_false "0" end
# File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 218 def quoted_true "1" end
# File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 246 def reconnect! disconnect! connect end
# File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 402 def rename_table(name, new_name) execute "RENAME TABLE #{name} TO #{new_name}" end
DATABASE STATEMENTS ======================================
# File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 258 def select_rows(sql, name = nil) @connection.query_with_result = true result = execute(sql, name) rows = [] result.each { |row| rows << row } result.free rows end
SHOW VARIABLES LIKE ‘name’
# File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 433 def show_variable(name) variables = select_all("SHOW VARIABLES LIKE '#{name}'") variables.first['Value'] unless variables.empty? end
# File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 439 def connect encoding = @config[:encoding] if encoding @connection.options(Mysql::SET_CHARSET_NAME, encoding) rescue nil end @connection.ssl_set(@config[:sslkey], @config[:sslcert], @config[:sslca], @config[:sslcapath], @config[:sslcipher]) if @config[:sslkey] @connection.real_connect(*@connection_options) execute("SET NAMES '#{encoding}'") if encoding # By default, MySQL 'where id is null' selects the last inserted id. # Turn this off. http://dev.rubyonrails.org/ticket/6778 execute("SET SQL_AUTO_IS_NULL=0") end
# File activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 453 def select(sql, name = nil) @connection.query_with_result = true result = execute(sql, name) rows = result.all_hashes result.free rows end