| 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'} || []
MC/DC100%1/1
$opts{'o'} || []
$opts{'o'}
Truth table100%2/2
A
A= $opts{'o'}
| |||||||||||||||||||||||||||||
| 21 | 2 | my ($key, $val) = split /=/, $o, 2; | |||||||||||||||||||||||||||
| 22 | 2 | ▶ | $val //= 1; # bare -o flag | ||||||||||||||||||||||||||
|
Condition50%1/2
$val //= 1
MC/DC0%0/1
$val //= 1
$val
Truth table50%1/2
A
A= $val
| |||||||||||||||||||||||||||||
| 23 | # Try to decode JSON values (for arrayrefs, hashrefs) | ||||||||||||||||||||||||||||
| 24 | 2 | ▶ | if ($val =~ /^\s*[\[\{]/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$schema_class and @connect_info
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | $connect_info[1] //= ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$connect_info[1] //= ""
MC/DC0%0/1
$connect_info[1] //= ""
$connect_info[1]
Truth table50%1/2
A
A= $connect_info[1]
| |||||||||||||||||||||||||||||
| 46 | 2 | $connect_info[2] = $password; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 4 | ▶ | $gen_opts{dump_directory} ||= './lib'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$gen_opts{"dump_directory"} ||= "./lib"
MC/DC0%0/1
$gen_opts{"dump_directory"} ||= "./lib"
$gen_opts{"dump_directory"}
Truth table50%1/2
A
A= $gen_opts{"dump_directory"}
| |||||||||||||||||||||||||||||
| 50 | 4 | ▶ | $gen_opts{schema_class} ||= $schema_class; | ||||||||||||||||||||||||||
|
Condition33%1/3
$gen_opts{"schema_class"} ||= $schema_class
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
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 4 | ▶ | if (my $style = $opts{style}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | die "Unknown style '$style' -- use cake, candy, vanilla, or moose\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 66 | 4 | ▶ | my $why = $@ || DBI->errstr; | ||||||||||||||||||||||||||
|
Condition66%2/3
$@ || 'DBI'->errstr
MC/DC0%0/2
$@ || 'DBI'->errstr
$@
'DBI'->errstr
Truth table66%2/3
A || B
A= $@B= 'DBI'->errstr
| |||||||||||||||||||||||||||||
| 67 | 4 | ▶ | $why =~ s/\n+\z// if defined $why; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | print "Done.\n" unless $gen_opts{quiet}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | __END__ | ||||||||||||||||||||||||||||