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