« blib/lib/PMLTQ/Relation/AncestorIteratorWithBoundedDepth.pm
↑ Summary
blib/lib/PMLTQ/Command/load.pm »
| 1 | package PMLTQ::Command; | ||||||||||||||||||||||||||||
| 2 | 9 | our $AUTHORITY = 'cpan:MATY'; | |||||||||||||||||||||||||||
| 3 | 9 | $PMLTQ::Command::VERSION = '1.2.2'; | |||||||||||||||||||||||||||
| 4 | # ABSTRACT: Command base class | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use PMLTQ::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use File::Slurp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use Pod::Usage 'pod2usage'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 0 | ▶ | has config => sub { die 'Command has no configuration'; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | has usage => sub {'Usage: '}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | has term => sub { | ||||||||||||||||||||||||||||
| 17 | 0 | ▶ | require Term::UI; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 18 | 0 | require Term::ReadLine; | |||||||||||||||||||||||||||
| 19 | 0 | Term::ReadLine->new('pmltq'); | |||||||||||||||||||||||||||
| 20 | 9 | }; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | has term_encoding => sub { | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | require Term::Encoding; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 24 | 0 | Term::Encoding::get_encoding(); | |||||||||||||||||||||||||||
| 25 | 9 | }; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | sub run { | ||||||||||||||||||||||||||||
| 28 | 0 | ▶ | die 'Override by parent class'; | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | sub extract_usage { | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
extract_usage: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | open my $handle, '>', \my $output; | |||||||||||||||||||||||||||
| 35 | 0 | pod2usage( -exitval => 'NOEXIT', -input => (caller)[1], -output => $handle ); | |||||||||||||||||||||||||||
| 36 | 0 | $output =~ s/\n$//; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | return $output; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub help { | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | print shift->usage; | ||||||||||||||||||||||||||
|
Subroutine
help: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub _db_connect { | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my ( $database, $host, $port, $user, $password ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_db_connect: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my $dbh = DBI->connect( 'DBI:Pg:dbname=' . $database . ';host=' . $host . ';port=' . $port, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | $user, $password, { RaiseError => 1, PrintError => 1 } ) | ||||||||||||||||||||||||||||
| 50 | or die "Unable to connect to database!\n$DBI::errstr\n"; | ||||||||||||||||||||||||||||
| 51 | 0 | return $dbh; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub db { | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
db: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | my $db = $self->config->{db}; | |||||||||||||||||||||||||||
| 58 | 0 | return _db_connect( $db->{name}, $db->{host}, $db->{port}, $db->{user}, $db->{password} ); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub sys_db { | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
sys_db: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | my $config = $self->config; | |||||||||||||||||||||||||||
| 65 | 0 | my $db = $config->{db}; | |||||||||||||||||||||||||||
| 66 | 0 | my $sys_db = $config->{sys_db}; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | unless ( ref $sys_db ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | $sys_db = { name => $sys_db }; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | $sys_db->{$_} = $db->{$_} for ( grep { !defined $sys_db->{$_} } qw/user password/ ); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | return _db_connect( $sys_db->{name}, $db->{host}, $db->{port}, $sys_db->{user}, $sys_db->{password} ); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | sub run_sql_from_file { | ||||||||||||||||||||||||||||
| 78 | 0 | ▶ | my ( $self, $file, $dir, $dbh ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
run_sql_from_file: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my $sqlfile = File::Spec->catfile( $dir, $file ); | |||||||||||||||||||||||||||
| 81 | 0 | my $sql = read_file($sqlfile); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | print STDERR "RUNNING SQL FROM $sqlfile\n"; | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ( $file =~ m/.ctl/ and my $copy = () = $sql =~ m/(COPY .*? FROM *?["'].*?["'])/g ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$file =~ /.ctl/ and my $copy = () = $sql =~ /(COPY .*? FROM *?["'].*?["'])/g
MC/DC0%0/2
$file =~ /.ctl/ and my $copy = () = $sql =~ /(COPY .*? FROM *?["'].*?["'])/g
$file =~ /.ctl/
my $copy = () = $sql =~ /(COPY .*? FROM *?["'].*?["'])/g
Truth table0%0/3
A and B
A= $file =~ /.ctl/B= my $copy = () = $sql =~ /(COPY .*? FROM *?["'].*?["'])/g
| |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | die "More COPY commands than one in file is not supported.\n\n$sql\n" if $copy > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | $sql =~ s/(COPY .*? FROM) *?["'](.*?)["']/$1 STDIN/; | |||||||||||||||||||||||||||
| 87 | 0 | my $dump_file = File::Spec->catfile( $dir, $2 ); | |||||||||||||||||||||||||||
| 88 | 0 | eval { | |||||||||||||||||||||||||||
| 89 | 0 | $dbh->do($sql); | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | open my $fh, '<', "$dump_file" or die "Can't open $dump_file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | while ( my $data = <$fh> ) { # Do not load whole file, but process it line by line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | next unless $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | $dbh->pg_putcopydata("$data"); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 0 | $dbh->pg_putcopyend(); | |||||||||||||||||||||||||||
| 96 | }; | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | warn $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | else { | ||||||||||||||||||||||||||||
| 100 | 0 | my @statements = split /\n\n/, $sql; | |||||||||||||||||||||||||||
| 101 | 0 | for my $s (@statements) { | |||||||||||||||||||||||||||
| 102 | 0 | eval { $dbh->do($s); }; | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | print STDERR "SQL FAILED:\t$s\n\t$@\n" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # Borrowed from https://metacpan.org/release/Dist-Zilla | ||||||||||||||||||||||||||||
| 109 | sub prompt_str { | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | my ( $self, $prompt, $arg ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
prompt_str: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | $arg ||= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$arg ||= {}
MC/DC0%0/1
$arg ||= {}
$arg
Truth table0%0/2
A
A= $arg
| |||||||||||||||||||||||||||||
| 113 | 0 | my $default = $arg->{default}; | |||||||||||||||||||||||||||
| 114 | 0 | my $check = $arg->{check}; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | require Encode; | |||||||||||||||||||||||||||
| 117 | 0 | my $term_encoding = $self->term_encoding; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | my $encode | ||||||||||||||||||||||||||||
| 120 | = $term_encoding | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | ? sub { Encode::encode( $term_encoding, shift, Encode::FB_CROAK() ) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | : sub {shift}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | my $decode | ||||||||||||||||||||||||||||
| 124 | = $term_encoding | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | ? sub { Encode::decode( $term_encoding, shift, Encode::FB_CROAK() ) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | : sub {shift}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | my $input_bytes = $self->term->get_reply( | ||||||||||||||||||||||||||||
| 129 | prompt => $encode->($prompt), | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | allow => $check || sub { defined $_[0] and length $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | ( defined $default | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$check || sub {
defined $_[0] and length $_[0];
}
MC/DC0%0/2
$check || sub {
defined $_[0] and length $_[0];
}
$check
sub {
defined $_[0] and length $_[0];
}
Truth table0%0/3
A || B
A= $checkB= sub {
defined $_[0] and length $_[0];
}
| |||||||||||||||||||||||||||||
| 132 | ? ( default => $encode->($default) ) | ||||||||||||||||||||||||||||
| 133 | : () | ||||||||||||||||||||||||||||
| 134 | ), | ||||||||||||||||||||||||||||
| 135 | ); | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | my $input = $decode->($input_bytes); | |||||||||||||||||||||||||||
| 138 | 0 | chomp $input; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | return $input; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | sub prompt_yn { | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | my ( $self, $prompt, $arg ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
prompt_yn: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | $arg ||= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$arg ||= {}
MC/DC0%0/1
$arg ||= {}
$arg
Truth table0%0/2
A
A= $arg
| |||||||||||||||||||||||||||||
| 146 | 0 | my $default = $arg->{default}; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | my $input = $self->term->ask_yn( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | prompt => $prompt, | ||||||||||||||||||||||||||||
| 150 | ( defined $default ? ( default => $default ) : () ), | ||||||||||||||||||||||||||||
| 151 | ); | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | return $input; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 9 | 1; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||