use DBIx::Class::ClassResolver::PassThrough;
use DBI;
+*dbi_commit = \&tx_commit;
+*dbi_rollback = \&tx_rollback;
+
=head1 NAME
DBIx::Class::DB - Simple DBIx::Class Database connection by class inheritance
$class->storage($storage);
}
-=head2 dbi_commit
+=head2 tx_begin
+
+Begins a transaction (does nothing if AutoCommit is off).
+
+=cut
+
+sub tx_commit { $_[0]->storage->tx_begin }
+
+=head2 tx_commit
-Issues a commit against the current dbh.
+Commits the current transaction.
=cut
-sub dbi_commit { $_[0]->storage->commit; }
+sub tx_commit { $_[0]->storage->tx_commit }
-=head2 dbi_rollback
+=head2 tx_rollback
-Issues a rollback against the current dbh.
+Rolls back the current transaction.
=cut
-sub dbi_rollback { $_[0]->storage->rollback; }
+sub tx_rollback { $_[0]->storage->tx_rollback }
sub resolve_class { return shift->class_resolver->class(@_); }
__PACKAGE__->mk_group_accessors('simple' =>
qw/connect_info _dbh _sql_maker debug cursor/);
+our $TRANSACTION = 0;
+
sub new {
my $new = bless({}, ref $_[0] || $_[0]);
$new->cursor("DBIx::Class::Storage::DBI::Cursor");
return DBI->connect(@info);
}
-=head2 commit
-
- $class->commit;
+=head2 tx_begin
-Issues a commit again the current dbh
+Calls begin_work on the current dbh.
=cut
-sub commit { $_[0]->dbh->commit; }
+sub tx_begin {
+ $_[0]->dbh->begin_work if $TRANSACTION++ == 0 and $_[0]->dbh->{AutoCommit};
+}
-=head2 rollback
+=head2 tx_commit
- $class->rollback;
+Issues a commit against the current dbh.
-Issues a rollback again the current dbh
+=cut
+
+sub tx_commit {
+ $_[0]->dbh->commit if --$TRANSACTION == 0;
+}
+
+=head2 tx_rollback
+
+Issues a rollback against the current dbh.
=cut
-sub rollback { $_[0]->dbh->rollback; }
+sub tx_rollback {
+ --$TRANSACTION == 0 ? $_[0]->dbh->rollback : die $@;
+}
sub _execute {
my ($self, $op, $extra_bind, $ident, @args) = @_;