Class: Axiom::SQL::Generator::Relation

Inherits:
Visitor
  • Object
show all
Extended by:
Identifier
Includes:
Attribute
Defined in:
lib/axiom/sql/generator/relation.rb,
lib/axiom/sql/generator/relation/set.rb,
lib/axiom/sql/generator/relation/base.rb,
lib/axiom/sql/generator/relation/unary.rb,
lib/axiom/sql/generator/relation/binary.rb,
lib/axiom/sql/generator/relation/insertion.rb,
lib/axiom/sql/generator/relation/materialized.rb

Overview

Abstract base class for SQL generation from a relation

Direct Known Subclasses

Binary, Materialized, Unary

Defined Under Namespace

Classes: Base, Binary, Insertion, Materialized, Set, Unary

Constant Summary collapse

EMPTY_STRING =
''.freeze
SEPARATOR =
', '.freeze
STAR =
'*'.freeze
EMPTY_HASH =
{}.freeze

Constants included from Identifier

Identifier::ESCAPED_QUOTE, Identifier::QUOTE

Constants inherited from Visitor

Visitor::DOUBLE_COLON, Visitor::NAME_REP, Visitor::NAME_SEP_REGEXP, Visitor::UNDERSCORE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Identifier

visit_identifier

Methods included from Attribute

#visit_axiom_attribute

Methods inherited from Visitor

handler_for

Constructor Details

#initializeundefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a Generator

API:

  • private



51
52
53
54
# File 'lib/axiom/sql/generator/relation.rb', line 51

def initialize
  @sql        = EMPTY_STRING
  @extensions = {}
end

Instance Attribute Details

#name#to_s (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the alias name

Returns:

API:

  • private



22
23
24
# File 'lib/axiom/sql/generator/relation.rb', line 22

def name
  @name
end

Class Method Details

.visit(relation) ⇒ Generator::Relation

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Factory method to instantiate the generator for the relation

Parameters:

Returns:

API:

  • private



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/axiom/sql/generator/relation.rb', line 31

def self.visit(relation)
  klass =
    case relation
    when Axiom::Relation::Operation::Insertion then self::Insertion
    when Axiom::Relation::Operation::Set       then self::Set
    when Axiom::Relation::Operation::Binary    then self::Binary
    when Axiom::Relation::Operation::Unary     then self::Unary
    when Axiom::Relation::Base                 then self::Base
    when Axiom::Relation::Materialized         then self::Materialized
    else
      fail InvalidRelationError, "#{relation.class} is not a visitable relation"
    end
  klass.new.visit(relation)
end

Instance Method Details

#to_s#to_s

Return the SQL for the unary relation

Examples:

sql = unary_relation.to_s

Returns:

API:

  • public



95
96
97
98
# File 'lib/axiom/sql/generator/relation.rb', line 95

def to_s
  return EMPTY_STRING unless visited?
  generate_sql(query_columns)
end

#to_sqlString

Returns the current SQL string

Examples:

sql = generator.to_sql

Returns:

API:

  • public



83
84
85
# File 'lib/axiom/sql/generator/relation.rb', line 83

def to_sql
  @sql
end

#to_subquery#to_s

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the SQL suitable for an subquery

Returns:

API:

  • private



105
106
107
108
# File 'lib/axiom/sql/generator/relation.rb', line 105

def to_subquery
  return EMPTY_STRING unless visited?
  Generator.parenthesize!(generate_sql(subquery_columns))
end

#visit(visitable) ⇒ self

Visit an object and generate SQL from each node

Examples:

generator.visit(visitable)

Parameters:

  • A visitable object

Returns:

Raises:

  • raised when the visitable object has no handler

API:

  • public



70
71
72
73
# File 'lib/axiom/sql/generator/relation.rb', line 70

def visit(visitable)
  @sql = dispatch(visitable).to_s.freeze
  freeze
end

#visited?Boolean

Test if a visitable object has been visited

Examples:

visitor.visited?  # true or false

Returns:

API:

  • public



118
119
120
# File 'lib/axiom/sql/generator/relation.rb', line 118

def visited?
  instance_variable_defined?(:@name)
end