line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooX::Role::DBIConnection; |
2
|
2
|
|
|
2
|
|
11352
|
use Moo::Role; |
|
2
|
|
|
|
|
17523
|
|
|
2
|
|
|
|
|
11
|
|
3
|
2
|
|
|
2
|
|
1834
|
use Filter::signatures; |
|
2
|
|
|
|
|
49996
|
|
|
2
|
|
|
|
|
12
|
|
4
|
2
|
|
|
2
|
|
74
|
use feature 'signatures'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
59
|
|
5
|
2
|
|
|
2
|
|
13
|
no warnings 'experimental::signatures'; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
67
|
|
6
|
2
|
|
|
2
|
|
3260
|
use DBI; |
|
2
|
|
|
|
|
36482
|
|
|
2
|
|
|
|
|
652
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
MooX::Role::DBIConnection - handy mixin for objects with a DB connection |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 SYNOPSIS |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
{ package My::Example; |
17
|
|
|
|
|
|
|
use Moo 2; |
18
|
|
|
|
|
|
|
with 'MooX::Role::DBIConnection'; |
19
|
|
|
|
|
|
|
}; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# Lazily connect using the parameters |
22
|
|
|
|
|
|
|
my $writer = My::Example->new( |
23
|
|
|
|
|
|
|
dbh => { |
24
|
|
|
|
|
|
|
dsn => '...', |
25
|
|
|
|
|
|
|
user => '...', |
26
|
|
|
|
|
|
|
password => '...', |
27
|
|
|
|
|
|
|
options => '...', |
28
|
|
|
|
|
|
|
}, |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# ... or alternatively if you have a connection already |
32
|
|
|
|
|
|
|
my $writer2 = My::Example->new( |
33
|
|
|
|
|
|
|
dbh => $dbh, |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
This module enhances your class constructor by allowing you to pass in either |
37
|
|
|
|
|
|
|
a premade C or the parameters needed to create one. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
The C method will then return either the passed-in database handle or |
40
|
|
|
|
|
|
|
try a connection to the database at the first use. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 OPTIONS |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
The following options can be passed in the hashref to specify |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=over 4 |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=item B |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
L dsn to connect to |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=item B |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Database user to use when connecting to the database. This is the second |
55
|
|
|
|
|
|
|
parameter used in the call to C<< DBI->connect(...) >>. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=item B |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Database password to use when connecting to the database. This is the third |
60
|
|
|
|
|
|
|
parameter used in the call to C<< DBI->connect(...) >>. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=item B |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Database options to use when connecting to the database. This is the fourth |
65
|
|
|
|
|
|
|
parameter used in the call to C<< DBI->connect(...) >>. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=item B |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Whether to connect to the database immediately or upon the first call to the |
70
|
|
|
|
|
|
|
the C<< ->dbh >>. The default is to make the connection lazily on first use. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=back |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
75
|
|
|
|
|
|
|
|
76
|
2
|
|
|
2
|
0
|
18590
|
sub BUILD( $self, $args ) { |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
3
|
|
77
|
2
|
50
|
|
|
|
13
|
if( my $_dbh = delete $args->{dbh}) { |
78
|
2
|
100
|
66
|
|
|
19
|
if(ref $_dbh eq 'HASH' && $_dbh->{eager}) { |
79
|
1
|
|
|
|
|
6
|
$_dbh = $self->_connect_db( $_dbh ); |
80
|
1
|
|
|
|
|
16
|
$self->{_dbh} = $_dbh; |
81
|
|
|
|
|
|
|
} else { |
82
|
1
|
|
|
|
|
12
|
$self->{_dbh_options} = $_dbh; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
}; |
86
|
|
|
|
|
|
|
|
87
|
2
|
|
|
2
|
|
3
|
sub _connect_db( $self, $dbh ) { |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
2
|
|
88
|
2
|
50
|
|
|
|
7
|
if( ref($dbh) eq 'HASH' ) { |
89
|
2
|
|
|
|
|
4
|
$dbh = DBI->connect( @{$dbh}{qw{dsn user password options}}); |
|
2
|
|
|
|
|
25
|
|
90
|
|
|
|
|
|
|
} |
91
|
2
|
|
|
|
|
2836
|
return $dbh |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
1
|
|
|
1
|
0
|
532
|
sub dbh( $self ) { |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
2
|
|
95
|
1
|
50
|
|
|
|
5
|
if( my $opt = delete $self->{_dbh_options}) { |
96
|
1
|
|
|
|
|
4
|
$self->{_dbh} = $self->_connect_db( $opt ); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
$self->{_dbh} |
99
|
1
|
|
|
|
|
12
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
1; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 REPOSITORY |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
The public repository of this module is |
106
|
|
|
|
|
|
|
L. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 SUPPORT |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
The public support forum of this module is L. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 BUG TRACKER |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Please report bugs in this module via the RT CPAN bug queue at |
115
|
|
|
|
|
|
|
L |
116
|
|
|
|
|
|
|
or via mail to L. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 AUTHOR |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Max Maischein C |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 COPYRIGHT (c) |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Copyright 2019-2023 by Max Maischein C. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 LICENSE |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
This module is released under the same terms as Perl itself. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=cut |