File Coverage

blib/lib/MooX/Role/DBIConnection.pm
Criterion Covered Total %
statement 36 36 100.0
branch 4 6 66.6
condition 4 6 66.6
subroutine 8 8 100.0
pod 2 3 66.6
total 54 59 91.5


line stmt bran cond sub pod time code
1             package MooX::Role::DBIConnection 0.07;
2 3     3   748415 use 5.020;
  3         13  
3 3     3   1712 use experimental 'signatures';
  3         5410  
  3         16  
4 3     3   1346 use Moo::Role;
  3         19051  
  3         15  
5 3     3   6691 use DBI;
  3         57339  
  3         1451  
6              
7             =head1 NAME
8              
9             MooX::Role::DBIConnection - handy mixin for objects with a DB connection
10              
11             =head1 SYNOPSIS
12              
13             { package My::Example;
14             use Moo 2;
15             with 'MooX::Role::DBIConnection';
16             };
17              
18             # Lazily connect using the parameters
19             my $writer = My::Example->new(
20             dbh => {
21             dsn => '...',
22             user => '...',
23             password => '...',
24             options => '...',
25             },
26             );
27              
28             # ... or alternatively if you have a connection already
29             my $writer2 = My::Example->new(
30             dbh => $dbh,
31             );
32              
33             This module enhances your class constructor by allowing you to pass in either
34             a premade C or the parameters needed to create one.
35              
36             The C method will then return either the passed-in database handle or
37             try a connection to the database at the first use.
38              
39             =head1 OPTIONS
40              
41             The following options can be passed in the hashref to specify
42              
43             =over 4
44              
45             =item B
46              
47             L dsn to connect to
48              
49             =item B
50              
51             Database user to use when connecting to the database. This is the second
52             parameter used in the call to C<< DBI->connect(...) >>.
53              
54             =item B
55              
56             Database password to use when connecting to the database. This is the third
57             parameter used in the call to C<< DBI->connect(...) >>.
58              
59             =item B
60              
61             Database options to use when connecting to the database. This is the fourth
62             parameter used in the call to C<< DBI->connect(...) >>.
63              
64             =item B
65              
66             Whether to connect to the database immediately or upon the first call to the
67             the C<< ->dbh >>. The default is to make the connection lazily on first use.
68              
69             =back
70              
71             =cut
72              
73 3     3 0 33779 sub BUILD( $self, $args ) {
  3         7  
  3         6  
  3         6  
74 3 50       17 if( my $_dbh = delete $args->{dbh}) {
75 3         14 $self->{_dbh_options} = $_dbh;
76 3 100 66     29 if(ref $_dbh eq 'HASH' && $_dbh->{eager}) {
77 2         9 $self->reconnect_dbh( $_dbh );
78             };
79             }
80             };
81              
82 4     4   7 sub _connect_db( $self, $dbh ) {
  4         6  
  4         8  
  4         5  
83 4 50       13 if( ref($dbh) eq 'HASH' ) {
84 4         7 $dbh = DBI->connect( @{$dbh}{qw{dsn user password options}});
  4         34  
85             }
86 4         6321 return $dbh
87             }
88              
89             =head1 METHODS
90              
91             The following methods are mixed in through this role:
92              
93             =head2 C<< ->dbh >>
94              
95             my $dbh = $self->dbh;
96              
97             Connects to the database if needed and returns the database handle
98              
99             =cut
100              
101 2     2 1 8036 sub dbh( $self ) {
  2         10  
  2         3  
102 2   66     15 return $self->{_dbh} //= $self->_connect_db( $self->{_dbh_options} );
103             }
104              
105             =head2 C<< ->reconnect_dbh >>
106              
107             if( my $child = fork ) {
108             ...
109             } else {
110             # in child process
111             # We need a fresh database connection
112             $self->reconnect_dbh;
113             }
114              
115             Opens a new database connection. If an old connection existed, it is
116             not closed.
117              
118             =cut
119              
120 3     3 1 4930 sub reconnect_dbh( $self, $options = $self->{_dbh_options} ) {
  3         6  
  3         5  
  3         5  
121 3         10 $self->{_dbh} = $self->_connect_db( $options )
122             }
123              
124             1;
125              
126             =head1 REPOSITORY
127              
128             The public repository of this module is
129             L.
130              
131             =head1 SUPPORT
132              
133             The public support forum of this module is L.
134              
135             =head1 BUG TRACKER
136              
137             Please report bugs in this module via the RT CPAN bug queue at
138             L
139             or via mail to L.
140              
141             =head1 AUTHOR
142              
143             Max Maischein C
144              
145             =head1 COPYRIGHT (c)
146              
147             Copyright 2019-2024 by Max Maischein C.
148              
149             =head1 LICENSE
150              
151             This module is released under the same terms as Perl itself.
152              
153             =cut