Sha256: 15eb82a07131af84e55a43bcc0edefb7dd2bb81a93ffdc2150b06c045340d6f0

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 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.1.0 lib/sql_tree/node/select_expression.rb
sql_tree-0.0.3 lib/sql_tree/node/select_expression.rb