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'

Constants inherited from AbstractMysqlAdapter

AbstractMysqlAdapter::LOST_CONNECTION_ERROR_MESSAGES, AbstractMysqlAdapter::NATIVE_DATABASE_TYPES

Instance Attribute Summary

Attributes inherited from AbstractAdapter

#in_use, #last_use, #logger, #open_transactions, #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, #add_column_position!, #begin_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, #emulate_booleans, #execute_and_free, #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, #show_variable, #structure_dump, #supports_bulk_alter?, #supports_index_sort_order?, #supports_migrations?, #supports_primary_key?, #supports_savepoints?, #table_exists?, #tables, #type_to_sql, #update_sql

Methods inherited from AbstractAdapter

#adapter_name, #case_insensitive_comparison, #case_sensitive_modifier, #clear_cache!, #close, #create_savepoint, #current_savepoint_name, #decrement_open_transactions, #disable_referential_integrity, #expire, #increment_open_transactions, #lease, #prefetch_primary_key?, #quote_table_name, #raw_connection, #release_savepoint, #requires_reloading?, #rollback_to_savepoint, #substitute_at, #supports_bulk_alter?, #supports_count_distinct?, #supports_ddl_transactions?, #supports_index_sort_order?, #supports_migrations?, #supports_primary_key?, #supports_savepoints?, #transaction_joinable=, #verify!

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

#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, #quoted_date, #quoted_false, #quoted_true, #type_cast

Methods included from DatabaseStatements

#add_transaction_record, #begin_db_transaction, #case_sensitive_equality_operator, #commit_db_transaction, #default_sequence_name, #delete, #empty_insert_statement_value, #insert, #insert_fixture, #join_to_update, #limited_update_conditions, #outside_transaction?, #reset_sequence!, #rollback_db_transaction, #sanitize_limit, #select_all, #select_one, #select_value, #select_values, #supports_statement_cache?, #to_sql, #transaction, #update

Methods included from SchemaStatements

#add_column, #add_column_options!, #add_index, #add_timestamps, #assume_migrated_upto_version, #change_column, #change_column_default, #change_table, #column_exists?, #columns, #create_table, #distinct, #drop_table, #dump_schema_information, #index_exists?, #index_name, #index_name_exists?, #initialize_schema_migrations_table, #native_database_types, #remove_column, #remove_index, #remove_index!, #remove_timestamps, #rename_column, #rename_index, #rename_table, #structure_dump, #table_alias_for, #table_exists?, #type_to_sql

Constructor Details

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

Returns a new instance of Mysql2Adapter.



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

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

Instance Method Details

#active?Boolean

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

Returns:

  • (Boolean)


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

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

#disconnect!Object

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



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

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

#each_hash(result) ⇒ Object

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



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

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



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

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



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

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

#exec_insert(sql, name, binds) ⇒ Object



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

def exec_insert(sql, name, binds)
  execute to_sql(sql, binds), name
end

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



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

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.



206
207
208
209
210
211
212
213
214
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 206

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



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

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

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



229
230
231
232
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 229

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



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

def last_inserted_id(result)
  @connection.last_id
end

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

:nodoc:



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

def new_column(field, default, type, null, collation) # :nodoc:
  Column.new(field, default, type, null, collation)
end

#quote_string(string) ⇒ Object

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



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

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

#reconnect!Object



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

def reconnect!
  disconnect!
  connect
end

#reset!Object



90
91
92
93
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 90

def reset!
  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.



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

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

#select_rows(sql, name = nil) ⇒ Object

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



201
202
203
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 201

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

#supports_explain?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 39

def supports_explain?
  true
end