Cleaned up code (mostly cosmetic), added normalization of column name,
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / Excel.pm
1 package SQL::Translator::Parser::Excel;
2
3 # -------------------------------------------------------------------
4 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
5 #                    darren chamberlain <darren@cpan.org>,
6 #                    Chris Mungall <cjm@fruitfly.org>,
7 #                    Mike Mellilo <mmelillo@users.sourceforge.net>
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation; version 2.
12 #
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # 02111-1307  USA
22 # -------------------------------------------------------------------
23
24 =head1 NAME
25
26 SQL::Translator::Parser::Excel - parser for Excel
27
28 =head1 SYNOPSIS
29
30   use SQL::Translator;
31   use SQL::Translator::Parser::Excel;
32
33   my $translator = SQL::Translator->new;
34   $translator->parser("SQL::Translator::Parser::Excel");
35
36 =head1 DESCRIPTION
37
38 Parses an Excel spreadsheet file for SQL::Translator.  You can then
39 turn the data into a database tables or graphs.
40
41 =cut
42
43 use strict;
44 use vars qw($DEBUG $VERSION @EXPORT_OK);
45 $DEBUG = 0 unless defined $DEBUG;
46
47 use Spreadsheet::ParseExcel;
48 use Exporter;
49 use SQL::Translator::Utils qw(debug normalize_name);
50
51 use base qw(Exporter);
52
53 @EXPORT_OK = qw(parse);
54
55 my %ET_to_ST  = (
56     'Text'    => 'VARCHAR',
57     'Date'    => 'DATETIME',
58     'Numeric' => 'DOUBLE',
59 );
60
61 # -------------------------------------------------------------------
62 # parse($tr, $data)
63 #
64 # Note that $data, in the case of this parser, is unuseful.
65 # Spreadsheet::ParseExcel works on files, not data streams.
66 # -------------------------------------------------------------------
67 sub parse {
68     my ($tr, $data) = @_;
69     my $filename    = $tr->filename || return;
70     my $wb          = Spreadsheet::ParseExcel::Workbook->Parse( $filename );
71     my $schema      = $tr->schema;
72     my $table_no    = 0;
73
74     my %parsed;
75     my $wb_count = $wb->{'SheetCount'} || 0;
76     for my $num ( 0 .. $wb_count - 1 ) {
77         $table_no++;
78         my $ws         = $wb->Worksheet( $num );
79         my $table_name = normalize_name( $ws->{'Name'} || "Table$table_no" );
80
81         my @cols = $ws->ColRange;
82         next unless $cols[1] > 0;
83
84         $parsed{ $table_name } = {
85             table_name  => $table_name,
86             type        => undef,
87             indices     => [ {} ],
88             fields      => { },
89         };
90         my $table = $schema->add_table( name => $table_name );
91
92         for my $col ( $cols[0] .. $cols[1] ) {
93             my $cell      = $ws->Cell(0, $col);
94             my $col_name  = normalize_name( $cell->{'Val'} );
95             my $data_type = ET_to_ST( $cell->{'Type'} );
96
97             $parsed{ $table_name }{'fields'}{ $col_name } = {
98                 type           => 'field',
99                 order          => $col,
100                 name           => $col_name,
101                 data_type      => $data_type,
102                 size           => [ 255 ],
103                 null           => 1,
104                 default        => '',
105                 is_auto_inc    => undef,
106
107                 # first field is the primary key
108                 is_primary_key => ($col == 0) ? 1 : undef,
109             };
110
111             my $field = $table->add_field(
112                 name              => $col_name,
113                 data_type         => $data_type,
114                 default_value     => '',
115                 size              => 255,
116                 is_nullable       => 1,
117                 is_auto_increment => undef,
118             ) or die $table->error;
119
120             if ( $col == 0 ) {
121                 $table->primary_key( $field->name );
122                 $field->is_primary_key(1);
123             }
124         }
125     }
126
127     return \%parsed;
128 }
129
130 sub ET_to_ST {
131     my $et = shift;
132     $ET_to_ST{$et} || $ET_to_ST{'Text'};
133 }
134
135 1;
136
137 # -------------------------------------------------------------------
138 # Education is an admirable thing,
139 # but it is as well to remember that
140 # nothing that is worth knowing can be taught.
141 # Oscar Wilde
142 # -------------------------------------------------------------------
143
144 =pod
145
146 =head1 AUTHORS
147
148 Mike Mellilo <mmelillo@users.sourceforge.net>,
149 darren chamberlain E<lt>dlc@users.sourceforge.netE<gt>
150 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
151
152 =head1 SEE ALSO
153
154 perl(1), Spreadsheet::ParseExcel.
155
156 =cut