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