Sha256: 55dd5c13f4eb8ada5effcd2c4d9032f639a5f56eea4e58bdb5ab2749aeae0c5a

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

module SQLTree::Node
  
  class SelectExpression < Base
    
    attr_accessor :expression, :variable
    
    def initialize(expression, variable = nil)
      @expression = expression
      @variable   = variable
    end
    
    def to_sql
      sql = @expression.to_sql
      sql << " AS " << quote_var(@variable) if @variable
      return sql
    end
    
    def self.parse(tokens)
      if tokens.peek == SQLTree::Token::MULTIPLY
        tokens.consume(SQLTree::Token::MULTIPLY)
        return SQLTree::Node::ALL_FIELDS
      else
        expression = SQLTree::Node::Expression.parse(tokens)
        expr = SQLTree::Node::SelectExpression.new(expression)
        if tokens.peek == SQLTree::Token::AS
          tokens.consume(SQLTree::Token::AS)
          expr.variable = SQLTree::Node::Variable.parse(tokens).name
        end
        return expr
      end
    end
    
    def ==(other)
      other.expression == self.expression && other.variable == self.variable
    end    
  end
  
  class AllFieldsExpression < Expression
    def to_sql
      '*'
    end
  end
  
  ALL_FIELDS = AllFieldsExpression.new
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sql_tree-0.0.2 lib/sql_tree/node/select_expression.rb
sql_tree-0.0.1 lib/sql_tree/node/select_expression.rb