Class: ActiveRecord::ConnectionAdapters::Mysql2Adapter

Inherits:
AbstractMysqlAdapter show all
Includes:
ActiveRecord::ConnectionAdapters::MySQL::DatabaseStatements
Defined in:
lib/active_record/connection_adapters/mysql2_adapter.rb

Constant Summary collapse

ER_BAD_DB_ERROR =
1049
ER_ACCESS_DENIED_ERROR =
1045
ER_CONN_HOST_ERROR =
2003
ER_UNKNOWN_HOST_ERROR =
2005
ADAPTER_NAME =
"Mysql2"

Constants inherited from AbstractMysqlAdapter

AbstractMysqlAdapter::NATIVE_DATABASE_TYPES, AbstractMysqlAdapter::TYPE_MAP, AbstractMysqlAdapter::TYPE_MAP_WITH_BOOLEAN

Constants inherited from AbstractAdapter

AbstractAdapter::COMMENT_REGEX, AbstractAdapter::SIMPLE_INT, AbstractAdapter::TYPE_MAP

Instance Attribute Summary

Attributes inherited from AbstractAdapter

#lock, #logger, #owner, #pool, #visitor

Attributes included from QueryCache

#query_cache, #query_cache_enabled

Attributes included from DatabaseStatements

#transaction_manager

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveRecord::ConnectionAdapters::MySQL::DatabaseStatements

#exec_delete, #exec_query, #execute, #explain, #high_precision_current_timestamp, #query, #select_all, #write_query?

Methods inherited from AbstractMysqlAdapter

#add_index, #add_sql_comment!, #begin_db_transaction, #begin_isolated_db_transaction, #build_insert_sql, #case_sensitive_comparison, #change_column, #change_column_comment, #change_column_default, #change_column_null, #change_table_comment, #charset, #check_constraints, #check_version, #collation, #columns_for_distinct, #commit_db_transaction, #create_database, #current_database, #default_index_type?, #disable_referential_integrity, #drop_database, #drop_table, #empty_insert_statement_value, #emulate_booleans, #exec_rollback_db_transaction, #execute, #execute_and_free, #field_ordered_value, #foreign_keys, #get_advisory_lock, #get_database_version, #index_algorithms, #mariadb?, #native_database_types, #primary_keys, #recreate_database, #release_advisory_lock, #rename_column, #rename_index, #rename_table, #show_variable, #strict_mode?, #supports_advisory_locks?, #supports_bulk_alter?, #supports_check_constraints?, #supports_common_table_expressions?, #supports_datetime_with_precision?, #supports_explain?, #supports_expression_index?, #supports_foreign_keys?, #supports_index_sort_order?, #supports_indexes_in_create?, #supports_insert_on_duplicate_skip?, #supports_insert_on_duplicate_update?, #supports_optimizer_hints?, #supports_transaction_isolation?, #supports_views?, #supports_virtual_columns?, #table_comment, #table_options

Methods included from ActiveRecord::ConnectionAdapters::MySQL::SchemaStatements

#create_schema_dumper, #create_table, #indexes, #internal_string_options_for_primary_key, #remove_column, #table_alias_length, #type_to_sql, #update_table_definition

Methods included from ActiveRecord::ConnectionAdapters::MySQL::Quoting

#column_name_matcher, #column_name_with_order_matcher, #quote_bound_value, #quote_column_name, #quote_table_name, #quoted_binary, #quoted_date, #type_cast, #unquoted_false, #unquoted_true

Methods inherited from AbstractAdapter

