blib/lib/DBI/Shell/Spool.pm

filter: Statementstmt 88.8% 48 / 54 Branchbran 60.0% 12 / 20 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 72.5% 66 / 91 CC 14 SCAR 27.0
CC
14
Cov
76%
do_spool
18.9
BEGIN
1.0
BEGIN
1.0
CRAP 14.9
« blib/lib/DBI/Format.pm ↑ Summary blib/lib/DBI/Shell/FindSqlFile.pm »
1 #
2 # Package meta, adds meta database commands to dbish
3 #
4 package DBI::Shell::Spool;
5  
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use IO::Tee;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub init {
12 4 ▶ my ($self, $sh, @arg) = @_;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
13  
14  
15 4 $sh->install_options(
16 [
17 [ 'spool' => 'off' ],
18 ]);
19 4 my $com_ref = $sh->{commands};
20 $com_ref->{spool} = {
21 4 hint =>
22 "spool: on/off or file name to send output to",
23 };
24
25 4 return $self;
26 }
27  
28 #------------------------------------------------------------------
29 #
30 # Start or Stop spooling output.
31 # The spool support the follow states:
32 # spool - returns the current state of spooling, if on includes the file name.
33 # spool on - set the state to on, opens a default name of spool.lst (Yes, the
34 # Oracle default name). If the spool current state is already on, returns a
35 # warning message (Already spooling to file X).
36 # spool /path/file/name - set the state on, attempt to open the file name
37 # (using the IO::Tee object to allow multiplex output), and set the new IO
38 # handle to the default handle.
39 # spool off - set the state to off. If the previous state was on, flush the
40 # current buffer and close the file handle. If the previous state was off,
41 # return a warning message (Not current spooling).
42 #
43 #------------------------------------------------------------------
44 sub do_spool {
45 4 ▶ my ($sh, @args) = @_;
Subroutine do_spool: called CC=14 SCAR=29.4
Pod undocumented
46  
47 # Get the current state of spool.
48 4 ▶ unless(@args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args) 2 2 4
49 2 ▶ if ($sh->is_spooling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sh->is_spooling) { } 1 1 2
50 return $sh->print_buffer( qq{spooling output to file: },
51 1 $sh->{spool_file} );
52 } else {
53 1 return $sh->print_buffer( qq{not spooling} );
54 }
55 }
56  
57 # So what command did I get at this point?
58 2 my $command = shift @args;
59  
60 2 ▶ if ($command =~ m/\boff/i) { # Turn the spool off (if on).
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ /\boff/i) 1 1 2
61 1 ▶ if ($sh->is_spooling) { # spool on
Branch: 1 branch
BranchTrueFalseTotal
if ($sh->is_spooling) 1 0 1
62 # The tee object contains the open handles, get a list, shift the
63 # first (this should be STDOUT), flush. Then for the remainder
64 # flush each and close.
65 1 my @fhs = $sh->{out_fh}->handles;
66 1 $sh->{out_fh} = shift @fhs; select $sh->{out_fh};
67  
68 1 $sh->{out_fh}->flush;
69 1 $sh->spool_off; $sh->{spool_file} = undef;
70 1 foreach my $fh (@fhs) {
71 1 $fh->flush;
72 1 $fh->close;
73 }
74 1 $sh->{spool_fh} = undef;
75 1 return $sh->{out_fh};
76 }
77 0 return $sh->print_buffer( qq{not spooling} );
78 }
79  
80 1 my $spool_file = undef;
81 1 ▶ if ($command =~ m/on/i) { # Turn the spool off (if on).
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ /on/i) 0 1 1
82 0 ▶ unless(@args or $args[0] !~ m/!/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args or not $args[0] =~ /!/) 0 0 0
Condition0%0/3
@args or not $args[0] =~ /!/
l!l&&r!l&&!r
000
MC/DC0%0/2
@args or not $args[0] =~ /!/
@args not $args[0] =~ /!/
Truth table0%0/3
A or not(B)
A= @argsB= not $args[0] =~ /!/
ABresult
1X1
011
000
83 0 $spool_file = q{on.lst};
84 }
85 }
86  
87 # OK, now we're at the one to open the spool file. How do I handle if the
88 # file exists? Well, unless the next arg is a !, open the file for append.
89 1 my $mode = q{a+};
90 1 ▶ if (@args and $args[0] =~ m/!/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args and $args[0] =~ /!/) 0 1 1
Condition33%1/3
@args and $args[0] =~ /!/
!ll&&!rl&&r
100
MC/DC0%0/2
@args and $args[0] =~ /!/
@args $args[0] =~ /!/
Truth table33%1/3
A and B
A= @argsB= $args[0] =~ /!/
ABresult
0X0
100
111
91 0 shift @args;
92 0 $mode = q{w};
93 }
94
95 1 my $out_fh = $sh->{out_fh};
96  
97 1 ▶ $spool_file = defined $spool_file ? $spool_file : $command;
Branch: 1 branch
BranchTrueFalseTotal
defined $spool_file ? : 0 1 1
98  
99 1 ▶ if (defined $spool_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spool_file) 1 0 1
100 1 ▶ my $tee_fh = new IO::Tee($out_fh, new IO::File($spool_file, $mode)) or
Branch: 1 branch
BranchTrueFalseTotal
my $tee_fh = "IO::Tee"->new($out_fh, "IO::File"->new($spool_file, $mode)) or return $sh->alert("Unable create IO::Tee ($spool_file) handle: $!\n") 0 1 1
101 return $sh->alert(qq{Unable create IO::Tee ($spool_file) handle: $!\n});
102 1 $sh->{out_fh} = $tee_fh;
103 1 $sh->{spool_file} = $spool_file; $sh->spool_on;
104 1 $sh->{spool_fh} = ($tee_fh->handles)[1];
105 1 select $tee_fh;
106 1 return $sh->print_buffer( qq{spooling $spool_file} );
107 }
108 0 return $sh->alert( qq{spool command failed for unknown reason} );
109 }
110  
111 4 1;
« blib/lib/DBI/Format.pm ↑ Summary blib/lib/DBI/Shell/FindSqlFile.pm »