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



145
146
147
# File 'lib/sqlpp/formatter.rb', line 145

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

#_format_As(node) ⇒ Object



141
142
143
# File 'lib/sqlpp/formatter.rb', line 141

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

#_format_Atom(node) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/sqlpp/formatter.rb', line 101

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sqlpp/formatter.rb', line 77

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



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

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_Limit(node) ⇒ Object



173
174
175
# File 'lib/sqlpp/formatter.rb', line 173

def _format_Limit(node)
  "LIMIT #{format(node.expr)}"
end

#_format_Offset(node) ⇒ Object



177
178
179
# File 'lib/sqlpp/formatter.rb', line 177

def _format_Offset(node)
  "OFFSET #{format(node.expr)}"
end

#_format_Parens(node) ⇒ Object



137
138
139
# File 'lib/sqlpp/formatter.rb', line 137

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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 ")
  output << "DISTINCT " if node.distinct
  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

  if node.limit || node.offset
    output << _indent

    if node.limit
      output << format(node.limit)
      output << " " if node.offset
    end

    output << format(node.offset) if node.offset

    output << "\n"
  end

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

  output << _indent
end

#_format_SortKey(node) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/sqlpp/formatter.rb', line 161

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



181
182
183
# File 'lib/sqlpp/formatter.rb', line 181

def _format_String(string)
  string
end

#_format_Unary(node) ⇒ Object



94
95
96
97
98
99
# File 'lib/sqlpp/formatter.rb', line 94

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

#_indentObject



185
186
187
# File 'lib/sqlpp/formatter.rb', line 185

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