DBBrowser/Table/Extensions/WindowFunctions.pm untested

filter: Statementstmt 0.0% 0 / 469 Branchbran 0.0% 0 / 172 Conditioncond 0.0% 0 / 74 MC/DCmcdc 0.0% 0 / 46 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 785 CC 87 SCAR 55.5
CC
87
Cov
0%
window_function
552.0
__get_win_func_stmt
380.0
__add_frame_clause
210.0
CRAP 258.5
1 package # hide from PAUSE
2 App::DBBrowser::Table::Extensions::WindowFunctions;
3  
4 use warnings;
5 use strict;
6 use 5.016;
7  
8 use Term::Choose qw();
9  
10 use App::DBBrowser::Auxil;
11 use App::DBBrowser::Table::Extensions;
12  
13  
14 sub new {
15 my ( $class, $info, $options, $d ) = @_;
16 bless {
17 i => $info,
18 o => $options,
19 d => $d
20 }, $class;
21 }
22  
23  
24 sub __choose_a_column {
25 my ( $sf, $sql, $clause, $cols, $r_data ) = @_;
26 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
27 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
28 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
29 my @pre = ( undef );
30 if ( $sf->{i}{menu_addition} ) {
31 push @pre, $sf->{i}{menu_addition};
32 }
33 my $info = $ax->get_sql_info( $sql ) . $ext->nested_func_info( $r_data );
34  
35 while ( 1 ) {
36 # Choose
37 my $choice = $tc->choose(
38 [ @pre, @$cols ],
39 { %{$sf->{i}{lyt_h}}, info => $info, prompt => 'Column:' }
40 );
41 if ( ! defined $choice ) {
42 return;
43 }
44 elsif ( $choice eq $sf->{i}{menu_addition} ) {
45 # from 'window_function': to avoid window function in window function
46 my $complex_col = $ext->column(
47 $sql, $clause, $r_data,
48 { from => 'window_function' }
49 );
50 if ( ! defined $complex_col ) {
51 next;
52 }
53 return $complex_col;
54 }
55 return $choice;
56 }
57 }
58  
59  
60 sub __get_win_func_stmt {
61 my ( $sf, $win_data, $placeholder ) = @_;
62 $placeholder //= '';
63 my @parts;
64 my $win_stmt = $win_data->{'func'};
65 $win_stmt .= '(' . $win_data->{'args_str'} // ''; ##
66 if ( $placeholder eq 'args_str' ) {
67 push @parts, $win_stmt;
68 $win_stmt = '';
69 }
70 $win_stmt .= ') OVER (';
71 if ( length $win_data->{'partition_by_stmt'} ) {
72 $win_stmt .= $win_data->{'partition_by_stmt'} . " ";
73 }
74 if ( $placeholder eq 'partition_by_stmt' ) {
75 push @parts, $win_stmt;
76 $win_stmt = '';
77 }
78 if ( length $win_data->{'order_by_stmt'} ) {
79 $win_stmt .= $win_data->{'order_by_stmt'} . " ";
80 }
81 if ( $placeholder eq 'order_by_stmt' ) {
82 push @parts, $win_stmt;
83 $win_stmt = '';
84 }
85 if ( length $win_data->{'frame_mode'} ) {
86 $win_stmt .= $win_data->{'frame_mode'} . " ";
87 if ( length $win_data->{'frame_start'} || $placeholder eq 'frame_start' ) {
88 if ( length $win_data->{'frame_end'} || $placeholder eq 'frame_end' ) {
89 $win_stmt .= "BETWEEN ";
90 }
91 if ( length $win_data->{'frame_start'} ) {
92 $win_stmt .= $win_data->{'frame_start'} . " ";
93 }
94 if ( $placeholder eq 'frame_start' ) {
95 push @parts, $win_stmt;
96 $win_stmt = '';
97 }
98 }
99 if ( length $win_data->{'frame_end'} || $placeholder eq 'frame_end' ) {
100 if ( length $win_data->{'frame_start'} || $placeholder eq 'frame_start' ) {
101 $win_stmt .= "AND ";
102 }
103 if ( length $win_data->{'frame_end'} ) {
104 $win_stmt .= $win_data->{'frame_end'} . " ";
105 }
106 if ( $placeholder eq 'frame_end' ) {
107 push @parts, $win_stmt;
108 $win_stmt = '';
109 }
110 }
111 if ( length $win_data->{'frame_exclusion'} ) {
112 $win_stmt .= $win_data->{'frame_exclusion'};
113 }
114 if ( $placeholder eq 'frame_exclusion' ) {
115 push @parts, $win_stmt;
116 $win_stmt = '';
117 }
118 }
119 if ( $placeholder eq 'frame_mode' ) {
120 push @parts, $win_stmt;
121 $win_stmt = '';
122 }
123 if ( ! $placeholder ) {
124 push @parts, $win_stmt;
125 $win_stmt = '';
126 }
127 $parts[0] =~ s/\s+\z//;
128 $win_stmt =~ s/\s+\z//;
129 $win_stmt .= ')';
130 push @parts, $win_stmt;
131 return @parts;
132 }
133  
134  
135 sub window_function {
136 my ( $sf, $sql, $clause, $cols, $r_data ) = @_;
137 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
138 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
139 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
140 my $func_count_all = 'COUNT*';
141 my $rx_func_count_all = quotemeta $func_count_all;
142 my @win_func_aggr = ( 'AVG', 'COUNT', $func_count_all, 'MAX', 'MIN', 'SUM' );
143 my @win_func_rank = ( 'CUME_DIST', 'DENSE_RANK', 'NTILE', 'PERCENT_RANK', 'RANK', 'ROW_NUMBER' );
144 my @win_func_value = ( 'FIRST_VALUE', 'LAG', 'LAST_VALUE', 'LEAD', 'NTH_VALUE' );
145  
146 my @functions = sort( @win_func_aggr, @win_func_rank, @win_func_value );
147  
148 my @func_no_col = ( 'CUME_DIST', 'DENSE_RANK', 'PERCENT_RANK', 'RANK', 'ROW_NUMBER' );
149 my $rx_func_no_col = join( '|', map { quotemeta } @func_no_col );
150  
151 my @func_col_is_number = ( 'NTILE' );
152 my $rx_func_col_is_number = join( '|', map { quotemeta } @func_col_is_number );
153  
154 my @func_with_offset = ( 'LAG', 'LEAD', 'NTH_VALUE' );
155 my $rx_func_with_offset = join( '|', map { quotemeta } @func_with_offset );
156  
157 my @func_with_offset_and_default = ( 'LAG', 'LEAD' ); # not if MariaDB ##
158 my $rx_func_with_offset_and_default = join( '|', map { quotemeta } @func_with_offset_and_default );
159 my $info_sql = $ax->get_sql_info( $sql );
160 push @$r_data, [ 'win' ];
161 my $hidden = 'Window function:';
162 my $old_idx_wf = 1;
163  
164 WINDOW_FUNCTION: while( 1 ) {
165 my $win_data = {};
166 $r_data->[-1] = [ 'win' ];
167 my $info = $info_sql . $ext->nested_func_info( $r_data );
168 my @pre = ( $hidden, undef );
169 my $menu = [ @pre, map { '- ' . $_ } @functions ];
170 # Choose
171 my $idx_wf = $tc->choose(
172 $menu,
173 { %{$sf->{i}{lyt_v}}, info => $info, prompt => '', index => 1,
174 default => $old_idx_wf, undef => '<=' }
175 );
176 if ( ! defined $idx_wf || ! defined $menu->[$idx_wf] ) {
177 pop @$r_data;
178 return;
179 }
180 if ( $sf->{o}{G}{menu_memory} ) {
181 if ( $old_idx_wf == $idx_wf && ! $ENV{TC_RESET_AUTO_UP} ) {
182 $old_idx_wf = 1;
183 next WINDOW_FUNCTION;
184 }
185 $old_idx_wf = $idx_wf;
186 }
187 if ( $menu->[$idx_wf] eq $hidden ) {
188 $ext->enable_extended_arguments( $info );
189 next WINDOW_FUNCTION;
190 }
191 my $func = $functions[$idx_wf-@pre];
192 $win_data->{'func'} = $func;
193  
194 COLUMN: while ( 1 ) {
195 $win_data->{'args_str'} = '';
196 $r_data->[-1] = [ 'win', $sf->__get_win_func_stmt( $win_data, 'args_str' ) ];
197 my $col;
198 if ( $func =~ /^$rx_func_count_all\z/i ) {
199 $col = '*';
200 $win_data->{'func'} = $func =~ s/\*\z//r;
201 }
202 elsif ( $func =~ /^(?:$rx_func_no_col)\z/i ) {
203 $col = '';
204 }
205 elsif ( $func =~ /^(?:$rx_func_col_is_number)\z/i ) {
206 # Readline
207 $col = $ext->argument(
208 $sql, $clause, $r_data,
209 { history => undef, prompt => 'n = ', is_numeric => 1, from => 'window_function' }
210 );
211 if ( ! length $col || $col eq "''" ) {
212 next WINDOW_FUNCTION;
213 }
214 }
215 else {
216 $col = $sf->__choose_a_column( $sql, $clause, $cols, $r_data );
217 if ( ! defined $col ) {
218 next WINDOW_FUNCTION;
219 }
220 }
221 $win_data->{'args_str'} = $col;
222 if ( $func =~ /^(?:$rx_func_with_offset)\z/i ) {
223 $r_data->[-1] = [ 'win', $sf->__get_win_func_stmt( $win_data, 'args_str' ) ];
224 # Readline
225 my $offset = $ext->argument(
226 $sql, $clause, $r_data,
227 { prompt => 'offset: ', is_numeric => 1 }
228 );
229 if ( length $offset && $offset ne "''" ) {
230 $win_data->{'args_str'} .= ',' . $offset;
231 if ( $func =~ /^(?:$rx_func_with_offset_and_default)\z/i ) {
232 $r_data->[-1] = [ 'win', $sf->__get_win_func_stmt( $win_data, 'args_str' ) ];
233 my $is_numeric = $ax->is_numeric( $sql, $col );
234 # Readline
235 my $default_value = $ext->argument(
236 $sql, $clause, $r_data,
237 { prompt => 'default: ', is_numeric => $is_numeric, from => 'window_function' }
238 );
239 if ( length $default_value && $default_value ne "''" ) {
240 $win_data->{'args_str'} .= ',' . $default_value;
241 #$r_data->[-1] = [ 'win', $sf->__get_win_func_stmt( $win_data ) ];
242 }
243 }
244 }
245 }
246 my $old_idx = 0;
247  
248 WINDOW_DEFINITION: while( 1 ) {
249 my ( $partition_by, $order_by, $frame_clause ) = ( '- Partition by', '- Order by', '- Frame clause' );
250 my @pre = ( undef, $sf->{i}{confirm} );
251 my $menu = [ @pre, $partition_by, $order_by, $frame_clause ];
252 $r_data->[-1] = [ 'win', $sf->__get_win_func_stmt( $win_data ) ];
253 my $info = $info_sql . $ext->nested_func_info( $r_data );
254 # Choose
255 my $idx_wd = $tc->choose(
256 $menu,
257 { %{$sf->{i}{lyt_v}}, info => $info, prompt => '', index => 1, default => $old_idx,
258 undef => $sf->{i}{back} }
259 );
260 if ( ! defined $idx_wd || ! defined $menu->[$idx_wd] ) {
261 if ( $win_data->{'partition_by_stmt'} || $win_data->{'order_by_stmt'} || $win_data->{'frame_mode'} ) {
262 $win_data = { func => $win_data->{'func'}, args_str => $win_data->{'args_str'} };
263 next WINDOW_DEFINITION;
264 }
265 if ( $func =~ /^(?:$rx_func_count_all|$rx_func_no_col)\z/ ) {
266 next WINDOW_FUNCTION;
267 }
268 next COLUMN;
269 }
270 if ( $sf->{o}{G}{menu_memory} ) {
271 if ( $old_idx == $idx_wd && ! $ENV{TC_RESET_AUTO_UP} ) {
272 $old_idx = 0;
273 next WINDOW_DEFINITION;
274 }
275 $old_idx = $idx_wd;
276 }
277 my $wd = $menu->[$idx_wd];
278 if ( $wd eq $sf->{i}{confirm} ) {
279 pop @$r_data;
280 my @parts = $sf->__get_win_func_stmt( $win_data );
281 return join '', @parts;
282 }
283 elsif ( $wd eq $partition_by ) {
284 $sf->__add_partition_by( $sql, $clause, $cols, $r_data, $win_data );
285 }
286 elsif ( $wd eq $order_by ) {
287 $sf->__add_order_by( $sql, $clause, $cols, $r_data, $win_data );
288 }
289 elsif ( $wd eq $frame_clause ) {
290 $sf->__add_frame_clause( $sql, $clause, $r_data, $win_data );
291 }
292 }
293 }
294 }
295 }
296  
297  
298 sub __add_partition_by {
299 my ( $sf, $sql, $clause, $cols, $r_data, $win_data ) = @_;
300 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
301 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
302 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
303 $win_data->{'partition_by_cols'} //= [];
304 my @pre = ( undef, $sf->{i}{ok} );
305 if ( $sf->{o}{enable}{extended_cols} ) {
306 push @pre, $sf->{i}{menu_addition};
307 }
308 my $menu = [ @pre, @$cols ];
309 my $info_sql = $ax->get_sql_info( $sql );
310  
311 PARTITION_BY: while ( 1 ) {
312 if ( @{$win_data->{'partition_by_cols'}} ) {
313 $win_data->{'partition_by_stmt'} = "PARTITION BY " . join ',', @{$win_data->{'partition_by_cols'}};
314 }
315 else {
316 $win_data->{'partition_by_stmt'} = "PARTITION BY";
317 }
318 $r_data->[-1] = [ 'win', $sf->__get_win_func_stmt( $win_data, 'partition_by_stmt' ) ];
319 my $info = $info_sql . $ext->nested_func_info( $r_data );
320 my $prompt = 'Partition by:';
321 # Choose
322 my @idx = $tc->choose(
323 $menu,
324 { %{$sf->{i}{lyt_h}}, info => $info, meta_items => [ 0 .. $#pre - 1 ], no_spacebar => [ $#pre ],
325 include_highlighted => 2, index => 1, prompt => $prompt }
326 );
327 $ax->print_sql_info( $info );
328 if ( ! $idx[0] ) {
329 if ( @{$win_data->{'partition_by_cols'}} ) {
330 pop @{$win_data->{'partition_by_cols'}};
331 next PARTITION_BY;
332 }
333 delete $win_data->{'partition_by_stmt'};
334 return;
335 }
336 elsif ( $menu->[$idx[0]] eq $sf->{i}{ok} ) {
337 shift @idx;
338 push @{$win_data->{'partition_by_cols'}}, @{$menu}[@idx];
339 if ( ! @{$win_data->{'partition_by_cols'}} ) {
340 delete $win_data->{'partition_by_stmt'};
341 }
342 else {
343 $win_data->{'partition_by_stmt'} = "PARTITION BY " . join ',', @{$win_data->{'partition_by_cols'}};
344 }
345 return 1;
346 }
347 elsif ( $menu->[$idx[0]] eq $sf->{i}{menu_addition} ) {
348 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
349 my $complex_column = $ext->column(
350 $sql, $clause, $r_data,
351 { from => 'window_function' }
352 );
353 if ( defined $complex_column ) {
354 push @{$win_data->{'partition_by_cols'}}, $complex_column;
355 }
356 next PARTITION_BY;
357 }
358 push @{$win_data->{'partition_by_cols'}}, @{$menu}[@idx];
359 }
360 }
361  
362  
363 sub __add_order_by {
364 my ( $sf, $sql, $clause, $cols, $r_data, $win_data ) = @_;
365 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
366 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
367 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
368 $win_data->{'order_by_cols'} //= [];
369 my @pre = ( undef, $sf->{i}{ok} );
370 if ( $sf->{o}{enable}{extended_cols} ) {
371 push @pre, $sf->{i}{menu_addition};
372 }
373 my $info_sql = $ax->get_sql_info( $sql );
374  
375 ORDER_BY: while ( 1 ) {
376 if ( @{$win_data->{'order_by_cols'}} ) {
377 $win_data->{'order_by_stmt'} = "ORDER BY " . join ',', @{$win_data->{'order_by_cols'}};
378 }
379 else {
380 $win_data->{'order_by_stmt'} = "ORDER BY";
381 }
382 $r_data->[-1] = [ 'win', $sf->__get_win_func_stmt( $win_data, 'order_by_stmt' ) ];
383 my $info = $info_sql . $ext->nested_func_info( $r_data );
384 my $prompt = 'Order by:';
385 # Choose
386 my $col = $tc->choose(
387 [ @pre, @$cols ],
388 { %{$sf->{i}{lyt_h}}, info => $info, prompt => $prompt }
389 );
390 $ax->print_sql_info( $info );
391 if ( ! defined $col ) {
392 if ( @{$win_data->{'order_by_cols'}} ) {
393 pop @{$win_data->{'order_by_cols'}};
394 next ORDER_BY;
395 }
396 delete $win_data->{'order_by_stmt'};
397 return
398 }
399 if ( $col eq $sf->{i}{ok} ) {
400 if ( ! @{$win_data->{'order_by_cols'}} ) {
401 delete $win_data->{'order_by_stmt'};
402 }
403 else {
404 $win_data->{'order_by_stmt'} = "ORDER BY " . join ',', @{$win_data->{'order_by_cols'}};
405 }
406 return 1;
407 }
408 elsif ( $col eq $sf->{i}{menu_addition} ) {
409 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
410 my $complex_column = $ext->column(
411 $sql, $clause, $r_data,
412 { from => 'window_function' }
413 );
414 if ( ! defined $complex_column ) {
415 next ORDER_BY;
416 }
417 $col = $complex_column;
418 }
419 push @{$win_data->{'order_by_cols'}}, $col;
420 $win_data->{'order_by_stmt'} = "ORDER BY " . join ',', @{$win_data->{'order_by_cols'}};
421 $r_data->[-1] = [ 'win', $sf->__get_win_func_stmt( $win_data, 'order_by_stmt' ) ];
422 $info = $info_sql . $ext->nested_func_info( $r_data );
423 # Choose
424 my $direction = $tc->choose(
425 [ undef, "ASC", "DESC" ],
426 { %{$sf->{i}{lyt_h}}, info => $info }
427 );
428 $ax->print_sql_info( $info );
429 if ( ! defined $direction ){
430 pop @{$win_data->{'order_by_cols'}};
431 next ORDER_BY;
432 }
433 else {
434 $win_data->{'order_by_cols'}[-1] .= ' ' . $direction;
435 }
436 }
437 }
438  
439  
440 sub __add_frame_clause {
441 my ( $sf, $sql, $clause, $r_data, $win_data ) = @_;
442 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
443 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
444 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
445 my $dbms = $sf->{i}{dbms};
446 my @frame_clause_modes = ( 'ROWS', 'RANGE' );
447 if ( $dbms =~ /^(?:SQLite|Pg|DuckDB|Oracle)\z/ ) {
448 push @frame_clause_modes, 'GROUPS';
449 }
450 if ( ! length $win_data->{'frame_mode'} ) {
451 $r_data->[-1] = [ 'win', $sf->__get_win_func_stmt( $win_data, 'frame_mode' ) ];
452 }
453 my $info_sql = $ax->get_sql_info( $sql );
454 my $old_idx_fc = 0;
455  
456 FRAME_CLAUSE: while ( 1 ) {
457 my $info = $info_sql . $ext->nested_func_info( $r_data );
458 my @pre = ( undef );
459 my $menu = [ @pre, map { '- ' . $_ } @frame_clause_modes ];
460 # Choose
461 my $idx_fc = $tc->choose(
462 $menu,
463 { %{$sf->{i}{lyt_v}}, info => $info, index => 1, default => $old_idx_fc, prompt => 'Frame clause:', undef => ' <=' }
464 );
465 $ax->print_sql_info( $info );
466 if ( ! defined $idx_fc || ! defined $menu->[$idx_fc] ) {
467 return;
468 }
469 if ( $sf->{o}{G}{menu_memory} ) {
470 if ( $old_idx_fc == $idx_fc && ! $ENV{TC_RESET_AUTO_UP} ) {
471 $old_idx_fc = 0;
472 next FRAME_CLAUSE;
473 }
474 $old_idx_fc = $idx_fc;
475 }
476 my $frame_mode = $frame_clause_modes[$idx_fc-@pre];
477 $win_data->{'frame_mode'} = $frame_mode;
478 my $old_idx_fe = 0;
479  
480 FRAME_END_AND_EXCLUSION: while ( 1 ) {
481 my $confirm = $sf->{i}{confirm};
482 my $back = $sf->{i}{back};
483 my @pre = ( undef, $confirm );
484 my ( $frame_start, $frame_end, $frame_exclusion ) = ( '- Add Frame start', '- Add Frame end', '- Add Frame exclusion' );
485 my $menu = [ @pre, $frame_start, $frame_end ];
486 if ( $dbms =~ /^(?:SQLite|Pg|DuckDB|Oracle)\z/ ) {
487 push @$menu, $frame_exclusion;
488 }
489 $r_data->[-1] = [ 'win', $sf->__get_win_func_stmt( $win_data, 'frame_mode' ) ];
490 my $info = $info_sql . $ext->nested_func_info( $r_data );
491 # Choose
492 my $idx_fe = $tc->choose(
493 $menu,
494 { %{$sf->{i}{lyt_v}}, info => $info, index => 1, default => $old_idx_fe, prompt => 'Frame clause:', undef => $back }
495 );
496 $ax->print_sql_info( $info );
497 if ( ! defined $idx_fe || ! defined $menu->[$idx_fe] ) {
498 if ( $win_data->{'frame_start'} || $win_data->{'frame_end'} || $win_data->{'frame_exclusion'} ) {
499 delete @{$win_data}{qw(frame_start frame_end frame_exclusion)};
500 next FRAME_END_AND_EXCLUSION;
501 }
502 delete $win_data->{'frame_mode'};
503 next FRAME_CLAUSE;
504 }
505 if ( $sf->{o}{G}{menu_memory} ) {
506 if ( $old_idx_fe == $idx_fe && ! $ENV{TC_RESET_AUTO_UP} ) {
507 $old_idx_fe = 0;
508 next FRAME_END_AND_EXCLUSION;
509 }
510 $old_idx_fe = $idx_fe;
511 }
512 my $choice = $menu->[$idx_fe];
513 if ( $choice eq $confirm ) {
514 return 1;
515 }
516 if ( $choice eq $frame_start ) {
517 $sf->__add_frame_start_or_end( $sql, $clause, $r_data, $win_data, 'frame_start' );
518 }
519 elsif ( $choice eq $frame_end ) {
520 $sf->__add_frame_start_or_end( $sql, $clause, $r_data, $win_data, 'frame_end' );
521 }
522 else {
523 $sf->__add_frame_exclusion( $sql, $r_data, $win_data );
524 }
525 }
526 }
527 }
528  
529  
530 sub __add_frame_start_or_end {
531 my ( $sf, $sql, $clause, $r_data, $win_data, $pos ) = @_;
532 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
533 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
534 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
535 my $dbms = $sf->{i}{dbms};
536 my ( @frame_point_types, $prompt );
537 if ( $pos eq 'frame_start' ) {
538 if ( $win_data->{'frame_mode'} eq 'RANGE' && $dbms eq 'MSSQL' ) {
539 @frame_point_types = ( 'UNBOUNDED PRECEDING', 'CURRENT ROW' );
540 }
541 else {
542 @frame_point_types = ( 'UNBOUNDED PRECEDING', 'n PRECEDING', 'CURRENT ROW', 'n FOLLOWING' );
543 }
544 $prompt = 'Frame start:';
545 }
546 elsif ( $pos eq 'frame_end' ) {
547 if ( $win_data->{'frame_mode'} eq 'RANGE' && $dbms eq 'MSSQL' ) {
548 @frame_point_types = ( 'CURRENT ROW', 'UNBOUNDED FOLLOWING' );
549 }
550 else {
551 @frame_point_types = ( 'n PRECEDING', 'CURRENT ROW', 'n FOLLOWING', 'UNBOUNDED FOLLOWING' );
552 }
553 $prompt = 'Frame end:';
554 }
555 my $info_sql = $ax->get_sql_info( $sql );
556  
557 FRAME_START: while ( 1 ) {
558 if ( ! length $win_data->{$pos} ) {
559 $r_data->[-1] = [ 'win', $sf->__get_win_func_stmt( $win_data, $pos ) ];
560 }
561 else {
562 $r_data->[-1] = [ 'win', $sf->__get_win_func_stmt( $win_data ) ];
563 }
564 my $info = $info_sql . $ext->nested_func_info( $r_data );
565 my $confirm = $sf->{i}{ok};
566 my @pre = ( undef, $confirm );
567 my $menu = [ @pre, map( '- ' . $_, @frame_point_types ) ];
568 # Choose
569 my $point = $tc->choose(
570 $menu,
571 { %{$sf->{i}{lyt_v}}, info => $info, prompt => $prompt, undef => '<=' }
572 );
573 $ax->print_sql_info( $info );
574 if ( ! defined $point ) {
575 delete $win_data->{$pos};
576 return;
577 }
578 elsif ( $point eq $confirm ) {
579 return 1;
580 }
581 else {
582 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
583 $point =~ s/-\s//;
584 $win_data->{$pos} = $point;
585 $r_data->[-1] = [ 'win', $sf->__get_win_func_stmt( $win_data ) ];
586 if ( $point =~ /^n / ) {
587 my $offset = $ext->argument(
588 $sql, $clause, $r_data,
589 { history => undef, prompt => 'n = ', is_numeric => 1 }
590 );
591 if ( ! length $offset || $offset eq "''" ) {
592 delete $win_data->{$pos};
593 next FRAME_START;
594 }
595 $point =~ s/^n/$offset/;
596 $win_data->{$pos} = $point;
597 }
598 }
599 }
600 }
601  
602  
603 sub __add_frame_exclusion {
604 my ( $sf, $sql, $r_data, $win_data ) = @_;
605 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
606 my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} );
607 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
608 my @exclusion_types = ( 'EXCLUDE CURRENT ROW', 'EXCLUDE GROUP', 'EXCLUDE TIES', 'EXCLUDE NO OTHERS' );
609 my $info_sql = $ax->get_sql_info( $sql );
610  
611 FRAME_EXCLUSION: while ( 1 ) {
612 if ( ! length $win_data->{'frame_exclusion'} ) {
613 $r_data->[-1] = [ 'win', $sf->__get_win_func_stmt( $win_data, 'frame_exclusion' ) ];
614 }
615 else {
616 $r_data->[-1] = [ 'win', $sf->__get_win_func_stmt( $win_data ) ];
617 }
618 my $info = $info_sql . $ext->nested_func_info( $r_data );
619 my $confirm = $sf->{i}{ok};
620 my @pre = ( undef, $confirm );
621 my $menu = [ @pre, map( '- ' . $_, @exclusion_types ) ];
622 # Choose
623 my $frame_exclusion = $tc->choose(
624 $menu,
625 { %{$sf->{i}{lyt_v}}, info => $info, prompt => 'Frame exclusion:', undef => '<=' }
626 );
627 $ax->print_sql_info( $info );
628 if ( ! defined $frame_exclusion ) {
629 delete $win_data->{'frame_exclusion'};
630 return;
631 }
632 elsif ( $frame_exclusion eq $confirm ) {
633 return 1;
634 }
635 else {
636 $frame_exclusion =~ s/^-\s//;
637 $win_data->{'frame_exclusion'} = $frame_exclusion;
638 }
639 }
640 }
641  
642  
643  
644 1;
645  
646 __END__