1df8835b8d219ad17b8e2829b4f42fbd34696760
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / DB.pm
1 package DBIx::Class::DB;
2
3 use base qw/Class::Data::Inheritable/;
4 use DBI;
5
6 __PACKAGE__->mk_classdata('_dbi_connect_info');
7 __PACKAGE__->mk_classdata('_dbi_connect_package');
8 __PACKAGE__->mk_classdata('_dbh');
9
10 sub _get_dbh {
11   my ($class) = @_;
12   my $dbh;
13   unless (($dbh = $class->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
14     $class->_populate_dbh;
15   }
16   return $class->_dbh;
17 }
18
19 sub _populate_dbh {
20   my ($class) = @_;
21   my @info = @{$class->_dbi_connect_info || []};
22   my $pkg = $class->_dbi_connect_package || $class;
23   $pkg->_dbh($class->_dbi_connect(@info));
24 }
25
26 sub _dbi_connect {
27   my ($class, @info) = @_;
28   return DBI->connect_cached(@info);
29 }
30
31 sub connection {
32   my ($class, @info) = @_;
33   $class->_dbi_connect_package($class);
34   $class->_dbi_connect_info(\@info);
35 }
36
37 1;