| 1 | package TSQL::FlatFile; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Text::CSV; | ||
| 8 | |||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | TSQL::FlatFile - secret module by Ded MedVed | ||
| 13 | |||
| 14 | =head1 VERSION | ||
| 15 | |||
| 16 | Version 1.02 | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | our $VERSION = '1.02'; | ||
| 21 | |||
| 22 | |||
| 23 | use Data::Dumper ; | ||
| 24 | use Carp ; | ||
| 25 | |||
| 26 | sub new { | ||
| 27 | |||
| 28 | local $_ = undef ; | ||
| 29 | |||
| 30 | #warn Dumper @_; | ||
| 31 | |||
| 32 | my $invocant = shift ; | ||
| 33 | my $class = ref($invocant) || $invocant ; | ||
| 34 | |||
| 35 | my @elems = @_ ; | ||
| 36 | my $self = bless {crlf => "\r\n" }, $class ; | ||
| 37 | |||
| 38 | # $self->_init(@elems) ; | ||
| 39 | return $self ; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub crlf { | ||
| 43 | my $self = shift; | ||
| 44 | return $self->{crlf}; | ||
| 45 | |||
| 46 | } | ||
| 47 | sub processLine { | ||
| 48 | |||
| 49 | my $self = shift or croak 'no self'; | ||
| 50 | |||
| 51 | my $asciifile = shift || croak 'no ascii file name'; | ||
| 52 | my $cfile = shift || croak 'no csv file'; | ||
| 53 | my $cfile_fh = shift || croak 'no csv file handle'; | ||
| 54 | |||
| 55 | my $filepos = shift || croak 'no file position given' ; | ||
| 56 | my $incrementalsearch = shift ; | ||
| 57 | if (!defined $incrementalsearch) { | ||
| 58 | croak 'no incrementalsearch flag given' ; | ||
| 59 | } my $debug = shift ; | ||
| 60 | |||
| 61 | if (!defined $debug) { | ||
| 62 | croak 'no debug flag given' ; | ||
| 63 | } | ||
| 64 | |||
| 65 | my $li=0; | ||
| 66 | |||
| 67 | my %csv_row; | ||
| 68 | my $ascii_row; | ||
| 69 | |||
| 70 | $cfile->header($cfile_fh); | ||
| 71 | while ((my $row = $cfile->getline_hr ($cfile_fh)) && ($li++ < $filepos)) { | ||
| 72 | # warn Dumper $row; | ||
| 73 | %csv_row = %$row; | ||
| 74 | } | ||
| 75 | #warn Dumper %csv_row; | ||
| 76 | #warn Dumper keys %csv_row; | ||
| 77 | my @vals = sort { length($csv_row{$b}) <=> length($csv_row{$a}) } keys %csv_row; | ||
| 78 | |||
| 79 | $li=0; | ||
| 80 | open(my $afile, "<", $asciifile) or die "Could not open file $!"; | ||
| 81 | #skip header | ||
| 82 | my $row = <$afile>; | ||
| 83 | while (defined(my $row = <$afile> ) && ($li++ < $filepos)) { | ||
| 84 | chomp $row; | ||
| 85 | $ascii_row = $row; | ||
| 86 | } | ||
| 87 | |||
| 88 | #say $ascii_row; | ||
| 89 | my %positions ; | ||
| 90 | foreach my $v (@vals){ | ||
| 91 | my $val = quotemeta($csv_row{$v})." *"; | ||
| 92 | $ascii_row =~ m/(?>$val)/; | ||
| 93 | $positions{$v} = [@-,@+]; | ||
| 94 | $ascii_row = $`. "^"x length($&) . $'; | ||
| 95 | } | ||
| 96 | |||
| 97 | my @sortedkeys = sort { $positions{$a}[0] <=> $positions{$b}[0]} keys %positions; | ||
| 98 | #warn of any mismatches | ||
| 99 | my $blanklines = ""; | ||
| 100 | foreach my $k (@sortedkeys) { | ||
| 101 | if ($positions{$k}[0] == $positions{$k}[1] ) { | ||
| 102 | warn "${k}: hasn't matched data - values ",$positions{$k}[0],":",$positions{$k}[1]; | ||
| 103 | $blanklines = $self->crlf; | ||
| 104 | }; | ||
| 105 | }; | ||
| 106 | #warn of any gaps in matching; | ||
| 107 | if ( $ascii_row !~ /\A[\^]*\z/igms ) { | ||
| 108 | warn $ascii_row; | ||
| 109 | $blanklines = $self->crlf unless $blanklines eq ""; | ||
| 110 | } | ||
| 111 | |||
| 112 | warn $blanklines if $blanklines; | ||
| 113 | |||
| 114 | my $result = ""; | ||
| 115 | if ($debug) { | ||
| 116 | my $i=1; | ||
| 117 | foreach my $k (@sortedkeys) { | ||
| 118 | if ($debug) { $result .= ($k . ("\t"x((55-length($k))/8)) . $positions{$k}[0] . "\t" . $positions{$k}[1]). $self->crlf }; | ||
| 119 | $i++; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | else { | ||
| 123 | $result .= "12.0" . $self->crlf; | ||
| 124 | $result .= (scalar(@sortedkeys)) .$self->crlf; | ||
| 125 | my $i=1; | ||
| 126 | foreach my $k (@sortedkeys) { | ||
| 127 | $result .= ($i . "\t" . "SQLCHAR" . "\t" . "0" . "\t" . ($positions{$k}[1]-$positions{$k}[0]) . "\t" . (($i == scalar(@sortedkeys)) ? '"\r\n"':'""') . "\t" . $i . "\t" . $k . "\t"x((55-length($k))/8) . "SQL_Latin1_general_CP1_CI_AS") . $self->crlf; | ||
| 128 | $i++; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | return $result; | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | sub getLinePositions { | ||
| 136 | |||
| 137 | my $self = shift or croak 'no self'; | ||
| 138 | |||
| 139 | my $asciifile = shift || croak 'no ascii file name'; | ||
| 140 | my $cfile = shift || croak 'no csv file'; | ||
| 141 | my $cfile_fh = shift || croak 'no csv file handle'; | ||
| 142 | |||
| 143 | my $filepos = shift || croak 'no file position given' ; | ||
| 144 | my $incrementalsearch = shift ; | ||
| 145 | if (!defined $incrementalsearch) { | ||
| 146 | croak 'no incrementalsearch flag given' ; | ||
| 147 | } my $debug = shift ; | ||
| 148 | |||
| 149 | my $li=0; | ||
| 150 | |||
| 151 | my %csv_row; | ||
| 152 | my $ascii_row; | ||
| 153 | |||
| 154 | $cfile->header($cfile_fh); | ||
| 155 | while ((my $row = $cfile->getline_hr ($cfile_fh)) && ($li++ < $filepos)) { | ||
| 156 | %csv_row = %$row; | ||
| 157 | } | ||
| 158 | my @vals = sort { length($csv_row{$b}) <=> length($csv_row{$a}) } keys %csv_row; | ||
| 159 | |||
| 160 | $li=0; | ||
| 161 | open(my $afile, "<", $asciifile) or die "Could not open file $!"; | ||
| 162 | #skip header | ||
| 163 | my $row = <$afile>; | ||
| 164 | while (defined(my $row = <$afile> ) && ($li++ < $filepos)) { | ||
| 165 | chomp $row; | ||
| 166 | $ascii_row = $row; | ||
| 167 | } | ||
| 168 | #say $ascii_row; | ||
| 169 | my %positions ; | ||
| 170 | foreach my $v (@vals){ | ||
| 171 | my $val = quotemeta($csv_row{$v})." *"; | ||
| 172 | $ascii_row =~ m/(?>$val)/; | ||
| 173 | $positions{$v} = [@-,@+]; | ||
| 174 | $ascii_row = $`. "^"x length($&) . $'; | ||
| 175 | } | ||
| 176 | |||
| 177 | my @sortedkeys = sort { $positions{$a}[0] <=> $positions{$b}[0]} keys %positions; | ||
| 178 | my @result = (); | ||
| 179 | { | ||
| 180 | my $i=0; | ||
| 181 | foreach my $k (@sortedkeys) { | ||
| 182 | $result[$i] = {key=>$k,start=>$positions{$k}[0],end=>$positions{$k}[1]}; | ||
| 183 | $i++; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | my $unmatchedcount = 0; | ||
| 188 | foreach my $k (@sortedkeys) { | ||
| 189 | if ($positions{$k}[0] == $positions{$k}[1] ) { | ||
| 190 | $unmatchedcount++; | ||
| 191 | }; | ||
| 192 | }; | ||
| 193 | |||
| 194 | my $unmatchedamount = length($ascii_row) - $ascii_row =~ tr/^//; | ||
| 195 | |||
| 196 | my @best_result = @result; | ||
| 197 | my $best_line = $filepos; | ||
| 198 | my $best_unmatchedcount = $unmatchedcount; | ||
| 199 | my $best_unmatchedamount = $unmatchedamount; | ||
| 200 | |||
| 201 | if ($incrementalsearch && ( $best_unmatchedcount > 0 || $best_unmatchedamount > 0 ) ) { | ||
| 202 | |||
| 203 | while ((my $crow = $cfile->getline_hr ($cfile_fh)) && (defined(my $row = <$afile> )) && ($li++ < $filepos+100 ) && ( $best_unmatchedcount > 0 || $best_unmatchedamount > 0 ) ) { | ||
| 204 | |||
| 205 | %csv_row = %$crow; | ||
| 206 | |||
| 207 | #warn Dumper %csv_row; | ||
| 208 | #warn Dumper keys %csv_row; | ||
| 209 | my @vals = sort { length($csv_row{$b}) <=> length($csv_row{$a}) } keys %csv_row; | ||
| 210 | |||
| 211 | |||
| 212 | chomp $row; | ||
| 213 | $ascii_row = $row; | ||
| 214 | |||
| 215 | # say $li," ", $ascii_row; | ||
| 216 | #say Dumper %csv_row; | ||
| 217 | my %positions ; | ||
| 218 | foreach my $v (@vals){ | ||
| 219 | my $val = quotemeta($csv_row{$v})." *"; | ||
| 220 | $ascii_row =~ m/(?>$val)/; | ||
| 221 | $positions{$v} = [@-,@+]; | ||
| 222 | #warn Dumper $val unless defined $` ; | ||
| 223 | #warn $ascii_row unless defined $` ; | ||
| 224 | |||
| 225 | $ascii_row = $`. "^"x length($&) . $'; | ||
| 226 | } | ||
| 227 | |||
| 228 | my @sortedkeys = sort { $positions{$a}[0] <=> $positions{$b}[0]} keys %positions; | ||
| 229 | my @result = (); | ||
| 230 | { | ||
| 231 | my $i=0; | ||
| 232 | foreach my $k (@sortedkeys) { | ||
| 233 | $result[$i] = {key=>$k,start=>$positions{$k}[0],end=>$positions{$k}[1]}; | ||
| 234 | $i++; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | |||
| 239 | my $unmatchedcount = 0; | ||
| 240 | foreach my $k (@sortedkeys) { | ||
| 241 | if ($positions{$k}[0] == $positions{$k}[1] ) { | ||
| 242 | $unmatchedcount++; | ||
| 243 | }; | ||
| 244 | }; | ||
| 245 | |||
| 246 | my $unmatchedamount = length($ascii_row) - $ascii_row =~ tr/^//; | ||
| 247 | |||
| 248 | @best_result = @result if $unmatchedcount < $best_unmatchedcount || $unmatchedamount < $best_unmatchedamount; | ||
| 249 | |||
| 250 | $best_line = $li if $unmatchedcount < $best_unmatchedcount || $unmatchedamount < $best_unmatchedamount; | ||
| 251 | $best_unmatchedcount = $unmatchedcount if $unmatchedcount < $best_unmatchedcount; | ||
| 252 | $best_unmatchedamount = $unmatchedamount if $unmatchedamount < $best_unmatchedamount; | ||
| 253 | |||
| 254 | @result = @best_result; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | return \{ positions => \@best_result, best_line => $best_line, best_unmatchedcount => $best_unmatchedcount, best_unmatchedamount => $best_unmatchedamount } ; | ||
| 259 | } | ||
| 260 | |||
| 261 | sub printDebugData { | ||
| 262 | #warn Dumper @_ ; | ||
| 263 | my $self = shift or croak 'no self'; | ||
| 264 | my $asciifile = shift || croak 'no ascii file name'; | ||
| 265 | my $datalinenumber = shift || croak 'no data line start position give'; | ||
| 266 | my $columns = shift || croak 'no columns given' ; | ||
| 267 | |||
| 268 | #warn Dumper $columns ; | ||
| 269 | |||
| 270 | my $li=0; | ||
| 271 | open(my $afile, "<", $asciifile) or die "Could not open file $!"; | ||
| 272 | my $row = <$afile>; | ||
| 273 | my $ascii_row = ""; | ||
| 274 | while (defined(my $row = <$afile> ) && ($li++ < $datalinenumber)) { } | ||
| 275 | while (defined(my $row = <$afile> ) && ($li++ < $datalinenumber + 20)) { | ||
| 276 | chomp $row; | ||
| 277 | $ascii_row = $row; | ||
| 278 | my $output_row = ""; | ||
| 279 | my $first=1; | ||
| 280 | foreach my $col (@$columns) { | ||
| 281 | my $start = $col->{start}; | ||
| 282 | my $length = $col->{end} - $start; | ||
| 283 | if (($length > 0 )) { | ||
| 284 | $output_row .= ($first? "":"|") . substr($ascii_row,$start,$length); | ||
| 285 | $first =0; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | say $output_row; | ||
| 289 | } | ||
| 290 | } | ||
| 291 | sub flatten { return map { @$_} @_ } ; | ||
| 292 | |||
| 293 | sub DESTROY {} | ||
| 294 | |||
| 295 | 1 ; | ||
| 296 | |||
| 297 | __DATA__ |