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_with_offset!
- begin_db_transaction
- columns
- commit_db_transaction
- create_database
- create_table
- delete
- drop_database
- execute
- insert
- native_database_types
- new
- quote_column_name
- quote_string
- recreate_database
- rollback_db_transaction
- select_all
- select_one
- structure_dump
- 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 83
83: def initialize(connection, logger, connection_options=nil)
84: super(connection, logger)
85: @connection_options = connection_options
86: end
Public Instance methods
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 88
88: def adapter_name
89: 'MySQL'
90: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 176
176: def add_limit_with_offset!(sql, limit, offset)
177: sql << " LIMIT #{offset}, #{limit}"
178: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 136
136: def begin_db_transaction
137: begin
138: execute "BEGIN"
139: rescue Exception
140: # Transactions aren't supported
141: end
142: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 102
102: def columns(table_name, name = nil)
103: sql = "SHOW FIELDS FROM #{table_name}"
104: columns = []
105: execute(sql, name).each { |field| columns << Column.new(field[0], field[4], field[1]) }
106: columns
107: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 144
144: def commit_db_transaction
145: begin
146: execute "COMMIT"
147: rescue Exception
148: # Transactions aren't supported
149: end
150: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 189
189: def create_database(name)
190: execute "CREATE DATABASE #{name}"
191: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 194
194: def create_table(name)
195: super(name, "ENGINE=InnoDB")
196: end
Alias for update
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 185
185: def drop_database(name)
186: execute "DROP DATABASE IF EXISTS #{name}"
187: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 114
114: def execute(sql, name = nil)
115: begin
116: return log(sql, name, @connection) { |connection| connection.query(sql) }
117: rescue ActiveRecord::StatementInvalid => exception
118: if LOST_CONNECTION_ERROR_MESSAGES.any? { |msg| exception.message.split(":").first =~ /^#{msg}/ }
119: @connection.real_connect(*@connection_options)
120: @logger.info("Retrying invalid statement with reopened connection") if @logger
121: return log(sql, name, @connection) { |connection| connection.query(sql) }
122: else
123: raise
124: end
125: end
126: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 109
109: def insert(sql, name = nil, pk = nil, id_value = nil)
110: execute(sql, name = nil)
111: return id_value || @connection.insert_id
112: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 67
67: def native_database_types
68: {
69: :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY",
70: :string => { :name => "varchar", :limit => 255 },
71: :text => { :name => "text" },
72: :integer => { :name => "int", :limit => 11 },
73: :float => { :name => "float" },
74: :datetime => { :name => "datetime" },
75: :timestamp => { :name => "datetime" },
76: :time => { :name => "datetime" },
77: :date => { :name => "date" },
78: :binary => { :name => "blob" },
79: :boolean => { :name => "tinyint", :limit => 1 }
80: }
81: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 161
161: def quote_column_name(name)
162: return "`#{name}`"
163: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 165
165: def quote_string(s)
166: Mysql::quote(s)
167: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 180
180: def recreate_database(name)
181: drop_database(name)
182: create_database(name)
183: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 152
152: def rollback_db_transaction
153: begin
154: execute "ROLLBACK"
155: rescue Exception
156: # Transactions aren't supported
157: end
158: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 93
93: def select_all(sql, name = nil)
94: select(sql, name)
95: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 97
97: def select_one(sql, name = nil)
98: result = select(sql, name)
99: result.nil? ? nil : result.first
100: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 170
170: def structure_dump
171: select_all("SHOW TABLES").inject("") do |structure, table|
172: structure += select_one("SHOW CREATE TABLE #{table.to_a.first.last}")["Create Table"] + ";\n\n"
173: end
174: end
This method is also aliased as
delete
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 128
128: def update(sql, name = nil)
129: execute(sql, name)
130: @connection.affected_rows
131: end