From d681a947424108dc43dc646a6625b8f2db8db88e Mon Sep 17 00:00:00 2001 From: "Thomas E. Enebo" Date: Wed, 27 Mar 2024 10:08:38 -0400 Subject: [PATCH] Update explain code in mysql (only removes deprecation warnings --- lib/arjdbc/mysql/adapter.rb | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/arjdbc/mysql/adapter.rb b/lib/arjdbc/mysql/adapter.rb index 37c64fa80..385084bb3 100644 --- a/lib/arjdbc/mysql/adapter.rb +++ b/lib/arjdbc/mysql/adapter.rb @@ -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 @@ -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