DBBrowser/Table/Extensions/Case.pm untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 170 CC 15 SCAR 41.7
CC
15
Cov
0%
case
132.0
format_case
30.0
new
2.0
CRAP 64.8
1 package # hide from PAUSE
2 App::DBBrowser::Table::Extensions::Case;
3  
4 use warnings;
5 use strict;
6 use 5.016;
7  
8 use Term::Choose qw();
9 use Term::Form::ReadLine qw();
10  
11 use App::DBBrowser::Auxil;
12 use App::DBBrowser::Table::Extensions;
13 use App::DBBrowser::Table::Substatement::Condition;
14  
15  
16 sub new {
17 my ( $class, $info, $options, $d ) = @_;
18 bless {
19 i => $info,
20 o => $options,
21 d => $d
22 }, $class;
23 }
24  
25  
26 sub case {
27 my ( $sf, $sql, $clause, $cols, $r_data ) = @_;
28 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
29 my $sc = App::DBBrowser::Table::Substatement::Condition->new( $sf->{i}, $sf->{o}, $sf->{d} );
30 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
31 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
32 my $depth = 0;
33 for my $e ( reverse @$r_data ) {
34 last if $e->[0] ne 'case';
35 $depth++;
36 }
37 my $case_parts = [ "CASE" ];
38 push @$r_data, [ 'case', @$case_parts ];
39 my @bu;
40 my $else_on = 0;
41  
42 SUBSTMT: while ( 1 ) {
43 my ( $when, $else, $end ) = ( 'WHEN', 'ELSE', 'END' );
44 my @pre = ( undef );
45 my $menu;
46 if ( $else_on ) {
47 $menu = [ @pre, $end ];
48 }
49 else {
50 $menu = [ @pre, $when, $else, $end ];
51 }
52 $r_data->[-1] = [ 'case', @$case_parts, '' ];
53 my $info = $ax->get_sql_info( $sql ) . $ext->nested_func_info( $r_data );
54 # Choose
55 my $idx = $tc->choose(
56 $menu,
57 { %{$sf->{i}{lyt_h}}, info => $info, prompt => 'Your choice:', index => 1, undef => '<=' }
58 );
59 $ax->print_sql_info( $info );
60 if ( ! $idx ) {
61 if ( @bu ) {
62 $case_parts = pop @bu;
63 $r_data->[-1] = [ 'case', @$case_parts ];
64 $else_on = 0;
65 next SUBSTMT;
66 }
67 pop @$r_data;
68 return;
69 }
70 if ( $menu->[$idx] eq $end ) {
71 pop @$r_data;
72 push @$case_parts, "END";
73 my $case_stmt = $sf->format_case( $case_parts, $depth );
74 if ( ! $depth ) {
75 $case_stmt = $ax->normalize_space_in_stmt( $case_stmt );
76 }
77 return $case_stmt;
78 }
79 push @bu, [ @$case_parts ];
80 my $operator = '=';
81 if ( $menu->[$idx] eq $when ) {
82 push @$case_parts, "WHEN";
83 $r_data->[-1] = [ 'case', @$case_parts ];
84 my $clause_when = $clause . '_WHEN';
85 my $tmp_sql = $ax->clone_data( $sql );
86 my $ret = $sc->add_condition( $tmp_sql, $clause_when, $cols, $r_data );
87 if ( ! defined $ret ) {
88 $case_parts = pop @bu;
89 next SUBSTMT;
90 }
91 $case_parts->[-1] = $tmp_sql->{when_stmt} . " THEN";
92 }
93 elsif ( $menu->[$idx] eq $else ) {
94 $else_on = 1;
95 push @$case_parts, "ELSE";
96 }
97 $r_data->[-1] = [ 'case', @$case_parts ];
98 my $value = $ext->value( $sql, $clause, $r_data, $operator, { is_numeric => -1 } );
99 if ( ! defined $value ) {
100 $case_parts = pop @bu;
101 next SUBSTMT;
102 }
103 $case_parts->[-1] .= ' ' . $value;
104 }
105 }
106  
107  
108 sub format_case {
109 my ( $sf, $case_parts, $depth ) = @_;
110 $depth++;
111 my $in = ' ' x $sf->{o}{G}{base_indent};
112 # my $pad1 = $in x $depth;
113 # my $pad2 = $in x ( $depth + 1 );
114 my $pad1 = '';
115 my $pad2 = '';
116 if ( $depth == 1 ) {
117 $pad1 = '';
118 $pad2 = $in x 2;
119 }
120 else {
121 my $d = $depth * 2;
122 $pad1 = $in x $d;
123 $pad2 = $in x ( $d + 2);
124 }
125 $pad1 .= $in;
126 $pad2 .= $in;
127 my $CASE = shift @$case_parts;
128 my $END;
129 if ( @$case_parts && $case_parts->[-1] eq "END" ) {
130 $END = pop @$case_parts;
131 }
132 my $case_stmt;
133 if ( $depth > 1 ) {
134 $case_stmt .= "\n";
135 }
136 $case_stmt .= $pad1 . $CASE;
137 for my $part ( @$case_parts ) {
138 $case_stmt .= "\n" . $pad2 . $part;
139 }
140 if ( length $END ) {
141 $case_stmt .= "\n" . $pad1 . "END";
142 }
143 return $case_stmt;
144 }
145  
146  
147  
148  
149  
150 1;
151  
152 __END__