#adapter_name, #advisory_locks_enabled?, #all_foreign_keys_valid?, #async_enabled?, #build_insert_sql, build_read_query_regexp, #case_insensitive_comparison, #case_sensitive_comparison, #check_if_write_query, #check_version, #clear_cache!, #close, #connection_class, #create_enum, #database_version, #default_index_type?, #default_uniqueness_comparison, #disable_extension, #disable_referential_integrity, #enable_extension, #expire, #extensions, #field_ordered_value, #get_advisory_lock, #get_database_version, #index_algorithms, #lease, #migration_context, #migrations_paths, #prefetch_primary_key?, #prepared_statements?, #prepared_statements_disabled_cache, #preventing_writes?, quoted_column_names, quoted_table_names, #raw_connection, #release_advisory_lock, #replica?, #requires_reloading?, #role, #schema_cache, #schema_cache=, #schema_migration, #schema_version, #seconds_idle, #shard, #steal!, #supports_advisory_locks?, #supports_bulk_alter?, #supports_check_constraints?, #supports_common_table_expressions?, #supports_concurrent_connections?, #supports_datetime_with_precision?, #supports_ddl_transactions?, #supports_deferrable_constraints?, #supports_explain?, #supports_expression_index?, #supports_extensions?, #supports_foreign_keys?, #supports_foreign_tables?, #supports_index_sort_order?, #supports_indexes_in_create?, #supports_insert_conflict_target?, #supports_insert_on_duplicate_skip?, #supports_insert_on_duplicate_update?, #supports_insert_returning?, #supports_materialized_views?, #supports_optimizer_hints?, #supports_partial_index?, #supports_partitioned_indexes?, #supports_transaction_isolation?, #supports_validate_constraints?, #supports_views?, #supports_virtual_columns?, #throw_away!, type_cast_config_to_boolean, type_cast_config_to_integer, #unprepared_statement, #use_metadata_table?, #valid_type?, #verify!, #with_instrumenter

Methods included from Savepoints

#create_savepoint, #current_savepoint_name, #exec_rollback_to_savepoint, #release_savepoint

Methods included from QueryCache

#cache, #clear_query_cache, dirties_query_cache, #disable_query_cache!, #enable_query_cache!, included, #select_all, #uncached

Methods included from DatabaseLimits

#index_name_length, #max_identifier_length, #table_alias_length

Methods included from Quoting

#column_name_matcher, #column_name_with_order_matcher, #lookup_cast_type_from_column, #quote, #quote_bound_value, #quote_column_name, #quote_default_expression, #quote_table_name, #quote_table_name_for_assignment, #quoted_binary, #quoted_date, #quoted_false, #quoted_time, #quoted_true, #sanitize_as_sql_comment, #type_cast, #unquoted_false, #unquoted_true

Methods included from DatabaseStatements

#add_transaction_record, #begin_db_transaction, #begin_isolated_db_transaction, #cacheable_query, #commit_db_transaction, #default_sequence_name, #delete, #empty_insert_statement_value, #exec_delete, #exec_insert, #exec_insert_all, #exec_query, #exec_rollback_db_transaction, #exec_update, #execute, #explain, #high_precision_current_timestamp, #insert, #insert_fixture, #insert_fixtures_set, #mark_transaction_written_if_write, #query, #query_value, #query_values, #reset_sequence!, #reset_transaction, #rollback_db_transaction, #rollback_to_savepoint, #sanitize_limit, #select_all, #select_one, #select_rows, #select_value, #select_values, #to_sql, #transaction, #transaction_isolation_levels, #transaction_open?, #truncate, #truncate_tables, #update, #with_yaml_fallback, #write_query?

Methods included from SchemaStatements

#add_check_constraint, #add_column, #add_columns, #add_foreign_key, #add_index, #add_index_options, #add_reference, #add_timestamps, #assume_migrated_upto_version, #change_column, #change_column_comment, #change_column_default, #change_column_null, #change_table, #change_table_comment, #check_constraint_options, #check_constraints, #column_exists?, #columns, #columns_for_distinct, #create_join_table, #create_schema_dumper, #create_table, #data_source_exists?, #data_sources, #distinct_relation_for_primary_key, #drop_join_table, #drop_table, #dump_schema_information, #foreign_key_column_for, #foreign_key_exists?, #foreign_key_options, #foreign_keys, #index_algorithm, #index_exists?, #index_name, #index_name_exists?, #indexes, #internal_string_options_for_primary_key, #native_database_types, #options_include_default?, #primary_key, #quoted_columns_for_index, #remove_check_constraint, #remove_column, #remove_columns, #remove_foreign_key, #remove_index, #remove_reference, #remove_timestamps, #rename_column, #rename_index, #rename_table, #table_alias_for, #table_comment, #table_exists?, #table_options, #tables, #type_to_sql, #update_table_definition, #view_exists?, #views

