module Bmg::Relation

Constants

Leaf

Public Class Methods

empty(type = Type::ANY) click to toggle source
# 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) click to toggle source
# 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

Public Instance Methods

_autosummarize(type, *args) click to toggle source
# File lib/bmg/relation/empty.rb, line 44
def _autosummarize(type, *args)
  Empty.new(type)
end
_autowrap(type, *args) click to toggle source
# File lib/bmg/relation/empty.rb, line 48
def _autowrap(type, *args)
  Empty.new(type)
end
_constants(type, cs) click to toggle source
# File lib/bmg/relation/empty.rb, line 52
def _constants(type, cs)
  Empty.new(type)
end
_count() click to toggle source
# File lib/bmg/relation.rb, line 127
def _count
  to_a.size
end
_extend(type, *args) click to toggle source
# File lib/bmg/relation/empty.rb, line 56
def _extend(type, *args)
  Empty.new(type)
end
_image(type, *args) click to toggle source
# File lib/bmg/relation/empty.rb, line 60
def _image(type, *args)
  Empty.new(type)
end
_project(type, *args) click to toggle source
# File lib/bmg/relation/empty.rb, line 64
def _project(type, *args)
  Empty.new(type)
end
_rename(type, *args) click to toggle source
# File lib/bmg/relation/empty.rb, line 68
def _rename(type, *args)
  Empty.new(type)
end
_restrict(type, predicate) click to toggle source
# File lib/bmg/relation/empty.rb, line 72
def _restrict(type, predicate)
  self
end
_union(type, other, options) click to toggle source
# File lib/bmg/relation/empty.rb, line 76
def _union(type, other, options)
  other
end
bind(binding) click to toggle source
# File lib/bmg/relation.rb, line 16
def bind(binding)
  self
end
count() click to toggle source
# File lib/bmg/relation.rb, line 119
def count
  if type.knows_keys?
    project(type.keys.first)._count
  else
    self._count
  end
end
debug(max_level = nil, on = STDERR) click to toggle source

Returns a String representing the query plan

# File lib/bmg/relation.rb, line 156
def debug(max_level = nil, on = STDERR)
  on.puts(self.inspect)
  self
end
delete() click to toggle source
# File lib/bmg/relation.rb, line 85
def delete
  raise InvalidUpdateError, "Cannot delete from this Relvar"
end
empty?() click to toggle source
# File lib/bmg/relation.rb, line 48
def empty?
  each{|t| return false }
  true
end
insert(arg) click to toggle source
# File lib/bmg/relation.rb, line 77
def insert(arg)
  raise InvalidUpdateError, "Cannot insert into this Relvar"
end
one() click to toggle source

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

# File lib/bmg/relation.rb, line 66
def one
  one_or_yield{ raise OneError, "Relation is empty" }
end
one_or_nil() click to toggle source

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

# File lib/bmg/relation.rb, line 73
def one_or_nil
  one_or_yield{ nil }
end
to_ast() click to toggle source

Converts to an sexpr expression.

# File lib/bmg/relation.rb, line 151
def to_ast
  raise "Bmg is missing a feature!"
end
to_csv(options = {}, string_or_io = nil, preferences = nil) click to toggle source

Writes the relation data to CSV.

`string_or_io` and `options` are what CSV::new itself recognizes, default options are CSV's.

When no string_or_io is used, the method uses a string.

The method always returns the string_or_io.

# File lib/bmg/relation.rb, line 144
def to_csv(options = {}, string_or_io = nil, preferences = nil)
  options, string_or_io = {}, options unless options.is_a?(Hash)
  string_or_io, preferences = nil, string_or_io if string_or_io.is_a?(Hash)
  Writer::Csv.new(options, preferences).call(self, string_or_io)
end
to_json(*args, &bl) click to toggle source

Returns a json representation

# File lib/bmg/relation.rb, line 132
def to_json(*args, &bl)
  to_a.to_json(*args, &bl)
end
to_xlsx(options = {}, path = nil, preferences = nil) click to toggle source
# File lib/bmg/writer/xlsx.rb, line 62
def to_xlsx(options = {}, path = nil, preferences = nil)
  options, path = {}, options unless options.is_a?(Hash)
  Writer::Xlsx.new(options, preferences).call(self, path)
end
type() click to toggle source
# File lib/bmg/relation.rb, line 20
def type
  Bmg::Type::ANY
end
update(arg) click to toggle source
# File lib/bmg/relation.rb, line 81
def update(arg)
  raise InvalidUpdateError, "Cannot update this Relvar"
end
visit(&visitor) click to toggle source
# File lib/bmg/relation.rb, line 89
def visit(&visitor)
  _visit(nil, visitor)
end
with_type(type) click to toggle source
# File lib/bmg/relation.rb, line 24
def with_type(type)
  dup.tap{|r|
    r.type = type
  }
end
with_type_attrlist() click to toggle source
# File lib/bmg/relation.rb, line 30
def with_type_attrlist
  return self if type.knows_attrlist?
  attrs = self.first.keys
  with_type(type.with_attrlist(attrs))
end
with_typecheck() click to toggle source
# File lib/bmg/relation.rb, line 36
def with_typecheck
  dup.tap{|r|
    r.type = r.type.with_typecheck
  }
end
without_typecheck() click to toggle source
# File lib/bmg/relation.rb, line 42
def without_typecheck
  dup.tap{|r|
    r.type = r.type.with_typecheck
  }
end
y_by_x(y, x, options = {}) click to toggle source
# File lib/bmg/relation.rb, line 98
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 = {}) click to toggle source
# File lib/bmg/relation.rb, line 104
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

Protected Instance Methods

_visit(parent, visitor) click to toggle source
# File lib/bmg/relation.rb, line 93
def _visit(parent, visitor)
  visitor.call(self, parent)
end

Private Instance Methods

one_or_yield(&bl) click to toggle source

Private helper to implement `one` and `one_or_nil`

# File lib/bmg/relation.rb, line 54
def one_or_yield(&bl)
  first = nil
  each do |x|
    raise OneError, "Relation has more than one tuple" unless first.nil?
    first = x
  end
  first.nil? ? bl.call : first
end