Class: SqlBeautifier::StatementAssembler

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ StatementAssembler

Returns a new instance of StatementAssembler.



9
10
11
# File 'lib/sql_beautifier/statement_assembler.rb', line 9

def initialize(value)
  @value = value
end

Class Method Details

.call ⇒ Object



5
6
7
# File 'lib/sql_beautifier/statement_assembler.rb', line 5

def self.call(...)
  new(...).call
end

Instance Method Details

#call ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sql_beautifier/statement_assembler.rb', line 13

def call
  removable_types = SqlBeautifier.config_for(:removable_comment_types)
  comment_result = CommentParser.call(@value, removable_types)

  statements = StatementSplitter.split(comment_result.stripped_sql)
  formatted_statements = statements.filter_map do |statement|
    Formatter.call(statement)&.chomp
  end
  return if formatted_statements.empty?

  trailing_semicolon = SqlBeautifier.config_for(:trailing_semicolon)
  separator = trailing_semicolon ? ";\n\n" : "\n\n"
  terminator = trailing_semicolon ? ";\n" : "\n"

  output = formatted_statements.join(separator) + terminator
  CommentParser.restore(output, comment_result.comment_map)
end