Class: SQLTree::Node::TableReference

Inherits:
Base
  • Object
show all
Defined in:
lib/active_record/turntable/sql_tree_patch.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#quote_field_name

Constructor Details

#initialize(table, table_alias = nil, index_hint = nil) ⇒ TableReference

Returns a new instance of TableReference.



156
157
158
159
160
# File 'lib/active_record/turntable/sql_tree_patch.rb', line 156

def initialize(table, table_alias = nil, index_hint = nil)
  @table = table
  @table_alias = table_alias
  @index_hint = index_hint
end

Class Method Details

.parse(tokens) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/active_record/turntable/sql_tree_patch.rb', line 169

def self.parse(tokens)
  if SQLTree::Token::Identifier === tokens.peek
    tokens.next
    table_reference = self.new(tokens.current.literal)
    if tokens.peek && !tokens.peek.possible_index_hint? &&
       (SQLTree::Token::AS === tokens.peek || SQLTree::Token::Identifier === tokens.peek)
      tokens.consume(SQLTree::Token::AS) if SQLTree::Token::AS === tokens.peek
      table_reference.table_alias = tokens.next.literal
    end
    if tokens.peek && tokens.peek.possible_index_hint? && tokens.peek(2).index_keyword?
      table_reference.index_hint = SQLTree::Node::IndexHint.parse(tokens)
    end
    return table_reference
  elsif SQLTree::Token::SELECT === tokens.peek(2)
    table_reference = self.new(SQLTree::Node::SubQuery.parse(tokens))
    if SQLTree::Token::AS === tokens.peek || SQLTree::Token::Identifier === tokens.peek
      tokens.consume(SQLTree::Token::AS) if SQLTree::Token::AS === tokens.peek
      table_reference.table_alias = tokens.next.literal
    end
    table_reference
  else
    raise SQLTree::Parser::UnexpectedToken, tokens.current
  end
end

Instance Method Details

#to_sql(options = {}) ⇒ Object



162
163
164
165
166
167
# File 'lib/active_record/turntable/sql_tree_patch.rb', line 162

def to_sql(options = {})
  sql = (SQLTree::Node::SubQuery === table) ? table.to_sql : quote_field_name(table)
  sql << " AS " << quote_field_name(table_alias) if table_alias
  sql << " " << index_hint.to_sql if index_hint
  sql
end