- A
- B
- C
- E
- L
- N
- P
- Q
- R
- S
- T
- CLASS ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::BindSubstitution
- CLASS ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::Column
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', '0' |
NATIVE_DATABASE_TYPES | = | { :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY", :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 } } |
By default, the MysqlAdapter will consider all columns of type tinyint(1) as boolean. If you wish to disable this emulation (which was the default behavior in versions 0.13.1 and earlier) you can add the following line to your application.rb file:
ActiveRecord::ConnectionAdapters::Mysql[2]Adapter.emulate_booleans = false
FIXME: Make the first parameter more similar for the two adapters
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 131 131: def initialize(connection, logger, connection_options, config) 132: super(connection, logger) 133: @connection_options, @config = connection_options, config 134: @quoted_column_names, @quoted_table_names = {}, {} 135: 136: if config.fetch(:prepared_statements) { true } 137: @visitor = Arel::Visitors::MySQL.new self 138: else 139: @visitor = BindSubstitution.new self 140: end 141: end
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 458 458: def add_column(table_name, column_name, type, options = {}) 459: execute("ALTER TABLE #{quote_table_name(table_name)} #{add_column_sql(table_name, column_name, type, options)}") 460: end
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 510 510: def add_column_position!(sql, options) 511: if options[:first] 512: sql << " FIRST" 513: elsif options[:after] 514: sql << " AFTER #{quote_column_name(options[:after])}" 515: end 516: end
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 462 462: def change_column_default(table_name, column_name, default) 463: column = column_for(table_name, column_name) 464: change_column table_name, column_name, column.sql_type, :default => default 465: end
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 467 467: def change_column_null(table_name, column_name, null, default = nil) 468: column = column_for(table_name, column_name) 469: 470: unless null || default.nil? 471: execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL") 472: end 473: 474: change_column table_name, column_name, column.sql_type, :null => null 475: end
Returns the database character set.
Returns the database collation strategy.
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
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 346 346: def create_database(name, options = {}) 347: if options[:collation] 348: execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`" 349: else 350: execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`" 351: end 352: end
Executes the SQL statement in the context of this connection.
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 239 239: def execute(sql, name = nil) 240: if name == :skip_logging 241: @connection.query(sql) 242: else 243: log(sql, name) { @connection.query(sql) } 244: end 245: rescue ActiveRecord::StatementInvalid => exception 246: if exception.message.split(":").first =~ /Packets out of order/ 247: 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." 248: else 249: raise 250: end 251: end
Returns a table’s primary key and belonging sequence.
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 525 525: def pk_and_sequence_for(table) 526: execute_and_free("SHOW CREATE TABLE #{quote_table_name(table)}", 'SCHEMA') do |result| 527: create_table = each_hash(result).first[:"Create Table"] 528: if create_table.to_s =~ /PRIMARY KEY\s+(?:USING\s+\w+\s+)?\((.+)\)/ 529: keys = $1.split(",").map { |key| key.gsub(/[`"]/, "") } 530: keys.length == 1 ? [keys.first, nil] : nil 531: else 532: nil 533: end 534: end 535: end
Returns just a table’s primary key
QUOTING ==================================================
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 196 196: def quote(value, column = nil) 197: if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary) 198: s = column.class.string_to_binary(value).unpack("H*")[0] 199: "x'#{s}'" 200: elsif value.kind_of?(BigDecimal) 201: value.to_s("F") 202: else 203: super 204: end 205: end
Drops the database specified on the name attribute and creates it again using the provided options.
Renames a table.
Example:
rename_table('octopuses', 'octopi')
SHOW VARIABLES LIKE ‘name’
Technically MySQL allows to create indexes with the sort order syntax but at the moment (5.5) it doesn’t yet implement them
Returns true, since this connection adapter supports migrations.
Returns true, since this connection adapter supports savepoints.
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 386 386: def table_exists?(name) 387: return false unless name 388: return true if tables(nil, nil, name).any? 389: 390: name = name.to_s 391: schema, table = name.split('.', 2) 392: 393: unless table # A table was provided without a schema 394: table = schema 395: schema = nil 396: end 397: 398: tables(nil, schema, table).any? 399: end
Maps logical Rails types to MySQL-specific data types.
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 486 486: def type_to_sql(type, limit = nil, precision = nil, scale = nil) 487: case type.to_s 488: when 'integer' 489: case limit 490: when 1; 'tinyint' 491: when 2; 'smallint' 492: when 3; 'mediumint' 493: when nil, 4, 11; 'int(11)' # compatibility with MySQL default 494: when 5..8; 'bigint' 495: else raise(ActiveRecordError, "No integer type has byte size #{limit}") 496: end 497: when 'text' 498: case limit 499: when 0..0xff; 'tinytext' 500: when nil, 0x100..0xffff; 'text' 501: when 0x10000..0xffffff; 'mediumtext' 502: when 0x1000000..0xffffffff; 'longtext' 503: else raise(ActiveRecordError, "No text type has character length #{limit}") 504: end 505: else 506: super 507: end 508: end
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 597 597: def add_column_sql(table_name, column_name, type, options = {}) 598: add_column_sql = "ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}" 599: add_column_options!(add_column_sql, options) 600: add_column_position!(add_column_sql, options) 601: add_column_sql 602: end
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 561 561: def add_index_length(option_strings, column_names, options = {}) 562: if options.is_a?(Hash) && length = options[:length] 563: case length 564: when Hash 565: column_names.each {|name| option_strings[name] += "(#{length[name]})" if length.has_key?(name) && length[name].present?} 566: when Fixnum 567: column_names.each {|name| option_strings[name] += "(#{length})"} 568: end 569: end 570: 571: return option_strings 572: end
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 642 642: def add_index_sql(table_name, column_name, options = {}) 643: index_name, index_type, index_columns = add_index_options(table_name, column_name, options) 644: "ADD #{index_type} INDEX #{index_name} (#{index_columns})" 645: end
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 604 604: def change_column_sql(table_name, column_name, type, options = {}) 605: column = column_for(table_name, column_name) 606: 607: unless options_include_default?(options) 608: options[:default] = column.default 609: end 610: 611: unless options.has_key?(:null) 612: options[:null] = column.null 613: end 614: 615: change_column_sql = "CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}" 616: add_column_options!(change_column_sql, options) 617: add_column_position!(change_column_sql, options) 618: change_column_sql 619: end
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 574 574: def quoted_columns_for_index(column_names, options = {}) 575: option_strings = Hash[column_names.map {|name| [name, '']}] 576: 577: # add index length 578: option_strings = add_index_length(option_strings, column_names, options) 579: 580: # add index sort order 581: option_strings = add_index_sort_order(option_strings, column_names, options) 582: 583: column_names.map {|name| quote_column_name(name) + option_strings[name]} 584: end
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 621 621: def rename_column_sql(table_name, column_name, new_column_name) 622: options = {} 623: 624: if column = columns(table_name).find { |c| c.name == column_name.to_s } 625: options[:default] = column.default 626: options[:null] = column.null 627: else 628: raise ActiveRecordError, "No such column: #{table_name}.#{column_name}" 629: end 630: 631: current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"] 632: rename_column_sql = "CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}" 633: add_column_options!(rename_column_sql, options) 634: rename_column_sql 635: end
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 586 586: def translate_exception(exception, message) 587: case error_number(exception) 588: when 1062 589: RecordNotUnique.new(message, exception) 590: when 1452 591: InvalidForeignKey.new(message, exception) 592: else 593: super 594: end 595: end