Class: Veritas::SQL::Generator::Relation

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

Overview

Abstract base class for SQL generation from a relation

Direct Known Subclasses

Binary, Unary

Defined Under Namespace

Classes: Base, Binary, 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_veritas_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


48
49
50
51
# File 'lib/veritas/sql/generator/relation.rb', line 48

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:


22
23
24
# File 'lib/veritas/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:

  • (Veritas::Relation)

Returns:


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

def self.visit(relation)
  klass = case relation
  when Veritas::Relation::Operation::Set    then self::Set
  when Veritas::Relation::Operation::Binary then self::Binary
  when Veritas::Relation::Operation::Unary  then self::Unary
  when Veritas::Relation::Base              then self::Base
  else
    raise 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:


92
93
94
95
# File 'lib/veritas/sql/generator/relation.rb', line 92

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:

  • (String)

80
81
82
# File 'lib/veritas/sql/generator/relation.rb', line 80

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:


102
103
104
105
# File 'lib/veritas/sql/generator/relation.rb', line 102

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:

  • visitable (Visitable)

    A visitable object

Returns:

  • (self)

Raises:


67
68
69
70
# File 'lib/veritas/sql/generator/relation.rb', line 67

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:

  • (Boolean)

115
116
117
# File 'lib/veritas/sql/generator/relation.rb', line 115

def visited?
  ! @name.nil?
end