Added PG support, more POD.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / Excel.pm
CommitLineData
7909b81e 1package SQL::Translator::Parser::Excel;
19de1991 2
3# -------------------------------------------------------------------
5b9183fb 4# $Id: Excel.pm,v 1.9 2003-10-09 21:49:53 kycl4rk Exp $
5eb4a350 5# -------------------------------------------------------------------
19de1991 6# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7# darren chamberlain <darren@cpan.org>,
7909b81e 8# Chris Mungall <cjm@fruitfly.org>,
9# Mike Mellilo <mmelillo@users.sourceforge.net>
19de1991 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
28SQL::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
7909b81e 40Parses an Excel spreadsheet file for SQL::Translator. You can then
41turn the data into a database tables or graphs.
19de1991 42
5eb4a350 43=head1 OPTIONS
44
45=over
46
47=item * scan_fields
48
49Indicates that the columns should be scanned to determine data types
50and field sizes. True by default.
51
52=back
53
19de1991 54=cut
55
56use strict;
ab0aa010 57use vars qw($DEBUG $VERSION @EXPORT_OK);
58$DEBUG = 0 unless defined $DEBUG;
5b9183fb 59$VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/;
19de1991 60
7909b81e 61use Spreadsheet::ParseExcel;
19de1991 62use Exporter;
ab0aa010 63use SQL::Translator::Utils qw(debug normalize_name);
64
19de1991 65use base qw(Exporter);
66
67@EXPORT_OK = qw(parse);
68
8c7f5c7b 69my %ET_to_ST = (
70 'Text' => 'VARCHAR',
71 'Date' => 'DATETIME',
72 'Numeric' => 'DOUBLE',
73);
74
19de1991 75# -------------------------------------------------------------------
ab0aa010 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# -------------------------------------------------------------------
19de1991 81sub parse {
ab0aa010 82 my ($tr, $data) = @_;
5eb4a350 83 my $args = $tr->parser_args;
8c7f5c7b 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
8c7f5c7b 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" );
ab0aa010 94
95 my @cols = $ws->ColRange;
96 next unless $cols[1] > 0;
97
8c7f5c7b 98 my $table = $schema->add_table( name => $table_name );
ab0aa010 99
5eb4a350 100 my @field_names = ();
8c7f5c7b 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'} );
5eb4a350 105 push @field_names, $col_name;
8c7f5c7b 106
8c7f5c7b 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);
ab0aa010 119 }
19de1991 120 }
5eb4a350 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'};
5b9183fb 143 next if !defined $data || $data eq '';
5eb4a350 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 ) {
5b9183fb 178 my $size = $field_info{ $field }{'size'} || 0;
5eb4a350 179 my $data_type =
5b9183fb 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 }
5eb4a350 187
188 my $field = $table->get_field( $field );
5b9183fb 189 $field->size( $size ) if $size;
5eb4a350 190 $field->data_type( $data_type );
191 }
192 }
19de1991 193 }
194
f62bd16c 195 return 1;
ab0aa010 196}
19de1991 197
5eb4a350 198# -------------------------------------------------------------------
ab0aa010 199sub ET_to_ST {
200 my $et = shift;
201 $ET_to_ST{$et} || $ET_to_ST{'Text'};
19de1991 202}
203
2041;
205
8c7f5c7b 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
19de1991 213=pod
214
7909b81e 215=head1 AUTHORS
19de1991 216
7909b81e 217Mike Mellilo <mmelillo@users.sourceforge.net>,
5eb4a350 218darren chamberlain E<lt>dlc@users.sourceforge.netE<gt>,
219Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
19de1991 220
221=head1 SEE ALSO
222
5eb4a350 223Spreadsheet::ParseExcel, SQL::Translator.
19de1991 224
225=cut