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.
- :encoding - (Optional) Sets the client encoding by executing "SET NAMES <encoding>" after connection.
- :reconnect - Defaults to false (See MySQL documentation: dev.mysql.com/doc/refman/5.0/en/auto-reconnect.html).
- :sslca - Necessary to use MySQL with an SSL connection.
- :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.
- active?
- case_sensitive_equality_operator
- change_column_null
- charset
- collation
- create_database
- create_savepoint
- current_database
- disconnect!
- drop_table
- limited_update_conditions
- new
- quote
- quoted_false
- quoted_true
- reconnect!
- release_savepoint
- rename_table
- reset!
- rollback_to_savepoint
- select_rows
- show_variable
- type_to_sql
| ADAPTER_NAME | = | 'MySQL'.freeze |
| LOST_CONNECTION_ERROR_MESSAGES | = | [ "Server shutdown in progress", "Broken pipe", "Lost connection to MySQL server during query", "MySQL server has gone away" ] |
| QUOTED_FALSE | = | '1'.freeze, '0'.freeze |
| NATIVE_DATABASE_TYPES | = | { :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY".freeze, :string => { :name => "varchar", :limit => 255 }, :text => { :name => "text" }, :integer => { :name => "int", :limit => 4 }, :float => { :name => "float" }, :decimal => { :name => "decimal" }, :datetime => { :name => "datetime" }, :timestamp => { :name => "datetime" }, :time => { :name => "time" }, :date => { :name => "date" }, :binary => { :name => "blob" }, :boolean => { :name => "tinyint", :limit => 1 } |
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 202
202: def initialize(connection, logger, connection_options, config)
203: super(connection, logger)
204: @connection_options, @config = connection_options, config
205: @quoted_column_names, @quoted_table_names = {}, {}
206: connect
207: end
CONNECTION MANAGEMENT ====================================
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 274
274: def active?
275: if @connection.respond_to?(:stat)
276: @connection.stat
277: else
278: @connection.query 'select 1'
279: end
280:
281: # mysql-ruby doesn't raise an exception when stat fails.
282: if @connection.respond_to?(:errno)
283: @connection.errno.zero?
284: else
285: true
286: end
287: rescue Mysql::Error
288: false
289: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 559
559: def case_sensitive_equality_operator
560: "= BINARY"
561: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 487
487: def change_column_null(table_name, column_name, null, default = nil)
488: column = column_for(table_name, column_name)
489:
490: unless null || default.nil?
491: execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
492: end
493:
494: change_column table_name, column_name, column.sql_type, :null => null
495: end
Returns the database character set.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 427
427: def charset
428: show_variable 'character_set_database'
429: end
Returns the database collation strategy.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 432
432: def collation
433: show_variable 'collation_database'
434: end
Create a new MySQL database with optional :charset and :collation. Charset defaults to utf8.
Example:
create_database 'charset_test', :charset => 'latin1', :collation => 'latin1_bin' create_database 'matt_development' create_database 'matt_development', :charset => :big5
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 410
410: def create_database(name, options = {})
411: if options[:collation]
412: execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
413: else
414: execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
415: end
416: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 359
359: def create_savepoint
360: execute("SAVEPOINT #{current_savepoint_name}")
361: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 422
422: def current_database
423: select_value 'SELECT DATABASE() as db'
424: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 296
296: def disconnect!
297: @connection.close rescue nil
298: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 444
444: def drop_table(table_name, options = {})
445: super(table_name, options)
446: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 563
563: def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
564: where_sql
565: end
QUOTING ==================================================
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 228
228: def quote(value, column = nil)
229: if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
230: s = column.class.string_to_binary(value).unpack("H*")[0]
231: "x'#{s}'"
232: elsif value.kind_of?(BigDecimal)
233: value.to_s("F")
234: else
235: super
236: end
237: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 255
255: def quoted_false
256: QUOTED_FALSE
257: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 251
251: def quoted_true
252: QUOTED_TRUE
253: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 291
291: def reconnect!
292: disconnect!
293: connect
294: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 367
367: def release_savepoint
368: execute("RELEASE SAVEPOINT #{current_savepoint_name}")
369: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 478
478: def rename_table(table_name, new_name)
479: execute "RENAME TABLE #{quote_table_name(table_name)} TO #{quote_table_name(new_name)}"
480: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 300
300: def reset!
301: if @connection.respond_to?(:change_user)
302: # See http://bugs.mysql.com/bug.php?id=33540 -- the workaround way to
303: # reset the connection is to change the user to the same user.
304: @connection.change_user(@config[:username], @config[:password], @config[:database])
305: configure_connection
306: end
307: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 363
363: def rollback_to_savepoint
364: execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
365: end
DATABASE STATEMENTS ======================================
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 311
311: def select_rows(sql, name = nil)
312: @connection.query_with_result = true
313: result = execute(sql, name)
314: rows = []
315: result.each { |row| rows << row }
316: result.free
317: rows
318: end
SHOW VARIABLES LIKE ‘name‘
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 543
543: def show_variable(name)
544: variables = select_all("SHOW VARIABLES LIKE '#{name}'")
545: variables.first['Value'] unless variables.empty?
546: end
Maps logical Rails types to MySQL-specific data types.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb, line 528
528: def type_to_sql(type, limit = nil, precision = nil, scale = nil)
529: return super unless type.to_s == 'integer'
530:
531: case limit
532: when 1; 'tinyint'
533: when 2; 'smallint'
534: when 3; 'mediumint'
535: when nil, 4, 11; 'int(11)' # compatibility with MySQL default
536: when 5..8; 'bigint'
537: else raise(ActiveRecordError, "No integer type has byte size #{limit}")
538: end
539: end