Module: Bmg::Relation

Includes:
Algebra, Enumerable
Included in:
Operator, Reader, Empty, InMemory, Sequel::Relation
Defined in:
lib/bmg/relation.rb,
lib/bmg/relation/empty.rb,
lib/bmg/relation/spied.rb,
lib/bmg/relation/in_memory.rb

Defined Under Namespace

Classes: Empty, InMemory, Spied

Constant Summary

Constants included from Algebra

Algebra::METHODS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Algebra

#allbut, #autosummarize, #autowrap, #constants, #extend, #image, #matching, #project, #rename, #restrict, #spied, #union, #unspied

Class Method Details

.empty(type = Type::ANY) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
# File 'lib/bmg/relation.rb', line 11

def self.empty(type = Type::ANY)
  raise ArgumentError, "Missing type" if type.nil?
  Relation::Empty.new(type)
end

.new(operand, type = Type::ANY) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
# File 'lib/bmg/relation.rb', line 6

def self.new(operand, type = Type::ANY)
  raise ArgumentError, "Missing type" if type.nil?
  operand.is_a?(Relation) ? operand : Bmg.in_memory(operand, type)
end

Instance Method Details

#debug(max_level = nil, on = STDERR) ⇒ Object

Returns a String representing the query plan



92
93
94
95
# File 'lib/bmg/relation.rb', line 92

def debug(max_level = nil, on = STDERR)
  on.puts(self.inspect)
  self
end

#deleteObject

Raises:



53
54
55
# File 'lib/bmg/relation.rb', line 53

def delete
  raise InvalidUpdateError, "Cannot delete from this Relvar"
end

#empty?Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/bmg/relation.rb', line 16

def empty?
  each{|t| return false }
  true
end

#insert(arg) ⇒ Object

Raises:



45
46
47
# File 'lib/bmg/relation.rb', line 45

def insert(arg)
  raise InvalidUpdateError, "Cannot insert into this Relvar"
end

#oneObject

Returns the only tuple that the relation contains. Throws a OneException when there is no tuple or more than one



34
35
36
# File 'lib/bmg/relation.rb', line 34

def one
  one_or_yield{ raise OneError, "Relation is empty" }
end

#one_or_nilObject

Returns the only tuple that the relation contains. Returns nil if the relation is empty. Throws a OneException when the relation contains more than one tuple



41
42
43
# File 'lib/bmg/relation.rb', line 41

def one_or_nil
  one_or_yield{ nil }
end

#to_astObject

Converts to an sexpr expression.



87
88
89
# File 'lib/bmg/relation.rb', line 87

def to_ast
  raise "Bmg is missing a feature!"
end

#to_json(*args, &bl) ⇒ Object

Returns a json representation



82
83
84
# File 'lib/bmg/relation.rb', line 82

def to_json(*args, &bl)
  to_a.to_json(*args, &bl)
end

#update(arg) ⇒ Object

Raises:



49
50
51
# File 'lib/bmg/relation.rb', line 49

def update(arg)
  raise InvalidUpdateError, "Cannot update this Relvar"
end

#visit(&visitor) ⇒ Object



57
58
59
# File 'lib/bmg/relation.rb', line 57

def visit(&visitor)
  _visit(nil, visitor)
end

#ys_by_x(y, x, options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bmg/relation.rb', line 66

def ys_by_x(y, x, options = {})
  ordering = options[:order]
  projection = [y, ordering].compact.uniq
  by_x = each.each_with_object({}) do |tuple,h|
    h[tuple[x]] ||= []
    h[tuple[x]] << TupleAlgebra.project(tuple, projection)
  end
  by_x.each_with_object({}) do |(x,ys),h|
    ys = ys.sort{|y1,y2| y1[ordering] <=> y2[ordering] } if ordering
    ys = ys.map{|t| t[y] }
    ys = ys.uniq if options[:distinct]
    h[x] = ys
  end
end