DBBrowser/Table/Extensions/ScalarFunctions/GetArguments.pm untested

filter: Statementstmt 0.0% 0 / 194 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 294 CC 29 SCAR 40.3
CC
29
Cov
0%
choose_columns
110.0
get_arguments
90.0
choose_a_column
42.0
CRAP 56.3
1 package # hide from PAUSE
2 App::DBBrowser::Table::Extensions::ScalarFunctions::GetArguments;
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 get_arguments {
27 my ( $sf, $sql, $clause, $func, $args_data, $r_data ) = @_;
28 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
29 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
30 my $info = $ax->get_sql_info( $sql );
31 my @args;
32  
33 for my $arg_data ( @$args_data ) {
34 my $tmp_info = $info . $ext->nested_func_info( $r_data );
35 # Readline
36 my $arg = $ext->argument(
37 $sql, $clause, $r_data,
38 { info => $tmp_info, history => $arg_data->{history}, prompt => $arg_data->{prompt},
39 is_numeric => $arg_data->{is_numeric} }
40 );
41 #last if ! defined $arg; ##
42 if ( ! length $arg || $arg eq "''" ) {
43 if ( $arg_data->{empty_ok} ) {
44 return @args if ! defined $arg;
45 $arg = "''";
46 }
47 elsif ( $arg_data->{skip_ok} ) {
48 next;
49 }
50 else {
51 return @args;
52 }
53 }
54 if ( $arg_data->{unquote} ) {
55 $arg = $ax->unquote_constant( $arg );
56 }
57 if ( $arg_data->{history_only} ) {
58 die "'history_only' without 'history!'" if ! @{$arg_data->{history}//[]};
59 my $rx_history = join '|', @{$arg_data->{history}};
60 if ( $arg !~ /^(?:$rx_history)\z/i ) {
61 my $message = ( $arg_data->{prompt} // 'Valid:') . $ax->format_list( $arg_data->{history} );
62 $ax->print_error_message( $message, $tmp_info );
63 redo;
64 }
65 }
66 push @args, $arg;
67 push @{$r_data->[-1]}, $arg;
68 }
69 return @args;
70 }
71  
72  
73 sub choose_columns {
74 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
75 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
76 my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} );
77 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
78 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
79 my $const = '[value]';
80 my @pre = ( undef, $sf->{i}{ok}, $sf->{i}{menu_addition}, $const );
81 my $menu = [ @pre, @$cols ];
82 my $subset = [];
83 my $info = $ax->get_sql_info( $sql );
84 my @bu;
85 my @bu_args_sofar = @{$r_data->[-1]};
86  
87 COLUMNS: while ( 1 ) {
88 $r_data->[-1] = [ @bu_args_sofar, @$subset ];
89 my $tmp_info = $info . $ext->nested_func_info( $r_data );
90 # Choose
91 my @idx = $tc->choose(
92 $menu,
93 { %{$sf->{i}{lyt_h}}, info => $tmp_info, prompt => 'Columns:', meta_items => [ 0 .. $#pre - 1 ], ##
94 no_spacebar => [ $#pre ], include_highlighted => 2, index => 1 }
95 );
96 if ( ! $idx[0] ) {
97 if ( @bu ) {
98 $subset = pop @bu;
99 next COLUMNS;
100 }
101 return;
102 }
103 if ( $menu->[$idx[0]] eq $sf->{i}{ok} ) {
104 shift @idx;
105 $sf->__add_chosen_cols_to_subset( $sql, $clause, $subset, [ @{$menu}[@idx] ], $r_data );
106 $r_data->[-1] = [ @bu_args_sofar, @$subset ];
107 if ( ! @$subset ) {
108 return;
109 }
110 return $subset;
111 }
112 elsif ( $menu->[$idx[0]] eq $sf->{i}{menu_addition} ) {
113 # recursion
114 my $complex_col = $ext->column( $sql, $clause, $r_data );
115 if ( ! defined $complex_col ) {
116 next COLUMNS;
117 }
118 push @bu, [ @$subset ];
119 push @$subset, $complex_col;
120 }
121 elsif ( $menu->[$idx[0]] eq $const ) {
122 my $history;
123 if ( $func eq 'CONCAT' ) { ##
124 $history = [ '%', '_', '*', '?', '|' ];
125 }
126 my $value = $tr->readline(
127 'Value: ',
128 { info => $tmp_info, history => $history }
129 );
130 if ( ! defined $value ) {
131 next COLUMNS;
132 }
133 push @bu, [ @$subset ];
134 push @$subset, $ax->quote_if_not_numeric( $value );
135 }
136 else {
137 push @bu, [ @$subset ];
138 $sf->__add_chosen_cols_to_subset( $sql, $clause, $subset, [ @{$menu}[@idx] ], $r_data );
139 }
140 }
141 }
142  
143  
144 sub __add_chosen_cols_to_subset {
145 my ( $sf, $sql, $clause, $subset, $chosen_cols, $r_data ) = @_;
146 if ( $sql->{aggregate_mode} && $clause =~ /^(?:select|having|order_by)\z/ ) {
147 my $sa = App::DBBrowser::Table::Substatement::Aggregate->new( $sf->{i}, $sf->{o}, $sf->{d} );
148 for my $aggr ( @$chosen_cols ) {
149 my $prep_aggr = $sa->get_prepared_aggr_func( $sql, $clause, $aggr, $r_data );
150 if ( ! length $prep_aggr ) {
151 next;
152 }
153 push @$subset, $prep_aggr;
154 push @{$r_data->[-1]}, $prep_aggr;
155 }
156 }
157 else {
158 push @$subset, @$chosen_cols;
159 }
160 }
161  
162  
163 sub choose_a_column {
164 my ( $sf, $sql, $clause, $cols, $r_data, $prompt ) = @_;
165 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
166 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
167 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
168 $prompt //= 'Column:';
169 my $info = $ax->get_sql_info( $sql ) . $ext->nested_func_info( $r_data );
170 my @pre = ( undef, $sf->{i}{menu_addition} );
171  
172 while ( 1 ) {
173 # Choose
174 my $col = $tc->choose(
175 [ @pre, @$cols ],
176 { %{$sf->{i}{lyt_h}}, info => $info, prompt => $prompt }
177 );
178 if ( ! defined $col ) {
179 return;
180 }
181 my $chosen_col;
182 if ( $col eq $sf->{i}{menu_addition} ) {
183 # recursion
184 my $complex_col = $ext->column( $sql, $clause, $r_data );
185 if ( ! defined $complex_col ) {
186 next;
187 }
188 $chosen_col = $complex_col;
189 }
190 elsif ( $sql->{aggregate_mode} && $clause =~ /^(?:select|having|order_by)\z/ ) {
191 my $sa = App::DBBrowser::Table::Substatement::Aggregate->new( $sf->{i}, $sf->{o}, $sf->{d} );
192 my $prep_aggr = $sa->get_prepared_aggr_func( $sql, $clause, $col, $r_data );
193 if ( ! length $prep_aggr ) {
194 next;
195 }
196 $chosen_col = $prep_aggr;
197 }
198 else {
199 $chosen_col = $col;
200 }
201 push @{$r_data->[-1]}, $chosen_col;
202 return $chosen_col;
203 }
204 }
205  
206  
207 sub sqlite_modifiers {
208 my ( $sf, $sql, $r_data ) = @_;
209 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
210 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
211 my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} );
212 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
213 my $history = [
214 'n days',
215 'n hours',
216 'n minutes',
217 'n seconds',
218 'n months',
219 'n years',
220 '[+-]YYYY-MM-DD HH:MM:SS.SSS',
221 'ceiling',
222 'floor',
223 'start of month',
224 'start of year',
225 'start of day',
226 'weekday n',
227 'unixepoch',
228 'julianday',
229 'auto',
230 'localtime',
231 'utc',
232 'subsec',
233 ];
234 my @modifiers;
235 my ( $ok, $add_modifier ) = ( '-OK-', 'Add modifier' );
236 my @args_sofar = @{$r_data->[-1]};
237  
238 while ( 1 ) {
239 $r_data->[-1] = [ @args_sofar, @modifiers ];
240 my $info = $ax->get_sql_info( $sql ) . $ext->nested_func_info( $r_data );
241 # Choose
242 my $choice = $tc->choose(
243 [ undef, $ok, $add_modifier ],
244 { %{$sf->{i}{lyt_h}}, info => $info }
245 );
246 if ( ! defined $choice ) {
247 if ( @modifiers ) {
248 pop @modifiers;
249 next;
250 }
251 $r_data->[-1] = [ @args_sofar ];
252 return;
253 }
254 if ( $choice eq $ok ) {
255 $r_data->[-1] = [ @args_sofar, @modifiers ];
256 return join ',', @modifiers;
257 }
258 else {
259 my $modifier = $tr->readline(
260 'Modifier: ',
261 { info => $info, history => $history }
262 );
263 $ax->print_sql_info( $info );
264 if ( ! length $modifier ) {
265 next;
266 }
267 push @modifiers, "'$modifier'";
268 }
269 }
270 }
271  
272 1;
273  
274 __END__