DBBrowser/Table/Extensions/Maths.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 134 CC 13 SCAR 46.9
CC
13
Cov
0%
maths
182.0
new
2.0
BEGIN
2.0
CRAP 108.4
1 package # hide from PAUSE
2 App::DBBrowser::Table::Extensions::Maths;
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::Aggregate;
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 maths {
27 my ( $sf, $sql, $clause, $cols, $r_data ) = @_;
28 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
29 my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} );
30 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
31 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
32 my ( $num, $op ) = ( '[num]', '[op]' );
33 my @pre = ( undef, $sf->{i}{ok}, $sf->{i}{menu_addition}, $num, $op );
34 my $menu = [ @pre, @$cols ];
35 my $info_sql = $ax->get_sql_info( $sql );
36 my $items = [];
37 push @$r_data, [ 'math' ];
38 my @bu;
39  
40 CHOICE: while ( 1 ) {
41 $r_data->[-1] = [ 'math', @$items ];
42 my $info = $info_sql . $ext->nested_func_info( $r_data );
43 # Choose
44 my $idx = $tc->choose(
45 $menu,
46 { %{$sf->{i}{lyt_h}}, info => $info, prompt => 'Math:', index => 1 }
47 );
48 if ( ! $idx ) {
49 if ( @bu ) {
50 $items = pop @bu;
51 next CHOICE;
52 }
53 pop @$r_data;
54 return;
55 }
56 if ( $menu->[$idx] eq $sf->{i}{ok} ) {
57 pop @$r_data;
58 if ( ! @$items ) {
59 return;
60 }
61 my $result = join ' ', @$items; ##
62 $result =~ s/\(\s/(/g;
63 $result =~ s/\s\)/)/g;
64 return '(' . $result . ')';
65 }
66 elsif ( $menu->[$idx] eq $sf->{i}{menu_addition} ) {
67 my $complex_col = $ext->column( $sql, $clause, $r_data );
68 if ( ! defined $complex_col ) {
69 next CHOICE;
70 }
71 push @bu, [ @$items ];
72 push @$items, $complex_col;
73 }
74 elsif ( $menu->[$idx] eq $op ) {
75 # Choose
76 my $operator = $tc->choose(
77 [ undef, ' + ', ' - ', ' * ', ' / ', ' % ', ' ( ', ' ) ' ],
78 { %{$sf->{i}{lyt_h}}, info => $info, prompt => 'Operator:', undef => '<=' }
79 );
80 if ( ! defined $operator ) {
81 next CHOICE;
82 }
83 push @bu, [ @$items ];
84 $operator =~ s/^\s+|\s+\z//g;
85 push @$items, $operator;
86 }
87 elsif ( $menu->[$idx] eq $num ) {
88 my $number = $tr->readline(
89 'Number: ',
90 { info => $info }
91 );
92 if ( ! length $number ) {
93 next CHOICE;
94 }
95 push @bu, [ @$items ];
96 push @$items, $number;
97 }
98 else {
99 push @bu, [ @$items ];
100 if ( $sql->{aggregate_mode} && $clause =~ /^(?:select|having|order_by)\z/ ) {
101 my $sa = App::DBBrowser::Table::Substatement::Aggregate->new( $sf->{i}, $sf->{o}, $sf->{d} );
102 my $prep_aggr = $sa->get_prepared_aggr_func( $sql, $clause, $menu->[$idx], $r_data );
103 if ( length $prep_aggr ) {
104 push @$items, $prep_aggr;
105 }
106 }
107 else {
108 push @$items, $menu->[$idx];
109 }
110 }
111 }
112 }
113  
114  
115  
116 1;
117 __END__