Skip to content

Commit

Permalink
Update explain code in mysql (only removes deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
enebo committed Mar 27, 2024
1 parent 2058151 commit d681a94
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions lib/arjdbc/mysql/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,27 @@ def write_query?(sql) # :nodoc:
!READ_QUERY.match?(sql)
end

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
def explain(arel, binds = [], options = [])
sql = build_explain_clause(options) + " " + 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

def build_explain_clause(options = [])
return "EXPLAIN" if options.empty?

explain_clause = "EXPLAIN #{options.join(" ").upcase}"

if analyze_without_explain? && explain_clause.include?("ANALYZE")
explain_clause.sub("EXPLAIN ", "")
else
explain_clause
end
end

def each_hash(result) # :nodoc:
if block_given?
# FIXME: This is C in mysql2 gem and I just made simplest Ruby
Expand Down Expand Up @@ -185,6 +197,11 @@ def discard! # :nodoc:
#

private
# https://mariadb.com/kb/en/analyze-statement/
def analyze_without_explain?
mariadb? && database_version >= "10.1.0"
end

def text_type?(type)
TYPE_MAP.lookup(type).is_a?(Type::String) || TYPE_MAP.lookup(type).is_a?(Type::Text)
end
Expand Down

0 comments on commit d681a94

Please sign in to comment.