bin/dbiogen

filter: Statementstmt 80.0% 52 / 65 Branchbran 33.3% 8 / 24 Conditioncond 45.0% 9 / 20 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 60.7% 79 / 130 CC 1 SCAR 0.0
CC
1
Cov
63%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/DBIO/Test/Schema/Link.pm ↑ Summary blib/lib/DBIO/Test/ForeignComponent/TestComp.pm »
1 #!/usr/bin/env perl
2 # ABSTRACT: Dump a database schema to DBIO Result classes
3 # PODNAME: dbiogen
4  
5  
6 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use DBI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use DBIO::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use DBIO::Componentised ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use DBIO::Generate ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use DBIO::Introspect::DBI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 my %opts;
17 6 GetOptions(\%opts, 'o=s@', 'style=s', 'password-env=s', 'password-stdin');
18  
19 6 my %gen_opts;
20 6 ▶for my $o (@{ $opts{o} || [] }) {
Condition100%2/2
$opts{'o'} || []
l!l
24
MC/DC100%1/1
$opts{'o'} || []
$opts{'o'}
Truth table100%2/2
A
A= $opts{'o'}
Aresult
11
00
21 2 my ($key, $val) = split /=/, $o, 2;
22 2 ▶ $val //= 1; # bare -o flag
Condition50%1/2
$val //= 1
l!l
20
MC/DC0%0/1
$val //= 1
$val
Truth table50%1/2
A
A= $val
Aresult
11
00
23 # Try to decode JSON values (for arrayrefs, hashrefs)
24 2 ▶ if ($val =~ /^\s*[\[\{]/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($val =~ /^\s*[\[\{]/) { } 0 2 2
elsif ($val =~ /^\d+$/) { } 2 0 2
25 0 $val = JSON::MaybeXS->new->decode($val);
26 }
27 # Booleans and numbers
28 elsif ($val =~ /^\d+$/) {
29 2 $val = $val + 0;
30 }
31 2 $gen_opts{$key} = $val;
32 }
33  
34 6 my ($schema_class, @connect_info) = @ARGV;
35  
36 6 ▶die "Usage: dbiogen [--style=cake|candy|vanilla|moose] [-o key=value ...] [--password-env VAR | --password-stdin] <schema_class> <dsn> [user] [pass]\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $schema_class and @connect_info 0 6 6
Condition33%1/3
$schema_class and @connect_info
!ll&&!rl&&r
006
MC/DC0%0/2
$schema_class and @connect_info
$schema_class @connect_info
Truth table33%1/3
A and B
A= $schema_classB= @connect_info
ABresult
0X0
100
111
37 unless $schema_class && @connect_info;
38  
39 # The password comes from the environment or STDIN rather than the arguments
40 # (ps and the shell history show those)
41 my $password = DBIO::Util::password_from_source(
42 6 env => $opts{'password-env'}, stdin => $opts{'password-stdin'},
43 );
44 4 ▶if (defined $password) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $password) 2 2 4
45 2 ▶ $connect_info[1] //= '';
Condition50%1/2
$connect_info[1] //= ""
l!l
20
MC/DC0%0/1
$connect_info[1] //= ""
$connect_info[1]
Truth table50%1/2
A
A= $connect_info[1]
Aresult
11
00
46 2 $connect_info[2] = $password;
47 }
48  
49 4 ▶$gen_opts{dump_directory} ||= './lib';
Condition50%1/2
$gen_opts{"dump_directory"} ||= "./lib"
l!l
04
MC/DC0%0/1
$gen_opts{"dump_directory"} ||= "./lib"
$gen_opts{"dump_directory"}
Truth table50%1/2
A
A= $gen_opts{"dump_directory"}
Aresult
11
00
50 4 ▶$gen_opts{schema_class} ||= $schema_class;
Condition33%1/3
$gen_opts{"schema_class"} ||= $schema_class
l!l&&r!l&&!r
040
MC/DC0%0/2
$gen_opts{"schema_class"} ||= $schema_class
$gen_opts{"schema_class"} $schema_class
Truth table33%1/3
A ||= B
A= $gen_opts{"schema_class"}B= $schema_class
ABresult
1X1
011
000
51  
52 4 ▶if (my $style = $opts{style}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $style = $opts{"style"}) 0 4 4
53 0 ▶ die "Unknown style '$style' -- use cake, candy, vanilla, or moose\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $style =~ /^(?:cake|candy|vanilla|moose)$/ 0 0 0
54 unless $style =~ /^(?:cake|candy|vanilla|moose)$/;
55 0 $gen_opts{style} = $style;
56 }
57  
58 # Connect to the database
59 # The connect failure is reported once, below, through the one masking of
60 # connect info.
61 # DBI->connect returns undef on a refused connection but dies on a data source
62 # it cannot work out a driver for, quoting the whole DSN: either way the
63 # report goes through the same masking.
64 4 my $dbh = eval { DBI->connect(@connect_info[0 .. 2], { PrintError => 0 }) };
65 4 ▶unless ($dbh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh) 4 0 4
66 4 ▶ my $why = $@ || DBI->errstr;
Condition66%2/3
$@ || 'DBI'->errstr
l!l&&r!l&&!r
130
MC/DC0%0/2
$@ || 'DBI'->errstr
$@ 'DBI'->errstr
Truth table66%2/3
A || B
A= $@B= 'DBI'->errstr
ABresult
1X1
011
000
67 4 ▶ $why =~ s/\n+\z// if defined $why;
Branch: 1 branch
BranchTrueFalseTotal
if defined $why 4 0 4
68 4 die "Could not connect to database: "
69 . DBIO::Util::mask_connect_secrets($why, \@connect_info) . "\n";
70 }
71  
72 # Pick the introspector: the installed driver's own if there is one, so the
73 # generated classes get information_schema-grade accuracy and driver extras
74 # (source_info, view definitions) instead of the generic DBI metadata subset.
75 my $introspect_class = delete $gen_opts{introspect_class}
76 0 ▶ || DBIO::Introspect::DBI->class_for_dbh($dbh);
Condition0%0/3
delete $gen_opts{'introspect_class'} || 'DBIO::Introspect::DBI'->class_for_dbh($dbh)
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $gen_opts{'introspect_class'} || 'DBIO::Introspect::DBI'->class_for_dbh($dbh)
delete $gen_opts{'introspect_class'} 'DBIO::Introspect::DBI'->class_for_dbh($dbh)
Truth table0%0/3
A || B
A= delete $gen_opts{'introspect_class'}B= 'DBIO::Introspect::DBI'->class_for_dbh($dbh)
ABresult
1X1
011
000
77  
78 # ensure_class_loaded takes a class name only (DBIO::Util::assert_class_name)
79 # and throws for anything else
80 0 ▶eval { DBIO::Componentised->ensure_class_loaded($introspect_class); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { "DBIO::Componentised"->ensure_class_loaded($introspect_class); 1 } } or die "Cannot load introspector '${introspect_class}': $@" 0 0 0
81 or die "Cannot load introspector '$introspect_class': $@";
82  
83 # STDERR, like DBIO::Generate's own "Wrote ..." progress lines, so the order
84 # it is reported in is the order it happened in
85 0 ▶print STDERR "Introspecting with $introspect_class\n" unless $gen_opts{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $gen_opts{"quiet"} 0 0 0
86  
87 0 my $introspect = $introspect_class->new(dbh => $dbh);
88  
89 # Create and run the generator
90 0 my $gen = DBIO::Generate->new(%gen_opts);
91  
92 0 $gen->dump($introspect);
93  
94 0 ▶$dbh->disconnect if $dbh;
Branch: 1 branch
BranchTrueFalseTotal
if $dbh 0 0 0
95  
96 0 ▶print "Done.\n" unless $gen_opts{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $gen_opts{"quiet"} 0 0 0
97  
98 __END__
« blib/lib/DBIO/Test/Schema/Link.pm ↑ Summary blib/lib/DBIO/Test/ForeignComponent/TestComp.pm »