Class: SqlMunger::TableName

Inherits:
Object
  • Object
show all
Includes:
Quoter
Defined in:
lib/sql_munger/table_name.rb

Overview

Essentially a qualified table name that’s parsed and makes the parts accessible. TODO reduce dependency on Treetop and parser.

Instance Attribute Summary collapse

Attributes included from Quoter

#quoter

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Quoter

#identifier_quoter, included, #value_quoter

Constructor Details

#initialize(qualified_identifier, quoter = nil) ⇒ TableName

Returns a new instance of TableName.



12
13
14
15
16
# File 'lib/sql_munger/table_name.rb', line 12

def initialize( qualified_identifier, quoter = nil )
  @quoter = quoter
  @qualified_identifier = qualified_identifier
  @tree = self.class.sql_parser.parse( @qualified_identifier )
end

Instance Attribute Details

#treeObject (readonly)

Returns the value of attribute tree.



18
19
20
# File 'lib/sql_munger/table_name.rb', line 18

def tree
  @tree
end

Class Method Details

.sql_parserObject



20
21
22
23
24
25
26
# File 'lib/sql_munger/table_name.rb', line 20

def self.sql_parser
  if @sql_parser.nil?
    @sql_parser = SqlParser.new
    @sql_parser.root = :qualified_identifier
  end
  @sql_parser
end

Instance Method Details

#nameObject



28
29
30
# File 'lib/sql_munger/table_name.rb', line 28

def name
  @tree.parts.last
end

#partsObject



32
33
34
# File 'lib/sql_munger/table_name.rb', line 32

def parts
  @tree.parts
end

#quoteObject



36
37
38
39
40
# File 'lib/sql_munger/table_name.rb', line 36

def quote
  parts.map do |part|
    quoter.quote_ident( part )
  end.join('.')
end

#to_sObject



42
43
44
# File 'lib/sql_munger/table_name.rb', line 42

def to_s
  @tree.parts.join( '.' )
end