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
Methods
- adapter_name
- add_limit_offset!
- begin_db_transaction
- change_column
- change_column_default
- columns
- commit_db_transaction
- create_database
- create_table
- delete
- drop_database
- execute
- insert
- native_database_types
- new
- quote_column_name
- quote_string
- recreate_database
- rename_column
- rollback_db_transaction
- select_all
- select_one
- structure_dump
- supports_migrations?
- update
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
[ show 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
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 92
92: def adapter_name
93: 'MySQL'
94: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 179
179: def add_limit_offset!(sql, options)
180: if options[:limit]
181: if options[:offset].blank?
182: sql << " LIMIT #{options[:limit]}"
183: else
184: sql << " LIMIT #{options[:offset]}, #{options[:limit]}"
185: end
186: end
187: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 145
145: def begin_db_transaction
146: execute "BEGIN"
147: rescue Exception
148: # Transactions aren't supported
149: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 208
208: def change_column(table_name, column_name, type, options = {})
209: options[:default] ||= select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Default"]
210:
211: change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{type_to_sql(type, options[:limit])}"
212: add_column_options!(change_column_sql, options)
213: execute(change_column_sql)
214: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 202
202: def change_column_default(table_name, column_name, default)
203: current_type = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Type"]
204:
205: change_column(table_name, column_name, current_type, { :default => default })
206: end
[ show 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
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 151
151: def commit_db_transaction
152: execute "COMMIT"
153: rescue Exception
154: # Transactions aren't supported
155: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 198
198: def create_database(name)
199: execute "CREATE DATABASE #{name}"
200: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 221
221: def create_table(name, options = {})
222: super(name, {:options => "ENGINE=InnoDB"}.merge(options))
223: end
Alias for update
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 194
194: def drop_database(name)
195: execute "DROP DATABASE IF EXISTS #{name}"
196: end
[ show 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: else
133: raise
134: end
135: end
[ show 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
[ show 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
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 164
164: def quote_column_name(name)
165: "`#{name}`"
166: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 168
168: def quote_string(string)
169: Mysql::quote(string)
170: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 189
189: def recreate_database(name)
190: drop_database(name)
191: create_database(name)
192: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 216
216: def rename_column(table_name, column_name, new_column_name)
217: current_type = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Type"]
218: execute "ALTER TABLE #{table_name} CHANGE #{column_name} #{new_column_name} #{current_type}"
219: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 157
157: def rollback_db_transaction
158: execute "ROLLBACK"
159: rescue Exception
160: # Transactions aren't supported
161: end
[ show 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
[ show 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
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 173
173: def structure_dump
174: select_all("SHOW TABLES").inject("") do |structure, table|
175: structure += select_one("SHOW CREATE TABLE #{table.to_a.first.last}")["Create Table"] + ";\n\n"
176: end
177: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 67
67: def supports_migrations?
68: true
69: end
This method is also aliased as
delete
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 137
137: def update(sql, name = nil)
138: execute(sql, name)
139: @connection.affected_rows
140: end