package DBIx::Class::DB;
use base qw/Class::Data::Inheritable/;
+use DBIx::Class::Storage::DBI;
+use DBIx::Class::ClassResolver::PassThrough;
+use DBI;
-__PACKAGE__->mk_classdata('_dbi_connect_info');
-__PACKAGE__->mk_classdata('_dbi_connect_package');
-__PACKAGE__->mk_classdata('_dbh');
+=head1 NAME
-sub _get_dbh {
- my ($class) = @_;
- unless ((my $dbh = $class->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
- $class->_populate_dbh;
- }
- return $class->_dbh;
-}
+DBIx::Class::DB - Simple DBIx::Class Database connection by class inheritance
-sub _populate_dbh {
- my ($class) = @_;
- my @info = @{$class->_dbi_connect_info || []};
- my $pkg = $class->_dbi_connect_package || $class;
- $pkg->_dbh($class->_dbi_connect(@info));
-}
+=head1 SYNOPSIS
+
+ package MyDB;
+
+ use base qw/DBIx::Class/;
+ __PACKAGE__->load_components('DB');
+
+ __PACKAGE__->connection('dbi:...', 'user', 'pass', \%attrs);
+
+ package MyDB::MyTable;
+
+ use base qw/MyDB/;
+ __PACKAGE__->load_components('Core');
+
+ ...
+
+=head1 DESCRIPTION
+
+This class provides a simple way of specifying a database connection.
+
+=head1 METHODS
+
+=over 4
+
+=cut
+
+__PACKAGE__->mk_classdata('storage');
+__PACKAGE__->mk_classdata('class_resolver' =>
+ 'DBIx::Class::ClassResolver::PassThrough');
+
+=item connection
+
+ __PACKAGE__->connection($dsn, $user, $pass, $attrs);
+
+Specifies the arguments that will be passed to DBI->connect(...) to
+instantiate the class dbh when required.
+
+=cut
-sub _dbi_connect {
+sub connection {
my ($class, @info) = @_;
- return DBI->connect_cached(@info);
+ my $storage = DBIx::Class::Storage::DBI->new;
+ $storage->connect_info(\@info);
+ $class->storage($storage);
}
+=item dbi_commit
+
+ $class->dbi_commit;
+
+Issues a commit again the current dbh
+
+=cut
+
+sub dbi_commit { $_[0]->storage->commit; }
+
+=item dbi_rollback
+
+ $class->dbi_rollback;
+
+Issues a rollback again the current dbh
+
+=cut
+
+sub dbi_rollback { $_[0]->storage->rollback; }
+
+sub resolve_class { return shift->class_resolver->class(@_); }
+
1;
+
+=back
+
+=head1 AUTHORS
+
+Matt S. Trout <mst@shadowcatsystems.co.uk>
+
+=head1 LICENSE
+
+You may distribute this code under the same terms as Perl itself.
+
+=cut
+