Class: SqlBeautifier::CteDefinition

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

Instance Method Summary collapse

Instance Method Details

#render_body(base_indent) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sql_beautifier/cte_definition.rb', line 20

def render_body(base_indent)
  indent_spaces = SqlBeautifier.config_for(:indent_spaces) || 4
  body_indent = base_indent + indent_spaces
  formatted = Formatter.new(@body_sql, depth: 0).call
  return "(#{@body_sql})" unless formatted

  indentation = Util.whitespace(body_indent)
  indented_lines = formatted.chomp.lines.map do |line|
    line.strip.empty? ? "\n" : "#{indentation}#{line}"
  end.join

  "(\n#{indented_lines}\n#{Util.whitespace(base_indent)})"
end

#render_header ⇒ Object



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

def render_header
  formatted_name = @name.start_with?(Constants::DOUBLE_QUOTE) ? @name : Util.format_table_name(@name)
  header = +formatted_name
  header << " (#{@column_list})" if @column_list
  header << " #{Util.format_keyword('as')}"
  header << " #{format_materialization}" if @materialization
  header << " "
  header
end