Class: SQLTree::Node::Join

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

[], #inspect, #quote_str, #quote_var

Constructor Details

#initialize(values = {}) ⇒ Join

Returns a new instance of Join.



7
8
9
# File 'lib/sql_tree/node/join.rb', line 7

def initialize(values = {})
  values.each { |key, value| self.send(:"#{key}=", value) }
end

Instance Attribute Details

#join_expressionObject

Returns the value of attribute join_expression.



5
6
7
# File 'lib/sql_tree/node/join.rb', line 5

def join_expression
  @join_expression
end

#join_typeObject

Returns the value of attribute join_type.



5
6
7
# File 'lib/sql_tree/node/join.rb', line 5

def join_type
  @join_type
end

#table_referenceObject

Returns the value of attribute table_reference.



5
6
7
# File 'lib/sql_tree/node/join.rb', line 5

def table_reference
  @table_reference
end

Class Method Details

.parse(tokens) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sql_tree/node/join.rb', line 26

def self.parse(tokens)
  join = self.new

  if tokens.peek == SQLTree::Token::FULL
    join.join_type = :outer
    tokens.consume(SQLTree::Token::FULL, SQLTree::Token::OUTER)
  elsif [SQLTree::Token::OUTER, SQLTree::Token::INNER, SQLTree::Token::LEFT, SQLTree::Token::RIGHT].include?(tokens.peek)
    join.join_type = tokens.next.literal.downcase.to_sym
  end


  tokens.consume(SQLTree::Token::JOIN)
  join.table_reference = SQLTree::Node::TableReference.parse(tokens)
  tokens.consume(SQLTree::Token::ON)
  join.join_expression = SQLTree::Node::Expression.parse(tokens)

  return join
end

Instance Method Details

#==(other) ⇒ Object



45
46
47
48
# File 'lib/sql_tree/node/join.rb', line 45

def ==(other)
  other.table = self.table && other.table_alias == self.table_alias &&
    other.join_type == self.join_type && other.join_expression == self.join_expression
end

#tableObject



18
19
20
# File 'lib/sql_tree/node/join.rb', line 18

def table
  table_reference.table
end

#table_aliasObject



22
23
24
# File 'lib/sql_tree/node/join.rb', line 22

def table_alias
  table_reference.table_alias
end

#to_sqlObject



11
12
13
14
15
16
# File 'lib/sql_tree/node/join.rb', line 11

def to_sql
  join_sql = join_type ? "#{join_type.to_s.upcase} " : ""
  join_sql << "JOIN #{table_reference.to_sql} "
  join_sql << "ON #{join_expression.to_sql}"
  join_sql
end