Class: ActiveRecord::ConnectionAdapters::Mysql2Adapter

Inherits:
AbstractMysqlAdapter
  • Object
show all
Includes:
Jdbc::ConnectionPoolCallbacks, ArJdbc::Abstract::ConnectionManagement, ArJdbc::Abstract::DatabaseStatements, ArJdbc::Abstract::StatementCache, ArJdbc::Abstract::TransactionSupport, ArJdbc::MySQL
Defined in:
lib/arjdbc/mysql/adapter.rb

Constant Summary collapse

ADAPTER_NAME =
'Mysql2'

Constants included from ArJdbc::Abstract::DatabaseStatements

ArJdbc::Abstract::DatabaseStatements::NO_BINDS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ArJdbc::Abstract::TransactionSupport

#begin_db_transaction, #begin_isolated_db_transaction, #commit_db_transaction, #create_savepoint, #exec_rollback_db_transaction, #exec_rollback_to_savepoint, #release_savepoint

Methods included from ArJdbc::Abstract::StatementCache

#delete_cached_statement, #fetch_cached_statement

Methods included from ArJdbc::Abstract::DatabaseStatements

#exec_insert, #exec_query, #exec_update, #execute, #select_all

Methods included from ArJdbc::Abstract::ConnectionManagement

#active?, #disconnect!, #really_valid?, #reconnect!

Methods included from Jdbc::ConnectionPoolCallbacks

#on_checkin, #on_checkout

Constructor Details

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

Returns a new instance of Mysql2Adapter.



36
37
38
39
40
41
# File 'lib/arjdbc/mysql/adapter.rb', line 36

def initialize(connection, logger, connection_options, config)
  superclass_config = config.reverse_merge(prepared_statements: false)
  super(connection, logger, connection_options, superclass_config)

  # configure_connection taken care of at ArJdbc::Abstract::Core
end

Class Method Details

.database_exists?(config) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
# File 'lib/arjdbc/mysql/adapter.rb', line 43

def self.database_exists?(config)
  conn = ActiveRecord::Base.mysql2_connection(config)
  conn && conn.really_valid?
rescue ActiveRecord::NoDatabaseError
  false
ensure
  conn.disconnect! if conn
end

Instance Method Details

#check_versionObject



52
53
54
55
56
57
# File 'lib/arjdbc/mysql/adapter.rb', line 52

def check_version
  # for JNDI, don't check version as the whole connection should be lazy
  return if ::ActiveRecord::ConnectionAdapters::JdbcConnection.jndi_config?(config)

  super
end

#clear_cache!Object

Reloading the type map in abstract/statement_cache.rb blows up postgres



109
110
111
112
113
# File 'lib/arjdbc/mysql/adapter.rb', line 109

def clear_cache!
  # FIXME: This seems to have disappeared in Rails 7?
  # reload_type_map
  super
end

#each_hash(result) ⇒ Object

:nodoc:



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/arjdbc/mysql/adapter.rb', line 115

def each_hash(result) # :nodoc:
  if block_given?
    # FIXME: This is C in mysql2 gem and I just made simplest Ruby
    result.each do |row|
      new_hash = {}
      row.each { |k, v| new_hash[k.to_sym] = v }
      yield new_hash
    end
  else
    to_enum(:each_hash, result)
  end
end

#error_number(exception) ⇒ Object



128
129
130
# File 'lib/arjdbc/mysql/adapter.rb', line 128

def error_number(exception)
  exception.error_code if exception.is_a?(JDBCError)
end

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



99
100
101
102
103
104
105
106
# File 'lib/arjdbc/mysql/adapter.rb', line 99

def explain(arel, binds = [])
  sql     = "EXPLAIN #{to_sql(arel, binds)}"
  start   = Concurrent.monotonic_time
  result  = exec_query(sql, "EXPLAIN", binds)
  elapsed = Concurrent.monotonic_time - start

  MySQL::ExplainPrettyPrinter.new.pp(result, elapsed)
end

#quote(value, comment = nil) ⇒ Object

FIXME: 5.1 crashes without this. I think this is Arel hitting a fallback path in to_sql.rb. So maybe an untested code path in their source. Still means we are doing something wrong to even hit it.



139
140
141
# File 'lib/arjdbc/mysql/adapter.rb', line 139

def quote(value, comment=nil)
  super(value)
end

#quoted_date(value) ⇒ Object

NOTE: quote_string(string) provided by ArJdbc::MySQL (native code), this piece is also native (mysql2) under MRI: @connection.escape(string)



146
147
148
149
150
151
152
# File 'lib/arjdbc/mysql/adapter.rb', line 146

def quoted_date(value)
  if supports_datetime_with_precision?
    super
  else
    super.sub(/\.\d{6}\z/, '')
  end
end

#supports_comments?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/arjdbc/mysql/adapter.rb', line 63

def supports_comments?
  true
end

#supports_comments_in_create?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/arjdbc/mysql/adapter.rb', line 67

def supports_comments_in_create?
  true
end

#supports_json?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/arjdbc/mysql/adapter.rb', line 59

def supports_json?
  !mariadb? && database_version >= '5.7.8'
end

#supports_lazy_transactions?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/arjdbc/mysql/adapter.rb', line 75

def supports_lazy_transactions?
  true
end

#supports_savepoints?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/arjdbc/mysql/adapter.rb', line 71

def supports_savepoints?
  true
end

#supports_set_server_option?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/arjdbc/mysql/adapter.rb', line 83

def supports_set_server_option?
  false
end

#supports_transaction_isolation?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/arjdbc/mysql/adapter.rb', line 79

def supports_transaction_isolation?
  true
end

#write_query?(sql) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


95
96
97
# File 'lib/arjdbc/mysql/adapter.rb', line 95

def write_query?(sql) # :nodoc:
  !READ_QUERY.match?(sql)
end