Class: SQLTree::Node::Expression

Inherits:
Base
  • Object
show all
Defined in:
lib/sql_tree/node/expression.rb

Overview

Abstract base class for all SQL expressions.

To parse a string as an SQL expression, use:

SQLTree::Node::Expression["(3 + 2 = 10 / 2) AND MD5('$ecret') = password"]

This is an abtract class: its parse method will never return an SQLTree::Node::Expression instance, but always an instance of one of its subclasses. The concrete expression classes are defined in the SQLTree::Node::Expression namespace.

Defined Under Namespace

Classes: BinaryOperator, Field, FunctionCall, IntervalValue, List, PostfixOperator, PrefixOperator, Value, Variable

Class Method Summary collapse

Methods inherited from Base

#==, [], child, #equal_children?, #equal_leafs?, inherited, #initialize, #inspect, leaf, parse_list, #quote_str, #quote_var

Constructor Details

This class inherits a constructor from SQLTree::Node::Base

Class Method Details

.parse(tokens) ⇒ Object

Parses an SQL expression from a stream of tokens.

This method will start trying to parse the token stream as a SQLTree::Node::Expression::BinaryOperator, which will in turn try to parse it as other kinds of expressions if a binary expression is not appropriate.

tokens

The token stream to parse from, which is an instance of SQLTree::Parser.



23
24
25
# File 'lib/sql_tree/node/expression.rb', line 23

def self.parse(tokens)
  SQLTree::Node::Expression::BinaryOperator.parse(tokens)
end

.parse_atomic(tokens) ⇒ Object

Parses a single, atomic SQL expression. This can be either:

  • a full expression (or set of expressions) within parentheses.

  • a logical NOT expression

  • an SQL variable

  • an SQL function

  • a literal SQL value (numeric or string)

tokens

The token stream to parse from, which is an instance of SQLTree::Parser.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sql_tree/node/expression.rb', line 36

def self.parse_atomic(tokens)
  if SQLTree::Token::LPAREN === tokens.peek
    tokens.consume(SQLTree::Token::LPAREN)
    expr = self.parse(tokens)
    tokens.consume(SQLTree::Token::RPAREN)
    expr
  elsif tokens.peek.prefix_operator?
    PrefixOperator.parse(tokens)
  elsif tokens.peek.variable?
    if SQLTree::Token::LPAREN === tokens.peek(2)
      FunctionCall.parse(tokens)
    elsif SQLTree::Token::DOT === tokens.peek(2)
      Field.parse(tokens)
    else
      Variable.parse(tokens)
    end
  elsif SQLTree::Token::STRING_ESCAPE == tokens.peek
    tokens.consume(SQLTree::Token::STRING_ESCAPE)
    Value.parse(tokens)
  elsif SQLTree::Token::INTERVAL === tokens.peek
    IntervalValue.parse(tokens)
  else
    Value.parse(tokens)
  end
end