The Sincerest Form

ZOMG, a Blog II post that isn’t about a new release of Sequel!

You may remember a few months ago where I talked about visualizing association relationships between models using sequel_dot. I recently attended RubyConf and saw Aaron Patterson’s awesome presentation. One of the things Aaron showed off in his presentation (slide 257), was a Graphviz visualization of an ARel Relation’s abstract syntax tree. I decided to produce something similar for a Sequel dataset. So today I pushed a commit that adds a to_dot extension to Sequel.

Here are a couple of examples of output. First, a simple example:

DB[:items].filter(:a=>1)

Second, a more complicated example (click to enlarge):

DB[:items, :items2].distinct.
  filter(:a=>1, :b=>[:a, :b.identifier, :c.qualify(:a)]).
  order(:a.desc, :b.asc(:nulls=>:first), :c).
  select(:a.cast(Integer).as(:b),
    {:d=>:b, :g=>:h}.case(:e, :f.sql_function(1, :b.sql_subscript(:c)))).
  select_more{sum(:over, :args=>[:a, 'a IN :v'.lit(:v=>[true, false, nil]), 
    '? = ?'.lit('c'.lit, 'd')], :partition=>[:b], :order=>:c.desc){\}\}.
  with(:a, db[:a]).
  natural_join(:b).
  join(:c, [:d]).
  join(:e, :f=>:g).
  group(:d, :e).
  having{e < g}.
  limit(42, 24).
  for_update