Database/AbstractProvider.pm

filter: Statementstmt 64.1% 120 / 187 Branchbran 36.1% 34 / 94 Conditioncond 20.0% 4 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 71.4% 25 / 35 Podpod 93.3% 14 / 15 totaltotal 54.4% 197 / 362 CC 56 SCAR 34.9
CC
56
Cov
52%
_column_info
156.0
client
90.0
_connect_tunnel
7.0
CRAP 32.7
« Database/Command/db/copy_to.pm ↑ Summary Database/OracleProvider.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Footprintless::Plugin::Database::AbstractProvider;
5 1 $Footprintless::Plugin::Database::AbstractProvider::VERSION = '1.06';
6 # ABSTRACT: A base class for database providers
7 # PODNAME: Footprintless::Plugin::Database::AbstractProvider
8  
9 1 ▶use parent qw(Footprintless::MixableBase);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use overload q{""} => 'to_string', fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Footprintless::Mixins qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
16 _entity
17 1 );
18 1 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 my $logger = Log::Any->get_logger();
21  
22 0 ▶sub backup {
Subroutine backup: not called CC=1 SCAR=6.9
Pod documented
23 0 die("abstract method invocation");
24 }
25  
26 0 ▶sub begin_transaction {
Subroutine begin_transaction: not called CC=2 SCAR=17.9
Pod documented
27 0 my ($self) = @_;
28 0 ▶ croak("not connected") unless ( $self->{connection} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"connection"} 0 0 0
29  
30 0 $self->{connection}->begin_work();
31  
32 0 return $self;
33 }
34  
35 0 ▶sub client {
Subroutine client: not called CC=9 SCAR=45.0
Pod documented
36 0 my ( $self, %options ) = @_;
37  
38 0 my $in_file;
39 0 eval {
40 0 my $in_handle = delete( $options{in_handle} );
41 0 ▶ if ( $options{in_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"in_file"}) 0 0 0
42 0 ▶ open( $in_file, '<', delete( $options{in_file} ) )
Branch: 1 branch
BranchTrueFalseTotal
open $in_file, "<", delete $options{"in_file"} or croak("invalid in_file: $!") 0 0 0
43 || croak("invalid in_file: $!");
44 }
45 0 ▶ if ( $options{in_string} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"in_string"}) 0 0 0
46 0 my $string = delete( $options{in_string} );
47 0 ▶ open( $in_file, '<', \$string )
Branch: 1 branch
BranchTrueFalseTotal
open $in_file, "<", \$string or croak("invalid in_string: $!") 0 0 0
48 || croak("invalid in_string: $!");
49 }
50 0 $self->_connect_tunnel();
51  
52 0 ▶ my $local_in = $in_handle || $in_file;
Condition0%0/3
$in_handle || $in_file
l!l&&r!l&&!r
000
MC/DC0%0/2
$in_handle || $in_file
$in_handle $in_file
Truth table0%0/3
A || B
A= $in_handleB= $in_file
ABresult
1X1
011
000
53 0 ▶ local (*STDIN) = $local_in if ($local_in);
Branch: 1 branch
BranchTrueFalseTotal
if $local_in 0 0 0
54  
55 0 require Footprintless::Plugin::Database::SqlShellAdapter;
56 Footprintless::Plugin::Database::SqlShellAdapter::sql_shell( $self->_connection_string(),
57 0 $self->{username}, $self->{password}, @{ $options{client_options} } );
58 };
59 0 my $error = $@;
60 0 $self->disconnect();
61 0 ▶ if ($in_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_file) 0 0 0
62 0 close($in_file);
63 }
64  
65 0 ▶ croak($error) if ($error);
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
66 }
67  
68 0 ▶sub _column_info {
Subroutine _column_info: not called CC=12 SCAR=50.5
69 0 my ( $self, $statement_handle ) = @_;
70 0 my $column_info = [];
71 0 ▶ if ( defined( $statement_handle->{NUM_OF_FIELDS} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $statement_handle->{"NUM_OF_FIELDS"}) 0 0 0
72 $column_info = [
73 map {
74 0 my $element = {};
75 $element->{name} = $statement_handle->{NAME}->[$_]
76 0 ▶ if defined( $statement_handle->{NAME} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $statement_handle->{"NAME"} 0 0 0
77 $element->{type} = $statement_handle->{TYPE}->[$_]
78 0 ▶ if defined( $statement_handle->{TYPE} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $statement_handle->{"TYPE"} 0 0 0
79 0 ▶ if ( my $type_info =
Branch: 1 branch
BranchTrueFalseTotal
if (my $type_info = $self->{"connection"}->type_info($statement_handle->{"TYPE"}[$_])) 0 0 0
80 $self->{connection}->type_info( $statement_handle->{TYPE}->[$_] ) )
81 {
82 $element->{type_name} = $type_info->{TYPE_NAME}
83 0 ▶ if defined( $type_info->{TYPE_NAME} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $type_info->{"TYPE_NAME"} 0 0 0
84 0 ▶ $element->{type_info} = $type_info if defined( $type_info->{NAME} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $type_info->{"NAME"} 0 0 0
85 }
86 $element->{column_size} = $statement_handle->{PRECISION}->[$_]
87 0 ▶ if defined( $statement_handle->{PRECISION} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $statement_handle->{"PRECISION"} 0 0 0
88 $element->{scale} = $statement_handle->{SCALE}->[$_]
89 0 ▶ if defined( $statement_handle->{SCALE} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $statement_handle->{"SCALE"} 0 0 0
90 0 ▶ if ( defined( $statement_handle->{NULLABLE} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $statement_handle->{"NULLABLE"} and (my $nullable = $statement_handle->{"NULLABLE"}[$_]) != 2) 0 0 0
Condition0%0/3
defined $statement_handle->{"NULLABLE"} and (my $nullable = $statement_handle->{"NULLABLE"}[$_]) != 2
!ll&&!rl&&r
000
MC/DC0%0/2
defined $statement_handle->{"NULLABLE"} and (my $nullable = $statement_handle->{"NULLABLE"}[$_]) != 2
defined $statement_handle->{"NULLABLE"} (my $nullable = $statement_handle->{"NULLABLE"}[$_]) != 2
Truth table0%0/3
A and B
A= defined $statement_handle->{"NULLABLE"}B= (my $nullable = $statement_handle->{"NULLABLE"}[$_]) != 2
ABresult
0X0
100
111
91 && ( my $nullable = $statement_handle->{NULLABLE}->[$_] ) != 2 )
92 {
93 0 ▶ $element->{nullable} = $nullable ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$nullable ? : 0 0 0
94 }
95 0 $element;
96 0 } 0 .. $statement_handle->{NUM_OF_FIELDS} - 1
97 ];
98 }
99 0 return $column_info;
100 }
101  
102 0 ▶sub commit_transaction {
Subroutine commit_transaction: not called CC=2 SCAR=17.9
Pod documented
103 0 my ($self) = @_;
104 0 ▶ croak("not connected") unless ( $self->{connection} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"connection"} 0 0 0
105  
106 0 $self->{connection}->commit();
107  
108 0 return $self;
109 }
110  
111 5 ▶sub connect {
Subroutine connect: called CC=3 SCAR=11.1
Pod documented
112 5 my ($self) = @_;
113  
114 5 ▶ return if ( $self->{connection} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"connection"} 0 5 5
115  
116 5 $self->_connect_tunnel();
117  
118 5 my ( $hostname, $port ) = $self->_hostname_port();
119  
120 5 $logger->debugf( 'connecting to %s', $self->to_string() );
121 $self->{connection} = DBI->connect( $self->_connection_string(),
122 5 ▶ $self->{username}, $self->{password}, { RaiseError => 1, AutoCommit => 1 } )
Condition50%1/2
'DBI'->connect($self->_connection_string, $self->{'username'}, $self->{'password'}, {'RaiseError', 1, 'AutoCommit', 1}) || croak("unable to connect to $hostname on port ${port}: $@")
l!l
50
MC/DC0%0/1
'DBI'->connect($self->_connection_string, $self->{'username'}, $self->{'password'}, {'RaiseError', 1, 'AutoCommit', 1}) || croak("unable to connect to $hostname on port ${port}: $@")
'DBI'->connect($self->_connection_string, $self->{'username'}, $self->{'password'}, {'RaiseError', 1, 'AutoCommit', 1})
Truth table50%1/2
A
A= 'DBI'->connect($self->_connection_string, $self->{'username'}, $self->{'password'}, {'RaiseError', 1, 'AutoCommit', 1})
Aresult
11
00
123 || croak("unable to connect to $hostname on port $port: $@");
124 5 $logger->tracef('connected');
125  
126 5 return $self;
127 }
128  
129 5 ▶sub _connect_tunnel {
Subroutine _connect_tunnel: called CC=4 SCAR=19.4
130 5 my ($self) = @_;
131  
132 5 ▶ return if ( $self->{tunnel} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tunnel"} 0 5 5
133  
134 5 ▶ if ( $self->{tunnel_hostname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tunnel_hostname"}) 0 5 5
135 0 $logger->debugf( 'opening tunnel through %s', $self->{tunnel_hostname} );
136 $self->{tunnel} = $self->{factory}->tunnel(
137 $self->{coordinate},
138 destination_hostname => $self->{tunnel_destination_hostname} || $self->{hostname},
139 destination_port => $self->{port}
140 0 ▶ );
Condition0%0/3
$self->{'tunnel_destination_hostname'} || $self->{'hostname'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'tunnel_destination_hostname'} || $self->{'hostname'}
$self->{'tunnel_destination_hostname'} $self->{'hostname'}
Truth table0%0/3
A || B
A= $self->{'tunnel_destination_hostname'}B= $self->{'hostname'}
ABresult
1X1
011
000
141 0 $self->{tunnel}->open();
142 }
143  
144 5 return $self;
145 }
146  
147 0 ▶sub _connection_string {
Subroutine _connection_string: not called CC=1 SCAR=6.9
148 0 die("abstract method invocation");
149 }
150  
151 7 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
152 7 my ($self) = @_;
153 7 $self->disconnect();
154 }
155  
156 12 ▶sub disconnect {
Subroutine disconnect: called CC=3 SCAR=11.7
Pod documented
157 12 my ($self) = @_;
158  
159 12 ▶ if ( $self->{connection} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"connection"}) 5 7 12
160 5 $logger->debugf( 'disconnecting from %s', $self->to_string() );
161 5 $self->{connection}->disconnect();
162 5 delete( $self->{connection} );
163 }
164  
165 12 ▶ if ( $self->{tunnel} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tunnel"}) 0 12 12
166 0 $logger->debug('closing tunnel');
167 0 $self->{tunnel}->close();
168 0 delete( $self->{tunnel} );
169 }
170  
171 12 return $self;
172 }
173  
174 1 ▶sub execute {
Subroutine execute: called CC=1 SCAR=0.0
Pod documented
175 1 my ( $self, $query ) = @_;
176  
177 1 my $result;
178 $self->_process_sql(
179 $query,
180 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
181 1 $result = $_[1];
182 }
183 1 );
184 1 return $result;
185 }
186  
187 0 ▶sub get_schema {
Subroutine get_schema: not called CC=1 SCAR=6.9
Pod documented
188 0 return $_[0]->{schema};
189 }
190  
191 25 ▶sub _hostname_port {
Subroutine _hostname_port: called CC=4 SCAR=16.6
192 25 my ($self) = @_;
193  
194 25 my ( $hostname, $port );
195 25 ▶ if ( $self->{tunnel} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'tunnel'}) { } 0 25 25
196 0 ▶ $hostname = $self->{tunnel}->get_local_hostname() || 'localhost';
Condition0%0/2
$self->{'tunnel'}->get_local_hostname || 'localhost'
l!l
00
MC/DC0%0/1
$self->{'tunnel'}->get_local_hostname || 'localhost'
$self->{'tunnel'}->get_local_hostname
Truth table0%0/2
A
A= $self->{'tunnel'}->get_local_hostname
Aresult
11
00
197 0 $port = $self->{tunnel}->get_local_port();
198 }
199 else {
200 25 $hostname = $self->{hostname};
201 25 $port = $self->{port};
202 }
203  
204 25 ▶ return ( $hostname eq 'localhost' ? '127.0.0.1' : $hostname, $port );
Branch: 1 branch
BranchTrueFalseTotal
$hostname eq 'localhost' ? : 25 0 25
205 }
206  
207 7 ▶sub _init {
Subroutine _init: called CC=2 SCAR=6.9
208 7 my ( $self, %options ) = @_;
209  
210 7 my $entity = $self->_entity( $self->{coordinate} );
211  
212 7 $self->{backup} = $entity->{backup};
213 7 $self->{database} = $entity->{database};
214 7 ▶ $self->{hostname} = $entity->{hostname} || 'localhost';
Condition50%1/2
$entity->{'hostname'} || 'localhost'
l!l
07
MC/DC0%0/1
$entity->{'hostname'} || 'localhost'
$entity->{'hostname'}
Truth table50%1/2
A
A= $entity->{'hostname'}
Aresult
11
00
215 7 $self->{password} = $entity->{password};
216 7 $self->{port} = $entity->{port};
217 7 $self->{schema} = $entity->{schema};
218 7 $self->{tunnel_destination_hostname} = $entity->{tunnel_destination_hostname};
219 7 $self->{tunnel_hostname} = $entity->{tunnel_hostname};
220 7 $self->{tunnel_username} = $entity->{tunnel_username};
221 7 $self->{username} = $entity->{username};
222  
223 7 return $self;
224 }
225  
226 11 ▶sub _process_sql {
Subroutine _process_sql: called CC=6 SCAR=19.2
227 11 my ( $self, $query, $statement_handler ) = @_;
228  
229 my ( $sql, $parameters ) =
230 ref($query) eq 'HASH'
231 ? ( $query->{sql}, $query->{parameters} )
232 11 ▶ : ($query);
Branch: 1 branch
BranchTrueFalseTotal
ref $query eq 'HASH' ? : 10 1 11
233 11 eval {
234 11 ▶ if ( $logger->is_trace() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logger->is_trace) 0 11 11
235 0 ▶ $logger->trace(
Branch: 1 branch
BranchTrueFalseTotal
$parameters ? : 0 0 0
236 "$self->{hostname}: '$sql'",
237 ( $parameters
238 ? ( ",[" . join( ',', @$parameters ) . "]" )
239 : ''
240 )
241 );
242 }
243 11 my $statement_handle = $self->{connection}->prepare_cached($sql);
244 11 ▶ &{$statement_handler}(
Branch: 1 branch
BranchTrueFalseTotal
defined $parameters ? : 2 9 11
245 $statement_handle,
246 ( defined($parameters)
247 ? $statement_handle->execute(@$parameters)
248 : $statement_handle->execute()
249 )
250 );
251 11 $statement_handle->finish();
252 };
253 11 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 11 11
254 0 croak("query failed: $@");
255 }
256 }
257  
258 8 ▶sub query {
Subroutine query: called CC=3 SCAR=11.7
Pod documented
259 8 my ( $self, $query, $result_handler, %options ) = @_;
260 8 my $hash = $options{hash};
261 8 ▶ my $column_info = $options{column_info} || ( $hash && [] );
Condition40%2/5
$options{'column_info'} || $hash && []
l!l&&r!l&&!r
008
$hash and []
!ll
80
MC/DC0%0/2
$options{'column_info'} || $hash && []
$options{'column_info'} $hash
Truth table33%1/3
A || B
A= $options{'column_info'}B= $hash
ABresult
1X1
011
000
262  
263 $self->_process_sql(
264 $query,
265 8 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=17.3
266 8 my ( $statement_handle, $execute_result ) = @_;
267 0 ▶ @$column_info = @{ $self->_column_info($statement_handle) } if $column_info;
Branch: 1 branch
BranchTrueFalseTotal
if $column_info 0 8 8
268 8 ▶ if ( !$options{no_fetch} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $options{"no_fetch"}) 8 0 8
269 8 ▶ while ( my @row = $statement_handle->fetchrow_array() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@row) = $statement_handle->fetchrow_array) 16 8 24
270 @row =
271 0 ▶ map { $column_info->[$_]->{name} => $row[$_] } 0 .. ( scalar(@row) - 1 )
Branch: 1 branch
BranchTrueFalseTotal
if $hash 0 16 16
272 if $hash;
273 16 &{$result_handler}(@row);
274 }
275 }
276 }
277 8 );
278 8 return;
279 }
280  
281 2 ▶sub query_for_list {
Subroutine query_for_list: called CC=3 SCAR=11.7
Pod documented
282 2 ▶ my ( $self, $query, $row_mapper, %options ) =
Branch: 1 branch
BranchTrueFalseTotal
scalar(@_) % 2 ? : 2 0 2
283 ( shift, shift, ( scalar(@_) % 2 ) ? shift : undef, @_ );
284  
285 2 my @results = ();
286 2 my $hash = $options{hash};
287 $self->query(
288 $query,
289 4 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
290 4 ▶ if ($row_mapper) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_mapper) { } 4 0 4
291 4 push( @results, &{$row_mapper}(@_) );
292 }
293 else {
294 0 ▶ push( @results, $hash ? {@_} : \@_ );
Branch: 1 branch
BranchTrueFalseTotal
$hash ? : 0 0 0
295 }
296 },
297 2 %options
298 );
299 2 ▶ return wantarray() ? @results : \@results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
300 }
301  
302 4 ▶sub query_for_map {
Subroutine query_for_map: called CC=3 SCAR=11.0
Pod documented
303 4 ▶ my ( $self, $query, $row_mapper, %options ) =
Branch: 1 branch
BranchTrueFalseTotal
scalar(@_) % 2 ? : 2 2 4
304 ( shift, shift, ( scalar(@_) % 2 ) ? shift : undef, @_ );
305  
306 4 my %results = ();
307 4 my $hash = $options{hash};
308 $self->query(
309 $query,
310 8 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
311 8 ▶ if ($row_mapper) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_mapper) { } 4 4 8
312 4 my $key_value_pair = &{$row_mapper}(@_);
313 4 $results{ $key_value_pair->[0] } = $key_value_pair->[1];
314 }
315 else {
316 4 ▶ $results{ $_[ $hash ? 1 : 0 ] } = $hash ? {@_} : \@_;
Branch: 2 branches
BranchTrueFalseTotal
$hash ? : 0 4 4
$hash ? : 0 4 4
317 }
318 },
319 4 %options
320 );
321 4 ▶ return wantarray() ? %results : \%results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 2 4
322 }
323  
324 2 ▶sub query_for_scalar {
Subroutine query_for_scalar: called CC=1 SCAR=0.0
Pod documented
325 2 my ( $self, $query, $row_mapper ) = @_;
326  
327 2 my $result;
328 $self->_process_sql(
329 $query,
330 2 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
331 2 my ( $statement_handle, $execute_result ) = @_;
332 2 ▶ if ( my @row = $statement_handle->fetchrow_array() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@row) = $statement_handle->fetchrow_array) 2 0 2
333 2 ▶ if ($row_mapper) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_mapper) { } 1 1 2
334 1 $result = &$row_mapper(@row);
335 }
336 else {
337 1 $result = $row[0];
338 }
339 }
340 }
341 2 );
342 2 return $result;
343 }
344  
345 0 ▶sub restore {
Subroutine restore: not called CC=1 SCAR=6.9
Pod documented
346 0 die("abstract method invocation");
347 }
348  
349 0 ▶sub rollback_transaction {
Subroutine rollback_transaction: not called CC=2 SCAR=17.9
Pod documented
350 0 my ($self) = @_;
351 0 ▶ croak("not connected") unless ( $self->{connection} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"connection"} 0 0 0
352  
353 0 $self->{connection}->rollback();
354  
355 0 return $self;
356 }
357  
358 0 ▶sub to_string {
Subroutine to_string: not called CC=1 SCAR=6.9
Pod undocumented
359 0 my ($self) = @_;
360 0 return "{schema=>'$self->{schema}',hostname=>'$self->{hostname}',port=>$self->{port}}";
361 }
362  
363 1 1;
364  
365 __END__
« Database/Command/db/copy_to.pm ↑ Summary Database/OracleProvider.pm »