dbMan/Extension/SQLOutputOldTable.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 150 CC 19 SCAR 52.3
CC
19
Cov
0%
handle_action
306.0
done
12.0
IDENTIFICATION
2.0
CRAP 187.6
1 package DBIx::dbMan::Extension::SQLOutputOldTable;
2  
3 use strict;
4 use base 'DBIx::dbMan::Extension';
5 use Data::ShowTable;
6 use Term::ANSIColor;
7  
8 our $VERSION = '0.06';
9  
10 1;
11  
12 sub IDENTIFICATION { return "000001-000053-000006"; }
13  
14 sub preference { return 0; }
15  
16 sub known_actions { return [ qw/SQL_OUTPUT/ ]; }
17  
18 sub init {
19 my $obj = shift;
20 $obj->{-mempool}->register('output_format','oldtable');
21 $obj->{-mempool}->register('output_format','sqlplus');
22 $obj->{-mempool}->register('output_format','records');
23 $obj->{-mempool}->register('output_format','colorrecords');
24 }
25  
26 sub done {
27 my $obj = shift;
28 $obj->{-mempool}->deregister('output_format','oldtable');
29 $obj->{-mempool}->deregister('output_format','sqlplus');
30 $obj->{-mempool}->deregister('output_format','records');
31 $obj->{-mempool}->deregister('output_format','colorrecords');
32 if ($obj->{-mempool}->get('output_format') =~ /^(oldtable|sqlplus|(color)?records)$/) {
33 my @all_formats = $obj->{-mempool}->get_register('output_format');
34 $obj->{-mempool}->set('output_format',@all_formats ? $all_formats[0] : '');
35 }
36 }
37
38 sub handle_action {
39 my ($obj,%action) = @_;
40  
41 $action{processed} = 1;
42 if ($action{action} eq 'SQL_OUTPUT') {
43 if ($obj->{-mempool}->get('output_format') =~ /^(oldtable|sqlplus|(color)?records)$/) {
44 my @widths = ();
45 for ($action{fieldnames},@{$action{result}}) {
46 my $i = 0;
47 for (@$_) {
48 $widths[$i] = length($_) if $widths[$i] < length($_);
49 ++$i;
50 }
51 }
52 my $i = 0;
53 my $table = '';
54 open F,">/tmp/dbman.$$.showtable";
55 if ( $obj->{ -interface }->is_utf8 ) {
56 binmode F, ':utf8';
57 }
58 *OLD = *STDOUT;
59 *STDOUT = *F;
60 if ($obj->{-mempool}->get('output_format') eq 'sqlplus') {
61 ShowSimpleTable({
62 titles => $action{fieldnames},
63 types => [ map { 'text' } @{$action{fieldnames}} ],
64 widths => \@widths,
65 row_sub => sub {
66 if (shift) { $i = 0; return 1; }
67 return () unless defined $action{result}->[$i];
68 return @{$action{result}->[$i++]};
69 }});
70 } elsif ( $obj->{-mempool}->get('output_format') =~ /^(color)?records$/ ) {
71 ShowListTable({
72 titles => $action{fieldnames},
73 types => [ map { 'text' } @{$action{fieldnames}} ],
74 widths => \@widths,
75 row_sub => sub {
76 if (shift) { $i = 0; return 1; }
77 return () unless defined $action{result}->[$i];
78 return @{$action{result}->[$i++]};
79 }});
80 } else {
81 ShowBoxTable({
82 titles => $action{fieldnames},
83 types => [ map { 'text' } @{$action{fieldnames}} ],
84 widths => \@widths,
85 row_sub => sub {
86 if (shift) { $i = 0; return 1; }
87 return () unless defined $action{result}->[$i];
88 return @{$action{result}->[$i++]};
89 }});
90 };
91 close F;
92 *STDOUT = *OLD;
93 if (open F,"/tmp/dbman.$$.showtable") {
94 if ( $obj->{ -interface }->is_utf8 ) {
95 binmode F, ':utf8';
96 }
97 $table = join '',<F>;
98 close F;
99 }
100 unlink "/tmp/dbman.$$.showtable" if -f "/tmp/dbman.$$.showtable";
101 $action{action} = 'OUTPUT';
102 if ( $obj->{-mempool}->get('output_format') eq 'colorrecords' ) {
103 $table =~ s/^([^:]+):(.*)$/color( $obj->{-config}->tablecolor_head || 'bright_yellow' ) . $1 . color( $obj->{-config}->tablecolor_lines || 'reset' ) . ':' . color( $obj->{-config}->tablecolor_content || 'bright_white' ) . $2/meg;
104 $table .= color( 'reset' );
105 }
106 $action{output} = $table;
107 delete $action{processed};
108 }
109 }
110  
111 return %action;
112 }