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

External Aliases

update -> delete

Public Class methods

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 84
84:       def initialize(connection, logger, connection_options, config)
85:         super(connection, logger)
86:         @connection_options, @config = connection_options, config
87:         @null_values_in_each_hash = Mysql.const_defined?(:VERSION)
88:         connect
89:       end

Public Instance methods

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 146
146:       def active?
147:         if @connection.respond_to?(:stat)
148:           @connection.stat
149:         else
150:           @connection.query 'select 1'
151:         end
152: 
153:         # mysql-ruby doesn't raise an exception when stat fails.
154:         if @connection.respond_to?(:errno)
155:           @connection.errno.zero?
156:         else
157:           true
158:         end
159:       rescue Mysql::Error
160:         false
161:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 226
226:       def add_limit_offset!(sql, options) #:nodoc
227:         if limit = options[:limit]
228:           unless offset = options[:offset]
229:             sql << " LIMIT #{limit}"
230:           else
231:             sql << " LIMIT #{offset}, #{limit}"
232:           end
233:         end
234:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 265
265:       def current_database
266:         select_one("SELECT DATABASE() as db")["db"]
267:       end

[Source]

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 99
 99:       def native_database_types #:nodoc
100:         {
101:           :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY",
102:           :string      => { :name => "varchar", :limit => 255 },
103:           :text        => { :name => "text" },
104:           :integer     => { :name => "int", :limit => 11 },
105:           :float       => { :name => "float" },
106:           :datetime    => { :name => "datetime" },
107:           :timestamp   => { :name => "datetime" },
108:           :time        => { :name => "time" },
109:           :date        => { :name => "date" },
110:           :binary      => { :name => "blob" },
111:           :boolean     => { :name => "tinyint", :limit => 1 }
112:         }
113:       end

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 118
118:       def quote(value, column = nil)
119:         if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
120:           s = column.class.string_to_binary(value).unpack("H*")[0]
121:           "x'#{s}'"
122:         else
123:           super
124:         end
125:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 139
139:       def quoted_false
140:         "0"
141:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 135
135:       def quoted_true
136:         "1"
137:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 163
163:       def reconnect!
164:         disconnect!
165:         connect
166:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 301
301:       def rename_table(name, new_name)
302:         execute "RENAME TABLE #{name} TO #{new_name}"
303:       end

[Validate]