| 1 | package Mojo::Pg::Transaction; | ||
| 2 | use Mojo::Base -base; | ||
| 3 | |||
| 4 | has db => undef, weak => 1; | ||
| 5 | |||
| 6 | sub DESTROY { | ||
| 7 | my $self = shift; | ||
| 8 | if ($self->{rollback} && (my $dbh = $self->{dbh})) { $dbh->rollback } | ||
| 9 | } | ||
| 10 | |||
| 11 | sub commit { | ||
| 12 | my $self = shift; | ||
| 13 | $self->{dbh}->commit if delete $self->{rollback}; | ||
| 14 | if (my $db = $self->db) { $db->_notifications } | ||
| 15 | } | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | my $self = shift->SUPER::new(@_, rollback => 1); | ||
| 19 | my $dbh = $self->{dbh} = $self->db->dbh; | ||
| 20 | $dbh->begin_work; | ||
| 21 | return $self; | ||
| 22 | } | ||
| 23 | |||
| 24 | 1; | ||
| 25 | |||
| 26 | =encoding utf8 | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | Mojo::Pg::Transaction - Transaction | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | use Mojo::Pg::Transaction; | ||
| 35 | |||
| 36 | my $tx = Mojo::Pg::Transaction->new(db => $db); | ||
| 37 | $tx->commit; | ||
| 38 | |||
| 39 | =head1 DESCRIPTION | ||
| 40 | |||
| 41 | L<Mojo::Pg::Transaction> is a scope guard for L<DBD::Pg> transactions used by L<Mojo::Pg::Database>. | ||
| 42 | |||
| 43 | =head1 ATTRIBUTES | ||
| 44 | |||
| 45 | L<Mojo::Pg::Transaction> implements the following attributes. | ||
| 46 | |||
| 47 | =head2 db | ||
| 48 | |||
| 49 | my $db = $tx->db; | ||
| 50 | $tx = $tx->db(Mojo::Pg::Database->new); | ||
| 51 | |||
| 52 | L<Mojo::Pg::Database> object this transaction belongs to. Note that this attribute is weakened. | ||
| 53 | |||
| 54 | =head1 METHODS | ||
| 55 | |||
| 56 | L<Mojo::Pg::Transaction> inherits all methods from L<Mojo::Base> and implements the following new ones. | ||
| 57 | |||
| 58 | =head2 commit | ||
| 59 | |||
| 60 | $tx->commit; | ||
| 61 | |||
| 62 | Commit transaction. | ||
| 63 | |||
| 64 | =head2 new | ||
| 65 | |||
| 66 | my $tx = Mojo::Pg::Transaction->new; | ||
| 67 | my $tx = Mojo::Pg::Transaction->new(db => Mojo::Pg::Database->new); | ||
| 68 | my $tx = Mojo::Pg::Transaction->new({db => Mojo::Pg::Database->new}); | ||
| 69 | |||
| 70 | Construct a new L<Mojo::Pg::Transaction> object. | ||
| 71 | |||
| 72 | =head1 SEE ALSO | ||
| 73 | |||
| 74 | L<Mojo::Pg>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||
| 75 | |||
| 76 | =cut |