Constructor Details

#initialize(connection, logger, connection_options, config) ⇒ Mysql2Adapter

Returns a new instance of Mysql2Adapter.



58
59
60
61
62
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 58

def initialize(connection, logger, connection_options, config)
  superclass_config = config.reverse_merge(prepared_statements: false)
  super(connection, logger, connection_options, superclass_config)
  configure_connection
end

Class Method Details

.database_exists?(config) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 64

def self.database_exists?(config)
  !!ActiveRecord::Base.mysql2_connection(config)
rescue ActiveRecord::NoDatabaseError
  false
end

.new_client(config) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 43

def new_client(config)
  Mysql2::Client.new(config)
rescue Mysql2::Error => error
  if error.error_number == ConnectionAdapters::Mysql2Adapter::ER_BAD_DB_ERROR
    raise ActiveRecord::NoDatabaseError.db_error(config[:database])
  elsif error.error_number == ConnectionAdapters::Mysql2Adapter::ER_ACCESS_DENIED_ERROR
    raise ActiveRecord::DatabaseConnectionError.username_error(config[:username])
  elsif [ConnectionAdapters::Mysql2Adapter::ER_CONN_HOST_ERROR, ConnectionAdapters::Mysql2Adapter::ER_UNKNOWN_HOST_ERROR].include?(error.error_number)
    raise ActiveRecord::DatabaseConnectionError.hostname_error(config[:host])
  else
    raise ActiveRecord::ConnectionNotEstablished, error.message
  end
end

Instance Method Details

#active?Boolean

– CONNECTION MANAGEMENT ==================================== ++

Returns:

  • (Boolean)


118
119
120
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 118

def active?
  @connection.ping
end

#discard!Object

:nodoc:



136
137
138
139
140
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 136

def discard! # :nodoc:
  super
  @connection.automatic_close = false
  @connection = nil
end

#disconnect!Object

Disconnects from the database if already connected. Otherwise, this method does nothing.



131
132
133
134
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 131

def disconnect!
  super
  @connection.close
end

#each_hash(result, &block) ⇒ Object

HELPER METHODS ===========================================



92
93
94
95
96
97
98
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 92

def each_hash(result, &block) # :nodoc:
  if block_given?
    result.each(as: :hash, symbolize_keys: true, &block)
  else
    to_enum(:each_hash, result)
  end
end

#error_number(exception) ⇒ Object



100
101
102
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 100

def error_number(exception)
  exception.error_number if exception.respond_to?(:error_number)
end

#quote_string(string) ⇒ Object

– QUOTING ================================================== ++



108
109
110
111
112
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 108

def quote_string(string)
  @connection.escape(string)
rescue Mysql2::Error => error
  raise translate_exception(error, message: error.message, sql: "<escape>", binds: [])
end

#reconnect!Object Also known as: reset!



122
123
124
125
126
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 122

def reconnect!
  super
  disconnect!
  connect
end

#supports_comments?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 74

def supports_comments?
  true
end

#supports_comments_in_create?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 78

def supports_comments_in_create?
  true
end

#supports_json?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 70

def supports_json?
  !mariadb? && database_version >= "5.7.8"
end

#supports_lazy_transactions?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 86

def supports_lazy_transactions?
  true
end

#supports_savepoints?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 82

def supports_savepoints?
  true
end