Class: ActiveRecord::ConnectionAdapters::Mysql2Adapter

Inherits:
AbstractMysqlAdapter show all
Defined in:
activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb

Defined Under Namespace

Classes: ExplainPrettyPrinter

Constant Summary collapse

ADAPTER_NAME =
'Mysql2'.freeze
MAX_INDEX_LENGTH_FOR_UTF8MB4 =
191

Constants inherited from AbstractMysqlAdapter

AbstractMysqlAdapter::INDEX_TYPES, AbstractMysqlAdapter::INDEX_USINGS, AbstractMysqlAdapter::LOST_CONNECTION_ERROR_MESSAGES, AbstractMysqlAdapter::NATIVE_DATABASE_TYPES

Constants inherited from AbstractAdapter

AbstractAdapter::SIMPLE_INT

Constants included from ActiveSupport::Callbacks

ActiveSupport::Callbacks::CALLBACK_FILTER_TYPES

Instance Attribute Summary

Attributes inherited from AbstractAdapter

#logger, #owner, #pool, #prepared_statements, #schema_cache, #visitor

Attributes included from QueryCache

#query_cache, #query_cache_enabled

Attributes included from DatabaseStatements

#transaction_manager

Instance Method Summary collapse

Methods inherited from AbstractMysqlAdapter

#_quote, #add_index, #begin_db_transaction, #begin_isolated_db_transaction, #bulk_change_table, #case_insensitive_comparison, #case_sensitive_comparison, #case_sensitive_modifier, #change_column, #change_column_default, #change_column_null, #charset, #clear_cache!, #collation, #columns, #columns_for_distinct, #commit_db_transaction, #create_database, #create_table, #current_database, #disable_referential_integrity, #drop_database, #drop_table, #empty_insert_statement_value, #emulate_booleans, #exec_rollback_db_transaction, #execute_and_free, #foreign_keys, #index_algorithms, #indexes, #join_to_update, #native_database_types, #new_column, #pk_and_sequence_for, #prepare_column_options, #primary_key, #quote_column_name, #quote_table_name, #quoted_date, #quoted_false, #quoted_true, #recreate_database, #rename_column, #rename_index, #rename_table, #schema_creation, #show_variable, #strict_mode?, #supports_bulk_alter?, #supports_datetime_with_precision?, #supports_foreign_keys?, #supports_index_sort_order?, #supports_indexes_in_create?, #supports_migrations?, #supports_primary_key?, #supports_transaction_isolation?, #supports_views?, #table_exists?, #tables, #truncate, #type_to_sql, #unquoted_false, #unquoted_true, #update_sql, #valid_type?

Methods included from Savepoints

#create_savepoint, #exec_rollback_to_savepoint, #release_savepoint, #supports_savepoints?

Methods inherited from AbstractAdapter

#adapter_name, #case_insensitive_comparison, #case_sensitive_comparison, #case_sensitive_modifier, #clear_cache!, #close, #collector, #column_name_for_operation, #create_savepoint, #current_savepoint_name, #disable_extension, #disable_referential_integrity, #enable_extension, #expire, #extensions, #index_algorithms, #lease, #lookup_cast_type, #new_column, #prefetch_primary_key?, #raw_connection, #release_savepoint, #requires_reloading?, #schema_creation, #substitute_at, #supports_bulk_alter?, #supports_ddl_transactions?, #supports_extensions?, #supports_foreign_keys?, #supports_index_sort_order?, #supports_indexes_in_create?, #supports_migrations?, #supports_partial_index?, #supports_primary_key?, #supports_savepoints?, #supports_transaction_isolation?, #supports_views?, type_cast_config_to_boolean, type_cast_config_to_integer, #type_map, #unprepared_statement, #valid_type?, #verify!

Methods included from ColumnDumper

#column_spec, #migration_keys, #prepare_column_options

Methods included from ActiveSupport::Callbacks

#run_callbacks

Methods included from ActiveSupport::Concern

#append_features, #class_methods, extended, #included

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

#allowed_index_name_length, #column_name_length, #columns_per_multicolumn_index, #columns_per_table, #in_clause_length, #index_name_length, #indexes_per_table, #joins_per_query, #sql_query_length, #table_alias_length, #table_name_length

Methods included from Quoting

#quote, #quote_column_name, #quote_table_name, #quote_table_name_for_assignment, #quoted_date, #quoted_false, #quoted_true, #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_rollback_db_transaction, #exec_rollback_to_savepoint, #insert, #insert_fixture, #join_to_delete, #join_to_update, #reset_sequence!, #reset_transaction, #rollback_db_transaction, #rollback_to_savepoint, #sanitize_limit, #select_all, #select_one, #select_value, #select_values, #supports_statement_cache?, #to_sql, #transaction, #transaction_isolation_levels, #transaction_open?, #transaction_state, #truncate, #update

