From: Ken Youens-Clark Date: Fri, 6 Jun 2003 22:31:56 +0000 (+0000) Subject: Cleaned up code (mostly cosmetic), added normalization of column name, X-Git-Tag: v0.02~79 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8c7f5c7b14f9cecc8440b04c98b0eb9a35d64b17;p=dbsrgits%2FSQL-Translator.git Cleaned up code (mostly cosmetic), added normalization of column name, use Schema objects, added a good quote. --- diff --git a/lib/SQL/Translator/Parser/Excel.pm b/lib/SQL/Translator/Parser/Excel.pm index 1ef080a..3796baf 100644 --- a/lib/SQL/Translator/Parser/Excel.pm +++ b/lib/SQL/Translator/Parser/Excel.pm @@ -52,6 +52,12 @@ use base qw(Exporter); @EXPORT_OK = qw(parse); +my %ET_to_ST = ( + 'Text' => 'VARCHAR', + 'Date' => 'DATETIME', + 'Numeric' => 'DOUBLE', +); + # ------------------------------------------------------------------- # parse($tr, $data) # @@ -60,46 +66,60 @@ use base qw(Exporter); # ------------------------------------------------------------------- sub parse { my ($tr, $data) = @_; - my $filename = $tr->filename || return; - my $wb = Spreadsheet::ParseExcel::Workbook->Parse($filename); - my (%parsed, $wb_count, $num); - my $table_no = 0; - - $wb_count = $wb->{'SheetCount'} || 0; - for $num (0 .. $wb_count - 1) { - my $ws = $wb->Worksheet($num); - my $name = $ws->{Name} || ++$table_no; - - $name = normalize_name($name); + my $filename = $tr->filename || return; + my $wb = Spreadsheet::ParseExcel::Workbook->Parse( $filename ); + my $schema = $tr->schema; + my $table_no = 0; + + my %parsed; + my $wb_count = $wb->{'SheetCount'} || 0; + for my $num ( 0 .. $wb_count - 1 ) { + $table_no++; + my $ws = $wb->Worksheet( $num ); + my $table_name = normalize_name( $ws->{'Name'} || "Table$table_no" ); my @cols = $ws->ColRange; next unless $cols[1] > 0; - $parsed{$name} = { - table_name => $name, + $parsed{ $table_name } = { + table_name => $table_name, type => undef, indices => [ {} ], fields => { }, }; + my $table = $schema->add_table( name => $table_name ); - for my $col ($cols[0] .. $cols[1]) { - my $cell = $ws->Cell(0, $col); - $parsed{$name}->{'fields'}->{$cell->{Val}} = { + for my $col ( $cols[0] .. $cols[1] ) { + my $cell = $ws->Cell(0, $col); + my $col_name = normalize_name( $cell->{'Val'} ); + my $data_type = ET_to_ST( $cell->{'Type'} ); + + $parsed{ $table_name }{'fields'}{ $col_name } = { type => 'field', order => $col, - name => $cell->{Val}, - - # Default datatype is 'char' - data_type => ET_to_ST($cell->{Type}), - - # default size is 8bits; something more reasonable? + name => $col_name, + data_type => $data_type, size => [ 255 ], null => 1, default => '', is_auto_inc => undef, - # field field is the primary key + # first field is the primary key is_primary_key => ($col == 0) ? 1 : undef, + }; + + my $field = $table->add_field( + name => $col_name, + data_type => $data_type, + default_value => '', + size => 255, + is_nullable => 1, + is_auto_increment => undef, + ) or die $table->error; + + if ( $col == 0 ) { + $table->primary_key( $field->name ); + $field->is_primary_key(1); } } } @@ -107,11 +127,6 @@ sub parse { return \%parsed; } -my %ET_to_ST = ( - 'Text' => 'VARCHAR', - 'Date' => 'DATETIME', - 'Numeric' => 'DOUBLE', -); sub ET_to_ST { my $et = shift; $ET_to_ST{$et} || $ET_to_ST{'Text'}; @@ -119,6 +134,13 @@ sub ET_to_ST { 1; +# ------------------------------------------------------------------- +# Education is an admirable thing, +# but it is as well to remember that +# nothing that is worth knowing can be taught. +# Oscar Wilde +# ------------------------------------------------------------------- + =pod =head1 AUTHORS