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



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

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

#_format_As(node) ⇒ Object



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

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

#_format_Atom(node) ⇒ Object



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
136
# File 'lib/sqlpp/formatter.rb', line 102

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
93
# 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 << "NOT " if node.not
    output << op << " "
    output << format(node.right)
  end
  output
end

#_format_Join(node) ⇒ Object



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

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



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

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

#_format_Offset(node) ⇒ Object



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

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

#_format_Parens(node) ⇒ Object



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

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



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

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



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

def _format_String(string)
  string
end

#_format_Subscript(node) ⇒ Object



186
187
188
189
190
191
# File 'lib/sqlpp/formatter.rb', line 186

def _format_Subscript(node)
  output = ""
  output << format(node.left)
  output << "[" << format(node.right) << "]"
  output
end

#_format_TypeCast(node) ⇒ Object



193
194
195
# File 'lib/sqlpp/formatter.rb', line 193

def _format_TypeCast(node)
  format(node.value) << '::' << format(node.type)
end

#_format_Unary(node) ⇒ Object



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

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

#_indentObject



197
198
199
# File 'lib/sqlpp/formatter.rb', line 197

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