blib/lib/Text/CSV/SQLhelper.pm

filter: Statementstmt 4.5% 8 / 175 Branchbran 0.0% 0 / 98 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 28.5% 2 / 7 Podpod 100% 5 / 5 totaltotal 4.9% 15 / 305 CC 54 SCAR 76.0
CC
54
Cov
3%
processFile
2450.0
new
30.0
errorblank
6.0
CRAP 2003.6
↑ Summary
1 package Text::CSV::SQLhelper;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Text::CSV::SQLhelper - Processes a CSV file and tries to figure out
9  
10 =head1 VERSION
11  
12 Version 0.0.0
13  
14 =cut
15  
16 2 our $VERSION = '0.0.0';
17  
18  
19 =head1 SYNOPSIS
20  
21  
22  
23 =head1 METHODS
24  
25 =head2 new
26  
27 =head3 args hash
28  
29 =head4 csv
30  
31 This is the text CSV object.
32  
33 =head4 defaultAllowNull
34  
35 If no null is found,
36  
37 It defaults to false.
38  
39 =cut
40  
41 0 ▶sub new{
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
42 0 my %args;
43 0 ▶ if(defined($_[1])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 0 0 0
44 0 %args= %{$_[1]};
45 }
46 0 my $method='new';
47  
48 0 my $self={
49 error=>undef,
50 perror=>undef,
51 errorString=>undef,
52 module=>'Text-CSV-SQLhelper',
53 };
54 0 bless $self;
55  
56 0 ▶ if (!defined( $args{csv} )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"csv"}) 0 0 0
57 0 $self->{error}=1;
58 0 $self->{perror}=1;
59 0 $self->{errorString}='No Text::CSV object passed';
60 0 warn( $self->{module}.' '.$method.':'.$self->{error}.': '.$self->{errorString} );
61 0 return $self;
62 }
63  
64 0 ▶ if (ref( $args{csv} ) ne 'Text::CSV') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"csv"} ne "Text::CSV") 0 0 0
65 0 $self->{error}=1;
66 0 $self->{perror}=1;
67 0 $self->{errorString}='No Text::CSV object passed';
68 0 warn( $self->{module}.' '.$method.':'.$self->{error}.': '.$self->{errorString} );
69 0 return $self;
70 }
71  
72 0 ▶ if (!defined($args{defaultAllowNull})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"defaultAllowNull"}) 0 0 0
73 0 $args{defaultAllowNull}=0;
74 }
75  
76 0 $self->{csv}=$args{csv};
77 0 $self->{allownull}=$args{defaultAllowNull};
78  
79 0 return $self;
80 }
81  
82 =head2
83  
84 =head2 error
85  
86 Returns the current error code and true if there is an error.
87  
88 If there is no error, undef is returned.
89  
90 my $error=$foo->error;
91 if($error){
92 print 'error code: '.$error."\n";
93 }
94  
95 =cut
96  
97 0 ▶sub error{
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
98 0 return $_[0]->{error};
99 }
100  
101 =head2 errorString
102  
103 Returns the error string if there is one. If there is not,
104 it will return ''.
105  
106 my $error=$foo->error;
107 if($error){
108 print 'error code:'.$error.': '.$foo->errorString."\n";
109 }
110  
111 =cut
112  
113 0 ▶sub errorString{
Subroutine errorString: not called CC=1 SCAR=6.9
Pod documented
114 0 return $_[0]->{errorString};
115 }
116  
117 =head2 processFile
118  
119 This processes the specified file.
120  
121 Two arguements are required. The first one is the file to read and the
122 second one is a boolean specifying if the top line should be skipped
123 or not.
124  
125 The returned value is a array.
126  
127 =head3 returned array
128  
129 =head4 allownull
130  
131 Wether or not a column can have a null value.
132  
133 This defaults to false.
134  
135 =head4 min
136  
137 This is the smallest value found. In regards to strings,
138 it represents number of characters in the shortest string.
139  
140 =head4 max
141  
142 This is the largest value found. In regards to strings,
143 it represents number of characters in the longest string.
144  
145 =head4 sql
146  
147 This is a SQL description for the column.
148  
149 =head4 type
150  
151 This is the data type of the column.
152  
153 The possible values are 'float', 'int', and 'string'.
154  
155  
156 #process the file, including the top line
157 my @columns=$foo->processFile($file);
158 if($foo->error){
159 print 'error code:'.$foo->error.': '.$foo->errorString."\n";
160 }
161  
162 #process the file, skipping the top line
163 my @columns=$foo->processFile($file, 1);
164 if($foo->error){
165 print 'error code:'.$foo->error.': '.$foo->errorString."\n";
166 }
167  
168  
169 =cut
170  
171 0 ▶sub processFile{
Subroutine processFile: not called CC=49 SCAR=78.0
Pod documented
172 0 my $self=$_[0];
173 0 my $file=$_[1];
174 0 my $removetop=$_[2];
175 0 my $method='processfile';
176  
177 0 $self->errorblank;
178 0 ▶ if ($self->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->error) 0 0 0
179 0 warn($self->{module}.' '.$method.': failed to blank the previous error');
180 0 return undef;
181 }
182  
183 0 ▶ if (!defined($file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 0 0
184 0 $self->{error}=2;
185 0 $self->{errorString}='No file specified';
186 0 warn( $self->{module}.' '.$method.':'.$self->{error}.': '.$self->{errorString} );
187 0 return undef;
188 }
189  
190 #read the file
191 0 my $fh;
192 0 ▶ if (!open($fh, "<:encoding(utf8)", $file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<:encoding(utf8)", $file) 0 0 0
193 0 $self->{error}=3;
194 0 $self->{errorString}='Failed to open the file, "'.$file.'",';
195 0 warn( $self->{module}.' '.$method.':'.$self->{error}.': '.$self->{errorString} );
196 0 return undef;
197 }
198  
199 #what will be returned
200 0 my @columns;
201  
202 #remove the top if asked to
203 0 ▶ if ($removetop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($removetop) 0 0 0
204 0 my @top=@{$self->{csv}->getline ($fh)};
205 }
206  
207 #processes each row
208 0 my @row;
209 0 my $continueloop=1;
210 #fetches the new row and we do get it, save it as a array and if we don't stop this loop
211 0 my $newrow=$self->{csv}->getline ($fh);
212 0 ▶ if (defined($newrow->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newrow->[0]) { } 0 0 0
213 0 @row = @{$newrow};
214 }else {
215 0 $continueloop=0;
216 }
217 0 ▶ while ($continueloop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($continueloop) 0 0 0
218 #processes each row
219 0 my $int=0;
220 0 ▶ while ($int <= $#row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int <= $#row) 0 0 0
221 0 my %rowinfo;
222 0 my $matched=0;
223  
224 #handles it if this item is undefined
225 0 ▶ if (!defined($row[$int])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $row[$int]) 0 0 0
226 0 ▶ if (!defined( $columns[$int] )) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $columns[$int]) { } 0 0 0
227 0 $rowinfo{allownull}=1;
228 0 $rowinfo{min}=0;
229 0 $rowinfo{max}=0;
230 0 $columns[$int]=\%rowinfo;
231 }else {
232 0 $columns[$int]{allownull}=1;
233 }
234 0 $matched=1;
235 }
236
237 #handles it if it is a int
238 0 ▶ if ( ($row[$int] =~ /^[0123456789]*$/ ) && (!$matched) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row[$int] =~ /^[0123456789]*$/ and not $matched) 0 0 0
Condition0%0/3
$row[$int] =~ /^[0123456789]*$/ and not $matched
!ll&&!rl&&r
000
MC/DC0%0/2
$row[$int] =~ /^[0123456789]*$/ and not $matched
$row[$int] =~ /^[0123456789]*$/ not $matched
Truth table0%0/3
A and not(B)
A= $row[$int] =~ /^[0123456789]*$/B= not $matched
ABresult
0X0
100
111
239 0 ▶ if (!defined( $columns[$int] )) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $columns[$int]) { } 0 0 0
240 0 $rowinfo{type}='int';
241 0 $rowinfo{max}=$row[$int];
242 0 $rowinfo{min}=$row[$int];
243 0 $rowinfo{allownull}=$self->{allownull};
244 0 $columns[$int]=\%rowinfo;
245 }else {
246 #handles it if the type has not been set previously.
247 0 ▶ if (defined($columns[$int]{type})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $columns[$int]{"type"}) 0 0 0
248 0 $columns[$int]{type}='int';
249 }
250  
251 #handles it if the last type was int as well
252 0 ▶ if ($columns[$int]{type} eq 'int') {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"type"} eq "int") 0 0 0
253 #updates the new min and max value, if needed
254 0 ▶ if ($columns[$int]{min} > $row[$int]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"min"} > $row[$int]) 0 0 0
255 0 $columns[$int]{min}=$row[$int];
256 }
257 0 ▶ if ($columns[$int]{max} < $row[$int]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"max"} < $row[$int]) 0 0 0
258 0 $columns[$int]{min}=$row[$int];
259 }
260 }
261 #handles it if it is a string
262 0 ▶ if ($columns[$int]{type} eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"type"} eq "string") 0 0 0
263 #updates the new min and max length, if needed
264 0 my $length=length($row[$int]);
265 0 ▶ if ($columns[$int]{min} > $length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"min"} > $length) 0 0 0
266 0 $columns[$int]{min}=$length;
267 }
268 0 ▶ if ($columns[$int]{max} < $length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"max"} < $length) 0 0 0
269 0 $columns[$int]{max}=$length;
270 }
271 }
272 #handles it if the last type was float
273 0 ▶ if ($columns[$int]{type} eq 'float') {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"type"} eq "float") 0 0 0
274 #updates the new min and max value, if needed
275 0 ▶ if ($columns[$int]{min} > $row[$int]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"min"} > $row[$int]) 0 0 0
276 0 $columns[$int]{min}=$row[$int];
277 }
278 0 ▶ if ($columns[$int]{max} < $row[$int]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"max"} < $row[$int]) 0 0 0
279 0 $columns[$int]{max}=$row[$int];
280 }
281 }
282 }
283 0 $matched=1;
284 }
285  
286 #handles it if it is a float
287 0 ▶ if ( ($row[$int] =~ /^[0123456789]*\.[0123456789]*$/ ) && (!$matched) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row[$int] =~ /^[0123456789]*\.[0123456789]*$/ and not $matched) 0 0 0
Condition0%0/3
$row[$int] =~ /^[0123456789]*\.[0123456789]*$/ and not $matched
!ll&&!rl&&r
000
MC/DC0%0/2
$row[$int] =~ /^[0123456789]*\.[0123456789]*$/ and not $matched
$row[$int] =~ /^[0123456789]*\.[0123456789]*$/ not $matched
Truth table0%0/3
A and not(B)
A= $row[$int] =~ /^[0123456789]*\.[0123456789]*$/B= not $matched
ABresult
0X0
100
111
288 0 ▶ if (!defined( $columns[$int] )) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $columns[$int]) { } 0 0 0
289 0 $rowinfo{type}='float';
290 0 $rowinfo{max}=$row[$int];
291 0 $rowinfo{min}=$row[$int];
292 0 $rowinfo{allownull}=$self->{allownull};
293 0 $columns[$int]=\%rowinfo;
294 }else {
295 #handles it if the type has not been set previously.
296 0 ▶ if (defined($columns[$int]{type})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $columns[$int]{"type"}) 0 0 0
297 0 $columns[$int]{type}='float';
298 }
299  
300 #handles it if the last type was int as well
301 0 ▶ if ($columns[$int]{type} eq 'int') {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"type"} eq "int") 0 0 0
302 #updates the new min and max value, if needed
303 0 ▶ if ($columns[$int]{min} > $row[$int]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"min"} > $row[$int]) 0 0 0
304 0 $columns[$int]{min}=$row[$int];
305 }
306 0 ▶ if ($columns[$int]{max} < $row[$int]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"max"} < $row[$int]) 0 0 0
307 0 $columns[$int]{max}=$row[$int];
308 }
309 #change the type to float as int can also be a float value as well, but float is
310 #more accurate
311 0 $columns[$int]{type}='float';
312 }
313 #handles it if it is a string
314 0 ▶ if ($columns[$int]{type} eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"type"} eq "string") 0 0 0
315 #updates the new min and max length, if needed
316 0 my $length=length($row[$int]);
317 0 ▶ if ($columns[$int]{min} > $length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"min"} > $length) 0 0 0
318 0 $columns[$int]{min}=$length;
319 }
320 0 ▶ if ($columns[$int]{max} < $length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"max"} < $length) 0 0 0
321 0 $columns[$int]{max}=$length;
322 }
323 }
324 #handles it if the last type was float
325 0 ▶ if ($columns[$int]{type} eq 'float') {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"type"} eq "float") 0 0 0
326 #updates the new min and max value, if needed
327 0 ▶ if ($columns[$int]{min} > $row[$int]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"min"} > $row[$int]) 0 0 0
328 0 $columns[$int]{min}=$row[$int];
329 }
330 0 ▶ if ($columns[$int]{max} < $row[$int]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"max"} < $row[$int]) 0 0 0
331 0 $columns[$int]{max}=$row[$int];
332 }
333 }
334 }
335 0 $matched=1;
336 }
337  
338 #handles it if it was not matched, which in this case only leaves a string
339 0 ▶ if (!$matched) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched) 0 0 0
340 0 ▶ if (!defined( $columns[$int] )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $columns[$int]) 0 0 0
341 0 $rowinfo{type}='string';
342 0 $rowinfo{allownull}=$self->{allownull};
343 0 my $length=length($row[$int]);
344 0 $columns[$int]{min}=$length;
345 0 $columns[$int]{max}=$length;
346 0 $columns[$int]=\%rowinfo;
347 }
348  
349 #just set this to a string instead of bothering to check if this was
350 #set to a int or float initially...
351 0 $columns[$int]{type}='string';
352  
353 #updates the new min and max length, if needed
354 0 my $length=length($row[$int]);
355 0 ▶ if ( (!defined( $columns[$int]{min} )) || ($columns[$int]{min} > $length) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $columns[$int]{"min"} or $columns[$int]{"min"} > $length) 0 0 0
Condition0%0/3
not defined $columns[$int]{"min"} or $columns[$int]{"min"} > $length
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $columns[$int]{"min"} or $columns[$int]{"min"} > $length
not defined $columns[$int]{"min"} $columns[$int]{"min"} > $length
Truth table0%0/3
not(A) or B
A= not defined $columns[$int]{"min"}B= $columns[$int]{"min"} > $length
ABresult
1X1
011
000
356 0 $columns[$int]{min}=$length;
357 }
358 0 ▶ if ( (!defined( $columns[$int]{max} )) || ($columns[$int]{max} < $length)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $columns[$int]{"max"} or $columns[$int]{"max"} < $length) 0 0 0
Condition0%0/3
not defined $columns[$int]{"max"} or $columns[$int]{"max"} < $length
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $columns[$int]{"max"} or $columns[$int]{"max"} < $length
not defined $columns[$int]{"max"} $columns[$int]{"max"} < $length
Truth table0%0/3
not(A) or B
A= not defined $columns[$int]{"max"}B= $columns[$int]{"max"} < $length
ABresult
1X1
011
000
359 0 $columns[$int]{max}=$length;
360 }
361 }
362  
363 0 $int++;
364 }
365
366 #fetches the new row and we do get it, save it as a array and if we don't stop this loop
367 0 $newrow=$self->{csv}->getline ($fh);
368 0 ▶ if (defined($newrow)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newrow) { } 0 0 0
369 0 my @row = @{$newrow};
370 }else {
371 0 $continueloop=0;
372 }
373 }
374  
375 #runs through each one setting up the SQL information and handling any consistently nulls
376 0 my $int=0;
377 0 ▶ while (defined($columns[$int])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $columns[$int]) 0 0 0
378 0 ▶ if (!defined($columns[$int]{type})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $columns[$int]{"type"}) 0 0 0
379 0 $columns[$int]{type}='string';
380 0 $columns[$int]{sql}='VARCHAR(1024)';
381 0 $columns[$int]{min}=0;
382 0 $columns[$int]{max}=1024;
383 }
384  
385 0 ▶ if ($columns[$int]{type} eq 'int') {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"type"} eq "int") 0 0 0
386 0 $columns[$int]{sql}='BIGINT';
387 }
388  
389 0 ▶ if ($columns[$int]{type} eq 'float') {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"type"} eq "float") 0 0 0
390 0 $columns[$int]{sql}='DOUBLE PRECISION';
391 }
392  
393 0 ▶ if ($columns[$int]{type} eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns[$int]{"type"} eq "string") 0 0 0
394 0 $columns[$int]{max}=((( $columns[$int]{max} / 1024) % 1024) + 1) * 1024;
395 0 $columns[$int]{sql}='VARCCHAR('.$columns[$int]{max}.')';
396 }
397  
398 0 ▶ if (!$columns[$int]{allownull}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($columns[$int]{"allownull"}) 0 0 0
399 0 $columns[$int]{sql}=$columns[$int]{sql}.' NOT NULL';
400 }
401  
402 0 $int++;
403 }
404  
405 0 return @columns;
406 }
407  
408 =head2 errorblank
409  
410 This blanks the error storage and is only meant for internal usage.
411  
412 If a permanent error is set, it will not be cleared.
413  
414 It does the following.
415  
416 $self->{error}=undef;
417 $self->{errorString}="";
418  
419 =cut
420  
421 0 ▶sub errorblank{
Subroutine errorblank: not called CC=2 SCAR=17.9
Pod documented
422 0 my $self=$_[0];
423  
424 0 ▶ if ($self->{perror}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"perror"}) 0 0 0
425 0 warn($self->{module}.' errorblank: A permanent error is set');
426 0 return undef;
427 }
428  
429 0 $self->{error}=undef;
430 0 $self->{errorString}="";
431  
432 0 return 1;
433 }
434  
435 =head1 ERROR CODES
436  
437 =head2 1
438  
439 No "Text::CSV" object specified or defined.
440  
441 =head2 2
442  
443 No file specified.
444  
445 =head3 3
446  
447 Failed to open the file.
448  
449 =head1 AUTHOR
450  
451 Zane C. Bowers, C<< <vvelox at vvelox.net> >>
452  
453 =head1 BUGS
454  
455 Please report any bugs or feature requests to C<bug-text-csv-sqlhelper at rt.cpan.org>, or through
456 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-CSV-SQLhelper>. I will be notified, and then you'll
457 automatically be notified of progress on your bug as I make changes.
458  
459  
460  
461  
462 =head1 SUPPORT
463  
464 You can find documentation for this module with the perldoc command.
465  
466 perldoc Text::CSV::SQLhelper
467  
468  
469 You can also look for information at:
470  
471 =over 4
472  
473 =item * RT: CPAN's request tracker
474  
475 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Text-CSV-SQLhelper>
476  
477 =item * AnnoCPAN: Annotated CPAN documentation
478  
479 L<http://annocpan.org/dist/Text-CSV-SQLhelper>
480  
481 =item * CPAN Ratings
482  
483 L<http://cpanratings.perl.org/d/Text-CSV-SQLhelper>
484  
485 =item * Search CPAN
486  
487 L<http://search.cpan.org/dist/Text-CSV-SQLhelper/>
488  
489 =back
490  
491  
492 =head1 ACKNOWLEDGEMENTS
493  
494  
495 =head1 COPYRIGHT & LICENSE
496  
497 Copyright 2010 Zane C. Bowers, all rights reserved.
498  
499 This program is free software; you can redistribute it and/or modify it
500 under the same terms as Perl itself.
501  
502  
503 =cut
504  
505 2 1; # End of Text::CSV::SQLhelper
↑ Summary