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

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 87
87:       def initialize(connection, logger, connection_options=nil)
88:         super(connection, logger)
89:         @connection_options = connection_options
90:       end

Public Instance methods

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 92
92:       def adapter_name
93:         'MySQL'
94:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 181
181:       def add_limit_offset!(sql, options)
182:         if options[:limit]
183:           if options[:offset].blank?
184:             sql << " LIMIT #{options[:limit]}"
185:           else
186:             sql << " LIMIT #{options[:offset]}, #{options[:limit]}"
187:           end
188:         end
189:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 147
147:       def begin_db_transaction
148:         execute "BEGIN"
149:       rescue Exception
150:         # Transactions aren't supported
151:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 210
210:       def change_column(table_name, column_name, type, options = {})
211:         options[:default] ||= select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Default"]
212:         
213:         change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{type_to_sql(type, options[:limit])}"
214:         add_column_options!(change_column_sql, options)
215:         execute(change_column_sql)
216:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 204
204:       def change_column_default(table_name, column_name, default)
205:         current_type = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Type"]
206: 
207:         change_column(table_name, column_name, current_type, { :default => default })
208:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 105
105:       def columns(table_name, name = nil)
106:         sql = "SHOW FIELDS FROM #{table_name}"
107:         columns = []
108:         execute(sql, name).each { |field| columns << Column.new(field[0], field[4], field[1]) }
109:         columns
110:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 153
153:       def commit_db_transaction
154:         execute "COMMIT"
155:       rescue Exception
156:         # Transactions aren't supported
157:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 200
200:       def create_database(name)
201:         execute "CREATE DATABASE #{name}"
202:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 223
223:       def create_table(name, options = {})
224:         super(name, {:options => "ENGINE=InnoDB"}.merge(options))
225:       end
delete(sql, name = nil)

Alias for update

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 196
196:       def drop_database(name)
197:         execute "DROP DATABASE IF EXISTS #{name}"
198:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 117
117:       def execute(sql, name = nil, retries = 2)
118:         unless @logger
119:           @connection.query(sql)
120:         else
121:           log(sql, name) { @connection.query(sql) }
122:         end
123:       rescue ActiveRecord::StatementInvalid => exception
124:         if LOST_CONNECTION_ERROR_MESSAGES.any? { |msg| exception.message.split(":").first =~ /^#{msg}/ }
125:           @connection.real_connect(*@connection_options)
126:           unless @logger
127:             @connection.query(sql)
128:           else
129:             @logger.info "Retrying invalid statement with reopened connection"
130:             log(sql, name) { @connection.query(sql) }
131:           end
132:         elsif exception.message.split(":").first =~ /Packets out of order/
133:           raise ActiveRecord::StatementInvalid, "'Packets out of order' error was received from the database. Please update your mysql bindings (gem update mysql) and read http://dev.mysql.com/doc/mysql/en/password-hashing.html for more information."
134:         else
135:           raise
136:         end
137:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 112
112:       def insert(sql, name = nil, pk = nil, id_value = nil)
113:         execute(sql, name = nil)
114:         id_value || @connection.insert_id
115:       end

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 71
71:       def native_database_types
72:         {
73:           :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY",
74:           :string      => { :name => "varchar", :limit => 255 },
75:           :text        => { :name => "text" },
76:           :integer     => { :name => "int", :limit => 11 },
77:           :float       => { :name => "float" },
78:           :datetime    => { :name => "datetime" },
79:           :timestamp   => { :name => "datetime" },
80:           :time        => { :name => "datetime" },
81:           :date        => { :name => "date" },
82:           :binary      => { :name => "blob" },
83:           :boolean     => { :name => "tinyint", :limit => 1 }
84:         }
85:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 166
166:       def quote_column_name(name)
167:         "`#{name}`"
168:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 170
170:       def quote_string(string)
171:         Mysql::quote(string)
172:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 191
191:       def recreate_database(name)
192:         drop_database(name)
193:         create_database(name)
194:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 218
218:       def rename_column(table_name, column_name, new_column_name)
219:         current_type = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Type"]
220:         execute "ALTER TABLE #{table_name} CHANGE #{column_name} #{new_column_name} #{current_type}"
221:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 159
159:       def rollback_db_transaction
160:         execute "ROLLBACK"
161:       rescue Exception
162:         # Transactions aren't supported
163:       end

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 96
96:       def select_all(sql, name = nil)
97:         select(sql, name)
98:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 100
100:       def select_one(sql, name = nil)
101:         result = select(sql, name)
102:         result.nil? ? nil : result.first
103:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 175
175:       def structure_dump
176:         select_all("SHOW TABLES").inject("") do |structure, table|
177:           structure += select_one("SHOW CREATE TABLE #{table.to_a.first.last}")["Create Table"] + ";\n\n"
178:         end
179:       end

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 67
67:       def supports_migrations?
68:         true
69:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 139
139:       def update(sql, name = nil)
140:         execute(sql, name)
141:         @connection.affected_rows
142:       end

[Validate]