Module: Bmg::Relation
- Includes:
- Algebra, Enumerable
- Included in:
- Operator, Reader, Empty, InMemory, Sql::Relation
- Defined in:
- lib/bmg/relation.rb,
lib/bmg/relation/empty.rb,
lib/bmg/relation/spied.rb,
lib/bmg/relation/in_memory.rb,
lib/bmg/relation/materialized.rb
Defined Under Namespace
Classes: Empty, InMemory, Materialized, Spied
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Algebra
#allbut, #autosummarize, #autowrap, #constants, #extend, #group, #image, #join, #matching, #materialize, #not_matching, #page, #project, #rename, #restrict, #spied, #summarize, #union, #unspied
#image, #join, #matching, #not_matching, #prefix, #rxmatch, #suffix
Class Method Details
.empty(type = Type::ANY) ⇒ Object
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
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
#bind(binding) ⇒ Object
16
17
18
|
# File 'lib/bmg/relation.rb', line 16
def bind(binding)
self
end
|
#debug(max_level = nil, on = STDERR) ⇒ Object
Returns a String representing the query plan
114
115
116
117
|
# File 'lib/bmg/relation.rb', line 114
def debug(max_level = nil, on = STDERR)
on.puts(self.inspect)
self
end
|
#delete ⇒ Object
69
70
71
|
# File 'lib/bmg/relation.rb', line 69
def delete
raise InvalidUpdateError, "Cannot delete from this Relvar"
end
|
#empty? ⇒ Boolean
32
33
34
35
|
# File 'lib/bmg/relation.rb', line 32
def empty?
each{|t| return false }
true
end
|
#insert(arg) ⇒ Object
61
62
63
|
# File 'lib/bmg/relation.rb', line 61
def insert(arg)
raise InvalidUpdateError, "Cannot insert into this Relvar"
end
|
#one ⇒ Object
Returns the only tuple that the relation contains. Throws a OneException when there is no tuple or more than one
50
51
52
|
# File 'lib/bmg/relation.rb', line 50
def one
one_or_yield{ raise OneError, "Relation is empty" }
end
|
#one_or_nil ⇒ Object
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
57
58
59
|
# File 'lib/bmg/relation.rb', line 57
def one_or_nil
one_or_yield{ nil }
end
|
#to_ast ⇒ Object
Converts to an sexpr expression.
109
110
111
|
# File 'lib/bmg/relation.rb', line 109
def to_ast
raise "Bmg is missing a feature!"
end
|
#to_json(*args, &bl) ⇒ Object
Returns a json representation
104
105
106
|
# File 'lib/bmg/relation.rb', line 104
def to_json(*args, &bl)
to_a.to_json(*args, &bl)
end
|
#update(arg) ⇒ Object
65
66
67
|
# File 'lib/bmg/relation.rb', line 65
def update(arg)
raise InvalidUpdateError, "Cannot update this Relvar"
end
|
#visit(&visitor) ⇒ Object
73
74
75
|
# File 'lib/bmg/relation.rb', line 73
def visit(&visitor)
_visit(nil, visitor)
end
|
#with_typecheck ⇒ Object
20
21
22
23
24
|
# File 'lib/bmg/relation.rb', line 20
def with_typecheck
dup.tap{|r|
r.type = r.type.with_typecheck
}
end
|
#without_typecheck ⇒ Object
26
27
28
29
30
|
# File 'lib/bmg/relation.rb', line 26
def without_typecheck
dup.tap{|r|
r.type = r.type.with_typecheck
}
end
|
#y_by_x(y, x, options = {}) ⇒ Object
82
83
84
85
86
|
# File 'lib/bmg/relation.rb', line 82
def y_by_x(y, x, options = {})
each_with_object({}) do |tuple, h|
h[tuple[x]] = tuple[y]
end
end
|
#ys_by_x(y, x, options = {}) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/bmg/relation.rb', line 88
def ys_by_x(y, x, options = {})
ordering = options[:order]
projection = [y, ordering].compact.uniq
by_x = 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
|