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

Inherits:
Veritas::SQL::Generator::Relation show all
Extended by:
Aliasable
Includes:
Direction, Literal, Logic
Defined in:
lib/veritas/sql/generator/relation/unary.rb

Overview

Generates an SQL statement for a unary relation

Direct Known Subclasses

Base

Constant Summary collapse

DISTINCT =
'DISTINCT '.freeze
COLLAPSIBLE =
{
  Algebra::Projection                   => Set[ Algebra::Projection, Algebra::Restriction,                                                                                                                                                                        ].freeze,
  Algebra::Restriction                  => Set[ Algebra::Projection,                       Veritas::Relation::Operation::Order, Veritas::Relation::Operation::Reverse,                                                                                            ].freeze,
  Veritas::Relation::Operation::Order   => Set[ Algebra::Projection, Algebra::Restriction, Veritas::Relation::Operation::Order, Veritas::Relation::Operation::Reverse,                                                                            Algebra::Rename ].freeze,
  Veritas::Relation::Operation::Reverse => Set[ Algebra::Projection, Algebra::Restriction, Veritas::Relation::Operation::Order, Veritas::Relation::Operation::Reverse,                                                                            Algebra::Rename ].freeze,
  Veritas::Relation::Operation::Offset  => Set[ Algebra::Projection, Algebra::Restriction, Veritas::Relation::Operation::Order, Veritas::Relation::Operation::Reverse,                                                                            Algebra::Rename ].freeze,
  Veritas::Relation::Operation::Limit   => Set[ Algebra::Projection, Algebra::Restriction, Veritas::Relation::Operation::Order, Veritas::Relation::Operation::Reverse, Veritas::Relation::Operation::Offset,                                      Algebra::Rename ].freeze,
  Algebra::Rename                       => Set[ Algebra::Projection, Algebra::Restriction, Veritas::Relation::Operation::Order, Veritas::Relation::Operation::Reverse, Veritas::Relation::Operation::Offset, Veritas::Relation::Operation::Limit                  ].freeze,
}.freeze

Constants included from Identifier

Identifier::ESCAPED_QUOTE, Identifier::QUOTE

Constants included from Literal

Literal::DATETIME_FORMAT, Literal::DATE_FORMAT, Literal::ESCAPED_QUOTE, Literal::FALSE, Literal::NULL, Literal::QUOTE, Literal::SEPARATOR, Literal::TIME_SCALE, Literal::TRUE

Constants included from Logic

Logic::AND, Logic::BETWEEN, Logic::EMPTY_ARRAY, Logic::EQUAL_TO, Logic::EQUAL_TO_NULL, Logic::GREATER_THAN, Logic::GREATER_THAN_OR_EQUAL_TO, Logic::IN, Logic::LESS_THAN, Logic::LESS_THAN_OR_EQUAL_TO, Logic::MATCH_ALL, Logic::MATCH_NONE, Logic::NOT_BETWEEN, Logic::NOT_EQUAL_TO, Logic::NOT_EQUAL_TO_NULL, Logic::NOT_IN, Logic::OR

Constants inherited from Veritas::SQL::Generator::Relation

ALL_COLUMNS, EMPTY_STRING, SEPARATOR

Constants inherited from Visitor

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

Instance Attribute Summary

Attributes inherited from Veritas::SQL::Generator::Relation

#name

Instance Method Summary collapse

Methods included from Direction

#visit_veritas_relation_operation_order_ascending, #visit_veritas_relation_operation_order_descending

Methods included from Attribute

#visit_veritas_attribute

Methods included from Identifier

#visit_identifier

Methods included from Literal

dup_frozen, #visit_class, #visit_date, #visit_date_time, #visit_enumerable, #visit_false_class, #visit_nil_class, #visit_numeric, #visit_string, #visit_time, #visit_true_class

Methods included from Logic

#visit_veritas_logic_connective_conjunction, #visit_veritas_logic_connective_disjunction, #visit_veritas_logic_connective_negation, #visit_veritas_logic_predicate_equality, #visit_veritas_logic_predicate_exclusion, #visit_veritas_logic_predicate_greater_than, #visit_veritas_logic_predicate_greater_than_or_equal_to, #visit_veritas_logic_predicate_inclusion, #visit_veritas_logic_predicate_inequality, #visit_veritas_logic_predicate_less_than, #visit_veritas_logic_predicate_less_than_or_equal_to, #visit_veritas_logic_proposition_contradiction, #visit_veritas_logic_proposition_tautology

