Class: SqlBeautifier::CompoundQuery

Inherits:
Base
  • Object
show all
Defined in:
lib/sql_beautifier/compound_query.rb

Constant Summary collapse

TRAILING_CLAUSE_KEYWORDS =
["order by", "limit"].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(normalized_sql, depth: 0) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/sql_beautifier/compound_query.rb', line 11

def self.parse(normalized_sql, depth: 0)
  boundaries = scan_set_operator_boundaries(normalized_sql)
  return nil if boundaries.empty?

  segments = build_segments(normalized_sql, boundaries)
  trailing_clauses = extract_trailing_clauses!(segments)

  new(segments: segments, trailing_clauses: trailing_clauses, depth: depth)
end

Instance Method Details

#render ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sql_beautifier/compound_query.rb', line 21

def render
  formatted_segments = []

  @segments.each_with_index do |segment, index|
    formatted_sql = Formatter.new(segment[:sql], depth: @depth).call
    return nil unless formatted_sql

    output = +""
    output << "\n#{Util.format_keyword(segment[:operator])}\n\n" if index.positive? && segment[:operator]
    output << formatted_sql.chomp
    formatted_segments << output
  end

  return nil if formatted_segments.empty?

  result = formatted_segments.join("\n")
  result << render_trailing_clauses if @trailing_clauses.present?

  "#{result}\n"
end