Class: Benedictus::Plan::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/benedictus/plan/parser.rb

Class Method Summary collapse

Class Method Details

.parse(json) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/benedictus/plan/parser.rb', line 7

def parse(json)
  data = json.is_a?(String) ? JSON.parse(json) : json

  raise Benedictus::DatabaseError, "EXPLAIN returned an empty result" if data.is_a?(Array) && data.empty?

  envelope = data.is_a?(Array) ? data.first : data

  unless envelope.is_a?(Hash) && envelope["Plan"].is_a?(Hash)
    raise Benedictus::DatabaseError,
          "EXPLAIN output did not contain a top-level 'Plan' object"
  end

  root = build_node(envelope["Plan"])

  Tree.new(
    root,
    planning_time: envelope["Planning Time"],
    execution_time: envelope["Execution Time"],
    triggers: envelope["Triggers"] || [],
    raw: data
  )
end