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