blib/lib/MooX/Role/DBIConnection.pm

filter: Statementstmt 100% 23 / 23 Branchbran 66.6% 4 / 6 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 8 / 8 Podpod 66.6% 2 / 3 totaltotal 84.0% 42 / 50 CC 7 SCAR 8.4
CC
7
Cov
89%
BUILD
4.1
dbh
2.1
_connect_db
2.0
CRAP 2.3
↑ Summary
1 package MooX::Role::DBIConnection 0.07;
2 3 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
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<dbh> or the parameters needed to create one.
35  
36 The C<dbh> 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<dsn>
46  
47 L<DBI> dsn to connect to
48  
49 =item B<user>
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<password>
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<options>
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<eager>
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 ▶sub BUILD( $self, $args ) {
Subroutine BUILD: called CC=4 SCAR=14.1
Pod undocumented
74 3 ▶ if( my $_dbh = delete $args->{dbh}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $_dbh = delete $args->{"dbh"}) 3 0 3
75 3 $self->{_dbh_options} = $_dbh;
76 3 ▶ if(ref $_dbh eq 'HASH' && $_dbh->{eager}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_dbh eq "HASH" and $_dbh->{"eager"}) 2 1 3
Condition66%2/3
ref $_dbh eq "HASH" and $_dbh->{"eager"}
!ll&&!rl&&r
012
MC/DC50%1/2
ref $_dbh eq "HASH" and $_dbh->{"eager"}
ref $_dbh eq "HASH" $_dbh->{"eager"}
Truth table66%2/3
A and B
A= ref $_dbh eq "HASH"B= $_dbh->{"eager"}
ABresult
0X0
100
111
77 2 $self->reconnect_dbh( $_dbh );
78 };
79 }
80 };
81  
82 4 ▶sub _connect_db( $self, $dbh ) {
Subroutine _connect_db: called CC=2 SCAR=7.0
83 4 ▶ if( ref($dbh) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dbh eq "HASH") 4 0 4
84 4 $dbh = DBI->connect( @{$dbh}{qw{dsn user password options}});
85 }
86 4 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 ▶sub dbh( $self ) {
Subroutine dbh: called CC=2 SCAR=7.2
Pod documented
102 2 ▶ return $self->{_dbh} //= $self->_connect_db( $self->{_dbh_options} );
Condition66%2/3
$self->{"_dbh"} //= $self->_connect_db($self->{"_dbh_options"})
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->{"_dbh"} //= $self->_connect_db($self->{"_dbh_options"})
$self->{"_dbh"} $self->_connect_db($self->{"_dbh_options"})
Truth table66%2/3
A //= B
A= $self->{"_dbh"}B= $self->_connect_db($self->{"_dbh_options"})
ABresult
1X1
011
000
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 ▶sub reconnect_dbh( $self, $options = $self->{_dbh_options} ) {
Subroutine reconnect_dbh: called CC=2 SCAR=6.9
Pod documented
121 3 $self->{_dbh} = $self->_connect_db( $options )
122 }
123  
124 3 1;
125  
126 =head1 REPOSITORY
127  
128 The public repository of this module is
129 L<https://github.com/Corion/MooX-Role-DBIConnection>.
130  
131 =head1 SUPPORT
132  
133 The public support forum of this module is L<https://perlmonks.org/>.
134  
135 =head1 BUG TRACKER
136  
137 Please report bugs in this module via the RT CPAN bug queue at
138 L<https://rt.cpan.org/Public/Dist/Display.html?Name=MooX-Role-DBIConnection>
139 or via mail to L<MooX-Role-DBIConnection@rt.cpan.org|mailto:MooX-Role-DBIConnection@rt.cpan.org>.
140  
141 =head1 AUTHOR
142  
143 Max Maischein C<corion@cpan.org>
144  
145 =head1 COPYRIGHT (c)
146  
147 Copyright 2019-2024 by Max Maischein C<corion@cpan.org>.
148  
149 =head1 LICENSE
150  
151 This module is released under the same terms as Perl itself.
152  
153 =cut
↑ Summary