« blib/lib/SQL/Translator/Producer/PostgreSQL.pm
↑ Summary
blib/lib/SQL/Translator/Parser/PostgreSQL.pm »
| 1 | package SQL::Translator::Parser::Excel; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||
| 5 | SQL::Translator::Parser::Excel - parser for Excel | ||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | use SQL::Translator; | ||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | my $translator = SQL::Translator->new; | ||||||||||||||||||||||||||||||||||
| 12 | $translator->parser('Excel'); | ||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | Parses an Excel spreadsheet file using Spreadsheet::ParseExcel. | ||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | =head1 OPTIONS | ||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | =over | ||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | =item * scan_fields | ||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | Indicates that the columns should be scanned to determine data types | ||||||||||||||||||||||||||||||||||
| 25 | and field sizes. True by default. | ||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||
| 27 | =back | ||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 33 | 1 | our ($DEBUG, @EXPORT_OK); | |||||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | $DEBUG = 0 unless defined $DEBUG; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 35 | 1 | our $VERSION = '1.66'; | |||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||
| 37 | 1 | ▶ | use Spreadsheet::ParseExcel; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 38 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 39 | 1 | ▶ | use SQL::Translator::Utils qw(debug normalize_name); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | 1 | @EXPORT_OK = qw(parse); | |||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||
| 45 | 1 | my %ET_to_ST = ( | |||||||||||||||||||||||||||||||||
| 46 | 'Text' => 'VARCHAR', | ||||||||||||||||||||||||||||||||||
| 47 | 'Date' => 'DATETIME', | ||||||||||||||||||||||||||||||||||
| 48 | 'Numeric' => 'DOUBLE', | ||||||||||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | # ------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 52 | # parse($tr, $data) | ||||||||||||||||||||||||||||||||||
| 53 | # | ||||||||||||||||||||||||||||||||||
| 54 | # Note that $data, in the case of this parser, is unuseful. | ||||||||||||||||||||||||||||||||||
| 55 | # Spreadsheet::ParseExcel works on files, not data streams. | ||||||||||||||||||||||||||||||||||
| 56 | # ------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | sub parse { | ||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=35 SCAR=36.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 58 | 1 | my ($tr, $data) = @_; | |||||||||||||||||||||||||||||||||
| 59 | 1 | my $args = $tr->parser_args; | |||||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | my $filename = $tr->filename || return; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$tr->filename || (return)
MC/DC0%0/1
$tr->filename || (return)
$tr->filename
Truth table50%1/2
A
A= $tr->filename
| |||||||||||||||||||||||||||||||||||
| 61 | 1 | my $wb = Spreadsheet::ParseExcel::Workbook->Parse($filename); | |||||||||||||||||||||||||||||||||
| 62 | 1 | my $schema = $tr->schema; | |||||||||||||||||||||||||||||||||
| 63 | 1 | my $table_no = 0; | |||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | 1 | ▶ | my $wb_count = $wb->{'SheetCount'} || 0; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$wb->{'SheetCount'} || 0
MC/DC0%0/1
$wb->{'SheetCount'} || 0
$wb->{'SheetCount'}
Truth table50%1/2
A
A= $wb->{'SheetCount'}
| |||||||||||||||||||||||||||||||||||
| 66 | 1 | for my $num (0 .. $wb_count - 1) { | |||||||||||||||||||||||||||||||||
| 67 | 3 | $table_no++; | |||||||||||||||||||||||||||||||||
| 68 | 3 | my $ws = $wb->Worksheet($num); | |||||||||||||||||||||||||||||||||
| 69 | 3 | ▶ | my $table_name = normalize_name($ws->{'Name'} || "Table$table_no"); | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ws->{'Name'} || "Table$table_no"
MC/DC0%0/1
$ws->{'Name'} || "Table$table_no"
$ws->{'Name'}
Truth table50%1/2
A
A= $ws->{'Name'}
| |||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||
| 71 | 3 | my @cols = $ws->ColRange; | |||||||||||||||||||||||||||||||||
| 72 | 3 | ▶ | next unless $cols[1] > 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | 1 | my $table = $schema->add_table(name => $table_name); | |||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | 1 | my @field_names = (); | |||||||||||||||||||||||||||||||||
| 77 | 1 | for my $col ($cols[0] .. $cols[1]) { | |||||||||||||||||||||||||||||||||
| 78 | 7 | my $cell = $ws->Cell(0, $col); | |||||||||||||||||||||||||||||||||
| 79 | 7 | my $col_name = normalize_name($cell->{'Val'}); | |||||||||||||||||||||||||||||||||
| 80 | 7 | my $data_type = ET_to_ST($cell->{'Type'}); | |||||||||||||||||||||||||||||||||
| 81 | 7 | push @field_names, $col_name; | |||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||
| 83 | 7 | ▶ | my $field = $table->add_field( | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 84 | name => $col_name, | ||||||||||||||||||||||||||||||||||
| 85 | data_type => $data_type, | ||||||||||||||||||||||||||||||||||
| 86 | default_value => '', | ||||||||||||||||||||||||||||||||||
| 87 | size => 255, | ||||||||||||||||||||||||||||||||||
| 88 | is_nullable => 1, | ||||||||||||||||||||||||||||||||||
| 89 | is_auto_increment => undef, | ||||||||||||||||||||||||||||||||||
| 90 | ) or die $table->error; | ||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||
| 92 | 7 | ▶ | if ($col == 0) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 93 | 1 | $table->primary_key($field->name); | |||||||||||||||||||||||||||||||||
| 94 | 1 | $field->is_primary_key(1); | |||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | # | ||||||||||||||||||||||||||||||||||
| 99 | # If directed, look at every field's values to guess size and type. | ||||||||||||||||||||||||||||||||||
| 100 | # | ||||||||||||||||||||||||||||||||||
| 101 | 1 | ▶ | unless (defined $args->{'scan_fields'} | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $args->{"scan_fields"} and $args->{"scan_fields"} == 0
MC/DC0%0/2
defined $args->{"scan_fields"} and $args->{"scan_fields"} == 0
defined $args->{"scan_fields"}
$args->{"scan_fields"} == 0
Truth table33%1/3
A and B
A= defined $args->{"scan_fields"}B= $args->{"scan_fields"} == 0
| |||||||||||||||||||||||||||||||||||
| 102 | && $args->{'scan_fields'} == 0) { | ||||||||||||||||||||||||||||||||||
| 103 | 1 | my %field_info = map { $_, {} } @field_names; | |||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||
| 105 | 1 | ▶ | for ( | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $ws->{"MaxRow"} and $iR <= $ws->{"MaxRow"}
MC/DC50%1/2
defined $ws->{"MaxRow"} and $iR <= $ws->{"MaxRow"}
defined $ws->{"MaxRow"}
$iR <= $ws->{"MaxRow"}
Truth table66%2/3
A and B
A= defined $ws->{"MaxRow"}B= $iR <= $ws->{"MaxRow"}
| |||||||||||||||||||||||||||||||||||
| 106 | my $iR = $ws->{'MinRow'} == 0 ? 1 : $ws->{'MinRow'}; | ||||||||||||||||||||||||||||||||||
| 107 | defined $ws->{'MaxRow'} && $iR <= $ws->{'MaxRow'}; | ||||||||||||||||||||||||||||||||||
| 108 | $iR++ | ||||||||||||||||||||||||||||||||||
| 109 | ) { | ||||||||||||||||||||||||||||||||||
| 110 | 4 | ▶ | for (my $iC = $ws->{'MinCol'}; defined $ws->{'MaxCol'} && $iC <= $ws->{'MaxCol'}; $iC++) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ws->{"MaxCol"} and $iC <= $ws->{"MaxCol"}
MC/DC50%1/2
defined $ws->{"MaxCol"} and $iC <= $ws->{"MaxCol"}
defined $ws->{"MaxCol"}
$iC <= $ws->{"MaxCol"}
Truth table66%2/3
A and B
A= defined $ws->{"MaxCol"}B= $iC <= $ws->{"MaxCol"}
| |||||||||||||||||||||||||||||||||||
| 111 | 28 | my $field = $field_names[$iC]; | |||||||||||||||||||||||||||||||||
| 112 | 28 | my $data = $ws->{'Cells'}[$iR][$iC]->{'_Value'}; | |||||||||||||||||||||||||||||||||
| 113 | 28 | ▶ | next if !defined $data || $data eq ''; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $data or $data eq ""
MC/DC50%1/2
not defined $data or $data eq ""
not defined $data
$data eq ""
Truth table66%2/3
not(A) or B
A= not defined $dataB= $data eq ""
| |||||||||||||||||||||||||||||||||||
| 114 | 6 | my $size = [ length $data ]; | |||||||||||||||||||||||||||||||||
| 115 | 6 | my $type; | |||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||
| 117 | 6 | ▶ | if ($data =~ /^-?\d+$/) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 118 | 2 | $type = 'integer'; | |||||||||||||||||||||||||||||||||
| 119 | ▶ | } elsif ($data =~ /^-?[,\d]+\.[\d+]?$/ | |||||||||||||||||||||||||||||||||
|
Condition66%4/6
$data =~ /^-?[,\d]+\.[\d+]?$/ or $data =~ /^-?[,\d]+?\.\d+$/ or $data =~ /^-?\.\d+$/
$data =~ /^-?[,\d]+\.[\d+]?$/ or $data =~ /^-?[,\d]+?\.\d+$/
MC/DC33%1/3
$data =~ /^-?[,\d]+\.[\d+]?$/ or $data =~ /^-?[,\d]+?\.\d+$/ or $data =~ /^-?\.\d+$/
$data =~ /^-?[,\d]+\.[\d+]?$/
$data =~ /^-?[,\d]+?\.\d+$/
$data =~ /^-?\.\d+$/
Truth table50%2/4
A or B or C
A= $data =~ /^-?[,\d]+\.[\d+]?$/B= $data =~ /^-?[,\d]+?\.\d+$/C= $data =~ /^-?\.\d+$/
| |||||||||||||||||||||||||||||||||||
| 120 | || $data =~ /^-?[,\d]+?\.\d+$/ | ||||||||||||||||||||||||||||||||||
| 121 | || $data =~ /^-?\.\d+$/) { | ||||||||||||||||||||||||||||||||||
| 122 | 1 | $type = 'float'; | |||||||||||||||||||||||||||||||||
| 123 | 1 | ▶ | my ($w, $d) = map { s/,//g; length $_ || 1 } | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
length $_ || 1
MC/DC0%0/1
length $_ || 1
length $_
Truth table50%1/2
A
A= length $_
| |||||||||||||||||||||||||||||||||||
| 124 | split(/\./, $data); | ||||||||||||||||||||||||||||||||||
| 125 | 1 | $size = [ $w + $d, $d ]; | |||||||||||||||||||||||||||||||||
| 126 | } else { | ||||||||||||||||||||||||||||||||||
| 127 | 3 | $type = 'char'; | |||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||
| 130 | 6 | for my $i (0, 1) { | |||||||||||||||||||||||||||||||||
| 131 | 12 | ▶ | next unless defined $size->[$i]; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 132 | 7 | ▶ | my $fsize = $field_info{$field}{'size'}[$i] || 0; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$field_info{$field}{'size'}[$i] || 0
MC/DC0%0/1
$field_info{$field}{'size'}[$i] || 0
$field_info{$field}{'size'}[$i]
Truth table50%1/2
A
A= $field_info{$field}{'size'}[$i]
| |||||||||||||||||||||||||||||||||||
| 133 | 7 | ▶ | if ($size->[$i] > $fsize) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 134 | 7 | $field_info{$field}{'size'}[$i] = $size->[$i]; | |||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||
| 138 | 6 | $field_info{$field}{$type}++; | |||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||
| 142 | 1 | for my $field (keys %field_info) { | |||||||||||||||||||||||||||||||||
| 143 | 7 | ▶ | my $size = $field_info{$field}{'size'} || [1]; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$field_info{$field}{'size'} || [1]
MC/DC100%1/1
$field_info{$field}{'size'} || [1]
$field_info{$field}{'size'}
Truth table100%2/2
A
A= $field_info{$field}{'size'}
| |||||||||||||||||||||||||||||||||||
| 144 | my $data_type | ||||||||||||||||||||||||||||||||||
| 145 | = $field_info{$field}{'char'} ? 'char' | ||||||||||||||||||||||||||||||||||
| 146 | : $field_info{$field}{'float'} ? 'float' | ||||||||||||||||||||||||||||||||||
| 147 | 7 | ▶ | : $field_info{$field}{'integer'} ? 'integer' | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 148 | : 'char'; | ||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||
| 150 | 7 | ▶ | if ($data_type eq 'char' && scalar @$size == 2) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$data_type eq "char" and scalar @$size == 2
MC/DC0%0/2
$data_type eq "char" and scalar @$size == 2
$data_type eq "char"
scalar @$size == 2
Truth table66%2/3
A and B
A= $data_type eq "char"B= scalar @$size == 2
| |||||||||||||||||||||||||||||||||||
| 151 | 0 | $size = [ $size->[0] + $size->[1] ]; | |||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||
| 154 | 7 | my $field = $table->get_field($field); | |||||||||||||||||||||||||||||||||
| 155 | 7 | ▶ | $field->size($size) if $size; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 156 | 7 | $field->data_type($data_type); | |||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||
| 161 | 1 | return 1; | |||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||
| 164 | 7 | ▶ | sub ET_to_ST { | ||||||||||||||||||||||||||||||||
|
Subroutine
ET_to_ST: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 165 | 7 | my $et = shift; | |||||||||||||||||||||||||||||||||
| 166 | 7 | ▶ | $ET_to_ST{$et} || $ET_to_ST{'Text'}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||
| 169 | 1 | 1; | |||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||
| 171 | # ------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 172 | # Education is an admirable thing, | ||||||||||||||||||||||||||||||||||
| 173 | # but it is as well to remember that | ||||||||||||||||||||||||||||||||||
| 174 | # nothing that is worth knowing can be taught. | ||||||||||||||||||||||||||||||||||
| 175 | # Oscar Wilde | ||||||||||||||||||||||||||||||||||
| 176 | # ------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||
| 178 | =pod | ||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||
| 180 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||
| 182 | Mike Mellilo <mmelillo@users.sourceforge.net>, | ||||||||||||||||||||||||||||||||||
| 183 | darren chamberlain E<lt>dlc@users.sourceforge.netE<gt>, | ||||||||||||||||||||||||||||||||||
| 184 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>. | ||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||
| 186 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||
| 188 | Spreadsheet::ParseExcel, SQL::Translator. | ||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||
| 190 | =cut | ||||||||||||||||||||||||||||||||||