Class: ActiveRecord::ConnectionAdapters::Mysql2Adapter
- Inherits:
-
AbstractMysqlAdapter
- Object
- AbstractMysqlAdapter
- ActiveRecord::ConnectionAdapters::Mysql2Adapter
- Includes:
- ArJdbc::Abstract::ConnectionManagement, ArJdbc::Abstract::DatabaseStatements, ArJdbc::Abstract::StatementCache, ArJdbc::Abstract::TransactionSupport, ArJdbc::MySQL, ArJdbc::MysqlConfig
- Defined in:
- lib/arjdbc/mysql/adapter.rb
Constant Summary collapse
- ADAPTER_NAME =
'Mysql2'
- TYPE_MAP =
NOTE: redefines constant defined in abstract class however this time will use methods defined in the mysql abstract class and map properly mysql types.
Type::TypeMap.new.tap { |m| initialize_type_map(m) }
Constants included from ArJdbc::Abstract::DatabaseStatements
ArJdbc::Abstract::DatabaseStatements::NO_BINDS
Class Method Summary collapse
Instance Method Summary collapse
-
#active? ⇒ Boolean
-- CONNECTION MANAGEMENT ==================================== ++.
- #build_explain_clause(options = []) ⇒ Object
-
#discard! ⇒ Object
:nodoc:.
-
#disconnect! ⇒ Object
Disconnects from the database if already connected.
-
#each_hash(result) ⇒ Object
:nodoc:.
- #error_number(exception) ⇒ Object
- #explain(arel, binds = [], options = []) ⇒ Object
-
#initialize ⇒ Mysql2Adapter
constructor
A new instance of Mysql2Adapter.
-
#quote(value, comment = nil) ⇒ Object
FIXME: 5.1 crashes without this.
-
#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)
. - #supports_comments? ⇒ Boolean
- #supports_comments_in_create? ⇒ Boolean
- #supports_json? ⇒ Boolean
- #supports_lazy_transactions? ⇒ Boolean
- #supports_savepoints? ⇒ Boolean
- #supports_set_server_option? ⇒ Boolean
- #supports_transaction_isolation? ⇒ Boolean
-
#write_query?(sql) ⇒ Boolean
:nodoc:.
Methods included from ArJdbc::MysqlConfig
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_update, #internal_exec_query, #select_all
Methods included from ArJdbc::Abstract::ConnectionManagement
Constructor Details
#initialize ⇒ Mysql2Adapter
Returns a new instance of Mysql2Adapter.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/arjdbc/mysql/adapter.rb', line 69 def initialize(...) super @config[:flags] ||= 0 # assign arjdbc extra connection params conn_params = build_connection_config(@config.compact) # JDBC mysql appears to use found rows by default: https://dev.mysql.com/doc/connector-j/en/connector-j-connp-props-connection.html # if @config[:flags].kind_of? Array # @config[:flags].push "FOUND_ROWS" # else # @config[:flags] |= ::Mysql2::Client::FOUND_ROWS # end @connection_parameters = conn_params end |
Class Method Details
.jdbc_connection_class ⇒ Object
42 43 44 |
# File 'lib/arjdbc/mysql/adapter.rb', line 42 def jdbc_connection_class ::ActiveRecord::ConnectionAdapters::MySQLJdbcConnection end |
.new_client(conn_params, adapter_instance) ⇒ Object
46 47 48 |
# File 'lib/arjdbc/mysql/adapter.rb', line 46 def new_client(conn_params, adapter_instance) jdbc_connection_class.new(conn_params, adapter_instance) end |
Instance Method Details
#active? ⇒ Boolean
-- CONNECTION MANAGEMENT ==================================== ++
200 201 202 |
# File 'lib/arjdbc/mysql/adapter.rb', line 200 def active? !(@raw_connection.nil? || @raw_connection.closed?) && @lock.synchronize { @raw_connection&.ping } || false end |
#build_explain_clause(options = []) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/arjdbc/mysql/adapter.rb', line 136 def build_explain_clause( = []) return "EXPLAIN" if .empty? explain_clause = "EXPLAIN #{.join(" ").upcase}" if analyze_without_explain? && explain_clause.include?("ANALYZE") explain_clause.sub("EXPLAIN ", "") else explain_clause end end |
#discard! ⇒ Object
:nodoc:
216 217 218 219 220 221 222 |
# File 'lib/arjdbc/mysql/adapter.rb', line 216 def discard! # :nodoc: @lock.synchronize do super @raw_connection&.automatic_close = false @raw_connection = nil end end |
#disconnect! ⇒ Object
Disconnects from the database if already connected. Otherwise, this method does nothing.
208 209 210 211 212 213 214 |
# File 'lib/arjdbc/mysql/adapter.rb', line 208 def disconnect! @lock.synchronize do super @raw_connection&.close @raw_connection = nil end end |
#each_hash(result) ⇒ Object
:nodoc:
148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/arjdbc/mysql/adapter.rb', line 148 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
161 162 163 |
# File 'lib/arjdbc/mysql/adapter.rb', line 161 def error_number(exception) exception.error_code if exception.is_a?(JDBCError) end |
#explain(arel, binds = [], options = []) ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'lib/arjdbc/mysql/adapter.rb', line 127 def explain(arel, binds = [], = []) sql = build_explain_clause() + " " + to_sql(arel, binds) start = Process.clock_gettime(Process::CLOCK_MONOTONIC) result = internal_exec_query(sql, "EXPLAIN", binds) elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - 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.
172 173 174 |
# File 'lib/arjdbc/mysql/adapter.rb', line 172 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)
179 180 181 182 183 184 185 |
# File 'lib/arjdbc/mysql/adapter.rb', line 179 def quoted_date(value) if supports_datetime_with_precision? super else super.sub(/\.\d{6}\z/, '') end end |
#supports_comments? ⇒ Boolean
91 92 93 |
# File 'lib/arjdbc/mysql/adapter.rb', line 91 def supports_comments? true end |
#supports_comments_in_create? ⇒ Boolean
95 96 97 |
# File 'lib/arjdbc/mysql/adapter.rb', line 95 def supports_comments_in_create? true end |
#supports_json? ⇒ Boolean
87 88 89 |
# File 'lib/arjdbc/mysql/adapter.rb', line 87 def supports_json? !mariadb? && database_version >= '5.7.8' end |
#supports_lazy_transactions? ⇒ Boolean
103 104 105 |
# File 'lib/arjdbc/mysql/adapter.rb', line 103 def supports_lazy_transactions? true end |
#supports_savepoints? ⇒ Boolean
99 100 101 |
# File 'lib/arjdbc/mysql/adapter.rb', line 99 def supports_savepoints? true end |
#supports_set_server_option? ⇒ Boolean
111 112 113 |
# File 'lib/arjdbc/mysql/adapter.rb', line 111 def supports_set_server_option? false end |
#supports_transaction_isolation? ⇒ Boolean
107 108 109 |
# File 'lib/arjdbc/mysql/adapter.rb', line 107 def supports_transaction_isolation? true end |
#write_query?(sql) ⇒ Boolean
:nodoc:
123 124 125 |
# File 'lib/arjdbc/mysql/adapter.rb', line 123 def write_query?(sql) # :nodoc: !READ_QUERY.match?(sql) end |