Class: ActiveRecord::ConnectionAdapters::Mysql2Adapter

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

Defined Under Namespace

Classes: Column, ExplainPrettyPrinter

Constant Summary collapse

ADAPTER_NAME =
'Mysql2'
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

Instance Attribute Summary

Attributes inherited from AbstractAdapter

#in_use, #last_use, #logger, #pool, #schema_cache, #visitor

Attributes included from QueryCache

#query_cache, #query_cache_enabled

Instance Method Summary collapse

Methods inherited from AbstractMysqlAdapter

#adapter_name, #add_column_position!, #add_index, #begin_db_transaction, #begin_isolated_db_transaction, #bulk_change_table, #case_insensitive_comparison, #case_sensitive_modifier, #change_column, #change_column_default, #change_column_null, #charset, #collation, #columns, #commit_db_transaction, #create_database, #create_savepoint, #create_table, #current_database, #disable_referential_integrity, #drop_database, #empty_insert_statement_value, #emulate_booleans, #execute_and_free, #index_algorithms, #indexes, #join_to_update, #limited_update_conditions, #native_database_types, #pk_and_sequence_for, #primary_key, #quote, #quote_column_name, #quote_table_name, #quoted_false, #quoted_true, #recreate_database, #release_savepoint, #rename_column, #rename_table, #rollback_db_transaction, #rollback_to_savepoint, #schema_creation, #show_variable, #strict_mode?, #supports_bulk_alter?, #supports_index_sort_order?, #supports_migrations?, #supports_primary_key?, #supports_savepoints?, #supports_transaction_isolation?, #table_exists?, #tables, #type_to_sql, #update_sql, #valid_type?

Methods inherited from AbstractAdapter

#active_threadsafe?, #adapter_name, #case_insensitive_comparison, #case_sensitive_modifier, #clear_cache!, #close, #create_savepoint, #current_savepoint_name, #decrement_open_transactions, #disable_extension, #disable_referential_integrity, #enable_extension, #expire, #extensions, #increment_open_transactions, #index_algorithms, #lease, #open_transactions, #prefetch_primary_key?, #raw_connection, #release_savepoint, #requires_reloading?, #rollback_to_savepoint, #schema_creation, #substitute_at, #supports_bulk_alter?, #supports_count_distinct?, #supports_ddl_transactions?, #supports_extensions?, #supports_index_sort_order?, #supports_migrations?, #supports_partial_index?, #supports_primary_key?, #supports_savepoints?, #supports_transaction_isolation?, #transaction_joinable=, type_cast_config_to_boolean, type_cast_config_to_integer, #unprepared_statement, #unprepared_visitor, #valid_type?, #verify!

Methods included from ColumnDumper

#column_spec, #migration_keys, #prepare_column_options

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

Methods included from DatabaseStatements

#add_transaction_record, #begin_db_transaction, #begin_isolated_db_transaction, #begin_transaction, #case_sensitive_equality_operator, #commit_db_transaction, #commit_transaction, #current_transaction, #default_sequence_name, #delete, #empty_insert_statement_value, #insert, #insert_fixture, #join_to_delete, #join_to_update, #limited_update_conditions, #reset_sequence!, #reset_transaction, #rollback_db_transaction, #rollback_transaction, #sanitize_limit, #select_all, #select_one, #select_value, #select_values, #supports_statement_cache?, #to_sql, #transaction, #transaction_isolation_levels, #transaction_open?, #update, #within_new_transaction

Methods included from SchemaStatements

#add_column, #add_column_options!, #add_index, #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, #distinct, #drop_join_table, #drop_table, #dump_schema_information, #index_exists?, #index_name, #index_name_exists?, #native_database_types, #remove_column, #remove_columns, #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

Constructor Details

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

Returns a new instance of Mysql2Adapter.



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

def initialize(connection, logger, connection_options, config)
  super
  @visitor = BindSubstitution.new self
  configure_connection
end

Instance Method Details

#active?Boolean

CONNECTION MANAGEMENT ====================================

Returns:

  • (Boolean)


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

def active?
  return false unless @connection
  @connection.ping
end

#disconnect!Object

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



96
97
98
99
100
101
102
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 96

def disconnect!
  super
  unless @connection.nil?
    @connection.close
    @connection = nil
  end
end

#each_hash(result) ⇒ Object

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



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

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



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

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



248
249
250
251
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 248

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



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

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



225
226
227
228
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 225

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.



215
216
217
218
219
220
221
222
223
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 215

def execute(sql, name = nil)
  if @connection
    # 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
  end

  super
end

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

DATABASE STATEMENTS ======================================



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

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



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

def initialize_schema_migrations_table
  if @config[:encoding] == '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



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

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



254
255
256
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 254

def last_inserted_id(result)
  @connection.last_id
end

#new_column(field, default, type, null, collation, extra = "") ⇒ Object

:nodoc:



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

def new_column(field, default, type, null, collation, extra = "") # :nodoc:
  Column.new(field, default, type, null, collation, strict_mode?, extra)
end

#quote_string(string) ⇒ Object

QUOTING ==================================================



76
77
78
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 76

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

#reconnect!Object Also known as: reset!



87
88
89
90
91
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 87

def reconnect!
  super
  disconnect!
  connect
end

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

Returns an array of record hashes with the column names as keys and column values as values.



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

def select(sql, name = nil, binds = [])
  exec_query(sql, name)
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.



210
211
212
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 210

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

#supports_explain?Boolean

Returns:

  • (Boolean)


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

def supports_explain?
  true
end