Class: SQLTree::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_tree/token.rb

Overview

The SQLTree::Token class is the base class for every token in the SQL language. Actual tokens are represented by a subclass.

Tokens are produced by the SQLTree::Tokenizer from a string and are consumed by the SQLTree::Parser to construct an abstract syntax tree for the query that is being parsed.

Direct Known Subclasses

Keyword, Operator, Value

Defined Under Namespace

Classes: Keyword, Number, Operator, String, Value, Variable

Constant Summary collapse

LPAREN =

Create some static token classes and a single instance of them

Class.new(SQLTree::Token).new('(')
RPAREN =
Class.new(SQLTree::Token).new(')')
DOT =
Class.new(SQLTree::Token).new('.')
COMMA =
Class.new(SQLTree::Token).new(',')
KEYWORDS =

A list of all the SQL reserverd keywords.

%w{SELECT FROM WHERE GROUP HAVING ORDER DISTINCT LEFT RIGHT INNER FULL OUTER NATURAL JOIN USING
AND OR NOT AS ON IS NULL BY LIKE ILIKE BETWEEN IN ASC DESC}
KEYWORD_COMBINATIONS =

A list of keywords that aways occur in fixed combinations. Register these as separate keywords.

[%w{IS NOT}, %w{NOT LIKE}, %w{NOT BETWEEN}, %w{NOT ILIKE}]
ARITHMETHIC_OPERATORS_HASH =
{ '+' => :plus, '-' => :minus, '*' => :multiply, '/' => :divide, '%' => :modulo }
COMPARISON_OPERATORS_HASH =
{ '=' => :eq, '!=' => :ne, '<>' => :ne, '>' => :gt, '<' => :lt, '>=' => :gte, '<=' => :lte }
OPERATORS_HASH =

Register a token class and single instance for every token.

ARITHMETHIC_OPERATORS_HASH.merge(COMPARISON_OPERATORS_HASH)
COMPARISON_OPERATORS =
COMPARISON_OPERATORS_HASH.map { |(literal, symbol)| const_get(symbol.to_s.upcase) } +
[SQLTree::Token::IN, SQLTree::Token::IS, SQLTree::Token::BETWEEN, SQLTree::Token::LIKE, SQLTree::Token::ILIKE, SQLTree::Token::NOT]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(literal) ⇒ Token

Creates a token instance with a given literal representation.

<tt>literal<tt>

The literal string value that was encountered while tokenizing.



19
20
21
# File 'lib/sql_tree/token.rb', line 19

def initialize(literal)
  @literal = literal
end

Instance Attribute Details

#literalObject

For some tokens, the encountered literal value is important during the parsing phase (e.g. strings and variable names). Therefore, the literal value encountered that represented the token in the original SQL query string is stored.



13
14
15
# File 'lib/sql_tree/token.rb', line 13

def literal
  @literal
end

Instance Method Details

#==(other) ⇒ Object

Compares two tokens. Tokens are considered equal when they are instances of the same class, i.e. do literal is not used.



25
26
27
# File 'lib/sql_tree/token.rb', line 25

def ==(other)
  other.class == self.class
end

#direction?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/sql_tree/token.rb', line 39

def direction?
  [SQLTree::Token::ASC, SQLTree::Token::DESC].include?(self)
end

#inspectObject

:nodoc:



29
30
31
# File 'lib/sql_tree/token.rb', line 29

def inspect # :nodoc:
  literal
end

#join?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/sql_tree/token.rb', line 33

def join?
  [SQLTree::Token::JOIN, SQLTree::Token::LEFT, SQLTree::Token::RIGHT,
    SQLTree::Token::INNER, SQLTree::Token::OUTER, SQLTree::Token::NATURAL,
    SQLTree::Token::FULL].include?(self)
end