Skip to content

Commit 3b07a9e

Browse files
committed
Update MySQL adapter for changes in Rail 7.1
* New initialize with argument forwarding for lazy connection init. * Remove check_version and clear_cache! in favor of base impls. * Add active?, disconnect!, discard!, text_type?, and configure_connection (dummy impl) based on Rails adapter. * Use any_raw_connection to access version early in adapter boot.
1 parent 9e73177 commit 3b07a9e

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

lib/arjdbc/mysql/adapter.rb

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,19 @@ class Mysql2Adapter < AbstractMysqlAdapter
3333

3434
include ArJdbc::MySQL
3535

36-
def initialize(connection, logger, connection_options, config)
37-
superclass_config = config.reverse_merge(prepared_statements: false)
38-
super(connection, logger, connection_options, superclass_config)
36+
def initialize(...)
37+
super
38+
39+
@config[:flags] ||= 0
40+
41+
# JDBC mysql appears to use found rows by default: https://dev.mysql.com/doc/connector-j/en/connector-j-connp-props-connection.html
42+
# if @config[:flags].kind_of? Array
43+
# @config[:flags].push "FOUND_ROWS"
44+
# else
45+
# @config[:flags] |= ::Mysql2::Client::FOUND_ROWS
46+
# end
3947

40-
# configure_connection taken care of at ArJdbc::Abstract::Core
48+
@connection_parameters ||= @config
4149
end
4250

4351
def self.database_exists?(config)
@@ -49,13 +57,6 @@ def self.database_exists?(config)
4957
conn.disconnect! if conn
5058
end
5159

52-
def check_version
53-
# for JNDI, don't check version as the whole connection should be lazy
54-
return if ::ActiveRecord::ConnectionAdapters::JdbcConnection.jndi_config?(config)
55-
56-
super
57-
end
58-
5960
def supports_json?
6061
!mariadb? && database_version >= '5.7.8'
6162
end
@@ -105,13 +106,6 @@ def explain(arel, binds = [])
105106
MySQL::ExplainPrettyPrinter.new.pp(result, elapsed)
106107
end
107108

108-
# Reloading the type map in abstract/statement_cache.rb blows up postgres
109-
def clear_cache!
110-
# FIXME: This seems to have disappeared in Rails 7?
111-
# reload_type_map
112-
super
113-
end
114-
115109
def each_hash(result) # :nodoc:
116110
if block_given?
117111
# FIXME: This is C in mysql2 gem and I just made simplest Ruby
@@ -164,19 +158,50 @@ def _quote(value)
164158
# CONNECTION MANAGEMENT ====================================
165159
#++
166160

161+
def active?
162+
!(@raw_connection.nil? || @raw_connection.closed?) && @lock.synchronize { @raw_connection&.execute_query("/* ping */ SELECT 1") } || false
163+
end
164+
167165
alias :reset! :reconnect!
168166

167+
# Disconnects from the database if already connected.
168+
# Otherwise, this method does nothing.
169+
def disconnect!
170+
@lock.synchronize do
171+
super
172+
@raw_connection&.close
173+
@raw_connection = nil
174+
end
175+
end
176+
177+
def discard! # :nodoc:
178+
@lock.synchronize do
179+
super
180+
@raw_connection&.automatic_close = false
181+
@raw_connection = nil
182+
end
183+
end
184+
169185
#
170186

171187
private
188+
def text_type?(type)
189+
TYPE_MAP.lookup(type).is_a?(Type::String) || TYPE_MAP.lookup(type).is_a?(Type::Text)
190+
end
191+
192+
def configure_connection
193+
# @raw_connection.query_options[:as] = :array
194+
# @raw_connection.query_options[:database_timezone] = default_timezone
195+
super
196+
end
172197

173198
# e.g. "5.7.20-0ubuntu0.16.04.1"
174199
def full_version
175200
schema_cache.database_version.full_version_string
176201
end
177202

178203
def get_full_version
179-
@full_version ||= @connection.full_version
204+
@full_version ||= any_raw_connection.full_version
180205
end
181206

182207
def jdbc_connection_class(spec)

0 commit comments

Comments
 (0)