Class: SqlBeautifier::Normalizer

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

Constant Summary collapse

SAFE_UNQUOTED_IDENTIFIER =
%r{\A[[:lower:]_][[:lower:][:digit:]_]*\z}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Normalizer

Returns a new instance of Normalizer.



11
12
13
# File 'lib/sql_beautifier/normalizer.rb', line 11

def initialize(value)
  @value = value
end

Class Method Details

.call ⇒ Object



7
8
9
# File 'lib/sql_beautifier/normalizer.rb', line 7

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

Instance Method Details

#call ⇒ 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
# File 'lib/sql_beautifier/normalizer.rb', line 15

def call
  return unless @value.present?

  @source = @value.strip
  return unless @source.present?

  @source = strip_trailing_semicolons(@source)
  @source = @source.strip
  return unless @source.present?

  @scanner = Scanner.new(@source)
  @output = +""

  until @scanner.finished?
    if @scanner.sentinel_at?
      @output << @scanner.consume_sentinel!
      next
    end

    case @scanner.current_char
    when Constants::SINGLE_QUOTE
      consume_string_literal!

    when Constants::DOUBLE_QUOTE
      consume_quoted_identifier!

    when Constants::WHITESPACE_CHARACTER_REGEX
      collapse_whitespace!

    else
      @output << @scanner.current_char.downcase
      @scanner.advance!
    end
  end

  @output
end