| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::GetContent::Filter::ConvertDate; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | use DateTime::Format::Strptime; | ||
| 9 | |||
| 10 | use Term::Choose qw(); | ||
| 11 | use Term::Form::ReadLine qw(); | ||
| 12 | |||
| 13 | use App::DBBrowser::Auxil; | ||
| 14 | use App::DBBrowser::GetContent::Filter; | ||
| 15 | |||
| 16 | |||
| 17 | sub new { | ||
| 18 | my ( $class, $info, $options, $d ) = @_; | ||
| 19 | my $sf = { | ||
| 20 | i => $info, | ||
| 21 | o => $options, | ||
| 22 | d => $d | ||
| 23 | }; | ||
| 24 | bless $sf, $class; | ||
| 25 | return $sf; | ||
| 26 | } | ||
| 27 | |||
| 28 | |||
| 29 | sub convert_date { | ||
| 30 | my ( $sf, $sql, $bu_insert_args, $filter_str ) = @_; | ||
| 31 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 32 | my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} ); | ||
| 33 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 34 | my $cf = App::DBBrowser::GetContent::Filter->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 35 | my $rx_locale_dependent = "\%[aAbBhcpPxX]"; | ||
| 36 | my $aoa = $sql->{insert_args}; | ||
| 37 | my $row_count = @$aoa; | ||
| 38 | my $busy_text = 'Convert datetime: '; | ||
| 39 | my $threshold_busy = 5_000; | ||
| 40 | my $threshold_progress = 100_000; | ||
| 41 | my ( $working, $fmt, $step ); | ||
| 42 | if ( $row_count > $threshold_busy ) { | ||
| 43 | $working = $busy_text . '...'; | ||
| 44 | if ( $row_count > $threshold_progress ) { | ||
| 45 | $step = 1_000; | ||
| 46 | my $total = int $row_count / $step; | ||
| 47 | $fmt = $busy_text . $total . '/%' . length( $total ) . 'd'; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | my $is_empty = $cf->__search_empty_cols( $aoa ); | ||
| 51 | my $header = $cf->__prepare_header( $aoa, $is_empty ); | ||
| 52 | |||
| 53 | COL: while ( 1 ) { | ||
| 54 | my $info = $cf->__get_filter_info( $sql, $filter_str); | ||
| 55 | my $prompt = "Choose column:"; | ||
| 56 | # Stop | ||
| 57 | my $col_idx = $cf->__choose_a_column_idx( $header, $info, $prompt ); | ||
| 58 | if ( ! defined $col_idx ) { | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | my $col = $header->[$col_idx]; | ||
| 62 | my @col_info = ( $filter_str, 'Column: ' . $col ); | ||
| 63 | $info = $cf->__get_filter_info( $sql, join( "\n", @col_info ) ); | ||
| 64 | |||
| 65 | SKIP_HEADER: while ( 1 ) { | ||
| 66 | my @pre = ( undef ); | ||
| 67 | my ( $no, $yes ) = ( 'NO', 'YES' ); | ||
| 68 | my $row_idx_begin = 0; | ||
| 69 | if ( $sf->{d}{stmt_types}[0] eq 'Create_Table' ) { | ||
| 70 | # Choose | ||
| 71 | my $choice = $tc->choose( | ||
| 72 | [ @pre, $yes, $no ], | ||
| 73 | { prompt => 'Skip the first row?', undef => $sf->{i}{s_back}, info => $info } | ||
| 74 | ); | ||
| 75 | if ( ! defined $choice ) { | ||
| 76 | next COL; | ||
| 77 | } | ||
| 78 | $row_idx_begin = 1 if $choice eq $yes; | ||
| 79 | } | ||
| 80 | my $count_error_in = 0; | ||
| 81 | my $default_pattern_in; | ||
| 82 | |||
| 83 | IN: while ( 1 ) { | ||
| 84 | my @in_info = @col_info; | ||
| 85 | $info = $cf->__get_filter_info( $sql, join( "\n", @in_info ) ); | ||
| 86 | my $prompt_patter_in = 'Pattern in: '; | ||
| 87 | # Readline | ||
| 88 | my $pattern_in = $tr->readline( | ||
| 89 | $prompt_patter_in, | ||
| 90 | { info => $info, default => $default_pattern_in, history => $sf->__pattern_history( 'in' ) } | ||
| 91 | ); | ||
| 92 | if ( ! length $pattern_in ) { | ||
| 93 | next SKIP_HEADER if $sf->{d}{stmt_types}[0] eq 'Create_Table'; | ||
| 94 | next COL; | ||
| 95 | } | ||
| 96 | $default_pattern_in = $count_error_in > 1 ? '' : $pattern_in; | ||
| 97 | push @in_info, $prompt_patter_in . $pattern_in; | ||
| 98 | my $formatter_args = { pattern => $pattern_in }; | ||
| 99 | if ( $pattern_in =~ /$rx_locale_dependent/ ) { | ||
| 100 | $info = $cf->__get_filter_info( $sql, join( "\n", @in_info ) ); | ||
| 101 | my $prompt_locale_in = 'Locale in: '; | ||
| 102 | # Readline | ||
| 103 | my $locale_in = $tr->readline( | ||
| 104 | $prompt_locale_in, | ||
| 105 | { info => $info, history => [] } | ||
| 106 | ); | ||
| 107 | if ( length $locale_in ) { | ||
| 108 | $formatter_args->{locale} = $locale_in; | ||
| 109 | push @in_info, $prompt_locale_in . $locale_in; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | my $formatter = DateTime::Format::Strptime->new( %$formatter_args, on_error => 'undef' ); | ||
| 113 | |||
| 114 | TYPE: while ( 1 ) { | ||
| 115 | $info = $cf->__get_filter_info( $sql, join( "\n", @in_info ) ); | ||
| 116 | my ( $format, $epoch ) = ( '- DateTime', '- Epoch' ); | ||
| 117 | # Choose | ||
| 118 | my $choice = $tc->choose( | ||
| 119 | [ @pre, $format, $epoch ], | ||
| 120 | { %{$sf->{i}{lyt_v}}, prompt => 'To: ', undef => $sf->{i}{s_back}, info => $info } | ||
| 121 | ); | ||
| 122 | if ( ! defined $choice ) { | ||
| 123 | ++$count_error_in; | ||
| 124 | next IN; | ||
| 125 | } | ||
| 126 | my $count_error_out = 0; | ||
| 127 | my $default_pattern_out; | ||
| 128 | |||
| 129 | OUT: while ( 1 ) { | ||
| 130 | my @tmp_info = @in_info; | ||
| 131 | if ( $choice eq $epoch ) { | ||
| 132 | $info = $cf->__get_filter_info( $sql, join( "\n", @tmp_info ) ); | ||
| 133 | my ( $seconds, $milliseconds, $microseconds, $fract ) = ( '- Seconds', '- Milliseconds', '- Microseconds', '- Seconds.Fract' ); | ||
| 134 | my $menu = [ @pre, $seconds, $milliseconds, $microseconds, $fract ]; | ||
| 135 | # Choose | ||
| 136 | my $epoch_type = $tc->choose( | ||
| 137 | $menu, | ||
| 138 | { %{$sf->{i}{lyt_v}}, undef => $sf->{i}{s_back}, info => $info } | ||
| 139 | ); | ||
| 140 | if ( ! defined $epoch_type ) { | ||
| 141 | next TYPE; | ||
| 142 | } | ||
| 143 | $cf->__print_busy_string( $working ); | ||
| 144 | if ( ! eval { | ||
| 145 | for my $row ( $row_idx_begin .. $#$aoa ) { | ||
| 146 | next if ! defined $aoa->[$row][$col_idx]; | ||
| 147 | my $dt = $formatter->parse_datetime( $aoa->[$row][$col_idx] ); | ||
| 148 | if ( ! defined $dt ) { | ||
| 149 | for my $row ( 1 .. $row ) { | ||
| 150 | $aoa->[$row][$col_idx] = $bu_insert_args->[$row][$col_idx]; | ||
| 151 | } | ||
| 152 | my $message = $sf->__error_messagte_parse_datetime( $formatter, $row, $aoa->[$row][$col_idx] ); | ||
| 153 | die $message; | ||
| 154 | } | ||
| 155 | if ( $epoch_type eq $seconds ) { | ||
| 156 | $aoa->[$row][$col_idx] = $dt->epoch(); | ||
| 157 | } | ||
| 158 | elsif ( $epoch_type eq $milliseconds ) { | ||
| 159 | $aoa->[$row][$col_idx] = int( $dt->hires_epoch() * 1_000 ); | ||
| 160 | } | ||
| 161 | elsif ( $epoch_type eq $microseconds ) { | ||
| 162 | $aoa->[$row][$col_idx] = int( $dt->hires_epoch() * 1_000_000 ); | ||
| 163 | } | ||
| 164 | else { | ||
| 165 | $aoa->[$row][$col_idx] = $dt->hires_epoch(); | ||
| 166 | } | ||
| 167 | if ( $fmt && ! ( $row % $step ) ) { | ||
| 168 | $cf->__print_busy_string( sprintf $fmt, $row / $step ); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | 1 } | ||
| 172 | ) { | ||
| 173 | $ax->print_error_message( $@ ); | ||
| 174 | ++$count_error_in; | ||
| 175 | next IN; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | else { | ||
| 179 | $info = $cf->__get_filter_info( $sql, join( "\n", @tmp_info ) ); | ||
| 180 | my $prompt_pattern_out = 'Pattern out: '; | ||
| 181 | # Readline | ||
| 182 | my $pattern_out = $tr->readline( | ||
| 183 | $prompt_pattern_out, | ||
| 184 | { info => $info, default => $default_pattern_out, history => $sf->__pattern_history( 'out' ) } | ||
| 185 | ); | ||
| 186 | if ( ! length $pattern_out ) { | ||
| 187 | next TYPE; | ||
| 188 | } | ||
| 189 | $default_pattern_out = $count_error_out > 1 ? '' : $pattern_out; | ||
| 190 | my $locale_out; | ||
| 191 | if ( $pattern_out =~ /$rx_locale_dependent/ ) { | ||
| 192 | push @tmp_info, $prompt_pattern_out . $pattern_out; | ||
| 193 | $info = $cf->__get_filter_info( $sql, join( "\n", @tmp_info ) ); | ||
| 194 | my $prompt_locale_out = 'Locale out: '; | ||
| 195 | # ReadLine | ||
| 196 | $locale_out = $tr->readline( | ||
| 197 | $prompt_locale_out, | ||
| 198 | { info => $info, history => [] } | ||
| 199 | ); | ||
| 200 | if ( length $locale_out ) { | ||
| 201 | push @tmp_info, $prompt_locale_out . $locale_out; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | $cf->__print_busy_string( $working ); | ||
| 205 | if ( ! eval { | ||
| 206 | for my $row ( $row_idx_begin .. $#$aoa ) { | ||
| 207 | next if ! length $aoa->[$row][$col_idx]; | ||
| 208 | my $dt = $formatter->parse_datetime( $aoa->[$row][$col_idx] ); | ||
| 209 | if ( ! defined $dt ) { | ||
| 210 | for my $row ( 1 .. $row ) { | ||
| 211 | $aoa->[$row][$col_idx] = $bu_insert_args->[$row][$col_idx]; | ||
| 212 | } | ||
| 213 | my $message = $sf->__error_messagte_parse_datetime( $formatter, $row, $aoa->[$row][$col_idx] ); | ||
| 214 | die $message; | ||
| 215 | } | ||
| 216 | if ( length $locale_out ) { | ||
| 217 | $dt->set_locale( $locale_out ); | ||
| 218 | } | ||
| 219 | $aoa->[$row][$col_idx] = $dt->strftime( $pattern_out ); | ||
| 220 | if ( $fmt && ! ( $row % $step ) ) { | ||
| 221 | $cf->__print_busy_string( sprintf $fmt, $row / $step ); | ||
| 222 | } | ||
| 223 | } | ||
| 224 | 1 } | ||
| 225 | ) { | ||
| 226 | $ax->print_error_message( $@ ); | ||
| 227 | if ( $@ =~ /^Pattern:/ ) { | ||
| 228 | ++$count_error_in; | ||
| 229 | next IN; | ||
| 230 | } | ||
| 231 | ++$count_error_out; | ||
| 232 | next OUT; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | $sql->{insert_args} = $aoa; | ||
| 236 | return; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | } | ||
| 240 | } | ||
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 | |||
| 245 | sub __error_messagte_parse_datetime { | ||
| 246 | my ( $sf, $formatter, $row, $string ) = @_; | ||
| 247 | my $message = 'Pattern: ' . $formatter->pattern() . ', ' . $formatter->locale(); | ||
| 248 | $message .= "\nRow: $row"; | ||
| 249 | $message .= "\nString: $string"; | ||
| 250 | $message .= "\n\n" . $formatter->errmsg(); | ||
| 251 | return $message; | ||
| 252 | } | ||
| 253 | |||
| 254 | |||
| 255 | sub __pattern_history { | ||
| 256 | my ( $sf, $in_out ) = @_; | ||
| 257 | if ( $in_out eq 'in' ) { | ||
| 258 | my $in = [ | ||
| 259 | '%Y-%m-%d %H:%M:%S', | ||
| 260 | '%Y-%m-%dT%H:%M:%S', | ||
| 261 | '%Y-%m-%d %H:%M:%S.%N%z', | ||
| 262 | '%a %d %b %Y %I:%M:%S %P', | ||
| 263 | '%d.%m.%Y %H:%M:%S', | ||
| 264 | '%d/%m/%Y %H:%M:%S', | ||
| 265 | '%m/%d/%Y %H:%M:%S', | ||
| 266 | '%y = two digit year (0-99)', | ||
| 267 | '%b = Sep; %B = September', | ||
| 268 | '%I = the hour on a 12-hour clock (01-12)', | ||
| 269 | '%p = the equivalent of AM or PM according to the locale in use', | ||
| 270 | '%z = timezone (eg. +2000); %Z timezone name (eg. EST)', | ||
| 271 | '%u = weekday number (1-7) with Monday is 1; %w = weekday number (0-6) with Sunday is 0', | ||
| 272 | ]; | ||
| 273 | return $in; | ||
| 274 | } | ||
| 275 | else { | ||
| 276 | my $out = [ | ||
| 277 | '%Y-%m-%d %H:%M:%S', | ||
| 278 | '%Y-%m-%d %H:%M:%S.%6N%z', | ||
| 279 | $sf->{i}{dbms} eq 'Oracle' ? '%d-%b-%y %I.%M.%S.%6N %p %Z' : '%a %d %b %Y %I:%M:%S.%6N %p %Z' | ||
| 280 | ]; | ||
| 281 | return $out; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | |||
| 286 | |||
| 287 | |||
| 288 | 1; | ||
| 289 | |||
| 290 | |||
| 291 | __END__ |