Class: ActiveRecord::ConnectionAdapters::Mysql2Adapter

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

Defined Under Namespace

Classes: Column, ExplainPrettyPrinter

Constant Summary

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

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

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_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_indexes_in_create?, #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

#adapter_name, #case_insensitive_comparison, #case_sensitive_comparison, #case_sensitive_modifier, #clear_cache!, #close, #collector, #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_ddl_transactions?, #supports_extensions?, #supports_index_sort_order?, #supports_indexes_in_create?, #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, #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_false, #quoted_true, #type_cast

Methods included from DatabaseStatements

#add_transaction_record, #begin_db_transaction, #begin_isolated_db_transaction, #begin_transaction, #cacheable_query, #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_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, #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

- (Mysql2Adapter) initialize(connection, logger, connection_options, config)

Returns a new instance of Mysql2Adapter



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

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

Instance Method Details

- (Boolean) active?

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

Returns:

  • (Boolean)


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

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

- (Object) disconnect!

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



110
111
112
113
114
115
116
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 110

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

- (Object) each_hash(result)

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



62
63
64
65
66
67
68
69
70
# File 'activerecord/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

- (Object) error_number(exception)



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

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

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



261
262
263
264
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 261

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

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



257
258
259
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 257

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

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



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

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

- (Object) execute(sql, name = nil)

Executes the SQL statement in the context of this connection.



229
230
231
232
233
234
235
236
237
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 229

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

- (Object) explain(arel, binds = [])

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



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

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

- (Object) initialize_schema_migrations_table



48
49
50
51
52
53
54
# File 'activerecord/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

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



251
252
253
254
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 251

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

- (Object) last_inserted_id(result)



267
268
269
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 267

def last_inserted_id(result)
  @connection.last_id
end

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

:nodoc:



72
73
74
# File 'activerecord/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

- (Object) quote_string(string)

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



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

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

- (Object) quoted_date(value)



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

def quoted_date(value)
  if value.acts_like?(:time) && value.respond_to?(:usec)
    "#{super}.#{sprintf("%06d", value.usec)}"
  else
    super
  end
end

- (Object) reconnect! Also known as: reset!



101
102
103
104
105
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 101

def reconnect!
  super
  disconnect!
  connect
end

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

Returns an ActiveRecord::Result instance.



247
248
249
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 247

def select(sql, name = nil, binds = [])
  exec_query(sql, name)
end

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

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



224
225
226
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 224

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

- (Boolean) supports_explain?

Returns:

  • (Boolean)


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

def supports_explain?
  true
end