Commit | Line | Data |
7909b81e |
1 | package SQL::Translator::Parser::Excel; |
19de1991 |
2 | |
3 | # ------------------------------------------------------------------- |
478f608d |
4 | # Copyright (C) 2002-2009 SQLFairy Authors |
19de1991 |
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; |
19de1991 |
28 | |
29 | my $translator = SQL::Translator->new; |
854b7210 |
30 | $translator->parser('Excel'); |
19de1991 |
31 | |
32 | =head1 DESCRIPTION |
33 | |
854b7210 |
34 | Parses an Excel spreadsheet file using Spreadsheet::ParseExcel. |
19de1991 |
35 | |
5eb4a350 |
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 | |
19de1991 |
47 | =cut |
48 | |
49 | use strict; |
da06ac74 |
50 | use vars qw($DEBUG $VERSION @EXPORT_OK); |
ab0aa010 |
51 | $DEBUG = 0 unless defined $DEBUG; |
11ad2df9 |
52 | $VERSION = '1.59'; |
19de1991 |
53 | |
7909b81e |
54 | use Spreadsheet::ParseExcel; |
19de1991 |
55 | use Exporter; |
ab0aa010 |
56 | use SQL::Translator::Utils qw(debug normalize_name); |
57 | |
19de1991 |
58 | use base qw(Exporter); |
59 | |
60 | @EXPORT_OK = qw(parse); |
61 | |
8c7f5c7b |
62 | my %ET_to_ST = ( |
63 | 'Text' => 'VARCHAR', |
64 | 'Date' => 'DATETIME', |
65 | 'Numeric' => 'DOUBLE', |
66 | ); |
67 | |
19de1991 |
68 | # ------------------------------------------------------------------- |
ab0aa010 |
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 | # ------------------------------------------------------------------- |
19de1991 |
74 | sub parse { |
ab0aa010 |
75 | my ($tr, $data) = @_; |
5eb4a350 |
76 | my $args = $tr->parser_args; |
8c7f5c7b |
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 | |
8c7f5c7b |
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" ); |
ab0aa010 |
87 | |
88 | my @cols = $ws->ColRange; |
89 | next unless $cols[1] > 0; |
90 | |
8c7f5c7b |
91 | my $table = $schema->add_table( name => $table_name ); |
ab0aa010 |
92 | |
5eb4a350 |
93 | my @field_names = (); |
8c7f5c7b |
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'} ); |
5eb4a350 |
98 | push @field_names, $col_name; |
8c7f5c7b |
99 | |
8c7f5c7b |
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); |
ab0aa010 |
112 | } |
19de1991 |
113 | } |
5eb4a350 |
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'}; |
5b9183fb |
136 | next if !defined $data || $data eq ''; |
5eb4a350 |
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'; |
7461b4ef |
151 | my ( $w, $d ) = |
152 | map { s/,//g; length $_ || 1 } |
153 | split( /\./, $data ) |
154 | ; |
155 | $size = [ $w + $d, $d ]; |
5eb4a350 |
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 ) { |
9979410c |
174 | my $size = $field_info{ $field }{'size'} || [ 1 ]; |
5eb4a350 |
175 | my $data_type = |
5b9183fb |
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 | } |
5eb4a350 |
183 | |
184 | my $field = $table->get_field( $field ); |
5b9183fb |
185 | $field->size( $size ) if $size; |
5eb4a350 |
186 | $field->data_type( $data_type ); |
187 | } |
188 | } |
19de1991 |
189 | } |
190 | |
f62bd16c |
191 | return 1; |
ab0aa010 |
192 | } |
19de1991 |
193 | |
5eb4a350 |
194 | # ------------------------------------------------------------------- |
ab0aa010 |
195 | sub ET_to_ST { |
196 | my $et = shift; |
197 | $ET_to_ST{$et} || $ET_to_ST{'Text'}; |
19de1991 |
198 | } |
199 | |
200 | 1; |
201 | |
8c7f5c7b |
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 | |
19de1991 |
209 | =pod |
210 | |
7909b81e |
211 | =head1 AUTHORS |
19de1991 |
212 | |
7909b81e |
213 | Mike Mellilo <mmelillo@users.sourceforge.net>, |
5eb4a350 |
214 | darren chamberlain E<lt>dlc@users.sourceforge.netE<gt>, |
11ad2df9 |
215 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>. |
19de1991 |
216 | |
217 | =head1 SEE ALSO |
218 | |
5eb4a350 |
219 | Spreadsheet::ParseExcel, SQL::Translator. |
19de1991 |
220 | |
221 | =cut |