Class: SqlBeautifier::Clauses::From

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

Constant Summary collapse

KEYWORD =
"from"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, table_registry:) ⇒ From

Returns a new instance of From.



12
13
14
15
16
# File 'lib/sql_beautifier/clauses/from.rb', line 12

def initialize(value, table_registry:)
  super(value)

  @table_registry = table_registry
end

Class Method Details

.call ⇒ Object



8
9
10
# File 'lib/sql_beautifier/clauses/from.rb', line 8

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

Instance Method Details

#call ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sql_beautifier/clauses/from.rb', line 18

def call
  join_parts = split_join_parts
  primary_table_text = join_parts.shift.strip
  primary_lookup_name = TableReference.derived_table_lookup_name_from(primary_table_text) || Util.first_word(primary_table_text)
  primary_reference = @table_registry.reference_for(primary_lookup_name)
  trailing_sentinels = Join.extract_trailing_sentinels(primary_table_text)

  lines = []
  lines << "#{keyword_prefix}#{primary_reference.render(trailing_sentinels: trailing_sentinels)}"

  join_parts.each do |join_text|
    join = Join.parse(join_text, table_registry: @table_registry)
    next unless join

    lines << join.render(continuation_indent: continuation_indent, condition_indent: join_condition_indentation)
  end

  lines.join("\n")
end