Force everything to 1.99, hopefully will work
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / Excel.pm
CommitLineData
7909b81e 1package SQL::Translator::Parser::Excel;
19de1991 2
3# -------------------------------------------------------------------
d4f84dd1 4# $Id: Excel.pm 1440 2009-01-17 16:31:57Z jawnsy $
5eb4a350 5# -------------------------------------------------------------------
478f608d 6# Copyright (C) 2002-2009 SQLFairy Authors
19de1991 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
25SQL::Translator::Parser::Excel - parser for Excel
26
27=head1 SYNOPSIS
28
29 use SQL::Translator;
19de1991 30
31 my $translator = SQL::Translator->new;
854b7210 32 $translator->parser('Excel');
19de1991 33
34=head1 DESCRIPTION
35
854b7210 36Parses an Excel spreadsheet file using Spreadsheet::ParseExcel.
19de1991 37
5eb4a350 38=head1 OPTIONS
39
40=over
41
42=item * scan_fields
43
44Indicates that the columns should be scanned to determine data types
45and field sizes. True by default.
46
47=back
48
19de1991 49=cut
50
51use strict;
da06ac74 52use vars qw($DEBUG $VERSION @EXPORT_OK);
ab0aa010 53$DEBUG = 0 unless defined $DEBUG;
da06ac74 54$VERSION = '1.99';
19de1991 55
7909b81e 56use Spreadsheet::ParseExcel;
19de1991 57use Exporter;
ab0aa010 58use SQL::Translator::Utils qw(debug normalize_name);
59
19de1991 60use base qw(Exporter);
61
62@EXPORT_OK = qw(parse);
63
8c7f5c7b 64my %ET_to_ST = (
65 'Text' => 'VARCHAR',
66 'Date' => 'DATETIME',
67 'Numeric' => 'DOUBLE',
68);
69
19de1991 70# -------------------------------------------------------------------
ab0aa010 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# -------------------------------------------------------------------
19de1991 76sub parse {
ab0aa010 77 my ($tr, $data) = @_;
5eb4a350 78 my $args = $tr->parser_args;
8c7f5c7b 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
8c7f5c7b 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" );
ab0aa010 89
90 my @cols = $ws->ColRange;
91 next unless $cols[1] > 0;
92
8c7f5c7b 93 my $table = $schema->add_table( name => $table_name );
ab0aa010 94
5eb4a350 95 my @field_names = ();
8c7f5c7b 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'} );
5eb4a350 100 push @field_names, $col_name;
8c7f5c7b 101
8c7f5c7b 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);
ab0aa010 114 }
19de1991 115 }
5eb4a350 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'};
5b9183fb 138 next if !defined $data || $data eq '';
5eb4a350 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';
7461b4ef 153 my ( $w, $d ) =
154 map { s/,//g; length $_ || 1 }
155 split( /\./, $data )
156 ;
157 $size = [ $w + $d, $d ];
5eb4a350 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 ) {
9979410c 176 my $size = $field_info{ $field }{'size'} || [ 1 ];
5eb4a350 177 my $data_type =
5b9183fb 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 }
5eb4a350 185
186 my $field = $table->get_field( $field );
5b9183fb 187 $field->size( $size ) if $size;
5eb4a350 188 $field->data_type( $data_type );
189 }
190 }
19de1991 191 }
192
f62bd16c 193 return 1;
ab0aa010 194}
19de1991 195
5eb4a350 196# -------------------------------------------------------------------
ab0aa010 197sub ET_to_ST {
198 my $et = shift;
199 $ET_to_ST{$et} || $ET_to_ST{'Text'};
19de1991 200}
201
2021;
203
8c7f5c7b 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
19de1991 211=pod
212
7909b81e 213=head1 AUTHORS
19de1991 214
7909b81e 215Mike Mellilo <mmelillo@users.sourceforge.net>,
5eb4a350 216darren chamberlain E<lt>dlc@users.sourceforge.netE<gt>,
217Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
19de1991 218
219=head1 SEE ALSO
220
5eb4a350 221Spreadsheet::ParseExcel, SQL::Translator.
19de1991 222
223=cut