DBBrowser/GetContent/Filter.pm untested

filter: Statementstmt 0.0% 0 / 734 Branchbran 0.0% 0 / 178 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 44 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 1010 CC 90 SCAR 52.7
CC
90
Cov
0%
input_filter
812.0
__join_columns
90.0
__range_of_rows
72.0
CRAP 194.7
1 package # hide from PAUSE
2 App::DBBrowser::GetContent::Filter;
3  
4 use warnings;
5 use strict;
6 use 5.016;
7  
8 use List::MoreUtils qw( any uniq );
9  
10 use Term::Choose qw();
11 use Term::Choose::Constants qw( EXTRA_W );
12 use Term::Choose::LineFold qw( line_fold print_columns );
13 use Term::Choose::Util qw( insert_sep get_term_width get_term_height unicode_sprintf );
14 use Term::Choose::Screen qw( clear_screen );
15 use Term::Form qw();
16 use Term::Form::ReadLine qw();
17  
18 use App::DBBrowser::Auxil;
19  
20  
21 sub new {
22 my ( $class, $info, $options, $d ) = @_;
23 my $sf = {
24 i => $info,
25 o => $options,
26 d => $d
27 };
28 bless $sf, $class;
29 }
30  
31  
32 sub input_filter {
33 my ( $sf, $sql ) = @_;
34 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
35 my $confirm = ' OK';
36 my $back = ' ' . $sf->{i}{s_back};
37 my $reset = ' ' . $sf->{i}{reset};
38 my $reparse = ' Reparse';
39 my $merge_rows = 'Merge_Rows';
40 my $range_rows = 'Range_Rows';
41 my $row_groups = 'Row_Groups';
42 my $empty_rows = 'Empty_Rows';
43 my $choose_rows = 'Choose_Rows';
44 my $remove_cell = 'Remove_Cell';
45 my $insert_cell = 'Insert_Cell';
46 my $split_table = 'Split_Table';
47 my $split_column = 'Split_Column';
48 my $join_columns = 'Join_Columns';
49 my $fill_up_rows = 'Fill_up_Rows';
50 my $empty_to_null = ' Empty_2_null';
51 my $empty_cols = 'Empty_Columns';
52 my $choose_cols = 'Choose_Columns';
53 my $append_col = 'Append_Columns';
54 my $cols_to_rows = 'Columns_to_Rows';
55 my $convert_date = 'Convert_DateTime';
56 my $s_and_replace = 'Search_&_Replace';
57 my $field_count = @{$sql->{insert_args}} * @{$sql->{insert_args}[0]};
58 my $bu_insert_args = [ map { [ @$_ ] } @{$sql->{insert_args}} ]; # copy the entire data
59 $sf->{empty_to_null} = $sf->{o}{insert}{empty_to_null_file};
60 my $working = $field_count > 500_000 ? 'Working ... ' : undef;
61 my $old_idx = 0;
62  
63 FILTER: while ( 1 ) {
64 my $skip = ' ';
65 my $menu = [
66 undef, $choose_rows, $range_rows, $row_groups,
67 $confirm, $choose_cols, $empty_rows, $empty_cols,
68 $reset, $s_and_replace, $convert_date, $skip,
69 $reparse, $remove_cell, $insert_cell, $skip,
70 $empty_to_null, $join_columns, $split_column, $append_col,
71 $cols_to_rows, $split_table, $merge_rows, $fill_up_rows,
72 ];
73 my $max_cols = 4;
74 my $info = $sf->__get_filter_info( $sql );
75 # Choose
76 my $idx = $tc->choose(
77 $menu,
78 { info => $info, prompt => 'Filter:', layout => 0, order => 0, max_cols => $max_cols, index => 1,
79 default => $old_idx, undef => $back, busy_string => $working }
80 );
81 $sf->__print_busy_string( $working );
82 if ( ! $idx ) {
83 $sql->{insert_args} = [];
84 delete $sf->{d}{fi};
85 return;
86 }
87 if ( $sf->{o}{G}{menu_memory} ) {
88 if ( $old_idx == $idx && ! $ENV{TC_RESET_AUTO_UP} ) {
89 $old_idx = 0;
90 next FILTER;
91 }
92 $old_idx = $idx;
93 }
94 my $filter = $menu->[$idx];
95 my $filter_str = sprintf( "Filter: %s", $filter );
96 if ( $filter eq $reset ) {
97 $sf->__print_busy_string( $working );
98 $sql->{insert_args} = [ map { [ @$_ ] } @{$bu_insert_args} ];
99 $sf->{empty_to_null} = $sf->{o}{insert}{empty_to_null_file};
100 next FILTER
101 }
102 elsif ( $filter eq $confirm ) {
103 if ( $sf->{empty_to_null} ) {
104 no warnings 'uninitialized';
105 $sql->{insert_args} = [ map { [ map { length $_ ? $_ : undef } @$_ ] } @{$sql->{insert_args}} ];
106 }
107 return 1;
108 }
109 elsif ( $filter eq $reparse ) {
110 return -1;
111 }
112 elsif ( $filter eq $choose_cols ) {
113 $sf->__choose_columns( $sql, $filter_str, $working );
114 }
115 elsif ( $filter eq $choose_rows ) {
116 $sf->__choose_rows( $sql, $filter_str, $working );
117 }
118 elsif ( $filter eq $range_rows ) {
119 $sf->__range_of_rows( $sql, $filter_str, $working );
120 }
121 elsif ( $filter eq $row_groups ) {
122 $sf->__row_groups( $sql, $filter_str, $working );
123 }
124 elsif ( $filter eq $empty_rows ) {
125 $sf->__remove_empty_rows( $sql, $filter_str, $working );
126 }
127 elsif ( $filter eq $empty_cols ) {
128 $sf->__remove_empty_columns( $sql, $filter_str, $working );
129 }
130 elsif ( $filter eq $remove_cell ) {
131 $sf->__remove_cell( $sql, $filter_str, $working );
132 }
133 elsif ( $filter eq $insert_cell ) {
134 $sf->__insert_cell( $sql, $filter_str, $working );
135 }
136 elsif ( $filter eq $append_col ) {
137 $sf->__append_col( $sql, $filter_str, $working );
138 }
139 elsif ( $filter eq $split_column ) {
140 $sf->__split_column( $sql, $filter_str, $working );
141 }
142 elsif ( $filter eq $convert_date ) {
143 require App::DBBrowser::GetContent::Filter::ConvertDate;
144 my $sr = App::DBBrowser::GetContent::Filter::ConvertDate->new( $sf->{i}, $sf->{o}, $sf->{d} );
145 $sr->convert_date( $sql, $bu_insert_args, $filter_str );
146 }
147 elsif ( $filter eq $s_and_replace ) {
148 require App::DBBrowser::GetContent::Filter::SearchAndReplace;
149 my $sr = App::DBBrowser::GetContent::Filter::SearchAndReplace->new( $sf->{i}, $sf->{o}, $sf->{d} );
150 $sr->search_and_replace( $sql, $bu_insert_args, $filter_str );
151 }
152 elsif ( $filter eq $split_table ) {
153 $sf->__split_table( $sql, $filter_str, $working );
154 }
155 elsif ( $filter eq $merge_rows ) {
156 $sf->__merge_rows( $sql, $filter_str, $working );
157 }
158 elsif ( $filter eq $join_columns ) {
159 $sf->__join_columns( $sql, $filter_str, $working );
160 }
161 elsif ( $filter eq $fill_up_rows ) {
162 $sf->__fill_up_rows( $sql, $filter_str, $working );
163 }
164 elsif ( $filter eq $cols_to_rows ) {
165 $sf->__transpose_rows_to_cols( $sql, $filter_str, $working );
166 }
167 elsif ( $filter eq $empty_to_null ) {
168 $sf->__empty_to_null( $sql );
169 }
170 }
171 }
172  
173  
174 sub __print_busy_string {
175 my ( $sf, $working ) = @_;
176 if ( $working ) {
177 print clear_screen();
178 print $working . "\r";
179 }
180 }
181  
182  
183 sub __get_filter_info {
184 my ( $sf, $sql, $filter_str ) = @_;
185 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
186 my $indent = '';
187 my $bu_stmt_types = [ @{$sf->{d}{stmt_types}} ];
188 $sf->{d}{stmt_types} = [];
189 my $rows = $ax->info_format_insert_args( $sql, $indent );
190 $sf->{d}{stmt_types} = $bu_stmt_types;
191 my $info = join( "\n", 'DATA:', @$rows, '' ); # '' == empty line after insert_args
192 if ( defined $filter_str ) {
193 $info .= "\n" . $filter_str;
194 }
195 return $info;
196 }
197  
198  
199 sub __choose_columns {
200 my ( $sf, $sql, $filter_str, $working ) = @_;
201 my $tu = Term::Choose::Util->new( $sf->{i}{tcu_default} );
202 my $aoa = $sql->{insert_args};
203 my $is_empty = $sf->__search_empty_cols( $aoa );
204 my $header = $sf->__prepare_header( $aoa, $is_empty );
205 my $info = $sf->__get_filter_info( $sql, $filter_str );
206 # Choose
207 my $col_idx = $tu->choose_a_subset(
208 $header,
209 { cs_label => 'Cols: ', layout => 0, order => 0, all_by_default => 1, index => 1,
210 info => $info, keep_chosen => 1, busy_string => $working }
211 );
212 $sf->__print_busy_string( $working );
213 if ( ! defined $col_idx ) {
214 return;
215 }
216 $sql->{insert_args} = [ map { [ @{$_}[@$col_idx] ] } @$aoa ];
217 return 1;
218 }
219  
220  
221 sub __remove_empty_columns {
222 my ( $sf, $sql, $filter_str, $working ) = @_;
223 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
224 my $remove_completely_empty_cols = '- Remove completely empty columns';
225 my $remove_cols_with_no_data = '- Remove columns without data';
226 my $menu = [ undef, $remove_completely_empty_cols, $remove_cols_with_no_data ];
227 my $info = $sf->__get_filter_info( $sql, $filter_str );
228 my $choice = $tc->choose(
229 $menu,
230 { info => $info, layout => 2, busy_string => $working }
231 );
232 $sf->__print_busy_string( $working );
233 if ( ! $choice ) {
234 return;
235 }
236 else {
237 my $aoa = $sql->{insert_args};
238 my $col_count = @{$aoa->[0]};
239 my $cols_with_data = [];
240 my $cols_with_header = [];
241 my $is_empty = $sf->__search_empty_cols( $aoa, 1 );
242  
243 for my $col_idx ( 0 .. $col_count - 1 ) {
244 if ( ! $is_empty->[$col_idx] ) {
245 push @$cols_with_data, $col_idx;
246 }
247 if ( length $aoa->[0][$col_idx] ) {
248 push @$cols_with_header, $col_idx;
249 }
250 }
251 my $non_empty_cols = [];
252 if ( $choice eq $remove_completely_empty_cols ) {
253 $non_empty_cols = [ uniq @$cols_with_header, @$cols_with_data ];
254 }
255 else {
256 $non_empty_cols = $cols_with_data;
257  
258 }
259 if ( @$non_empty_cols != $col_count ) {
260 $sql->{insert_args} = [ map { [ @{$_}[@$non_empty_cols] ] } @$aoa ];
261 }
262 return 1;
263 }
264 }
265  
266 sub __remove_empty_rows {
267 my ( $sf, $sql, $filter_str, $working ) = @_;
268 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
269 my $remove_completely_empty_rows = '- Remove completely empty rows';
270 my $remove_all_fields_empty_rows = '- Remove rows with all fields empty or undefined';
271 my $menu = [ undef, $remove_completely_empty_rows, $remove_all_fields_empty_rows ];
272 my $info = $sf->__get_filter_info( $sql, $filter_str );
273 my $choice = $tc->choose(
274 $menu,
275 { info => $info, layout => 2, busy_string => $working }
276 );
277 $sf->__print_busy_string( $working );
278 if ( ! $choice ) {
279 return;
280 }
281 else {
282 my $aoa = $sql->{insert_args};
283 my $tmp = [];
284  
285 ROW: for my $row ( @$aoa ) {
286 if ( $choice eq $remove_completely_empty_rows && @$row > 1 ) {
287 push @$tmp, $row;
288 next ROW;
289 }
290 for my $col ( @$row ) {
291 if ( length $col ) {
292 push @$tmp, $row;
293 next ROW;
294 }
295 }
296 }
297 $sql->{insert_args} = $tmp;
298 }
299 return 1;
300 }
301  
302  
303 sub __choose_rows {
304 my ( $sf, $sql, $filter_str, $working ) = @_;
305 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
306 my $aoa = $sql->{insert_args};
307 my @pre = ( undef, $sf->{i}{ok} );
308 my $stringified_rows = [];
309 $sf->__print_busy_string( $working );
310 {
311 no warnings 'uninitialized';
312 for my $i ( 0 .. $#$aoa ) {
313 push @$stringified_rows, join ',', @{$aoa->[$i]};
314 }
315 }
316 my $prompt = 'Choose rows:';
317 $sql->{insert_args} = []; # $sql->{insert_args} refers to a new empty array - this doesn't delete $aoa
318  
319 while ( 1 ) {
320 my $info = $sf->__get_filter_info( $sql, $filter_str );
321 # Choose
322 my @idx = $tc->choose(
323 [ @pre, @$stringified_rows ],
324 { %{$sf->{i}{lyt_v}}, prompt => $prompt, info => $info, meta_items => [ 0 .. $#pre ],
325 include_highlighted => 2, index => 1, undef => $sf->{i}{s_back}, busy_string => $working }
326 );
327 $sf->__print_busy_string( $working );
328 if ( ! $idx[0] ) {
329 $sql->{insert_args} = $aoa;
330 return;
331 }
332 if ( $idx[0] == $#pre ) {
333 shift @idx;
334 for my $i ( @idx ) {
335 my $idx = $i - @pre;
336 push @{$sql->{insert_args}}, $aoa->[$idx];
337 }
338 if ( ! @{$sql->{insert_args}} ) {
339 $sql->{insert_args} = $aoa;
340 }
341 return 1;
342 }
343 for my $i ( @idx ) {
344 my $idx = $i - @pre;
345 push @{$sql->{insert_args}}, $aoa->[$idx];
346 }
347 }
348 }
349  
350  
351 sub __range_of_rows {
352 my ( $sf, $sql, $filter_str, $working ) = @_;
353 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
354 my $aoa = $sql->{insert_args};
355 $sql->{insert_args} = []; # temporarily for the info output
356 my @last_indexes;
357 my ( $back, $confirm, $add_range ) = ( $sf->{i}{_back}, $sf->{i}{_confirm}, '- Add Range' );
358  
359 while ( 1 ) {
360 if ( @{$sql->{insert_args}} ) {
361 my $info = $sf->__get_filter_info( $sql, $filter_str );
362 # Choose
363 my $choice = $tc->choose(
364 [ undef, $confirm, $add_range ],
365 { %{$sf->{i}{lyt_v}}, info => $info, undef => $back }
366 );
367 $sf->__print_busy_string( $working );
368 if ( ! $choice ) {
369 if ( @last_indexes ) {
370 my $li = pop @last_indexes;
371 $#{$sql->{insert_args}} = $li;
372 next;
373 }
374 $sql->{insert_args} = $aoa;
375 return;
376 }
377 if ( $choice eq $confirm ) {
378 return 1;
379 }
380 }
381 my $info = $sf->__get_filter_info( $sql, $filter_str );
382 my $prompt = "Begin range:";
383 # Stop
384 my $idx_first_row = $sf->__choose_a_row_idx( $aoa, $info, $prompt, $working );
385 if ( ! defined $idx_first_row ) {
386 if ( @{$sql->{insert_args}} ) {
387 next;
388 }
389 $sql->{insert_args} = $aoa;
390 return;
391 }
392 push @{$sql->{insert_args}}, $aoa->[$idx_first_row]; # temporarily for the info output
393 $info = $sf->__get_filter_info( $sql, $filter_str );
394 pop @{$sql->{insert_args}};
395 $prompt = "End range:";
396 # Stop
397 my $idx_last_row = $sf->__choose_a_row_idx( [ @{$aoa}[$idx_first_row .. $#$aoa] ], $info, $prompt, $working );
398 if ( defined $idx_last_row ) {
399 push @last_indexes, $#{$sql->{insert_args}};
400 $idx_last_row += $idx_first_row;
401 push @{$sql->{insert_args}}, @{$aoa}[$idx_first_row .. $idx_last_row];
402 }
403 }
404 }
405  
406  
407 sub __row_groups {
408 my ( $sf, $sql, $filter_str, $working ) = @_;
409 my $tu = Term::Choose::Util->new( $sf->{i}{tcu_default} );
410 my $aoa = $sql->{insert_args};
411 my %group; # group rows by the number of cols
412 for my $row_idx ( 0 .. $#$aoa ) {
413 my $col_count = scalar @{$aoa->[$row_idx]};
414 push @{$group{$col_count}}, $row_idx;
415 }
416 # sort keys by group size
417 my @keys_sorted = sort { scalar( @{$group{$b}} ) <=> scalar( @{$group{$a}} ) } keys %group;
418 my @choices_groups;
419 my $len = length insert_sep( scalar @{$group{$keys_sorted[0]}}, $sf->{i}{info_thsd_sep} );
420 for my $col_count ( @keys_sorted ) {
421 my $row_count = scalar @{$group{$col_count}};
422 my $row_str = $row_count == 1 ? 'row has ' : 'rows have';
423 my $col_str = $col_count == 1 ? 'column ' : 'columns';
424 push @choices_groups, sprintf ' %*s %s %2d %s',
425 $len, insert_sep( $row_count, $sf->{i}{info_thsd_sep} ), $row_str,
426 $col_count, $col_str;
427 }
428 my $prompt = 'Choose group:';
429 my $info = $sf->__get_filter_info( $sql, $filter_str );
430 # Choose
431 my $idxs = $tu->choose_a_subset(
432 \@choices_groups,
433 { info => $info, prompt => $prompt, layout => 2, index => 1, all_by_default => 1,
434 cs_label => "Chosen groups:\n", cs_separator => "\n", cs_end => "\n", busy_string => $working }
435 );
436 $sf->__print_busy_string( $working );
437 if ( ! defined $idxs ) {
438 return;
439 }
440 else {
441 my $row_idxs = [];
442 for my $idx ( @$idxs ) {
443 push @$row_idxs, @{$group{ $keys_sorted[$idx] }};
444 }
445 $sql->{insert_args} = [ @{$aoa}[sort { $a <=> $b } @$row_idxs] ];
446 return;
447 }
448 }
449  
450  
451 sub __remove_cell {
452 my ( $sf, $sql, $filter_str, $working ) = @_;
453 my $aoa = $sql->{insert_args};
454  
455 while ( 1 ) {
456 my $prompt = "Choose row:";
457 my $info = $filter_str;
458 # Stop
459 my $row_idx = $sf->__choose_a_row_idx( $aoa, $info, $prompt, $working );
460 if ( ! defined $row_idx ) {
461 return;
462 }
463 $prompt = "Choose cell:";
464 $info = $sf->__get_filter_info( $sql, $filter_str );
465 # Stop
466 my $col_idx = $sf->__choose_a_column_idx( $aoa->[$row_idx], $info, $prompt );
467 if ( ! defined $col_idx ) {
468 next;
469 }
470 splice( @{$aoa->[$row_idx]}, $col_idx, 1 );
471 $sql->{insert_args} = $aoa;
472 return;
473 }
474 }
475  
476 sub _stringify_row { # used only once
477 my ( $row ) = @_;
478 no warnings 'uninitialized';
479 my $stringified_row = '"' . join( '", "', @$row ) . '"';
480 return $stringified_row;
481 }
482  
483  
484 sub __insert_cell {
485 my ( $sf, $sql, $filter_str, $working ) = @_;
486 my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} );
487 my $aoa = $sql->{insert_args};
488  
489 while ( 1 ) {
490 my $prompt = "Choose row:";
491 my $info = $filter_str;
492 # Stop
493 my $row_idx = $sf->__choose_a_row_idx( $aoa, $info, $prompt, $working );
494 if ( ! defined $row_idx ) {
495 return;
496 }
497 my $cols = [ @{$aoa->[$row_idx]}, 'END_of_Row' ];
498 $prompt = "Insert cell before:";
499 $info = $sf->__get_filter_info( $sql, $filter_str );
500 # Stop
501 my $col_idx = $sf->__choose_a_column_idx( $cols, $info, $prompt );
502 if ( ! defined $col_idx ) {
503 next;
504 }
505 my @row = @{$aoa->[$row_idx]};
506 splice( @row, $col_idx, 0, '<*>' );
507 my $str_row_with_placeholder = _stringify_row( \@row );
508 $str_row_with_placeholder =~ s/"<\*>"/<*>/;
509 my $label = 'Row: ';
510 my @tmp_info = ( $filter_str );
511 push @tmp_info, line_fold(
512 $label . $str_row_with_placeholder, { subseq_tab => ' ' x length $label, join => 0 }
513 );
514 $prompt = "<*>: ";
515 $info = $sf->__get_filter_info( $sql, join( "\n", @tmp_info ) );
516 # Readline
517 my $cell = $tr->readline(
518 $prompt,
519 { info => $info, history => [] }
520 );
521 $sf->__print_busy_string( $working );
522 splice( @{$aoa->[$row_idx]}, $col_idx, 0, $cell );
523 $sql->{insert_args} = $aoa;
524 return;
525 }
526 }
527  
528  
529 sub __fill_up_rows {
530 my ( $sf, $sql, $filter_str, $working ) = @_;
531 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
532 my $aoa = $sql->{insert_args};
533 my $menu = [ undef, '- YES' ];
534 my $prompt = 'Fill up shorter rows?';
535 my $info = $sf->__get_filter_info( $sql, $filter_str );
536 # Choose
537 my $ok = $tc->choose(
538 $menu,
539 { info => $info, prompt => $prompt, index => 1, undef => '- NO', layout => 2 }
540 );
541 $sf->__print_busy_string( $working );
542 if ( ! $ok ) {
543 return;
544 }
545 my $longest_row = 0;
546 for my $row ( @$aoa ) {
547 my $col_count = scalar @$row;
548 if ( $col_count > $longest_row ) {
549 $longest_row = $col_count;
550 }
551 }
552 my $last_idx = $longest_row - 1;
553 for my $row ( @$aoa ) {
554 $#$row = $last_idx;
555 }
556 $sql->{insert_args} = $aoa;
557 return;
558 }
559  
560  
561 sub __append_col {
562 my ( $sf, $sql, $filter_str, $working ) = @_;
563 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
564 my $aoa = $sql->{insert_args};
565 my $menu = [ undef, '- YES' ];
566 my $prompt = 'Append an empty column?';
567 my $info = $sf->__get_filter_info( $sql, $filter_str );
568 # Choose
569 my $ok = $tc->choose(
570 $menu,
571 { info => $info, prompt => $prompt, index => 1, undef => '- NO', layout => 2 }
572 );
573 $sf->__print_busy_string( $working );
574 if ( $ok ) {
575 my $new_last_idx = $#{$aoa->[0]} + 1;
576 for my $row ( @$aoa ) {
577 $#$row = $new_last_idx;
578 }
579 $sql->{insert_args} = $aoa;
580 }
581 return;
582 }
583  
584 sub __split_column {
585 my ( $sf, $sql, $filter_str, $working ) = @_;
586 my $tf = Term::Form->new( $sf->{i}{tf_default} );
587 my $aoa = $sql->{insert_args};
588 my $is_empty = $sf->__search_empty_cols( $aoa );
589 my $header = $sf->__prepare_header( $aoa, $is_empty );
590 my $prompt = 'Choose column:';
591 my $info = $sf->__get_filter_info( $sql, $filter_str );
592 # Stop
593 my $idx = $sf->__choose_a_column_idx( $header, $info, $prompt );
594 if ( ! defined $idx ) {
595 return;
596 }
597 my $fields = [
598 [ 'Pattern', ],
599 [ 'Limit', ],
600 [ 'Left trim', '\s+' ],
601 [ 'Right trim', '\s+' ]
602 ];
603 my $back = $sf->{i}{back} . ' ' x 3;
604 $prompt = "Split column \"$header->[$idx]\"";
605 $info = $sf->__get_filter_info( $sql, $filter_str );
606 # Fill_form
607 my $form = $tf->fill_form(
608 $fields,
609 { info => $info, prompt => $prompt, confirm => $sf->{i}{confirm}, back => $back }
610 );
611 $sf->__print_busy_string( $working );
612 if ( ! $form ) {
613 return;
614 }
615 my ( $pattern, $limit, $left_trim, $right_trim ) = map { $_->[1] } @$form;
616 $pattern //= '';
617  
618 for my $row ( @$aoa ) { # modifies $aoa
619 my $col = splice @$row, $idx, 1;
620 my @split_col;
621 if ( length $limit ) {
622 @split_col = split /$pattern/, $col, $limit;
623 }
624 else {
625 @split_col = split /$pattern/, $col;
626 }
627 for ( @split_col ) {
628 $_ =~ s/^$left_trim// if length $left_trim;
629 $_ =~ s/$right_trim\z// if length $right_trim;
630 }
631 splice @$row, $idx, 0, @split_col;
632 }
633 $sql->{insert_args} = $aoa;
634 }
635  
636  
637 sub __split_table {
638 my ( $sf, $sql, $filter_str, $working ) = @_;
639 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
640 my $tu = Term::Choose::Util->new( $sf->{i}{tcu_default} );
641 my $aoa = $sql->{insert_args};
642 my $digits = length( scalar @{$aoa->[0]} );
643 my $col_count;
644  
645 CHOOSE_A_NUMBER: while( 1 ) {
646 my $info = $sf->__get_filter_info( $sql, $filter_str );
647 # Choose
648 $col_count = $tu->choose_a_number(
649 $digits,
650 { info => $info, cs_label => 'Number columns new table: ', small_first => 1,
651 confirm => $sf->{i}{confirm}, back => $sf->{i}{back} }
652 );
653 $sf->__print_busy_string( $working );
654 if ( ! $col_count ) {
655 return;
656 }
657 if ( @{$aoa->[0]} < $col_count ) {
658 my $prompt = sprintf 'Chosen number(%d) bigger than the available columns(%d)!', $col_count, scalar( @{$aoa->[0]} );
659 my $info = $sf->__get_filter_info( $sql, $filter_str );
660 $tc->choose(
661 [ 'Continue with ENTER' ],
662 { info => $info, prompt => $prompt }
663 );
664 $sf->__print_busy_string( $working );
665 next CHOOSE_A_NUMBER;
666 }
667 if ( @{$aoa->[0]} % $col_count ) {
668 my $prompt = sprintf 'The number of available columns(%d) cannot be divided by the selected number(%d) without remainder!', scalar( @{$aoa->[0]} ), $col_count;
669 my $info = $sf->__get_filter_info( $sql, $filter_str );
670 $tc->choose(
671 [ 'Continue with ENTER' ],
672 { info => $info, prompt => $prompt }
673 );
674 $sf->__print_busy_string( $working );
675 next CHOOSE_A_NUMBER;
676 }
677 last CHOOSE_A_NUMBER;
678 }
679 my $begin = 0;
680 my $end = $col_count - 1;
681 my $tmp = [];
682  
683 while ( 1 ) {
684 for my $row ( @$aoa ) {
685 push @$tmp, [ @{$row}[ $begin .. $end ] ];
686 }
687 $begin = $end + 1;
688 if ( $begin > $#{$aoa->[0]} ) {
689 last;
690 }
691 $end = $end + $col_count;
692 }
693 $sql->{insert_args} = $tmp;
694 }
695  
696  
697 sub __merge_rows {
698 my ( $sf, $sql, $filter_str, $working ) = @_;
699 my $tu = Term::Choose::Util->new( $sf->{i}{tcu_default} );
700 my $tf = Term::Form->new( $sf->{i}{tf_default} );
701 my $aoa = $sql->{insert_args};
702 my $term_w = get_term_width() + EXTRA_W;
703 my $stringified_rows;
704 {
705 no warnings 'uninitialized';
706 @$stringified_rows = map {
707 my $str_row = join( ',', @$_ );
708 if ( print_columns( $str_row ) > $term_w ) {
709 $str_row = unicode_sprintf( $str_row, $term_w, { suffix_on_truncate => $sf->{i}{dots} } );
710 }
711 else {
712 $str_row;
713 }
714 } @$aoa;
715 }
716 my $prompt = 'Choose rows:';
717 my $info = $filter_str;
718 # Choose
719 my $chosen_idxs = $tu->choose_a_subset(
720 $stringified_rows,
721 { cs_separator => "\n", cs_end => "\n", layout => 2, order => 0, all_by_default => 0, prompt => $prompt,
722 index => 1, info => $info, busy_string => $working }
723 );
724 $sf->__print_busy_string( $working );
725 if ( ! defined $chosen_idxs || ! @$chosen_idxs ) {
726 return;
727 }
728 my $merged = [];
729 for my $col ( 0 .. $#{$aoa->[$chosen_idxs->[0]]} ) {
730 my @tmp;
731 for my $row ( @$chosen_idxs ) {
732 next if ! defined $aoa->[$row][$col];
733 next if $aoa->[$row][$col] =~ /^\s*\z/;
734 $aoa->[$row][$col] =~ s/^\s+|\s+\z//g;
735 push @tmp, $aoa->[$row][$col];
736 }
737 $merged->[$col] = join ' ', @tmp;
738 }
739 $prompt = @$chosen_idxs == 1 ? 'Edit row cells:' : 'Edit cells of merged rows:';
740 my $col_number = 0;
741 my $fields = [ map { [ ++$col_number, defined $_ ? "$_" : '' ] } @$merged ];
742 $info = $sf->__get_filter_info( $sql, $filter_str );
743 # Fill_form
744 my $form = $tf->fill_form(
745 $fields,
746 { info => $info, prompt => $prompt, confirm => $sf->{i}{confirm},
747 back => $sf->{i}{back} . ' ' }
748 );
749 $sf->__print_busy_string( $working );
750 if ( ! $form ) {
751 return;
752 }
753 $merged = [ map { $_->[1] } @$form ];
754 my $first_idx = shift @$chosen_idxs;
755 $aoa->[$first_idx] = $merged; # modifies $aoa
756 for my $idx ( sort { $b <=> $a } @$chosen_idxs ) {
757 splice @$aoa, $idx, 1;
758 }
759 $sql->{insert_args} = $aoa;
760 return;
761 }
762  
763  
764 sub __join_columns {
765 my ( $sf, $sql, $filter_str, $working ) = @_;
766 my $tu = Term::Choose::Util->new( $sf->{i}{tcu_default} );
767 my $tf = Term::Form->new( $sf->{i}{tf_default} );
768 my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} );
769 my $aoa = $sql->{insert_args};
770 my $is_empty = $sf->__search_empty_cols( $aoa );
771 my $header = $sf->__prepare_header( $aoa, $is_empty );
772 my $info = $sf->__get_filter_info( $sql, $filter_str );
773 # Choose
774 my $chosen_idxs = $tu->choose_a_subset(
775 $header,
776 { cs_label => 'Cols: ', layout => 0, order => 0, index => 1, info => $info, busy_string => $working }
777 );
778 $sf->__print_busy_string( $working );
779 if ( ! defined $chosen_idxs || ! @$chosen_idxs ) {
780 return;
781 }
782 my $join_char = '';
783 my $prompt;
784 if ( @$chosen_idxs == 1 ) {
785 $prompt = 'Edit cells of ' . ( $aoa->[0][$chosen_idxs->[0]] // '--' ) . ':';
786 }
787 else {
788 $prompt = 'Edit cells of joined columns:';
789 my @tmp_info = ( $filter_str );
790 my $label = 'Cols: ';
791 push @tmp_info, line_fold(
792 $label . '"' . join( '", "', @{$header}[@$chosen_idxs] ) . '"',
793 { subseq_tab => ' ' x length $label, join => 0 }
794 );
795 $info = $sf->__get_filter_info( $sql, join( "\n", @tmp_info ) );
796 # Readline
797 $join_char = $tr->readline(
798 'Join-string: ',
799 { info => $info, history => [ '-', ' ', '_', ',', '/', '=', '+' ] }
800 );
801 $sf->__print_busy_string( $working );
802 if ( ! defined $join_char ) {
803 return;
804 }
805 }
806 my $merged = [];
807 for my $row ( 0 .. $#{$aoa} ) {
808 my @tmp;
809 for my $col ( @$chosen_idxs ) {
810 next if ! defined $aoa->[$row][$col];
811 next if $aoa->[$row][$col] =~ /^\s*\z/;
812 $aoa->[$row][$col] =~ s/^\s+|\s+\z//g;
813 push @tmp, $aoa->[$row][$col];
814 }
815 $merged->[$row] = join $join_char, @tmp;
816 }
817 my $col_number = 0;
818 my $fields = [ map { [ ++$col_number, defined $_ ? "$_" : '' ] } @$merged ];
819 $info = $filter_str;
820 # Fill_form
821 my $form = $tf->fill_form(
822 $fields,
823 { info => $info, prompt => $prompt, confirm => $sf->{i}{confirm},
824 back => $sf->{i}{back} . ' ' }
825 );
826 $sf->__print_busy_string( $working );
827 if ( ! $form ) {
828 $sql->{insert_args} = $aoa;
829 return;
830 }
831 $merged = [ map { $_->[1] } @$form ];
832 my $first_idx = shift @$chosen_idxs;
833 for my $row ( 0 .. $#{$aoa} ) { # modifies $aoa
834 $aoa->[$row][$first_idx] = $merged->[$row];
835 for my $idx ( sort { $b <=> $a } @$chosen_idxs ) {
836 splice @{$aoa->[$row]}, $idx, 1 if $idx < @{$aoa->[$row]};
837 }
838 }
839 $sql->{insert_args} = $aoa;
840 return;
841 }
842  
843  
844 sub __transpose_rows_to_cols {
845 my ( $sf, $sql, $filter_str, $working ) = @_;
846 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
847 my $aoa = $sql->{insert_args};
848 my $menu = [ undef, '- YES' ];
849 my $prompt = 'Transpose columns to rows?';
850 my $info = $sf->__get_filter_info( $sql, $filter_str );
851 my $ok = $tc->choose(
852 $menu,
853 { info => $info, prompt => $prompt, index => 1, undef => '- NO', layout => 2, busy_string => $working }
854 );
855 $sf->__print_busy_string( $working );
856 if ( $ok ) {
857 my $tmp_aoa = [];
858 for my $row ( 0 .. $#$aoa ) {
859 for my $col ( 0 .. $#{$aoa->[$row]} ) {
860 $tmp_aoa->[$col][$row] = $aoa->[$row][$col];
861 }
862 }
863 $sql->{insert_args} = $tmp_aoa;
864 }
865 return;
866 }
867  
868 sub __empty_to_null {
869 my ( $sf, $sql ) = @_;
870 my $tu = Term::Choose::Util->new( $sf->{i}{tcu_default} );
871 my $menu_elements = [
872 [ 'empty_to_null', " Empty fields to NULL", [ 'NO', 'YES' ] ]
873 ];
874 my $tmp = { empty_to_null => $sf->{empty_to_null} };
875 my $info = $sf->__get_filter_info( $sql );
876 $tu->settings_menu(
877 $menu_elements,
878 $tmp,
879 { info => $info, back => $sf->{i}{_back}, confirm => $sf->{i}{_confirm} }
880 );
881 $sf->{empty_to_null} = $tmp->{empty_to_null};
882 }
883  
884  
885 sub __search_empty_cols {
886 my ( $sf, $aoa, $first_row ) = @_;
887 $first_row //= 0;
888 my $is_empty ;
889  
890 COL: for my $col_idx ( 0 .. $#{$aoa->[0]} ) {
891 for my $row_idx ( $first_row .. $#$aoa ) {
892 if ( length $aoa->[$row_idx][$col_idx] ) {
893 $is_empty->[$col_idx] = 0;
894 next COL;
895 }
896 }
897 $is_empty->[$col_idx] = 1;
898 }
899 return $is_empty;
900 }
901  
902  
903 sub __prepare_header {
904 my ( $sf, $aoa, $is_empty ) = @_;
905 my $header = [];
906 for my $col_idx ( 0 .. $#{$aoa->[0]} ) {
907 if ( $is_empty->[$col_idx] ) {
908 $header->[$col_idx] = '--';
909 }
910 else {
911 if ( length $aoa->[0][$col_idx] ) {
912 $header->[$col_idx] = $aoa->[0][$col_idx];
913 }
914 else {
915 $header->[$col_idx] = 'tmp_' . ( $col_idx + 1 );
916 }
917 }
918 }
919 return $header;
920 }
921  
922  
923 sub __choose_a_column_idx {
924 my ( $sf, $columns, $info, $prompt ) = @_;
925 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
926 my @pre = ( undef );
927 # Choose
928 my $col_idx = $tc->choose(
929 [ @pre, map( defined $_ ? $_ : '', @$columns ) ],
930 { layout => 0, order => 0, index => 1, info => $info, prompt => $prompt, empty => '--' }
931 );
932 if ( ! $col_idx ) {
933 return;
934 }
935 return $col_idx - @pre;
936 }
937  
938  
939 sub __choose_a_row_idx {
940 my ( $sf, $aoa, $info, $prompt, $working ) = @_;
941 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
942 my @stringified_rows;
943 {
944 no warnings 'uninitialized';
945 @stringified_rows = map { join ',', @$_ } @$aoa;
946 }
947 my @pre = ( undef );
948 # Choose
949 my $row_idx = $tc->choose(
950 [ @pre, @stringified_rows ],
951 { layout => 2, index => 1, info => $info, prompt => $prompt, busy_string => $working }
952 );
953 $sf->__print_busy_string( $working );
954 if ( ! $row_idx ) {
955 return;
956 }
957 return $row_idx - @pre;
958 }
959  
960  
961  
962 1;
963  
964  
965 __END__