aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/arel/visitors/dot.rb
blob: 76830412d45e873a231d2085f7037c3ca94b7243 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# frozen_string_literal: true

module Arel # :nodoc: all
  module Visitors
    class Dot < Arel::Visitors::Visitor
      class Node # :nodoc:
        attr_accessor :name, :id, :fields

        def initialize(name, id, fields = [])
          @name   = name
          @id     = id
          @fields = fields
        end
      end

      class Edge < Struct.new :name, :from, :to # :nodoc:
      end

      def initialize
        super()
        @nodes      = []
        @edges      = []
        @node_stack = []
        @edge_stack = []
        @seen       = {}
      end

      def accept(object, collector)
        visit object
        collector << to_dot
      end

      private

        def visit_Arel_Nodes_Ordering(o)
          visit_edge o, "expr"
        end

        def visit_Arel_Nodes_TableAlias(o)
          visit_edge o, "name"
          visit_edge o, "relation"
        end

        def visit_Arel_Nodes_Count(o)
          visit_edge o, "expressions"
          visit_edge o, "distinct"
        end

        def visit_Arel_Nodes_Values(o)
          visit_edge o, "expressions"
        end

        def visit_Arel_Nodes_StringJoin(o)
          visit_edge o, "left"
        end

        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

        def visit_Arel_Nodes_DeleteStatement(o)
          visit_edge o, "relation"
          visit_edge o, "wheres"
        end

        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_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

        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

        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

        def visit_Arel_Nodes_NamedFunction(o)
          visit_edge o, "name"
          visit_edge o, "expressions"
          visit_edge o, "distinct"
          visit_edge o, "alias"
        end

        def visit_Arel_Nodes_InsertStatement(o)
          visit_edge o, "relation"
          visit_edge o, "columns"
          visit_edge o, "values"
        end

        def visit_Arel_Nodes_SelectCore(o)
          visit_edge o, "source"
          visit_edge o, "projections"
          visit_edge o, "wheres"
          visit_edge o,  "windows"
        end

        def visit_Arel_Nodes_SelectStatement(o)
          visit_edge o, "cores"
          visit_edge o, "limit"
          visit_edge o, "orders"
          visit_edge o, "offset"
        end

        def visit_Arel_Nodes_UpdateStatement(o)
          visit_edge o, "relation"
          visit_edge o, "wheres"
          visit_edge o, "values"
        end

        def visit_Arel_Table(o)
          visit_edge o, "name"
        end

        def visit_Arel_Nodes_Casted(o)
          visit_edge o, "val"
          visit_edge o, "attribute"
        end

        def visit_Arel_Attribute(o)
          visit_edge o, "relation"
          visit_edge o, "name"
        end
        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

        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
        end

        def visit_Array(o)
          o.each_with_index do |x, i|
            edge(i) { visit x }
          end
        end
        alias :visit_Set :visit_Array

        def visit_edge(o, method)
          edge(method) { visit o.send(method) }
        end

        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
        end

        def edge(name)
          edge = Edge.new(name, @node_stack.last)
          @edge_stack.push edge
          @edges << edge
          yield
          @edge_stack.pop
        end

        def with_node(node)
          if edge = @edge_stack.last
            edge.to = node
          end

          @node_stack.push node
          yield
          @node_stack.pop
        end

        def quote(string)
          string.to_s.gsub('"', '\"')
        end

        def to_dot
          "digraph \"Arel\" {\nnode [width=0.375,height=0.25,shape=record];\n" +
            @nodes.map { |node|
              label = "<f0>#{node.name}"

              node.fields.each_with_index do |field, i|
                label += "|<f#{i + 1}>#{quote field}"
              end

              "#{node.id} [label=\"#{label}\"];"
            }.join("\n") + "\n" + @edges.map { |edge|
              "#{edge.from.id} -> #{edge.to.id} [label=\"#{edge.name}\"];"
            }.join("\n") + "\n}"
        end
    end
  end
end