class Sequel::ToDot

Constants

TO_DOT_OPTIONS

The option keys that should be included in the dot output.

Public Class Methods

new(ds) click to toggle source

Given a Dataset, parse the internal structure to generate a dataset visualization.

   # File lib/sequel/extensions/to_dot.rb
37 def initialize(ds)
38   @i = 0
39   @stack = [@i]
40   @dot = ["digraph G {", "0 [label=\"self\"];"]
41   v(ds, "")
42   @dot << "}"
43 end
output(ds) click to toggle source

Given a Dataset, return a string in dot format that will generate a visualization of the dataset.

   # File lib/sequel/extensions/to_dot.rb
31 def self.output(ds)
32   new(ds).output
33 end

Public Instance Methods

output() click to toggle source

Output the dataset visualization as a string in dot format.

   # File lib/sequel/extensions/to_dot.rb
46 def output
47   @dot.join("\n")
48 end

Private Instance Methods

dot(label, j=nil) click to toggle source

Add an entry to the dot output with the given label. If j is given, it is used directly as the node or transition. Otherwise a node is created for the current object.

   # File lib/sequel/extensions/to_dot.rb
55 def dot(label, j=nil)
56   label = case label
57   when nil
58     "<nil>"
59   else
60     label.to_s
61   end
62   @dot << "#{j||@i} [label=#{label.inspect}];"
63 end
v(e, l) click to toggle source

Recursive method that parses all of Sequel’s internal datastructures, adding the appropriate nodes and transitions to the internal dot structure.

    # File lib/sequel/extensions/to_dot.rb
 68 def v(e, l)
 69   @i += 1
 70   dot(l, "#{@stack.last} -> #{@i}")
 71   @stack.push(@i)
 72   case e
 73   when LiteralString
 74     dot "Sequel.lit(#{e.to_s.inspect})"
 75   when Symbol, Numeric, String, Class, TrueClass, FalseClass, NilClass
 76     dot e.inspect
 77   when Array
 78     dot "Array"
 79     e.each_with_index do |val, j|
 80       v(val, j)
 81     end
 82   when Hash
 83     dot "Hash"
 84     e.each do |k, val|
 85       v(val, k)
 86     end
 87   when SQL::ComplexExpression 
 88     dot "ComplexExpression: #{e.op}"
 89     e.args.each_with_index do |val, j|
 90       v(val, j)
 91     end
 92   when SQL::Identifier
 93     dot "Identifier"
 94     v(e.value, :value)
 95   when SQL::QualifiedIdentifier
 96     dot "QualifiedIdentifier"
 97     v(e.table, :table)
 98     v(e.column, :column)
 99   when SQL::OrderedExpression
100     dot "OrderedExpression: #{e.descending ? :DESC : :ASC}#{" NULLS #{e.nulls.to_s.upcase}" if e.nulls}"
101     v(e.expression, :expression)
102   when SQL::AliasedExpression
103     dot "AliasedExpression"
104     v(e.expression, :expression)
105     v(e.alias, :alias)
106     v(e.columns, :columns) if e.columns
107   when SQL::CaseExpression
108     dot "CaseExpression"
109     v(e.expression, :expression) if e.expression
110     v(e.conditions, :conditions)
111     v(e.default, :default)
112   when SQL::Cast
113     dot "Cast"
114     v(e.expr, :expr)
115     v(e.type, :type)
116   when SQL::Function
117     dot "Function: #{e.name}"
118     e.args.each_with_index do |val, j|
119       v(val, j)
120     end
121     v(e.args, :args)
122     v(e.opts, :opts)
123   when SQL::Subscript 
124     dot "Subscript"
125     v(e.f, :f)
126     v(e.sub, :sub)
127   when SQL::Window
128     dot "Window"
129     v(e.opts, :opts)
130   when SQL::PlaceholderLiteralString
131     str = e.str
132     str = "(#{str})" if e.parens
133     dot "PlaceholderLiteralString: #{str.inspect}"
134     v(e.args, :args)
135   when SQL::JoinClause
136     str = "#{e.join_type.to_s.upcase} JOIN".dup
137     if e.is_a?(SQL::JoinOnClause)
138       str << " ON"
139     elsif e.is_a?(SQL::JoinUsingClause)
140       str << " USING"
141     end
142     dot str
143     v(e.table_expr, :table)
144     if e.is_a?(SQL::JoinOnClause)
145       v(e.on, :on) 
146     elsif e.is_a?(SQL::JoinUsingClause)
147       v(e.using, :using) 
148     end
149   when Dataset
150     dot "Dataset"
151     TO_DOT_OPTIONS.each do |k|
152       if val = e.opts[k]
153         v(val, k) 
154       end
155     end
156   else
157     dot "Unhandled: #{e.inspect}"
158   end
159   @stack.pop
160 end