class Sequel::ASTTransformer

The ASTTransformer class is designed to handle the abstract syntax trees that Sequel uses internally and produce modified copies of them. By itself it only produces a straight copy. It’s designed to be subclassed and have subclasses returned modified copies of the specific nodes that need to be modified.

Public Instance Methods

transform(obj) click to toggle source

Return obj or a potentially transformed version of it.

   # File lib/sequel/ast_transformer.rb
11 def transform(obj)
12   v(obj)
13 end

Private Instance Methods

v(o) click to toggle source

Recursive version that handles all of Sequel’s internal object types and produces copies of them.

   # File lib/sequel/ast_transformer.rb
19 def v(o)
20   case o
21   when Symbol, Numeric, String, Class, TrueClass, FalseClass, NilClass
22     o
23   when Array
24     o.map{|x| v(x)}
25   when Hash
26     h = {}
27     o.each{|k, val| h[v(k)] = v(val)}
28     h
29   when SQL::NumericExpression
30     if o.op == :extract
31       o.class.new(o.op, o.args[0], v(o.args[1]))
32     else
33       o.class.new(o.op, *v(o.args))
34     end
35   when SQL::ComplexExpression
36     o.class.new(o.op, *v(o.args))
37   when SQL::Identifier
38     SQL::Identifier.new(v(o.value))
39   when SQL::QualifiedIdentifier
40     SQL::QualifiedIdentifier.new(v(o.table), v(o.column))
41   when SQL::OrderedExpression
42     SQL::OrderedExpression.new(v(o.expression), o.descending, :nulls=>o.nulls)
43   when SQL::AliasedExpression
44     SQL::AliasedExpression.new(v(o.expression), o.alias, o.columns)
45   when SQL::CaseExpression
46     args = [v(o.conditions), v(o.default)]
47     args << v(o.expression) if o.expression?
48     SQL::CaseExpression.new(*args)
49   when SQL::Cast
50     SQL::Cast.new(v(o.expr), o.type)
51   when SQL::Function
52     h = {}
53     o.opts.each do |k, val|
54       h[k] = v(val)
55     end
56     SQL::Function.new!(o.name, v(o.args), h)
57   when SQL::Subscript
58     SQL::Subscript.new(v(o.expression), v(o.sub))
59   when SQL::Window
60     opts = o.opts.dup
61     opts[:partition] = v(opts[:partition]) if opts[:partition]
62     opts[:order] = v(opts[:order]) if opts[:order]
63     SQL::Window.new(opts)
64   when SQL::PlaceholderLiteralString
65     args = if o.args.is_a?(Hash)
66       h = {}
67       o.args.each{|k,val| h[k] = v(val)}
68       h
69     else
70       v(o.args)
71     end
72     SQL::PlaceholderLiteralString.new(o.str, args, o.parens)
73   when SQL::JoinOnClause
74     SQL::JoinOnClause.new(v(o.on), o.join_type, v(o.table_expr))
75   when SQL::JoinUsingClause
76     SQL::JoinUsingClause.new(v(o.using), o.join_type, v(o.table_expr))
77   when SQL::JoinClause
78     SQL::JoinClause.new(o.join_type, v(o.table_expr))
79   when SQL::DelayedEvaluation
80     SQL::DelayedEvaluation.new(lambda{|ds| v(o.call(ds))})
81   when SQL::Wrapper
82     SQL::Wrapper.new(v(o.value))
83   when SQL::Expression
84     if o.respond_to?(:sequel_ast_transform)
85       o.sequel_ast_transform(method(:v))
86     else
87       o
88     end
89   else
90     o
91   end
92 end