Class: SqlBeautifier::TableRegistry

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_content) ⇒ TableRegistry

Returns a new instance of TableRegistry.



7
8
9
10
11
12
13
14
# File 'lib/sql_beautifier/table_registry.rb', line 7

def initialize(from_content)
  @from_content = from_content
  @alias_strategy = SqlBeautifier.config_for(:alias_strategy)
  @references = []
  @references_by_name = {}

  build!
end

Instance Attribute Details

#references ⇒ Object (readonly)

Returns the value of attribute references.



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

def references
  @references
end

Instance Method Details

#alias_for(table_name) ⇒ Object



16
17
18
# File 'lib/sql_beautifier/table_registry.rb', line 16

def alias_for(table_name)
  @references_by_name[table_name]&.alias_name
end

#apply_aliases(text) ⇒ Object



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
# File 'lib/sql_beautifier/table_registry.rb', line 28

def apply_aliases(text)
  return text if @references_by_name.empty?

  scanner = Scanner.new(text)
  output = +""

  until scanner.finished?
    case scanner.current_char
    when Constants::SINGLE_QUOTE
      output << scanner.consume_single_quoted_string!
    when Constants::DOUBLE_QUOTE
      output << scanner.consume_double_quoted_identifier!
    else
      replacement = find_table_replacement_at(text, scanner.position, scanner)

      if replacement
        table_name, table_alias = replacement

        output << "#{table_alias}."
        scanner.advance!(table_name.length + 1)
      else
        output << scanner.current_char
        scanner.advance!
      end
    end
  end

  output
end

#reference_for(table_name) ⇒ Object



20
21
22
# File 'lib/sql_beautifier/table_registry.rb', line 20

def reference_for(table_name)
  @references_by_name[table_name]
end

#table_map ⇒ Object



24
25
26
# File 'lib/sql_beautifier/table_registry.rb', line 24

def table_map
  @references_by_name.transform_values(&:alias_name)
end