Methods included from SchemaStatements

#add_column, #add_foreign_key, #add_index, #add_index_options, #add_reference, #add_timestamps, #assume_migrated_upto_version, #change_column, #change_column_default, #change_column_null, #change_table, #column_exists?, #columns, #columns_for_distinct, #create_join_table, #create_table, #data_source_exists?, #data_sources, #drop_join_table, #drop_table, #dump_schema_information, #foreign_key_column_for, #foreign_keys, #index_exists?, #index_name, #index_name_exists?, #native_database_types, #remove_column, #remove_columns, #remove_foreign_key, #remove_index, #remove_index!, #remove_reference, #remove_timestamps, #rename_column, #rename_index, #rename_table, #table_alias_for, #table_exists?, #type_to_sql, #update_table_definition

Methods included from TimestampDefaultDeprecation

#emit_warning_if_null_unspecified

Constructor Details

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


34
35
36
37
38
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 34

def initialize(connection, logger, connection_options, config)
  super
  @prepared_statements = false
  configure_connection
end

Instance Method Details

#active?Boolean

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


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

def active?
  @connection.ping
end

#disconnect!Object

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


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

def disconnect!
  super
  @connection.close
end

#each_hash(result) ⇒ Object

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


55
56
57
58
59
60
61
62
63
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 55

def each_hash(result) # :nodoc:
  if block_given?
    result.each(:as => :hash, :symbolize_keys => true) do |row|
      yield row
    end
  else
    to_enum(:each_hash, result)
  end
end

#error_number(exception) ⇒ Object


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

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

#exec_delete(sql, name, binds) ⇒ Object Also known as: exec_update


237
238
239
240
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 237

def exec_delete(sql, name, binds)
  execute to_sql(sql, binds), name
  @connection.affected_rows
end

#exec_insert(sql, name, binds, pk = nil, sequence_name = nil) ⇒ Object


233
234
235
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 233

def exec_insert(sql, name, binds, pk = nil, sequence_name = nil)
  execute to_sql(sql, binds), name
end

#exec_query(sql, name = 'SQL', binds = []) ⇒ Object Also known as: exec_without_stmt


220
221
222
223
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 220

def exec_query(sql, name = 'SQL', binds = [])
  result = execute(sql, name)
  ActiveRecord::Result.new(result.fields, result.to_a)
end

#execute(sql, name = nil) ⇒ Object

Executes the SQL statement in the context of this connection.


212
213
214
215
216
217
218
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 212

def execute(sql, name = nil)
  # make sure we carry over any changes to ActiveRecord::Base.default_timezone that have been
  # made since we established the connection
  @connection.query_options[:database_timezone] = ActiveRecord::Base.default_timezone

  super
end

#explain(arel, binds = []) ⇒ Object

– DATABASE STATEMENTS ====================================== ++


103
104
105
106
107
108
109
110
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 103

def explain(arel, binds = [])
  sql     = "EXPLAIN #{to_sql(arel, binds.dup)}"
  start   = Time.now
  result  = exec_query(sql, 'EXPLAIN', binds)
  elapsed = Time.now - start

  ExplainPrettyPrinter.new.pp(result, elapsed)
end

#initialize_schema_migrations_tableObject


41
42
43
44
45
46
47
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 41

def initialize_schema_migrations_table
  if charset == 'utf8mb4'
    ActiveRecord::SchemaMigration.create_table(MAX_INDEX_LENGTH_FOR_UTF8MB4)
  else
    ActiveRecord::SchemaMigration.create_table
  end
end

#insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object Also known as: create


227
228
229
230
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 227

def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
  super
  id_value || @connection.last_id
end

#last_inserted_id(result) ⇒ Object


243
244
245
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 243

def last_inserted_id(result)
  @connection.last_id
end

#quote_string(string) ⇒ Object

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


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

def quote_string(string)
  @connection.escape(string)
end

#reconnect!Object Also known as: reset!


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

def reconnect!
  super
  disconnect!
  connect
end

#select_rows(sql, name = nil, binds = []) ⇒ Object

Returns an array of arrays containing the field values. Order is the same as that returned by columns.


207
208
209
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 207

def select_rows(sql, name = nil, binds = [])
  execute(sql, name).to_a
end

#supports_explain?Boolean


49
50
51
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 49

def supports_explain?
  true
end