]> Dogcows Code - chaz/p5-File-KDBX/blob - lib/File/KDBX/Transaction.pm
Add recursive transactions
[chaz/p5-File-KDBX] / lib / File / KDBX / Transaction.pm
1 package File::KDBX::Transaction;
2 # ABSTRACT: Make multiple database edits atomically
3
4 use warnings;
5 use strict;
6
7 use Devel::GlobalDestruction;
8 use namespace::clean;
9
10 our $VERSION = '999.999'; # VERSION
11
12 =method new
13
14 $txn = File::KDBX::Transaction->new($object);
15
16 Construct a new database transaction for editing an object atomically.
17
18 =cut
19
20 sub new {
21 my $class = shift;
22 my $object = shift;
23 $object->begin_work(@_);
24 return bless {object => $object}, $class;
25 }
26
27 sub DESTROY { !in_global_destruction and $_[0]->rollback }
28
29 =attr object
30
31 Get the object being transacted on.
32
33 =cut
34
35 sub object { $_[0]->{object} }
36
37 =method commit
38
39 $txn->commit;
40
41 Commit the transaction, making updates to the L</object> permanent.
42
43 =cut
44
45 sub commit {
46 my $self = shift;
47 return if $self->{done};
48
49 my $obj = $self->object;
50 $obj->commit;
51 $self->{done} = 1;
52 return $obj;
53 }
54
55 =method rollback
56
57 $txn->rollback;
58
59 Roll back the transaction, throwing away any updates to the L</object> made since the transaction began. This
60 happens automatically when the transaction is released, unless it has already been committed.
61
62 =cut
63
64 sub rollback {
65 my $self = shift;
66 return if $self->{done};
67
68 my $obj = $self->object;
69 $obj->rollback;
70 $self->{done} = 1;
71 return $obj;
72 }
73
74 1;
This page took 0.035272 seconds and 4 git commands to generate.