2016-08-31 11:20:36 -04:00
|
|
|
# frozen_string_literal: true
|
2018-02-24 01:45:50 -05:00
|
|
|
|
2018-02-24 02:41:47 -05:00
|
|
|
module Arel # :nodoc: all
|
2010-08-13 21:05:38 -04:00
|
|
|
module Visitors
|
2010-10-19 14:47:00 -04:00
|
|
|
class Dot < Arel::Visitors::Visitor
|
2010-08-13 21:05:38 -04:00
|
|
|
class Node # :nodoc:
|
|
|
|
attr_accessor :name, :id, :fields
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def initialize(name, id, fields = [])
|
2010-08-13 21:05:38 -04:00
|
|
|
@name = name
|
|
|
|
@id = id
|
|
|
|
@fields = fields
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Edge < Struct.new :name, :from, :to # :nodoc:
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
2014-09-22 14:01:03 -04:00
|
|
|
super()
|
2010-08-13 21:05:38 -04:00
|
|
|
@nodes = []
|
|
|
|
@edges = []
|
|
|
|
@node_stack = []
|
|
|
|
@edge_stack = []
|
|
|
|
@seen = {}
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def accept(object, collector)
|
2014-04-08 20:01:53 -04:00
|
|
|
visit object
|
2014-06-14 17:33:05 -04:00
|
|
|
collector << to_dot
|
2010-08-13 21:05:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2014-04-08 20:01:53 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_Ordering(o)
|
|
|
|
visit_edge o, "expr"
|
|
|
|
end
|
2010-09-07 18:10:27 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_TableAlias(o)
|
|
|
|
visit_edge o, "name"
|
|
|
|
visit_edge o, "relation"
|
|
|
|
end
|
2010-09-06 21:07:36 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_Count(o)
|
|
|
|
visit_edge o, "expressions"
|
|
|
|
visit_edge o, "distinct"
|
|
|
|
end
|
2010-09-14 17:27:37 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_Values(o)
|
|
|
|
visit_edge o, "expressions"
|
|
|
|
end
|
2010-09-29 17:32:52 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_StringJoin(o)
|
|
|
|
visit_edge o, "left"
|
|
|
|
end
|
2010-09-29 17:32:52 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_InnerJoin(o)
|
|
|
|
visit_edge o, "left"
|
|
|
|
visit_edge o, "right"
|
|
|
|
end
|
|
|
|
alias :visit_Arel_Nodes_FullOuterJoin :visit_Arel_Nodes_InnerJoin
|
|
|
|
alias :visit_Arel_Nodes_OuterJoin :visit_Arel_Nodes_InnerJoin
|
|
|
|
alias :visit_Arel_Nodes_RightOuterJoin :visit_Arel_Nodes_InnerJoin
|
2010-09-29 17:32:52 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_DeleteStatement(o)
|
|
|
|
visit_edge o, "relation"
|
|
|
|
visit_edge o, "wheres"
|
|
|
|
end
|
2012-02-22 09:25:10 -05:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def unary(o)
|
|
|
|
visit_edge o, "expr"
|
|
|
|
end
|
|
|
|
alias :visit_Arel_Nodes_Group :unary
|
|
|
|
alias :visit_Arel_Nodes_Cube :unary
|
|
|
|
alias :visit_Arel_Nodes_RollUp :unary
|
|
|
|
alias :visit_Arel_Nodes_GroupingSet :unary
|
|
|
|
alias :visit_Arel_Nodes_GroupingElement :unary
|
|
|
|
alias :visit_Arel_Nodes_Grouping :unary
|
|
|
|
alias :visit_Arel_Nodes_Having :unary
|
|
|
|
alias :visit_Arel_Nodes_Limit :unary
|
|
|
|
alias :visit_Arel_Nodes_Not :unary
|
|
|
|
alias :visit_Arel_Nodes_Offset :unary
|
|
|
|
alias :visit_Arel_Nodes_On :unary
|
|
|
|
alias :visit_Arel_Nodes_Top :unary
|
|
|
|
alias :visit_Arel_Nodes_UnqualifiedColumn :unary
|
|
|
|
alias :visit_Arel_Nodes_Preceding :unary
|
|
|
|
alias :visit_Arel_Nodes_Following :unary
|
|
|
|
alias :visit_Arel_Nodes_Rows :unary
|
|
|
|
alias :visit_Arel_Nodes_Range :unary
|
|
|
|
|
|
|
|
def window(o)
|
|
|
|
visit_edge o, "partitions"
|
|
|
|
visit_edge o, "orders"
|
|
|
|
visit_edge o, "framing"
|
|
|
|
end
|
|
|
|
alias :visit_Arel_Nodes_Window :window
|
2010-08-24 20:59:03 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def named_window(o)
|
|
|
|
visit_edge o, "partitions"
|
|
|
|
visit_edge o, "orders"
|
|
|
|
visit_edge o, "framing"
|
|
|
|
visit_edge o, "name"
|
|
|
|
end
|
|
|
|
alias :visit_Arel_Nodes_NamedWindow :named_window
|
2012-02-23 08:06:05 -05:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def function(o)
|
|
|
|
visit_edge o, "expressions"
|
|
|
|
visit_edge o, "distinct"
|
|
|
|
visit_edge o, "alias"
|
|
|
|
end
|
|
|
|
alias :visit_Arel_Nodes_Exists :function
|
|
|
|
alias :visit_Arel_Nodes_Min :function
|
|
|
|
alias :visit_Arel_Nodes_Max :function
|
|
|
|
alias :visit_Arel_Nodes_Avg :function
|
|
|
|
alias :visit_Arel_Nodes_Sum :function
|
|
|
|
|
|
|
|
def extract(o)
|
|
|
|
visit_edge o, "expressions"
|
|
|
|
visit_edge o, "alias"
|
|
|
|
end
|
|
|
|
alias :visit_Arel_Nodes_Extract :extract
|
2011-01-04 23:27:34 -05:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_NamedFunction(o)
|
|
|
|
visit_edge o, "name"
|
|
|
|
visit_edge o, "expressions"
|
|
|
|
visit_edge o, "distinct"
|
|
|
|
visit_edge o, "alias"
|
|
|
|
end
|
2010-08-16 18:46:53 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_InsertStatement(o)
|
|
|
|
visit_edge o, "relation"
|
|
|
|
visit_edge o, "columns"
|
|
|
|
visit_edge o, "values"
|
|
|
|
end
|
2010-08-13 21:28:38 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_SelectCore(o)
|
|
|
|
visit_edge o, "source"
|
|
|
|
visit_edge o, "projections"
|
|
|
|
visit_edge o, "wheres"
|
|
|
|
visit_edge o, "windows"
|
|
|
|
end
|
2010-08-13 21:28:38 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_SelectStatement(o)
|
|
|
|
visit_edge o, "cores"
|
|
|
|
visit_edge o, "limit"
|
|
|
|
visit_edge o, "orders"
|
|
|
|
visit_edge o, "offset"
|
|
|
|
end
|
2010-08-16 22:59:52 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_UpdateStatement(o)
|
|
|
|
visit_edge o, "relation"
|
|
|
|
visit_edge o, "wheres"
|
|
|
|
visit_edge o, "values"
|
|
|
|
end
|
2010-08-13 21:05:38 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Table(o)
|
|
|
|
visit_edge o, "name"
|
|
|
|
end
|
2016-08-27 08:39:52 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_Casted(o)
|
|
|
|
visit_edge o, "val"
|
|
|
|
visit_edge o, "attribute"
|
|
|
|
end
|
2010-08-13 21:05:38 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Attribute(o)
|
|
|
|
visit_edge o, "relation"
|
|
|
|
visit_edge o, "name"
|
2010-12-10 12:16:41 -05:00
|
|
|
end
|
2018-02-24 01:45:50 -05:00
|
|
|
alias :visit_Arel_Attributes_Integer :visit_Arel_Attribute
|
|
|
|
alias :visit_Arel_Attributes_Float :visit_Arel_Attribute
|
|
|
|
alias :visit_Arel_Attributes_String :visit_Arel_Attribute
|
|
|
|
alias :visit_Arel_Attributes_Time :visit_Arel_Attribute
|
|
|
|
alias :visit_Arel_Attributes_Boolean :visit_Arel_Attribute
|
|
|
|
alias :visit_Arel_Attributes_Attribute :visit_Arel_Attribute
|
|
|
|
|
|
|
|
def nary(o)
|
|
|
|
o.children.each_with_index do |x, i|
|
|
|
|
edge(i) { visit x }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alias :visit_Arel_Nodes_And :nary
|
2010-12-10 12:16:41 -05:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def binary(o)
|
|
|
|
visit_edge o, "left"
|
|
|
|
visit_edge o, "right"
|
|
|
|
end
|
|
|
|
alias :visit_Arel_Nodes_As :binary
|
|
|
|
alias :visit_Arel_Nodes_Assignment :binary
|
|
|
|
alias :visit_Arel_Nodes_Between :binary
|
|
|
|
alias :visit_Arel_Nodes_Concat :binary
|
|
|
|
alias :visit_Arel_Nodes_DoesNotMatch :binary
|
|
|
|
alias :visit_Arel_Nodes_Equality :binary
|
|
|
|
alias :visit_Arel_Nodes_GreaterThan :binary
|
|
|
|
alias :visit_Arel_Nodes_GreaterThanOrEqual :binary
|
|
|
|
alias :visit_Arel_Nodes_In :binary
|
|
|
|
alias :visit_Arel_Nodes_JoinSource :binary
|
|
|
|
alias :visit_Arel_Nodes_LessThan :binary
|
|
|
|
alias :visit_Arel_Nodes_LessThanOrEqual :binary
|
|
|
|
alias :visit_Arel_Nodes_Matches :binary
|
|
|
|
alias :visit_Arel_Nodes_NotEqual :binary
|
|
|
|
alias :visit_Arel_Nodes_NotIn :binary
|
|
|
|
alias :visit_Arel_Nodes_Or :binary
|
|
|
|
alias :visit_Arel_Nodes_Over :binary
|
|
|
|
|
|
|
|
def visit_String(o)
|
|
|
|
@node_stack.last.fields << o
|
|
|
|
end
|
|
|
|
alias :visit_Time :visit_String
|
|
|
|
alias :visit_Date :visit_String
|
|
|
|
alias :visit_DateTime :visit_String
|
|
|
|
alias :visit_NilClass :visit_String
|
|
|
|
alias :visit_TrueClass :visit_String
|
|
|
|
alias :visit_FalseClass :visit_String
|
|
|
|
alias :visit_Integer :visit_String
|
|
|
|
alias :visit_Fixnum :visit_String
|
|
|
|
alias :visit_BigDecimal :visit_String
|
|
|
|
alias :visit_Float :visit_String
|
|
|
|
alias :visit_Symbol :visit_String
|
|
|
|
alias :visit_Arel_Nodes_SqlLiteral :visit_String
|
|
|
|
|
|
|
|
def visit_Arel_Nodes_BindParam(o); end
|
|
|
|
|
|
|
|
def visit_Hash(o)
|
|
|
|
o.each_with_index do |pair, i|
|
|
|
|
edge("pair_#{i}") { visit pair }
|
|
|
|
end
|
2010-08-13 21:28:38 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Array(o)
|
|
|
|
o.each_with_index do |x, i|
|
|
|
|
edge(i) { visit x }
|
|
|
|
end
|
2010-08-13 21:05:38 -04:00
|
|
|
end
|
2018-02-24 01:45:50 -05:00
|
|
|
alias :visit_Set :visit_Array
|
2010-08-13 21:05:38 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_edge(o, method)
|
|
|
|
edge(method) { visit o.send(method) }
|
|
|
|
end
|
2010-08-13 21:05:38 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit(o)
|
|
|
|
if node = @seen[o.object_id]
|
|
|
|
@edge_stack.last.to = node
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
node = Node.new(o.class.name, o.object_id)
|
|
|
|
@seen[node.id] = node
|
|
|
|
@nodes << node
|
|
|
|
with_node node do
|
|
|
|
super
|
|
|
|
end
|
2010-08-13 21:05:38 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def edge(name)
|
|
|
|
edge = Edge.new(name, @node_stack.last)
|
|
|
|
@edge_stack.push edge
|
|
|
|
@edges << edge
|
|
|
|
yield
|
|
|
|
@edge_stack.pop
|
2010-08-13 21:05:38 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def with_node(node)
|
|
|
|
if edge = @edge_stack.last
|
|
|
|
edge.to = node
|
|
|
|
end
|
2010-08-13 21:05:38 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
@node_stack.push node
|
|
|
|
yield
|
|
|
|
@node_stack.pop
|
2010-08-13 21:05:38 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def quote(string)
|
|
|
|
string.to_s.gsub('"', '\"')
|
|
|
|
end
|
2010-08-16 17:09:48 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def to_dot
|
|
|
|
"digraph \"Arel\" {\nnode [width=0.375,height=0.25,shape=record];\n" +
|
|
|
|
@nodes.map { |node|
|
|
|
|
label = "<f0>#{node.name}"
|
2010-08-13 21:05:38 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
node.fields.each_with_index do |field, i|
|
|
|
|
label += "|<f#{i + 1}>#{quote field}"
|
|
|
|
end
|
2010-08-13 21:05:38 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
"#{node.id} [label=\"#{label}\"];"
|
|
|
|
}.join("\n") + "\n" + @edges.map { |edge|
|
|
|
|
"#{edge.from.id} -> #{edge.to.id} [label=\"#{edge.name}\"];"
|
|
|
|
}.join("\n") + "\n}"
|
|
|
|
end
|
2010-08-13 21:05:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|