Methods inherited from Veritas::SQL::Generator::Relation

subquery, #to_sql, visit, #visit, #visited?

Methods inherited from Visitor

handler_for, #visit, #visited?

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 Unary relation SQL generator



31
32
33
34
# File 'lib/veritas/sql/generator/relation/unary.rb', line 31

def initialize
  super
  @scope = ::Set.new
end

Instance Method Details

#to_s#to_s

Return the SQL for the unary relation

Examples:

sql = unary_relation.to_s

Returns:



147
148
149
# File 'lib/veritas/sql/generator/relation/unary.rb', line 147

def to_s
  generate_sql(@columns)
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:



156
157
158
# File 'lib/veritas/sql/generator/relation/unary.rb', line 156

def to_subquery
  generate_sql(all_columns? ? ALL_COLUMNS : @columns)
end

#visit_veritas_algebra_projection(projection) ⇒ self

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.

Visit a Projection

Parameters:

  • projection (Algebra::Projection)

Returns:

  • (self)


57
58
59
60
61
62
63
# File 'lib/veritas/sql/generator/relation/unary.rb', line 57

def visit_veritas_algebra_projection(projection)
  @from     = subquery_for(projection)
  @distinct = DISTINCT
  @columns  = columns_for(projection)
  scope_query(projection)
  self
end

#visit_veritas_algebra_rename(rename) ⇒ self

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.

Visit a Rename

Parameters:

  • rename (Algebra::Rename)

Returns:

  • (self)


72
73
74
75
76
77
# File 'lib/veritas/sql/generator/relation/unary.rb', line 72

def visit_veritas_algebra_rename(rename)
  @from    = subquery_for(rename)
  @columns = columns_for(rename.operand, rename.aliases.to_hash)
  scope_query(rename)
  self
end

#visit_veritas_algebra_restriction(restriction) ⇒ self

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.

Visit a Restriction

Parameters:

  • restriction (Algebra::Restriction)

Returns:

  • (self)


86
87
88
89
90
91
92
# File 'lib/veritas/sql/generator/relation/unary.rb', line 86

def visit_veritas_algebra_restriction(restriction)
  @from      = subquery_for(restriction)
  @where     = dispatch(restriction.predicate)
  @columns ||= columns_for(restriction)
  scope_query(restriction)
  self
end

#visit_veritas_base_relation(base_relation) ⇒ self

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.

Visit a Base Relation

Parameters:

Returns:

  • (self)


43
44
45
46
47
48
# File 'lib/veritas/sql/generator/relation/unary.rb', line 43

def visit_veritas_base_relation(base_relation)
  @name    = base_relation.name
  @from    = visit_identifier(@name)
  @columns = columns_for(base_relation)
  self
end

#visit_veritas_relation_operation_limit(limit) ⇒ self

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.

Visit a Limit

Parameters:

  • limit (Relation::Operation::Limit)

Returns:

  • (self)


116
117
118
119
120
121
122
# File 'lib/veritas/sql/generator/relation/unary.rb', line 116

def visit_veritas_relation_operation_limit(limit)
  @from      = subquery_for(limit)
  @limit     = limit.limit
  @columns ||= columns_for(limit)
  scope_query(limit)
  self
end

#visit_veritas_relation_operation_offset(offset) ⇒ self

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.

Visit an Offset

Parameters:

  • offset (Relation::Operation::Offset)

Returns:

  • (self)


131
132
133
134
135
136
137
# File 'lib/veritas/sql/generator/relation/unary.rb', line 131

def visit_veritas_relation_operation_offset(offset)
  @from      = subquery_for(offset)
  @offset    = offset.offset
  @columns ||= columns_for(offset)
  scope_query(offset)
  self
end

#visit_veritas_relation_operation_order(order) ⇒ self

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.

Visit an Order

Parameters:

  • order (Relation::Operation::Order)

Returns:

  • (self)


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

def visit_veritas_relation_operation_order(order)
  @from      = subquery_for(order)
  @order     = order_for(order.directions)
  @columns ||= columns_for(order)
  scope_query(order)
  self
end