DBBrowser/GetContent.pm untested

filter: Statementstmt 0.0% 0 / 220 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 47 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 415 CC 52 SCAR 76.8
CC
52
Cov
0%
get_content
2756.0
new
2.0
BEGIN
2.0
CRAP 2171.8
1 package # hide from PAUSE
2 App::DBBrowser::GetContent;
3  
4 use warnings;
5 use strict;
6 use 5.016;
7  
8 use Encode qw( encode );
9 use File::Basename qw( basename );
10  
11 use Encode::Locale qw();
12  
13 use Term::Choose qw();
14  
15 use App::DBBrowser::GetContent::Filter;
16 use App::DBBrowser::GetContent::Parse;
17 use App::DBBrowser::GetContent::Source;
18 use App::DBBrowser::Options;
19 use App::DBBrowser::Options::ReadWrite;
20  
21 use open ':encoding(locale)';
22  
23  
24 sub new {
25 my ( $class, $info, $options, $d ) = @_;
26 my $sf = {
27 i => $info,
28 o => $options,
29 d => $d
30 };
31 bless $sf, $class;
32 }
33  
34  
35 sub get_content {
36 my ( $sf, $sql, $source, $goto_filter ) = @_;
37 my $cs = App::DBBrowser::GetContent::Source->new( $sf->{i}, $sf->{o}, $sf->{d} );
38 my $cp = App::DBBrowser::GetContent::Parse->new( $sf->{i}, $sf->{o}, $sf->{d} );
39 my $cf = App::DBBrowser::GetContent::Filter->new( $sf->{i}, $sf->{o}, $sf->{d} );
40 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
41 my @choices = (
42 [ 'plain', '- Plain' ],
43 [ 'file', '- From File' ],
44 );
45 my $data_source_choice_idx = $sf->{o}{insert}{'data_source_' . lc( $sf->{d}{stmt_types}[0]) };
46 $source->{old_idx_menu} //= 0;
47  
48 MENU: while ( 1 ) {
49 if ( $goto_filter ) {
50 # keep current source type
51 }
52 elsif ( $data_source_choice_idx =~ /^(?:0|1)\z/ ) {
53 $source->{source_type} = $choices[$data_source_choice_idx][0];
54 }
55 else {
56 my $prompt = 'Source type:';
57 my @pre = ( undef );
58 my $menu = [ @pre, map( $_->[1], @choices ) ];
59 # Choose
60 my $idx = $tc->choose(
61 $menu,
62 { %{$sf->{i}{lyt_v}}, prompt => $prompt, index => 1, default => $source->{old_idx_menu},
63 undef => ' <=' }
64 );
65 if ( ! defined $idx || ! defined $menu->[$idx] ) {
66 return;
67 }
68 if ( $sf->{o}{G}{menu_memory} ) {
69 if ( $source->{old_idx_menu} == $idx && ! $ENV{TC_RESET_AUTO_UP} ) {
70 $source->{old_idx_menu} = 0;
71 next MENU;
72 }
73 $source->{old_idx_menu} = $idx;
74 }
75 $source->{source_type} = $choices[$idx-@pre][0];
76 }
77 if ( $source->{source_type} eq 'plain' ) {
78 my $ok = $cs->from_col_by_col( $sql );
79 if ( ! $ok ) {
80 return if $data_source_choice_idx =~ /^(?:0|1)\z/;
81 $goto_filter = 0;
82 next MENU;
83 }
84 return 1;
85 }
86 $source->{old_idx_dir} //= 0;
87  
88 DIR: while ( 1 ) {
89 if ( $goto_filter ) {
90 # keep current source dir
91 }
92 elsif ( ! $sf->{o}{insert}{history_dirs} ) {
93 $source->{dir} = $cs->__new_search_dir();
94 if ( ! length $source->{dir} ) {
95 return if $data_source_choice_idx =~ /^(?:0|1)\z/;
96 next MENU;
97 }
98 }
99 elsif ( $sf->{o}{insert}{history_dirs} == 1 ) {
100 my $dirs = $cs->__avail_directories();
101 if ( ! @$dirs ) {
102 $source->{dir} = $cs->__new_search_dir();
103 if ( ! length $source->{dir} ) {
104 return if $data_source_choice_idx =~ /^(?:0|1)\z/;
105 next MENU;
106 }
107 }
108 else{
109 $source->{dir} = $dirs->[0];
110 }
111 }
112 else {
113 my $dirs = $cs->__avail_directories();
114 my $prompt = 'Choose a Dir:';
115 my $new_search = ' NEW search';
116 my @pre = ( undef, $new_search );
117 my $menu = [ @pre, map( '- ' . $_, @$dirs ) ];
118 # Choose
119 my $idx = $tc->choose(
120 $menu,
121 { %{$sf->{i}{lyt_v}}, prompt => $prompt, index => 1, default => $source->{old_idx_dir}, undef => ' <=' }
122 );
123 if ( ! defined $idx || ! defined $menu->[$idx] ) {
124 return if $data_source_choice_idx =~ /^(?:0|1)\z/;
125 next MENU;
126 }
127 if ( $sf->{o}{G}{menu_memory} ) {
128 if ( $source->{old_idx_dir} == $idx && ! $ENV{TC_RESET_AUTO_UP} ) {
129 $source->{old_idx_dir} = 0;
130 next DIR;
131 }
132 $source->{old_idx_dir} = $idx;
133 }
134 if ( $menu->[$idx] eq $new_search ) {
135 $source->{dir} = $cs->__new_search_dir();
136 if ( ! length $source->{dir} ) {
137 next DIR;
138 }
139 }
140 else {
141 $source->{dir} = $dirs->[$idx-@pre];
142 }
143 }
144 $cs->__add_to_history( $source->{dir} );
145 my $files_in_chosen_dir = $cs->files_in_dir( $source->{dir} );
146 if ( $goto_filter && ! $sf->{o}{insert}{enable_input_filter} && ! $source->{saved_book} ) {
147 $goto_filter = 0;
148 }
149 $source->{old_idx_file} //= 1;
150  
151 FILE: while ( 1 ) {
152 if ( $goto_filter ) {
153 # keep current source file
154 }
155 else {
156 my $hidden = 'Choose a File:';
157 my @pre = ( $hidden, undef );
158 my $change_dir = ' Change dir';
159 if ( $sf->{o}{insert}{history_dirs} == 1 ) {
160 push @pre, $change_dir;
161 }
162 my $menu = [ @pre, map { ' ' . basename $_ } @$files_in_chosen_dir ]; #
163 # Choose
164 my $idx = $tc->choose(
165 $menu,
166 { %{$sf->{i}{lyt_v}}, prompt => '', index => 1, default => $source->{old_idx_file},
167 undef => ' <=' }
168 );
169 if ( ! defined $idx || ! defined $menu->[$idx] ) {
170 if ( $sf->{o}{insert}{history_dirs} == 1 ) {
171 return if $data_source_choice_idx =~ /^(?:0|1)\z/;
172 next MENU;
173 }
174 next DIR;
175 }
176 if ( $sf->{o}{G}{menu_memory} ) {
177 if ( $source->{old_idx_file} == $idx && ! $ENV{TC_RESET_AUTO_UP} ) {
178 $source->{old_idx_file} = 1;
179 next FILE;
180 }
181 $source->{old_idx_file} = $idx;
182 }
183 if ( $menu->[$idx] eq $hidden ) {
184 my $op = App::DBBrowser::Options->new( $sf->{i}, $sf->{o} );
185 my $op_rw = App::DBBrowser::Options::ReadWrite->new( $sf->{i}, $sf->{o} );
186 $op->config_groups( [ { name => 'group_import', text => "- Import" } ], $sf->{i}{plugin} );
187 $op_rw->read_config_file( $sf->{i}{driver}, $sf->{i}{plugin} );
188 next FILE;
189 }
190 elsif ( $menu->[$idx] eq $change_dir ) {
191 $source->{dir} = $cs->__new_search_dir();
192 if ( length $source->{dir} ) {
193 $cs->__add_to_history( $source->{dir} );
194 $files_in_chosen_dir = $cs->files_in_dir( $source->{dir} );
195 }
196 next FILE;
197 }
198 else {
199 my $old_file_fs = $source->{file_fs};
200 $source->{file_fs} = encode( 'locale_fs', $files_in_chosen_dir->[$idx-@pre] );
201 if ( ! defined $old_file_fs || $old_file_fs ne $source->{file_fs} ) {
202 delete $source->{saved_book};
203 delete $source->{sheet_name};
204 }
205 }
206 }
207 $source->{old_idx_sheet} = 0;
208  
209 PARSE: while ( 1 ) {
210 my $parse_mode_idx = $sf->{o}{insert}{parse_mode_input_file};
211 if ( $goto_filter && ! $sf->{o}{insert}{enable_input_filter} ) {
212 $goto_filter = 0;
213 }
214 if ( $goto_filter ) {
215 # keep current insert_args
216 }
217 else {
218 $sql->{insert_args} = [];
219 if ( $parse_mode_idx < 3 && -T $source->{file_fs} ) {
220 my $open_mode;
221 if ( length $sf->{o}{insert}{file_encoding} ) {
222 $open_mode = '<:encoding(' . $sf->{o}{insert}{file_encoding} . ')';
223 }
224 else {
225 $open_mode = '<';
226 }
227 open my $fh, $open_mode, $source->{file_fs} or die $!;
228 my $parse_ok;
229 if ( $parse_mode_idx == 0 ) {
230 $parse_ok = $cp->parse_with_Text_CSV( $sql, $fh );
231 }
232 elsif ( $parse_mode_idx == 1 ) {
233 $parse_ok = $cp->parse_with_split( $sql, $fh );
234 }
235 elsif ( $parse_mode_idx == 2 ) {
236 $parse_ok = $cp->parse_with_template( $sql, $fh );
237 if ( $parse_ok && $parse_ok == -1 ) { # reparse
238 next PARSE;
239 }
240 }
241 if ( ! $parse_ok ) {
242 next FILE;
243 }
244 if ( ! @{$sql->{insert_args}} ) {
245 $tc->choose(
246 [ 'Press ENTER' ],
247 { prompt => 'Empty File!' }
248 );
249 close $fh;
250 next FILE;
251 }
252 }
253 else {
254 SHEET: while ( 1 ) {
255 my $ok = $cp->parse_with_Spreadsheet_Read( $sql, $source, $source->{file_fs} );
256 if ( ! $ok ) {
257 next FILE;
258 }
259 if ( ! @{$sql->{insert_args}} || ! @{$sql->{insert_args}[0]} ) {
260 $tc->choose(
261 [ 'Press ENTER' ],
262 { prompt => 'Empty Sheet!' }
263 );
264 next SHEET if $source->{saved_book};
265 next FILE;
266 }
267 last SHEET;
268 }
269 }
270 }
271 if ( ! $sf->{o}{insert}{enable_input_filter} ) {
272 return 1;
273 }
274 $goto_filter = 0;
275  
276 FILTER: while ( 1 ) {
277 my $ok = $cf->input_filter( $sql );
278 if ( ! $ok ) {
279 if ( $source->{saved_book} ) {
280 next PARSE;
281 }
282 next FILE;
283 }
284 elsif ( $ok == -1 ) { # -1 -> REPARSE
285 my $op = App::DBBrowser::Options->new( $sf->{i}, $sf->{o} );
286 my $op_rw = App::DBBrowser::Options::ReadWrite->new( $sf->{i}, $sf->{o} );
287 $op->config_groups( [ { name => 'group_import', text => "- Import" } ], $sf->{i}{plugin} );
288 $op_rw->read_config_file( $sf->{i}{driver}, $sf->{i}{plugin} );
289 next PARSE;
290 }
291 return 1;
292 }
293 }
294 }
295 }
296 }
297 }
298  
299  
300  
301  
302  
303  
304 1;
305  
306  
307 __END__