Commit | Line | Data |
ea2e61bf |
1 | package DBIx::Class::DB; |
2 | |
3 | use base qw/Class::Data::Inheritable/; |
604d9f38 |
4 | use 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 | |
12 | DBIx::Class::DB - DBIx::Class Database connection |
13 | |
14 | =head1 SYNOPSIS |
15 | |
16 | =head1 DESCRIPTION |
17 | |
18 | This class represents the connection to the database |
19 | |
20 | =head1 METHODS |
21 | |
22 | =over 4 |
23 | |
24 | =cut |
25 | |
ea2e61bf |
26 | sub _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 | |
35 | sub _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 | |
42 | sub _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 | |
51 | Specifies the arguments that will be passed to DBI->connect(...) to |
52 | instantiate the class dbh when required. |
53 | |
54 | =cut |
55 | |
8fe001e1 |
56 | sub 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 | |
66 | Issues a commit again the current dbh |
67 | |
68 | =cut |
69 | |
a29644e1 |
70 | sub dbi_commit { $_[0]->_get_dbh->commit; } |
71 | |
39fe0e65 |
72 | =item dbi_rollback |
73 | |
74 | $class->dbi_rollback; |
75 | |
76 | Issues a rollback again the current dbh |
77 | |
78 | =cut |
79 | |
a29644e1 |
80 | sub dbi_rollback { $_[0]->_get_dbh->rollback; } |
81 | |
ea2e61bf |
82 | 1; |
34d52be2 |
83 | |
84 | =back |
85 | |
86 | =head1 AUTHORS |
87 | |
88 | Matt S. Trout <perl-stuff@trout.me.uk> |
89 | |
90 | =head1 LICENSE |
91 | |
92 | You may distribute this code under the same terms as Perl itself. |
93 | |
94 | =cut |
95 | |