Class: Fast::SQL::Rewriter

Inherits:
Rewriter show all
Defined in:
lib/fast/sql/rewriter.rb

Overview

Extends fast rewriter to support SQL

See Also:

Instance Attribute Summary

Attributes inherited from Rewriter

#ast, #match_index, #replacement, #search

Instance Method Summary collapse

Methods inherited from Rewriter

#buffer, #execute_replacement, #initialize, #insert_after, #insert_before, #match?, #remove, #replace, #rewrite, #wrap

Constructor Details

This class inherits a constructor from Fast::Rewriter

Instance Method Details

#replace_on(*types) ⇒ Object

Generate methods for all affected types. Note the strategy is different from parent class, it will not stop on first match, but will execute the replacement on all matching elements.

See Also:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fast/sql/rewriter.rb', line 82

def replace_on(*types)
  types.map do |type|
    self.instance_exec do
      self.class.define_method :"on_#{type}" do |node|
        # SQL nodes are not being automatically invoked by the rewriter,
        # so we need to match the root node and invoke on matching inner elements.
        Fast.search(search, ast).each_with_index do |node, i|
          @match_index += 1
          execute_replacement(node, i)
        end
      end
    end
  end
end

#rewrite!Object



44
45
46
47
48
49
50
51
52
# File 'lib/fast/sql/rewriter.rb', line 44

def rewrite!
  replace_on(*types)
  case ast
  when Array
    rewrite(buffer, ast.first)
  else
    rewrite(buffer, ast)
  end
end

#sourceObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fast/sql/rewriter.rb', line 54

def source
  super ||
    begin
      case ast
      when Array
        ast.first
      else
        ast
      end.location.expression.source_buffer.source
    end
end

#typesArray<Symbol>

Returns with all types that matches.

Returns:

  • (Array<Symbol>)

    with all types that matches



66
67
68
69
70
71
72
73
74
75
# File 'lib/fast/sql/rewriter.rb', line 66

def types
  case ast
  when Array
    ast.map(&:type)
  when NilClass
    []
  else
    ast.type
  end
end