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 ” 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?()
click to toggle source
CONNECTION MANAGEMENT ====================================
236: def active?
237: if @connection.respond_to?(:stat)
238: @connection.stat
239: else
240: @connection.query 'select 1'
241: end
242:
243:
244: if @connection.respond_to?(:errno)
245: @connection.errno.zero?
246: else
247: true
248: end
249: rescue Mysql::Error
250: false
251: end
add_column(table_name, column_name, type, options = {})
click to toggle source
452: def add_column(table_name, column_name, type, options = {})
453: add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
454: add_column_options!(add_column_sql, options)
455: add_column_position!(add_column_sql, options)
456: execute(add_column_sql)
457: end
add_column_position!(sql, options)
click to toggle source
519: def add_column_position!(sql, options)
520: if options[:first]
521: sql << " FIRST"
522: elsif options[:after]
523: sql << " AFTER #{quote_column_name(options[:after])}"
524: end
525: end
case_sensitive_equality_operator()
click to toggle source
550: def case_sensitive_equality_operator
551: "= BINARY"
552: end
change_column_null(table_name, column_name, null, default = nil)
click to toggle source
464: def change_column_null(table_name, column_name, null, default = nil)
465: column = column_for(table_name, column_name)
466:
467: unless null || default.nil?
468: execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
469: end
470:
471: change_column table_name, column_name, column.sql_type, :null => null
472: end
charset()
click to toggle source
Returns the database character set.
396: def charset
397: show_variable 'character_set_database'
398: end
collation()
click to toggle source
Returns the database collation strategy.
401: def collation
402: show_variable 'collation_database'
403: end
create_database(name, options = {})
click to toggle source
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
379: def create_database(name, options = {})
380: if options[:collation]
381: execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
382: else
383: execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
384: end
385: end
create_savepoint()
click to toggle source
328: def create_savepoint
329: execute("SAVEPOINT #{current_savepoint_name}")
330: end
current_database()
click to toggle source
391: def current_database
392: select_value 'SELECT DATABASE() as db'
393: end
disconnect!()
click to toggle source
258: def disconnect!
259: @connection.close rescue nil
260: end
drop_table(table_name, options = {})
click to toggle source
413: def drop_table(table_name, options = {})
414: super(table_name, options)
415: end
limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
click to toggle source
554: def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
555: where_sql
556: end
primary_key(table)
click to toggle source
Returns just a table’s primary key
545: def primary_key(table)
546: pk_and_sequence = pk_and_sequence_for(table)
547: pk_and_sequence && pk_and_sequence.first
548: end
quote(value, column = nil)
click to toggle source
QUOTING ==================================================
190: def quote(value, column = nil)
191: if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
192: s = column.class.string_to_binary(value).unpack("H*")[0]
193: "x'#{s}'"
194: elsif value.kind_of?(BigDecimal)
195: value.to_s("F")
196: else
197: super
198: end
199: end
quoted_false()
click to toggle source
217: def quoted_false
218: QUOTED_FALSE
219: end
quoted_true()
click to toggle source
213: def quoted_true
214: QUOTED_TRUE
215: end
reconnect!()
click to toggle source
253: def reconnect!
254: disconnect!
255: connect
256: end
release_savepoint()
click to toggle source
336: def release_savepoint
337: execute("RELEASE SAVEPOINT #{current_savepoint_name}")
338: end
rename_table(table_name, new_name)
click to toggle source
448: def rename_table(table_name, new_name)
449: execute "RENAME TABLE #{quote_table_name(table_name)} TO #{quote_table_name(new_name)}"
450: end
reset!()
click to toggle source
262: def reset!
263: if @connection.respond_to?(:change_user)
264:
265:
266: @connection.change_user(@config[:username], @config[:password], @config[:database])
267: configure_connection
268: end
269: end
rollback_to_savepoint()
click to toggle source
332: def rollback_to_savepoint
333: execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
334: end
select_rows(sql, name = nil)
click to toggle source
DATABASE STATEMENTS ======================================
273: def select_rows(sql, name = nil)
274: @connection.query_with_result = true
275: result = execute(sql, name)
276: rows = []
277: result.each { |row| rows << row }
278: result.free
279: @connection.more_results && @connection.next_result
280: rows
281: end
show_variable(name)
click to toggle source
SHOW VARIABLES LIKE ‘name’
528: def show_variable(name)
529: variables = select_all("SHOW VARIABLES LIKE '#{name}'")
530: variables.first['Value'] unless variables.empty?
531: end
type_to_sql(type, limit = nil, precision = nil, scale = nil)
click to toggle source
Maps logical Rails types to MySQL-specific data types.
506: def type_to_sql(type, limit = nil, precision = nil, scale = nil)
507: return super unless type.to_s == 'integer'
508:
509: case limit
510: when 1; 'tinyint'
511: when 2; 'smallint'
512: when 3; 'mediumint'
513: when nil, 4, 11; 'int(11)'
514: when 5..8; 'bigint'
515: else raise(ActiveRecordError, "No integer type has byte size #{limit}")
516: end
517: end
column_for(table_name, column_name)
click to toggle source
635: def column_for(table_name, column_name)
636: unless column = columns(table_name).find { |c| c.name == column_name.to_s }
637: raise "No such column: #{table_name}.#{column_name}"
638: end
639: column
640: end
connect()
click to toggle source
586: def connect
587: encoding = @config[:encoding]
588: if encoding
589: @connection.options(Mysql::SET_CHARSET_NAME, encoding) rescue nil
590: end
591:
592: if @config[:sslca] || @config[:sslkey]
593: @connection.ssl_set(@config[:sslkey], @config[:sslcert], @config[:sslca], @config[:sslcapath], @config[:sslcipher])
594: end
595:
596: @connection.options(Mysql::OPT_CONNECT_TIMEOUT, @config[:connect_timeout]) if @config[:connect_timeout]
597: @connection.options(Mysql::OPT_READ_TIMEOUT, @config[:read_timeout]) if @config[:read_timeout]
598: @connection.options(Mysql::OPT_WRITE_TIMEOUT, @config[:write_timeout]) if @config[:write_timeout]
599:
600: @connection.real_connect(*@connection_options)
601:
602:
603: @connection.reconnect = !!@config[:reconnect] if @connection.respond_to?(:reconnect=)
604:
605: configure_connection
606: end
select(sql, name = nil)
click to toggle source
617: def select(sql, name = nil)
618: @connection.query_with_result = true
619: result = execute(sql, name)
620: rows = []
621: result.each_hash { |row| rows << row }
622: result.free
623: @connection.more_results && @connection.next_result
624: rows
625: end
supports_views?()
click to toggle source
627: def supports_views?
628: version[0] >= 5
629: end
version()
click to toggle source
631: def version
632: @version ||= @connection.server_info.scan(/^(\d+)\.(\d+)\.(\d+)/).flatten.map { |v| v.to_i }
633: end