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