DBIx::Class is now a component loader
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / DB.pm
CommitLineData
ea2e61bf 1package DBIx::Class::DB;
2
3use base qw/Class::Data::Inheritable/;
604d9f38 4use DBI;
ea2e61bf 5
6__PACKAGE__->mk_classdata('_dbi_connect_info');
7__PACKAGE__->mk_classdata('_dbi_connect_package');
8__PACKAGE__->mk_classdata('_dbh');
9
34d52be2 10=head1 NAME
11
12DBIx::Class::DB - DBIx::Class Database connection
13
14=head1 SYNOPSIS
15
16=head1 DESCRIPTION
17
18This class represents the connection to the database
19
20=head1 METHODS
21
22=over 4
23
24=cut
25
ea2e61bf 26sub _get_dbh {
27 my ($class) = @_;
8fe001e1 28 my $dbh;
29 unless (($dbh = $class->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
ea2e61bf 30 $class->_populate_dbh;
31 }
32 return $class->_dbh;
33}
34
35sub _populate_dbh {
36 my ($class) = @_;
37 my @info = @{$class->_dbi_connect_info || []};
38 my $pkg = $class->_dbi_connect_package || $class;
39 $pkg->_dbh($class->_dbi_connect(@info));
40}
41
42sub _dbi_connect {
43 my ($class, @info) = @_;
95a70f01 44 return DBI->connect(@info);
8fe001e1 45}
46
39fe0e65 47=item connection
48
49 __PACKAGE__->connection($dsn, $user, $pass, $attrs);
50
51Specifies the arguments that will be passed to DBI->connect(...) to
52instantiate the class dbh when required.
53
54=cut
55
8fe001e1 56sub connection {
57 my ($class, @info) = @_;
58 $class->_dbi_connect_package($class);
59 $class->_dbi_connect_info(\@info);
ea2e61bf 60}
61
39fe0e65 62=item dbi_commit
63
64 $class->dbi_commit;
65
66Issues a commit again the current dbh
67
68=cut
69
a29644e1 70sub dbi_commit { $_[0]->_get_dbh->commit; }
71
39fe0e65 72=item dbi_rollback
73
74 $class->dbi_rollback;
75
76Issues a rollback again the current dbh
77
78=cut
79
a29644e1 80sub dbi_rollback { $_[0]->_get_dbh->rollback; }
81
ea2e61bf 821;
34d52be2 83
84=back
85
86=head1 AUTHORS
87
88Matt S. Trout <perl-stuff@trout.me.uk>
89
90=head1 LICENSE
91
92You may distribute this code under the same terms as Perl itself.
93
94=cut
95