Class: Alf::Sequel::UnitOfWork::Update

Inherits:
Object
  • Object
show all
Includes:
Atomic
Defined in:
lib/alf/sequel/unit_of_work/update.rb

Instance Attribute Summary

Attributes included from Atomic

#connection, #failure

Instance Method Summary collapse

Methods included from Atomic

#failed?, #ran?, #run

Constructor Details

#initialize(connection, relvar_name, updating, predicate) ⇒ Update

Returns a new instance of Update.



7
8
9
10
11
12
# File 'lib/alf/sequel/unit_of_work/update.rb', line 7

def initialize(connection, relvar_name, updating, predicate)
  super(connection)
  @relvar_name = relvar_name
  @updating    = updating
  @predicate   = predicate
end

Instance Method Details

#matching_relationObject

Raises:

  • (IllegalStateError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/alf/sequel/unit_of_work/update.rb', line 14

def matching_relation
  raise IllegalStateError, "Unit of work not ran" unless ran?

  # Check that updating is understandable
  unless TupleLike===@updating
    raise UnsupportedError, "Non-tuple update feedback is unsupported."
  end
  updating = @updating.to_hash
  updating_keys = updating.keys

  # extract all keys, and pk in particular
  keys = connection.keys(@relvar_name)
  pk   = keys.first

  # Strategy 1), @updating contains a key
  if key = keys.to_a.find{|k| !(k & updating_keys).empty? }
    return Relation(@updating.project(key).to_hash)
  end

  raise UnsupportedError, "Unable to extract update matching relation"
end

#pk_matching_relationObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/alf/sequel/unit_of_work/update.rb', line 36

def pk_matching_relation
  mr, pkey = matching_relation, connection.keys(@relvar_name).first
  if mr.to_attr_list == pkey.to_attr_list
    mr
  else
    filter = mr.tuple_extract.to_hash
    tuples = connection.dataset(@relvar_name)
                       .filter(filter)
                       .select(pkey.to_a)
    Relation(tuples)
  end
end