Sha256: 3a798f212856c70df8d1a6b92c401d3e3257121cdae903b496e64e8d110e9857

Contents?: true

Size: 861 Bytes

Versions: 2

Compression:

Stored size: 861 Bytes

Contents

module SQLTree::Node

  class TableReference < Base

    leaf :table
    leaf :table_alias

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

    def to_sql(options = {})
      sql = quote_var(table)
      sql << " AS " << quote_var(table_alias) if table_alias
      return sql
    end

    def self.parse(tokens)
      if SQLTree::Token::Identifier === tokens.next
        table_reference = self.new(tokens.current.literal)
        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
        return table_reference
      else
        raise SQLTree::Parser::UnexpectedToken.new(tokens.current)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sql_tree-0.2.0 lib/sql_tree/node/table_reference.rb
sql_tree-0.1.1 lib/sql_tree/node/table_reference.rb