Class: SQLPP::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/sqlpp/formatter.rb

Instance Method Summary collapse

Constructor Details

#initializeFormatter

Returns a new instance of Formatter.



3
4
5
6
# File 'lib/sqlpp/formatter.rb', line 3

def initialize
  @indent = nil
  @state = nil
end

Instance Method Details

#_format_Alias(node) ⇒ Object



127
128
129
# File 'lib/sqlpp/formatter.rb', line 127

def _format_Alias(node)
  format(node.expr) + " " + format(node.name)
end

#_format_As(node) ⇒ Object



123
124
125
# File 'lib/sqlpp/formatter.rb', line 123

def _format_As(node)
  format(node.expr) + " AS " + format(node.name)
end

#_format_Atom(node) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/sqlpp/formatter.rb', line 83

def _format_Atom(node)
  output = ""

  case node.type
    when :range
      output << format(node.left) << " AND " << format(node.right)
    when :list
      output << "(" << node.left.map { |c| format(c) }.join(", ") << ")"
    when :func
      output << format(node.left) << "("
      output << node.right.map { |c| format(c) }.join(", ")
      output << ")"
    when :lit
      output << node.left
    when :attr
      output << node.left
      output << "." << node.right if node.right
    when :case
      output << "CASE "
      output << format(node.left) << " " if node.left
      node.right.each do |child|
        if child.is_a?(Array)
          output << "WHEN " << format(child[0]) << " "
          output << "THEN " << format(child[1]) << " "
        else
          output << "ELSE " << format(child) << " "
        end
      end
      output << "END"
    else
      raise ArgumentError, "unknown atom type #{node.type.inspect}"
  end

  output
end

#_format_Expr(node) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sqlpp/formatter.rb', line 59

def _format_Expr(node)
  output = format(node.left)
  if node.op
    op = node.op.to_s.upcase

    if @state == :where && %w(AND OR).include?(op)
      output << "\n#{_indent}"
    else
      output << " "
    end

    output << op << " "
    output << format(node.right)
  end
  output
end

#_format_Join(node) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/sqlpp/formatter.rb', line 131

def _format_Join(node)
  output = ""

  output << format(node.left)
  output << "\n#{_indent}"
  output << node.type.upcase << " JOIN "
  output << format(node.right)
  output << "\n#{_indent}ON " << format(node.on) if node.on

  output
end

#_format_Parens(node) ⇒ Object



119
120
121
# File 'lib/sqlpp/formatter.rb', line 119

def _format_Parens(node)
  "(" + format(node.value) + ")"
end

#_format_Select(node) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sqlpp/formatter.rb', line 13

def _format_Select(node)
  output = ""

  if @indent.nil?
    @indent = 0
  else
    @indent += 2
    output << "\n"
  end

  output << "#{_indent}SELECT "
  output << node.projections.map { |c| format(c) }.join(", ")
  output << "\n"

  if node.froms
    output << "#{_indent}FROM "
    output << node.froms.map { |c| format(c) }.join(", ")
    output << "\n"
  end

  if node.wheres
    save, @state = @state, :where
    output << "#{_indent}WHERE "
    output << format(node.wheres)
    output << "\n"
    @state = save
  end

  if node.groups
    output << "#{_indent}GROUP BY "
    output << node.groups.map { |c| format(c) }.join(", ")
    output << "\n"
  end

  if node.orders
    output << "#{_indent}ORDER BY "
    output << node.orders.map { |c| format(c) }.join(", ")
    output << "\n"
  end

  @indent -= 2
  @indent = nil if @indent < 0

  output << _indent
end

#_format_SortKey(node) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/sqlpp/formatter.rb', line 143

def _format_SortKey(node)
  output = ""
  output << format(node.key)

  if node.options.any?
    output << " "
    output << node.options.map { |opt| opt.upcase }.join(", ")
  end

  output
end

#_format_String(string) ⇒ Object



155
156
157
# File 'lib/sqlpp/formatter.rb', line 155

def _format_String(string)
  string
end

#_format_Unary(node) ⇒ Object



76
77
78
79
80
81
# File 'lib/sqlpp/formatter.rb', line 76

def _format_Unary(node)
  op = node.op.to_s.upcase
  output = op
  output << " " if op =~ /\w/
  output << format(node.expr)
end

#_indentObject



159
160
161
# File 'lib/sqlpp/formatter.rb', line 159

def _indent
  " " * (@indent || 0)
end

#format(node) ⇒ Object



8
9
10
11
# File 'lib/sqlpp/formatter.rb', line 8

def format(node)
  name = node.class.to_s.split(/::/).last
  send(:"_format_#{name}", node)
end