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_table, #current_database, #disable_referential_integrity, #drop_database, #drop_table, #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, #rename_column, #rename_index, #rename_table, #rollback_db_transaction, #schema_creation, #show_variable, #strict_mode?, #supports_bulk_alter?, #supports_index_sort_order?, #supports_migrations?, #supports_primary_key?, #supports_transaction_isolation?, #table_exists?, #tables, #type_cast, #type_to_sql, #update_sql, #valid_type?

Methods included from Savepoints

#create_savepoint, #release_savepoint, #rollback_to_savepoint, #supports_savepoints?

Methods inherited from AbstractAdapter

#active_threadsafe?, #adapter_name, #case_insensitive_comparison, #case_sensitive_modifier, #clear_cache!, #close, #create_savepoint, #current_savepoint_name, #disable_extension, #disable_referential_integrity, #enable_extension, #expire, #extensions, #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?, 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, #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_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, #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.



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

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

Instance Method Details

#active?Boolean

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

Returns:

  • (Boolean)


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

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

#disconnect!Object

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



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

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

#each_hash(result) ⇒ Object

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



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

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



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

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



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

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



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

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



231
232
233
234
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 231

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.



221
222
223
224
225
226
227
228
229
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 221

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 ======================================



112
113
114
115
116
117
118
119
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 112

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



48
49
50
51
52
53
54
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 48

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



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

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



259
260
261
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 259

def last_inserted_id(result)
  @connection.last_id
end

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

:nodoc:



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

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 ==================================================



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

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

#reconnect!Object Also known as: reset!



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

def reconnect!
  super
  disconnect!
  connect
end

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

Returns an ActiveRecord::Result instance.



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

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.



216
217
218
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 216

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

#supports_explain?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 56

def supports_explain?
  true
end