| 1 | #!/bin/perl | ||
| 2 | |||
| 3 | use Modern::Perl; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use autodie qw(:all); | ||
| 7 | no indirect ':fatal'; | ||
| 8 | |||
| 9 | use Carp; | ||
| 10 | |||
| 11 | use Text::CSV ; | ||
| 12 | use Try::Tiny; | ||
| 13 | |||
| 14 | use Getopt::Euclid qw( :vars<opt_> ); | ||
| 15 | use Data::Dumper; | ||
| 16 | use Text::CSV; | ||
| 17 | use File::Basename; | ||
| 18 | |||
| 19 | use version ; our $VERSION = qv('1.0.1'); | ||
| 20 | |||
| 21 | our $opt_inputfile; | ||
| 22 | my $inputfile = $opt_inputfile; | ||
| 23 | |||
| 24 | my ($filename,$path,$suffix) = fileparse($inputfile,(".csv")); | ||
| 25 | |||
| 26 | my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1 }); | ||
| 27 | open my $fh, "<:encoding(utf8)", $inputfile or die "inputfile: $!"; | ||
| 28 | |||
| 29 | my $ccount=0; | ||
| 30 | my $maxccount=0; | ||
| 31 | my $minccount=99999999; | ||
| 32 | my $li = 0; | ||
| 33 | while ((my $row = $csv->getline ($fh)) && ($li++ <= 100) ) { | ||
| 34 | $ccount = scalar(@$row); | ||
| 35 | $maxccount = $ccount if $maxccount < $ccount; | ||
| 36 | $minccount = $ccount if $minccount > $ccount; | ||
| 37 | } | ||
| 38 | close $fh; | ||
| 39 | |||
| 40 | if (($minccount == $maxccount) ) { | ||
| 41 | { | ||
| 42 | my $first = 1; | ||
| 43 | say "create table BLACKBOOK.${filename}_csv"; | ||
| 44 | foreach my $i ( 1 .. $minccount) { | ||
| 45 | say $first ? "(" : "," | ||
| 46 | , "\t" | ||
| 47 | , "c${i}" | ||
| 48 | , "\t\tvarchar(1000)"; | ||
| 49 | $first=0; | ||
| 50 | } | ||
| 51 | say ");"; | ||
| 52 | say "GO"; | ||
| 53 | } | ||
| 54 | say ""; | ||
| 55 | { | ||
| 56 | my $first = 1; | ||
| 57 | say "create table BLACKBOOK.${filename}_ascii"; | ||
| 58 | foreach my $i ( 1 .. $minccount) { | ||
| 59 | say $first?"(":",", "\t", "c${i}","\t\tvarchar(1000)"; | ||
| 60 | $first=0; | ||
| 61 | } | ||
| 62 | say ");"; | ||
| 63 | say "GO"; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | exit ; | ||
| 69 | |||
| 70 | # ####################################################################################### | ||
| 71 | |||
| 72 | |||
| 73 | END { | ||
| 74 | } | ||
| 75 | |||
| 76 | __DATA__ |