1
require 'active_record/connection_adapters/abstract_adapter'
4
module MysqlCompat #:nodoc:
5
# add all_hashes method to standard mysql-c bindings or pure ruby version
6
def self.define_all_hashes_method!
7
raise 'Mysql not loaded' unless defined?(::Mysql)
9
target = defined?(Mysql::Result) ? Mysql::Result : MysqlRes
10
return if target.instance_methods.include?('all_hashes') ||
11
target.instance_methods.include?(:all_hashes)
13
# Ruby driver has a version string and returns null values in each_hash
14
# C driver >= 2.7 returns null values in each_hash
15
if Mysql.const_defined?(:VERSION) && (Mysql::VERSION.is_a?(String) || Mysql::VERSION >= 20700)
16
target.class_eval <<-'end_eval'
17
def all_hashes # def all_hashes
19
each_hash { |row| rows << row } # each_hash { |row| rows << row }
24
# adapters before 2.7 don't have a version constant
25
# and don't return null values in each_hash
27
target.class_eval <<-'end_eval'
28
def all_hashes # def all_hashes
30
all_fields = fetch_fields.inject({}) { |fields, f| # all_fields = fetch_fields.inject({}) { |fields, f|
31
fields[f.name] = nil; fields # fields[f.name] = nil; fields
33
each_hash { |row| rows << all_fields.dup.update(row) } # each_hash { |row| rows << all_fields.dup.update(row) }
39
unless target.instance_methods.include?('all_hashes') ||
40
target.instance_methods.include?(:all_hashes)
41
raise "Failed to defined #{target.name}#all_hashes method. Mysql::VERSION = #{Mysql::VERSION.inspect}"
48
# Establishes a connection to the database that's used by all Active Record objects.
49
def self.mysql_connection(config) # :nodoc:
50
config = config.symbolize_keys
53
socket = config[:socket]
54
username = config[:username] ? config[:username].to_s : 'root'
55
password = config[:password].to_s
56
database = config[:database]
58
# Require the MySQL driver and define Mysql::Result.all_hashes
61
require_library_or_gem('mysql')
63
$stderr.puts '!!! The bundled mysql.rb driver has been removed from Rails 2.2. Please install the mysql gem and try again: gem install mysql.'
68
MysqlCompat.define_all_hashes_method!
71
mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslca] || config[:sslkey]
73
default_flags = Mysql.const_defined?(:CLIENT_MULTI_RESULTS) ? Mysql::CLIENT_MULTI_RESULTS : 0
74
options = [host, username, password, database, port, socket, default_flags]
75
ConnectionAdapters::MysqlAdapter.new(mysql, logger, options, config)
79
module ConnectionAdapters
80
class MysqlColumn < Column #:nodoc:
81
def extract_default(default)
82
if sql_type =~ /blob/i || type == :text
84
return null ? nil : ''
86
raise ArgumentError, "#{type} columns cannot have a default value: #{default.inspect}"
88
elsif missing_default_forged_as_empty_string?(default)
96
return false if sql_type =~ /blob/i || type == :text #mysql forbids defaults on blob and text columns
101
def simplified_type(field_type)
102
return :boolean if MysqlAdapter.emulate_booleans && field_type.downcase.index("tinyint(1)")
103
return :string if field_type =~ /enum/i
107
def extract_limit(sql_type)
116
2147483647 # mysql only allows 2^31-1, not 2^32-1, somewhat inconsistently with the tiny/medium/normal cases
118
super # we could return 65535 here, but we leave it undecorated by default
122
when /^mediumint/i; 3
130
# MySQL misreports NOT NULL column default when none is given.
131
# We can't detect this for columns which may have a legitimate ''
132
# default (string) but we can for others (integer, datetime, boolean,
135
# Test whether the column has default '', is not null, and is not
136
# a type allowing default ''.
137
def missing_default_forged_as_empty_string?(default)
138
type != :string && !null && default == ''
142
# The MySQL adapter will work with both Ruby/MySQL, which is a Ruby-based MySQL adapter that comes bundled with Active Record, and with
143
# the faster C-based MySQL/Ruby adapter (available both as a gem and from http://www.tmtm.org/en/mysql/ruby/).
147
# * <tt>:host</tt> - Defaults to "localhost".
148
# * <tt>:port</tt> - Defaults to 3306.
149
# * <tt>:socket</tt> - Defaults to "/tmp/mysql.sock".
150
# * <tt>:username</tt> - Defaults to "root"
151
# * <tt>:password</tt> - Defaults to nothing.
152
# * <tt>:database</tt> - The name of the database. No default, must be provided.
153
# * <tt>:encoding</tt> - (Optional) Sets the client encoding by executing "SET NAMES <encoding>" after connection.
154
# * <tt>:reconnect</tt> - Defaults to false (See MySQL documentation: http://dev.mysql.com/doc/refman/5.0/en/auto-reconnect.html).
155
# * <tt>:sslca</tt> - Necessary to use MySQL with an SSL connection.
156
# * <tt>:sslkey</tt> - Necessary to use MySQL with an SSL connection.
157
# * <tt>:sslcert</tt> - Necessary to use MySQL with an SSL connection.
158
# * <tt>:sslcapath</tt> - Necessary to use MySQL with an SSL connection.
159
# * <tt>:sslcipher</tt> - Necessary to use MySQL with an SSL connection.
161
class MysqlAdapter < AbstractAdapter
165
# By default, the MysqlAdapter will consider all columns of type <tt>tinyint(1)</tt>
166
# as boolean. If you wish to disable this emulation (which was the default
167
# behavior in versions 0.13.1 and earlier) you can add the following line
168
# to your environment.rb file:
170
# ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans = false
171
cattr_accessor :emulate_booleans
172
self.emulate_booleans = true
174
ADAPTER_NAME = 'MySQL'.freeze
176
LOST_CONNECTION_ERROR_MESSAGES = [
177
"Server shutdown in progress",
179
"Lost connection to MySQL server during query",
180
"MySQL server has gone away" ]
182
QUOTED_TRUE, QUOTED_FALSE = '1'.freeze, '0'.freeze
184
NATIVE_DATABASE_TYPES = {
185
:primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY".freeze,
186
:string => { :name => "varchar", :limit => 255 },
187
:text => { :name => "text" },
188
:integer => { :name => "int", :limit => 4 },
189
:float => { :name => "float" },
190
:decimal => { :name => "decimal" },
191
:datetime => { :name => "datetime" },
192
:timestamp => { :name => "datetime" },
193
:time => { :name => "time" },
194
:date => { :name => "date" },
195
:binary => { :name => "blob" },
196
:boolean => { :name => "tinyint", :limit => 1 }
199
def initialize(connection, logger, connection_options, config)
200
super(connection, logger)
201
@connection_options, @config = connection_options, config
202
@quoted_column_names, @quoted_table_names = {}, {}
206
def adapter_name #:nodoc:
210
def supports_migrations? #:nodoc:
214
def supports_primary_key? #:nodoc:
218
def supports_savepoints? #:nodoc:
222
def native_database_types #:nodoc:
223
NATIVE_DATABASE_TYPES
227
# QUOTING ==================================================
229
def quote(value, column = nil)
230
if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
231
s = column.class.string_to_binary(value).unpack("H*")[0]
233
elsif value.kind_of?(BigDecimal)
240
def quote_column_name(name) #:nodoc:
241
@quoted_column_names[name] ||= "`#{name}`"
244
def quote_table_name(name) #:nodoc:
245
@quoted_table_names[name] ||= quote_column_name(name).gsub('.', '`.`')
248
def quote_string(string) #:nodoc:
249
@connection.quote(string)
260
# REFERENTIAL INTEGRITY ====================================
262
def disable_referential_integrity(&block) #:nodoc:
263
old = select_value("SELECT @@FOREIGN_KEY_CHECKS")
266
update("SET FOREIGN_KEY_CHECKS = 0")
269
update("SET FOREIGN_KEY_CHECKS = #{old}")
273
# CONNECTION MANAGEMENT ====================================
276
if @connection.respond_to?(:stat)
279
@connection.query 'select 1'
282
# mysql-ruby doesn't raise an exception when stat fails.
283
if @connection.respond_to?(:errno)
284
@connection.errno.zero?
298
@connection.close rescue nil
302
if @connection.respond_to?(:change_user)
303
# See http://bugs.mysql.com/bug.php?id=33540 -- the workaround way to
304
# reset the connection is to change the user to the same user.
305
@connection.change_user(@config[:username], @config[:password], @config[:database])
310
# DATABASE STATEMENTS ======================================
312
def select_rows(sql, name = nil)
313
@connection.query_with_result = true
314
result = execute(sql, name)
316
result.each { |row| rows << row }
321
# Executes a SQL query and returns a MySQL::Result object. Note that you have to free the Result object after you're done using it.
322
def execute(sql, name = nil) #:nodoc:
323
log(sql, name) { @connection.query(sql) }
324
rescue ActiveRecord::StatementInvalid => exception
325
if exception.message.split(":").first =~ /Packets out of order/
326
raise ActiveRecord::StatementInvalid, "'Packets out of order' error was received from the database. Please update your mysql bindings (gem install mysql) and read http://dev.mysql.com/doc/mysql/en/password-hashing.html for more information. If you're on Windows, use the Instant Rails installer to get the updated mysql bindings."
332
def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
334
id_value || @connection.insert_id
337
def update_sql(sql, name = nil) #:nodoc:
339
@connection.affected_rows
342
def begin_db_transaction #:nodoc:
345
# Transactions aren't supported
348
def commit_db_transaction #:nodoc:
351
# Transactions aren't supported
354
def rollback_db_transaction #:nodoc:
357
# Transactions aren't supported
361
execute("SAVEPOINT #{current_savepoint_name}")
364
def rollback_to_savepoint
365
execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
368
def release_savepoint
369
execute("RELEASE SAVEPOINT #{current_savepoint_name}")
372
def add_limit_offset!(sql, options) #:nodoc:
373
if limit = options[:limit]
374
limit = sanitize_limit(limit)
375
unless offset = options[:offset]
376
sql << " LIMIT #{limit}"
378
sql << " LIMIT #{offset.to_i}, #{limit}"
384
# SCHEMA STATEMENTS ========================================
386
def structure_dump #:nodoc:
388
sql = "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'"
393
select_all(sql).inject("") do |structure, table|
394
table.delete('Table_type')
395
structure += select_one("SHOW CREATE TABLE #{quote_table_name(table.to_a.first.last)}")["Create Table"] + ";\n\n"
399
def recreate_database(name, options = {}) #:nodoc:
401
create_database(name, options)
404
# Create a new MySQL database with optional <tt>:charset</tt> and <tt>:collation</tt>.
405
# Charset defaults to utf8.
408
# create_database 'charset_test', :charset => 'latin1', :collation => 'latin1_bin'
409
# create_database 'matt_development'
410
# create_database 'matt_development', :charset => :big5
411
def create_database(name, options = {})
412
if options[:collation]
413
execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
415
execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
419
def drop_database(name) #:nodoc:
420
execute "DROP DATABASE IF EXISTS `#{name}`"
424
select_value 'SELECT DATABASE() as db'
427
# Returns the database character set.
429
show_variable 'character_set_database'
432
# Returns the database collation strategy.
434
show_variable 'collation_database'
437
def tables(name = nil) #:nodoc:
439
result = execute("SHOW TABLES", name)
440
result.each { |field| tables << field[0] }
445
def drop_table(table_name, options = {})
446
super(table_name, options)
449
def indexes(table_name, name = nil)#:nodoc:
452
result = execute("SHOW KEYS FROM #{quote_table_name(table_name)}", name)
454
if current_index != row[2]
455
next if row[2] == "PRIMARY" # skip the primary key
456
current_index = row[2]
457
indexes << IndexDefinition.new(row[0], row[2], row[1] == "0", [])
460
indexes.last.columns << row[4]
466
def columns(table_name, name = nil)#:nodoc:
467
sql = "SHOW FIELDS FROM #{quote_table_name(table_name)}"
469
result = execute(sql, name)
470
result.each { |field| columns << MysqlColumn.new(field[0], field[4], field[1], field[2] == "YES") }
475
def create_table(table_name, options = {}) #:nodoc:
476
super(table_name, options.reverse_merge(:options => "ENGINE=InnoDB"))
479
def rename_table(table_name, new_name)
480
execute "RENAME TABLE #{quote_table_name(table_name)} TO #{quote_table_name(new_name)}"
483
def change_column_default(table_name, column_name, default) #:nodoc:
484
column = column_for(table_name, column_name)
485
change_column table_name, column_name, column.sql_type, :default => default
488
def change_column_null(table_name, column_name, null, default = nil)
489
column = column_for(table_name, column_name)
491
unless null || default.nil?
492
execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
495
change_column table_name, column_name, column.sql_type, :null => null
498
def change_column(table_name, column_name, type, options = {}) #:nodoc:
499
column = column_for(table_name, column_name)
501
unless options_include_default?(options)
502
options[:default] = column.default
505
unless options.has_key?(:null)
506
options[:null] = column.null
509
change_column_sql = "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
510
add_column_options!(change_column_sql, options)
511
execute(change_column_sql)
514
def rename_column(table_name, column_name, new_column_name) #:nodoc:
516
if column = columns(table_name).find { |c| c.name == column_name.to_s }
517
options[:default] = column.default
518
options[:null] = column.null
520
raise ActiveRecordError, "No such column: #{table_name}.#{column_name}"
522
current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"]
523
rename_column_sql = "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}"
524
add_column_options!(rename_column_sql, options)
525
execute(rename_column_sql)
528
# Maps logical Rails types to MySQL-specific data types.
529
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
530
return super unless type.to_s == 'integer'
536
when nil, 4, 11; 'int(11)' # compatibility with MySQL default
538
else raise(ActiveRecordError, "No integer type has byte size #{limit}")
543
# SHOW VARIABLES LIKE 'name'
544
def show_variable(name)
545
variables = select_all("SHOW VARIABLES LIKE '#{name}'")
546
variables.first['Value'] unless variables.empty?
549
# Returns a table's primary key and belonging sequence.
550
def pk_and_sequence_for(table) #:nodoc:
552
result = execute("describe #{quote_table_name(table)}")
553
result.each_hash do |h|
554
keys << h["Field"]if h["Key"] == "PRI"
557
keys.length == 1 ? [keys.first, nil] : nil
560
# Returns just a table's primary key
561
def primary_key(table)
562
pk_and_sequence = pk_and_sequence_for(table)
563
pk_and_sequence && pk_and_sequence.first
566
def case_sensitive_equality_operator
570
def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
576
encoding = @config[:encoding]
578
@connection.options(Mysql::SET_CHARSET_NAME, encoding) rescue nil
581
if @config[:sslca] || @config[:sslkey]
582
@connection.ssl_set(@config[:sslkey], @config[:sslcert], @config[:sslca], @config[:sslcapath], @config[:sslcipher])
585
@connection.options(Mysql::OPT_CONNECT_TIMEOUT, @config[:connect_timeout]) if @config[:connect_timeout]
586
@connection.options(Mysql::OPT_READ_TIMEOUT, @config[:read_timeout]) if @config[:read_timeout]
587
@connection.options(Mysql::OPT_WRITE_TIMEOUT, @config[:write_timeout]) if @config[:write_timeout]
589
@connection.real_connect(*@connection_options)
591
# reconnect must be set after real_connect is called, because real_connect sets it to false internally
592
@connection.reconnect = !!@config[:reconnect] if @connection.respond_to?(:reconnect=)
597
def configure_connection
598
encoding = @config[:encoding]
599
execute("SET NAMES '#{encoding}'") if encoding
601
# By default, MySQL 'where id is null' selects the last inserted id.
602
# Turn this off. http://dev.rubyonrails.org/ticket/6778
603
execute("SET SQL_AUTO_IS_NULL=0")
606
def select(sql, name = nil)
607
@connection.query_with_result = true
608
result = execute(sql, name)
609
rows = result.all_hashes
619
@version ||= @connection.server_info.scan(/^(\d+)\.(\d+)\.(\d+)/).flatten.map { |v| v.to_i }
622
def column_for(table_name, column_name)
623
unless column = columns(table_name).find { |c| c.name == column_name.to_s }
624
raise "No such column: #{table_name}.#{column_name}"