Reduce $Id to its normal form
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / Excel.pm
1 package SQL::Translator::Parser::Excel;
2
3 # -------------------------------------------------------------------
4 # $Id$
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-2009 SQLFairy Authors
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 # 02111-1307  USA
21 # -------------------------------------------------------------------
22
23 =head1 NAME
24
25 SQL::Translator::Parser::Excel - parser for Excel
26
27 =head1 SYNOPSIS
28
29   use SQL::Translator;
30
31   my $translator = SQL::Translator->new;
32   $translator->parser('Excel');
33
34 =head1 DESCRIPTION
35
36 Parses an Excel spreadsheet file using Spreadsheet::ParseExcel.
37
38 =head1 OPTIONS
39
40 =over
41
42 =item * scan_fields
43
44 Indicates that the columns should be scanned to determine data types
45 and field sizes.  True by default.
46
47 =back
48
49 =cut
50
51 use strict;
52 use vars qw($DEBUG $VERSION @EXPORT_OK);
53 $DEBUG = 0 unless defined $DEBUG;
54 $VERSION = '1.99';
55
56 use Spreadsheet::ParseExcel;
57 use Exporter;
58 use SQL::Translator::Utils qw(debug normalize_name);
59
60 use base qw(Exporter);
61
62 @EXPORT_OK = qw(parse);
63
64 my %ET_to_ST  = (
65     'Text'    => 'VARCHAR',
66     'Date'    => 'DATETIME',
67     'Numeric' => 'DOUBLE',
68 );
69
70 # -------------------------------------------------------------------
71 # parse($tr, $data)
72 #
73 # Note that $data, in the case of this parser, is unuseful.
74 # Spreadsheet::ParseExcel works on files, not data streams.
75 # -------------------------------------------------------------------
76 sub parse {
77     my ($tr, $data) = @_;
78     my $args        = $tr->parser_args;
79     my $filename    = $tr->filename || return;
80     my $wb          = Spreadsheet::ParseExcel::Workbook->Parse( $filename );
81     my $schema      = $tr->schema;
82     my $table_no    = 0;
83
84     my $wb_count = $wb->{'SheetCount'} || 0;
85     for my $num ( 0 .. $wb_count - 1 ) {
86         $table_no++;
87         my $ws         = $wb->Worksheet( $num );
88         my $table_name = normalize_name( $ws->{'Name'} || "Table$table_no" );
89
90         my @cols = $ws->ColRange;
91         next unless $cols[1] > 0;
92
93         my $table = $schema->add_table( name => $table_name );
94
95         my @field_names = ();
96         for my $col ( $cols[0] .. $cols[1] ) {
97             my $cell      = $ws->Cell(0, $col);
98             my $col_name  = normalize_name( $cell->{'Val'} );
99             my $data_type = ET_to_ST( $cell->{'Type'} );
100             push @field_names, $col_name;
101
102             my $field = $table->add_field(
103                 name              => $col_name,
104                 data_type         => $data_type,
105                 default_value     => '',
106                 size              => 255,
107                 is_nullable       => 1,
108                 is_auto_increment => undef,
109             ) or die $table->error;
110
111             if ( $col == 0 ) {
112                 $table->primary_key( $field->name );
113                 $field->is_primary_key(1);
114             }
115         }
116
117         #
118         # If directed, look at every field's values to guess size and type.
119         #
120         unless ( 
121             defined $args->{'scan_fields'} &&
122             $args->{'scan_fields'} == 0
123         ) {
124             my %field_info = map { $_, {} } @field_names;
125
126             for(
127                 my $iR = $ws->{'MinRow'} == 0 ? 1 : $ws->{'MinRow'};
128                 defined $ws->{'MaxRow'} && $iR <= $ws->{'MaxRow'}; 
129                 $iR++
130             ) {
131                for ( 
132                     my $iC = $ws->{'MinCol'};
133                     defined $ws->{'MaxCol'} && $iC <= $ws->{'MaxCol'}; 
134                     $iC++
135                 ) {
136                     my $field = $field_names[ $iC ];
137                     my $data  = $ws->{'Cells'}[ $iR ][ $iC ]->{'_Value'};
138                     next if !defined $data || $data eq '';
139                     my $size  = [ length $data ];
140                     my $type;
141
142                     if ( $data =~ /^-?\d+$/ ) {
143                         $type = 'integer';
144                     }
145                     elsif ( 
146                         $data =~ /^-?[,\d]+\.[\d+]?$/ 
147                         ||
148                         $data =~ /^-?[,\d]+?\.\d+$/  
149                         ||
150                         $data =~ /^-?\.\d+$/  
151                     ) {
152                         $type = 'float';
153                         my ( $w, $d ) = 
154                             map { s/,//g; length $_ || 1 } 
155                             split( /\./, $data )
156                         ;
157                         $size = [ $w + $d, $d ];
158                     }
159                     else {
160                         $type = 'char';
161                     }
162
163                     for my $i ( 0, 1 ) {
164                         next unless defined $size->[ $i ];
165                         my $fsize = $field_info{ $field }{'size'}[ $i ] || 0;
166                         if ( $size->[ $i ] > $fsize ) {
167                             $field_info{ $field }{'size'}[ $i ] = $size->[ $i ];
168                         }
169                     }
170
171                     $field_info{ $field }{ $type }++;
172                 }
173             }
174
175             for my $field ( keys %field_info ) {
176                 my $size      = $field_info{ $field }{'size'} || [ 1 ];
177                 my $data_type = 
178                     $field_info{ $field }{'char'}    ? 'char'    : 
179                     $field_info{ $field }{'float'}   ? 'float'   :
180                     $field_info{ $field }{'integer'} ? 'integer' : 'char';
181
182                 if ( $data_type eq 'char' && scalar @$size == 2 ) {
183                     $size = [ $size->[0] + $size->[1] ];
184                 }
185
186                 my $field = $table->get_field( $field );
187                 $field->size( $size ) if $size;
188                 $field->data_type( $data_type );
189             }
190         }
191     }
192
193     return 1;
194 }
195
196 # -------------------------------------------------------------------
197 sub ET_to_ST {
198     my $et = shift;
199     $ET_to_ST{$et} || $ET_to_ST{'Text'};
200 }
201
202 1;
203
204 # -------------------------------------------------------------------
205 # Education is an admirable thing,
206 # but it is as well to remember that
207 # nothing that is worth knowing can be taught.
208 # Oscar Wilde
209 # -------------------------------------------------------------------
210
211 =pod
212
213 =head1 AUTHORS
214
215 Mike Mellilo <mmelillo@users.sourceforge.net>,
216 darren chamberlain E<lt>dlc@users.sourceforge.netE<gt>,
217 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
218
219 =head1 SEE ALSO
220
221 Spreadsheet::ParseExcel, SQL::Translator.
222
223 =cut