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

Public Instance methods

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

[Source]

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

[Source]

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

[Source]

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

[Source]

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

[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:           :decimal     => { :name => "decimal" },
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:         elsif value.kind_of?(BigDecimal)
123:           "'#{value.to_s("F")}'"
124:         else
125:           super
126:         end
127:       end

[Source]

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

[Source]

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

[Source]

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

[Source]

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

[Validate]