Class: SQLPP::Formatter

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

Instance Method Summary collapse

Constructor Details

#initialize(projections: nil) ⇒ Formatter

Returns a new instance of Formatter.



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

def initialize(projections: nil)
  @indent = nil
  @state = nil

  @projections = projections
end

Instance Method Details

#_format_Alias(node) ⇒ Object



131
132
133
# File 'lib/sqlpp/formatter.rb', line 131

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

#_format_As(node) ⇒ Object



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

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

#_format_Atom(node) ⇒ Object



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
118
119
120
121
# File 'lib/sqlpp/formatter.rb', line 87

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sqlpp/formatter.rb', line 63

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



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/sqlpp/formatter.rb', line 135

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



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

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

#_format_Select(node) ⇒ Object



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
58
59
60
61
# File 'lib/sqlpp/formatter.rb', line 15

def _format_Select(node)
  output = ""

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

  output << (select = "#{_indent}SELECT ")
  link = ","
  link << ((@projections == :wrap) ? "\n#{" " * select.length}" : " ")
  output << node.projections.map { |c| format(c) }.join(link)
  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



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/sqlpp/formatter.rb', line 147

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



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

def _format_String(string)
  string
end

#_format_Unary(node) ⇒ Object



80
81
82
83
84
85
# File 'lib/sqlpp/formatter.rb', line 80

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

#_indentObject



163
164
165
# File 'lib/sqlpp/formatter.rb', line 163

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

#format(node) ⇒ Object



10
11
12
13
# File 'lib/sqlpp/formatter.rb', line 10

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