Skip to content

Commit d681a94

Browse files
committed
Update explain code in mysql (only removes deprecation warnings
1 parent 2058151 commit d681a94

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

lib/arjdbc/mysql/adapter.rb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,27 @@ def write_query?(sql) # :nodoc:
9797
!READ_QUERY.match?(sql)
9898
end
9999

100-
def explain(arel, binds = [])
101-
sql = "EXPLAIN #{to_sql(arel, binds)}"
102-
start = Concurrent.monotonic_time
103-
result = exec_query(sql, "EXPLAIN", binds)
104-
elapsed = Concurrent.monotonic_time - start
100+
def explain(arel, binds = [], options = [])
101+
sql = build_explain_clause(options) + " " + to_sql(arel, binds)
102+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
103+
result = internal_exec_query(sql, "EXPLAIN", binds)
104+
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
105105

106106
MySQL::ExplainPrettyPrinter.new.pp(result, elapsed)
107107
end
108108

109+
def build_explain_clause(options = [])
110+
return "EXPLAIN" if options.empty?
111+
112+
explain_clause = "EXPLAIN #{options.join(" ").upcase}"
113+
114+
if analyze_without_explain? && explain_clause.include?("ANALYZE")
115+
explain_clause.sub("EXPLAIN ", "")
116+
else
117+
explain_clause
118+
end
119+
end
120+
109121
def each_hash(result) # :nodoc:
110122
if block_given?
111123
# FIXME: This is C in mysql2 gem and I just made simplest Ruby
@@ -185,6 +197,11 @@ def discard! # :nodoc:
185197
#
186198

187199
private
200+
# https://mariadb.com/kb/en/analyze-statement/
201+
def analyze_without_explain?
202+
mariadb? && database_version >= "10.1.0"
203+
end
204+
188205
def text_type?(type)
189206
TYPE_MAP.lookup(type).is_a?(Type::String) || TYPE_MAP.lookup(type).is_a?(Type::Text)
190207
end

0 commit comments

Comments
 (0)