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