| 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.00 | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | our $VERSION = '1.00'; | ||
| 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 {}, $class ; | ||
| 37 | |||
| 38 | $self->_init(@elems) ; | ||
| 39 | return $self ; | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | sub _init { | ||
| 44 | |||
| 45 | local $_ = undef ; | ||
| 46 | |||
| 47 | #warn Dumper @_; | ||
| 48 | |||
| 49 | my $self = shift ; | ||
| 50 | my $class = ref($self) || $self ; | ||
| 51 | my $ref = shift or croak "no arg"; | ||
| 52 | |||
| 53 | #print Dumper $ref ; | ||
| 54 | |||
| 55 | return ; | ||
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | sub processLine { | ||
| 60 | |||
| 61 | my $self = shift or croak 'no self'; | ||
| 62 | |||
| 63 | my $afile = shift || croak 'no ascii file'; | ||
| 64 | my $cfile = shift || croak 'no csv file'; | ||
| 65 | |||
| 66 | my $filepos = shift || croak 'no file position given' ; | ||
| 67 | |||
| 68 | |||
| 69 | my $li=0; | ||
| 70 | |||
| 71 | my %csv_row; | ||
| 72 | my $ascii_row; | ||
| 73 | |||
| 74 | my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1 }); | ||
| 75 | open my $fh, "<:encoding(utf8)", $csvfile or die "csvfile: $!"; | ||
| 76 | $cfile->header($fh); | ||
| 77 | while ((my $row = $csv->getline_hr ($fh)) && ($li++ < $linenumber)) { | ||
| 78 | # warn Dumper $row; | ||
| 79 | %csv_row = %$row; | ||
| 80 | } | ||
| 81 | #warn Dumper %csv_row; | ||
| 82 | #warn Dumper keys %csv_row; | ||
| 83 | my @vals = sort { length($csv_row{$b}) <=> length($csv_row{$a}) } keys %csv_row; | ||
| 84 | |||
| 85 | close $fh; | ||
| 86 | |||
| 87 | |||
| 88 | $li=0; | ||
| 89 | open(my $afile, "<", $asciifile) or die "Could not open file $!"; | ||
| 90 | #skip header | ||
| 91 | my $row = <$afile>; | ||
| 92 | while (defined(my $row = <$afile> ) && ($li++ < $linenumber)) { | ||
| 93 | chomp $row; | ||
| 94 | $ascii_row = $row; | ||
| 95 | } | ||
| 96 | #warn Dumper $ascii_row; | ||
| 97 | #print "done\n"; | ||
| 98 | |||
| 99 | #find where vals in csv match ascii | ||
| 100 | |||
| 101 | #say $ascii_row; | ||
| 102 | my %positions ; | ||
| 103 | foreach my $v (@vals){ | ||
| 104 | my $val = $csv_row{$v}." *"; | ||
| 105 | # warn Dumper $val; | ||
| 106 | |||
| 107 | $ascii_row =~ m/(?>$val)/; | ||
| 108 | $positions{$v} = [@-,@+]; | ||
| 109 | #warn Dumper @-, @+; | ||
| 110 | |||
| 111 | $ascii_row = $`. "^"x length($&) . $'; | ||
| 112 | #say $ascii_row; | ||
| 113 | # warn Dumper $$_[0][0],$$_[1][0],$$_[2][0],$$_[3],$$_[4] for exhaustive($ascii_row => qr/(?>$val)/, qw[ @- $^R @+ $` $']); | ||
| 114 | } | ||
| 115 | |||
| 116 | my @sortedkeys = sort { $positions{$a}[0] <=> $positions{$b}[0]} keys %positions; | ||
| 117 | { | ||
| 118 | my $i=1; | ||
| 119 | foreach my $k (@sortedkeys) { | ||
| 120 | if ($debug) { say $k,"\t"x((55-length($k))/8), $positions{$k}[0],"\t", $positions{$k}[1]}; | ||
| 121 | $i++; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | if (!$debug) {say "12.0"}; | ||
| 125 | if (!$debug) {say scalar(@sortedkeys)}; | ||
| 126 | { | ||
| 127 | my $i=1; | ||
| 128 | foreach my $k (@sortedkeys) { | ||
| 129 | if (!$debug) {say $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"}; | ||
| 130 | $i++; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | |||
| 141 | |||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | |||
| 147 | |||
| 148 | sub commentifyPreTestAction { | ||
| 149 | my $self = shift; | ||
| 150 | my $commentChars = shift or croak 'No Chars' ; | ||
| 151 | return <<"EOF"; | ||
| 152 | ${commentChars} | ||
| 153 | ${commentChars}@{[$self->preTestAction()]} | ||
| 154 | ${commentChars} | ||
| 155 | EOF | ||
| 156 | } | ||
| 157 | |||
| 158 | sub commentifyTestAction { | ||
| 159 | my $self = shift; | ||
| 160 | my $commentChars = shift or croak 'No Chars' ; | ||
| 161 | return <<"EOF"; | ||
| 162 | ${commentChars} | ||
| 163 | ${commentChars}@{[$self->testAction()]} | ||
| 164 | ${commentChars} | ||
| 165 | EOF | ||
| 166 | } | ||
| 167 | |||
| 168 | sub commentifyPostTestAction { | ||
| 169 | my $self = shift; | ||
| 170 | my $commentChars = shift or croak 'No Chars' ; | ||
| 171 | return <<"EOF"; | ||
| 172 | ${commentChars} | ||
| 173 | ${commentChars}@{[$self->postTestAction()]} | ||
| 174 | ${commentChars} | ||
| 175 | EOF | ||
| 176 | } | ||
| 177 | |||
| 178 | sub commentifyActionDataName { | ||
| 179 | my $self = shift; | ||
| 180 | my $commentChars = shift or croak 'No Chars' ; | ||
| 181 | return <<"EOF"; | ||
| 182 | ${commentChars} | ||
| 183 | ${commentChars}@{[$self->testActionDataName()]} | ||
| 184 | ${commentChars} | ||
| 185 | EOF | ||
| 186 | } | ||
| 187 | |||
| 188 | sub preTest_conditions { | ||
| 189 | my $self = shift or croak 'no self'; | ||
| 190 | my $conditions ; | ||
| 191 | $conditions = shift if @_; | ||
| 192 | if ( defined $conditions ) { | ||
| 193 | my @conditions = @$conditions ; | ||
| 194 | $self->{PRETEST_TESTCONDITIONS} = \@conditions ; | ||
| 195 | } | ||
| 196 | return $self->{PRETEST_TESTCONDITIONS} ; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub test_conditions { | ||
| 200 | my $self = shift or croak 'no self'; | ||
| 201 | my $conditions ; | ||
| 202 | $conditions = shift if @_; | ||
| 203 | if ( defined $conditions ) { | ||
| 204 | my @conditions = @$conditions ; | ||
| 205 | $self->{TEST_TESTCONDITIONS} = \@conditions ; | ||
| 206 | } | ||
| 207 | return $self->{TEST_TESTCONDITIONS} ; | ||
| 208 | } | ||
| 209 | |||
| 210 | sub postTest_conditions { | ||
| 211 | my $self = shift or croak 'no self'; | ||
| 212 | my $conditions ; | ||
| 213 | $conditions = shift if @_; | ||
| 214 | if ( defined $conditions ) { | ||
| 215 | my @conditions = @$conditions ; | ||
| 216 | $self->{POSTTEST_TESTCONDITIONS} = \@conditions ; | ||
| 217 | } | ||
| 218 | return $self->{POSTTEST_TESTCONDITIONS} ; | ||
| 219 | } | ||
| 220 | |||
| 221 | sub conditions { | ||
| 222 | my $self = shift or croak 'no self'; | ||
| 223 | my $conditions ; | ||
| 224 | $conditions = shift if @_; | ||
| 225 | if ( defined $conditions ) { | ||
| 226 | croak 'obsoleted method' ; | ||
| 227 | } | ||
| 228 | my $preTestConditions = $self->preTest_conditions() ; | ||
| 229 | my $testConditions = $self->test_conditions() ; | ||
| 230 | my $postTestConditions = $self->postTest_conditions() ; | ||
| 231 | my @Conditions = flatten ([@$preTestConditions,@$testConditions,@$postTestConditions]); | ||
| 232 | |||
| 233 | return \@Conditions ; | ||
| 234 | } | ||
| 235 | |||
| 236 | sub preTestAction { | ||
| 237 | my $self = shift or croak 'no self'; | ||
| 238 | my $action ; | ||
| 239 | $action = shift if @_; | ||
| 240 | # normalise | ||
| 241 | if ( defined $action ) { | ||
| 242 | $action = 'null' if $action =~ m{^null|nothing$}ix ; | ||
| 243 | $self->{PRETESTACTION} = $action ; | ||
| 244 | } | ||
| 245 | return $self->{PRETESTACTION} ; | ||
| 246 | |||
| 247 | } | ||
| 248 | sub testAction { | ||
| 249 | my $self = shift or croak 'no self'; | ||
| 250 | my $action ; | ||
| 251 | $action = shift if @_; | ||
| 252 | # normalise | ||
| 253 | if ( defined $action ) { | ||
| 254 | $action = 'null' if $action =~ m{^null|nothing$}ix ; | ||
| 255 | $self->{TESTACTION} = $action ; | ||
| 256 | } | ||
| 257 | return $self->{TESTACTION} ; | ||
| 258 | |||
| 259 | } | ||
| 260 | sub postTestAction { | ||
| 261 | my $self = shift or croak 'no self'; | ||
| 262 | my $action ; | ||
| 263 | $action = shift if @_; | ||
| 264 | # normalise | ||
| 265 | if ( defined $action ) { | ||
| 266 | $action = 'null' if $action =~ m{^null|nothing$}ix ; | ||
| 267 | $self->{POSTTESTACTION} = $action ; | ||
| 268 | } | ||
| 269 | return $self->{POSTTESTACTION} ; | ||
| 270 | |||
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | sub flatten { return map { @$_} @_ } ; | ||
| 275 | |||
| 276 | sub DESTROY {} | ||
| 277 | |||
| 278 | 1 ; | ||
| 279 | |||
| 280 | __DATA__ |