Class: Bmg::Sequel::Relation

Inherits:
Bmg::Sql::Relation show all
Defined in:
lib/bmg/sequel/relation.rb

Instance Attribute Summary collapse

Attributes inherited from Bmg::Sql::Relation

#type

Instance Method Summary collapse

Methods inherited from Bmg::Sql::Relation

#bind

Methods included from Relation

#bind, #count, #debug, empty, #empty?, new, #one, #one_or_nil, #to_csv, #to_json, #to_xlsx, #type, #visit, #with_type, #with_type_attrlist, #with_typecheck, #without_typecheck, #y_by_x, #ys_by_x

Methods included from Algebra

#allbut, #autosummarize, #autowrap, #constants, #extend, #group, #image, #join, #left_join, #matching, #materialize, #not_matching, #page, #project, #rename, #restrict, #spied, #summarize, #transform, #ungroup, #union, #unspied, #unwrap

Methods included from Algebra::Shortcuts

#exclude, #image, #images, #join, #left_join, #matching, #not_matching, #prefix, #rxmatch, #suffix, #ungroup, #unwrap, #where

Constructor Details

#initialize(type, builder, source, sequel_db) ⇒ Relation

Returns a new instance of Relation.



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

def initialize(type, builder, source, sequel_db)
  super(type, builder, source)
  @sequel_db = sequel_db
end

Instance Attribute Details

#sequel_dbObject (readonly)

Returns the value of attribute sequel_db.



9
10
11
# File 'lib/bmg/sequel/relation.rb', line 9

def sequel_db
  @sequel_db
end

Instance Method Details

#_countObject



47
48
49
# File 'lib/bmg/sequel/relation.rb', line 47

def _count
  dataset.count
end

#delete(predicate = Predicate.tautology) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/bmg/sequel/relation.rb', line 16

def delete(predicate = Predicate.tautology)
  target = base_table
  unless predicate.tautology?
    compiled = compile_predicate(predicate)
    target = base_table.where(compiled)
  end
  target.delete
end

#each(&bl) ⇒ Object



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

def each(&bl)
  return to_enum unless block_given?
  dataset.each(&bl)
end

#insert(arg) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bmg/sequel/relation.rb', line 25

def insert(arg)
  case arg
  when Hash then
    base_table.insert(arg.merge(type.predicate.constants))
  when Enumerable then
    base_table.multi_insert(arg.map { |x|
      x.merge(type.predicate.constants)
    })
  else
    base_table.insert(arg.merge(type.predicate.constants))
  end
end

#to_astObject



51
52
53
# File 'lib/bmg/sequel/relation.rb', line 51

def to_ast
  [:sequel, dataset.sql]
end

#to_sObject Also known as: inspect



59
60
61
# File 'lib/bmg/sequel/relation.rb', line 59

def to_s
  "(sequel #{dataset.sql})"
end

#to_sqlObject



55
56
57
# File 'lib/bmg/sequel/relation.rb', line 55

def to_sql
  dataset.sql
end

#update(arg, predicate = Predicate.tautology) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/bmg/sequel/relation.rb', line 38

def update(arg, predicate = Predicate.tautology)
  target = base_table
  unless predicate.tautology?
    compiled = compile_predicate(predicate)
    target = base_table.where(compiled)
  end
  target.update(arg)
end