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.
- :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
- active?
- add_limit_offset!
- current_database
- disconnect!
- native_database_types
- new
- quote
- quoted_false
- quoted_true
- reconnect!
- rename_table
LOST_CONNECTION_ERROR_MESSAGES | = | [ "Server shutdown in progress", "Broken pipe", "Lost connection to MySQL server during query", "MySQL server has gone away" |
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 83 83: def initialize(connection, logger, connection_options, config) 84: super(connection, logger) 85: @connection_options, @config = connection_options, config 86: @null_values_in_each_hash = Mysql.const_defined?(:VERSION) 87: connect 88: end
CONNECTION MANAGEMENT ====================================
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 145 145: def active? 146: if @connection.respond_to?(:stat) 147: @connection.stat 148: else 149: @connection.query 'select 1' 150: end 151: 152: # mysql-ruby doesn't raise an exception when stat fails. 153: if @connection.respond_to?(:errno) 154: @connection.errno.zero? 155: else 156: true 157: end 158: rescue Mysql::Error 159: false 160: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 225 225: def add_limit_offset!(sql, options) #:nodoc 226: if limit = options[:limit] 227: unless offset = options[:offset] 228: sql << " LIMIT #{limit}" 229: else 230: sql << " LIMIT #{offset}, #{limit}" 231: end 232: end 233: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 264 264: def current_database 265: select_one("SELECT DATABASE() as db")["db"] 266: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 167 167: def disconnect! 168: @connection.close rescue nil 169: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 98 98: def native_database_types #:nodoc 99: { 100: :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY", 101: :string => { :name => "varchar", :limit => 255 }, 102: :text => { :name => "text" }, 103: :integer => { :name => "int", :limit => 11 }, 104: :float => { :name => "float" }, 105: :datetime => { :name => "datetime" }, 106: :timestamp => { :name => "datetime" }, 107: :time => { :name => "time" }, 108: :date => { :name => "date" }, 109: :binary => { :name => "blob" }, 110: :boolean => { :name => "tinyint", :limit => 1 } 111: } 112: end
QUOTING ==================================================
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 117 117: def quote(value, column = nil) 118: if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary) 119: s = column.class.string_to_binary(value).unpack("H*")[0] 120: "x'#{s}'" 121: else 122: super 123: end 124: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 138 138: def quoted_false 139: "0" 140: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 134 134: def quoted_true 135: "1" 136: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 162 162: def reconnect! 163: disconnect! 164: connect 165: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 300 300: def rename_table(name, new_name) 301: execute "RENAME TABLE #{name} TO #{new_name}" 302: end