blib/lib/App/CLI/Plugin/DBI.pm

filter: Statementstmt 34.6% 18 / 52 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 40.0% 4 / 10 Podpod 60.0% 3 / 5 totaltotal 29.0% 25 / 86 CC 9 SCAR 23.7
CC
9
Cov
26%
dbh
20.0
_dbi_option
20.0
setup
6.0
CRAP 10.7
↑ Summary
1 package App::CLI::Plugin::DBI;
2  
3 =pod
4  
5 =head1 NAME
6  
7 App::CLI::Plugin::DBI - for App::CLI::Extension database base module
8  
9 =head1 VERSION
10  
11 1.1
12  
13 =head1 SYNOPSIS
14  
15 # MyApp.pm
16 package MyApp;
17
18 use strict;
19 use base qw(App::CLI::Extension);
20
21 # extension method
22 __PACKAGE__->load_plugins(qw(DBI));
23 __PACKAGE__->config(dbi => ["dbi:Pg:dbname=app_db", "foo", "bar", { RaiseError => 1, pg_enable_utf8 => 1 }]);
24
25 1;
26
27 # MyApp/Hello.pm
28 package MyApp::Hello;
29 use strict;
30 use base qw(App::CLI::Command);
31 our $VERSION = '1.0';
32
33 sub run {
34
35 my($self, @args) = @_;
36 my $sql = "select id, name, age from member where id = ?";
37 my $sth = $self->dbh->prepare($sql);
38 $sth->execute($args[0]);
39 while (my $ref = $sth->fetchrow_hashref) {
40 # anything to do...
41 }
42 $sth->finish;
43 }
44  
45 =head1 DESCRIPTION
46  
47 App::CLI::Extension DBI plugin module
48  
49 dbh method setting
50  
51 normal setting
52  
53 # config (example: PostgreSQL)
54 __PACKAGE__->config(dbi => ["dbi:Pg:dbname=app_db", "foo", "bar", { RaiseError => 1, pg_enable_utf8 => 1 }]);
55
56 # get DBI handle
57 my $dbh = $self->dbh;
58  
59 multi db setting
60  
61 # config
62 __PACKAGE__->config(dbi => {
63 default => ["dbi:Pg:dbname=app_db", "foo", "bar", { RaiseError => 1, pg_enable_utf8 => 1 }]
64 other => ["dbi:Pg:dbname=other_db;host=192.168.1.100;port=5432", "foo", "bar", { RaiseError => 1, pg_enable_utf8 => 1 }]
65 );
66
67 # get DBI handle
68 my $default_dbh = $self->dbh; # same as $self->dbh("default")
69 my $other_dbh = $self->dbh("other");
70  
71 =cut
72  
73 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 4 ▶use base qw(Class::Accessor::Grouped);
Subroutine BEGIN: called CC=1 SCAR=0.0
76 4 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 4 our $DEFAULT_HANDLE = "default";
79 4 our $VERSION = '1.1';
80  
81 #__PACKAGE__->mk_classaccessor(_dbh => {});
82 #__PACKAGE__->mk_classaccessor(dbi_default_handle => $DEFAULT_HANDLE);
83 4 __PACKAGE__->mk_group_accessors(inherited => "_dbh", "dbi_default_handle");
84 4 __PACKAGE__->_dbh({});
85 4 __PACKAGE__->dbi_default_handle($DEFAULT_HANDLE);
86  
87 =pod
88  
89 =head1 METHOD
90  
91 =cut
92  
93 0 ▶sub setup {
Subroutine setup: not called CC=2 SCAR=17.9
Pod undocumented
94  
95 0 my($self, @argv) = @_;
96  
97 0 ▶ if (!exists $ENV{APPCLI_DISABLE_DB_AUTO_CONNECT}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ENV{"APPCLI_DISABLE_DB_AUTO_CONNECT"}) 0 0 0
98 0 $self->dbi_connect;
99 }
100 0 $self->maybe::next::method(@argv);
101 }
102  
103 0 ▶sub finish {
Subroutine finish: not called CC=2 SCAR=17.9
Pod undocumented
104  
105 0 my($self, @argv) = @_;
106  
107 0 ▶ if (!exists $ENV{APPCLI_DISABLE_DB_AUTO_CONNECT}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ENV{"APPCLI_DISABLE_DB_AUTO_CONNECT"}) 0 0 0
108 0 $self->dbi_disconnect;
109 }
110 0 $self->maybe::next::method(@argv);
111 }
112  
113 =pod
114  
115 =head2 dbi_connect
116  
117 initialize DBI connect setting. setup phase to run normally with no need to perform explicitly.
118  
119 However, the environment variable "APPCLI_DISABLE_DB_AUTO_CONNECT" If you have defined not to run the setup phase,
120  
121 the need to call this method yourself
122  
123 =cut
124  
125 0 ▶sub dbi_connect {
Subroutine dbi_connect: not called CC=1 SCAR=6.9
Pod documented
126  
127 0 my $self = shift;
128  
129 0 my $dbi_option = $self->_dbi_option;
130 0 map { $self->_dbh->{$_} = DBI->connect(@{$dbi_option->{$_}}) } keys %{$dbi_option};
131 }
132  
133 =head2 dbi_disconnect
134  
135 destroy DBI disconnect. finish phase to run normally with no need to perform explicitly.
136  
137 However, the environment variable "APPCLI_DISABLE_DB_AUTO_CONNECT" If you have defined not to run the setup phase,
138  
139 the need to call this method yourself
140  
141 =cut
142  
143 0 ▶sub dbi_disconnect {
Subroutine dbi_disconnect: not called CC=1 SCAR=6.9
Pod documented
144  
145 0 my $self = shift;
146  
147 0 map { $self->_dbh->{$_}->disconnect } keys %{$self->_dbh};
148 }
149  
150 =pod
151  
152 =head2 dbh
153  
154 get DBD::db. default handle name is "default"(actual value and the value returned dbi_default_handle method)
155  
156 Example
157  
158 # same as $self->dbh($self->dbi_default_handle);
159 my $dbh = $self->dbh;
160
161 # if you set up if you want to connect multiple databases
162 my $default_dbh = $self->dbh;
163 my $other1_dbh = $self->dbh("other1");
164 my $other2_dbh = $self->dbh("other2");
165  
166 =head2 dbi_default_handle
167  
168 get default handle name.
169  
170 get current default handle name
171  
172 # $handle is "default"
173 my $handle = $self->dbi_default_handle;
174  
175 to change the default handle name
176  
177 $self->dbi_default_handle("new_handle_name");
178 # get new_handle_name db handle
179 my $dbh = $self->dbh;
180  
181 =cut
182  
183 0 ▶sub dbh {
Subroutine dbh: not called CC=4 SCAR=30.0
Pod documented
184  
185 0 my($self, $handle) = @_;
186  
187 0 ▶ $handle ||= $self->dbi_default_handle;
Condition0%0/3
$handle ||= $self->dbi_default_handle
l!l&&r!l&&!r
000
MC/DC0%0/2
$handle ||= $self->dbi_default_handle
$handle $self->dbi_default_handle
Truth table0%0/3
A ||= B
A= $handleB= $self->dbi_default_handle
ABresult
1X1
011
000
188 0 ▶ if (keys(%{$self->_dbh}) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$self->_dbh;} == 0) 0 0 0
189 0 die "still not connected to database?";
190 }
191 0 ▶ if (!exists $self->_dbh->{$handle}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_dbh->{$handle}) 0 0 0
192 0 die "$handle is not undefined dbh handle";
193 }
194 0 return $self->_dbh->{$handle};
195 }
196  
197  
198 ####################################
199 # private method
200 ####################################
201 0 ▶sub _dbi_option {
Subroutine _dbi_option: not called CC=4 SCAR=30.0
202  
203 0 my $self = shift;
204  
205 0 ▶ if (!defined $self->config->{dbi}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->config->{"dbi"}) 0 0 0
206 0 die "dbi option is always required";
207 }
208 0 my $dbi_option;
209 0 ▶ if (ref($self->config->{dbi}) eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->config->{'dbi'} eq 'ARRAY') { } 0 0 0
elsif (ref $self->config->{'dbi'} eq 'HASH') { } 0 0 0
210 0 $dbi_option = { $self->dbi_default_handle => $self->config->{dbi} };
211 } elsif (ref($self->config->{dbi}) eq "HASH") {
212 0 $dbi_option = $self->config->{dbi};
213 }
214  
215 0 return $dbi_option;
216 }
217  
218 4 1;
219  
220 __END__
↑ Summary