Commit | Line | Data |
19de1991 |
1 | package SQL::Translator::Parser::MySQL; |
2 | |
3 | # ------------------------------------------------------------------- |
4 | # ------------------------------------------------------------------- |
5 | # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>, |
6 | # darren chamberlain <darren@cpan.org>, |
7 | # Chris Mungall <cjm@fruitfly.org> |
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 | The basic point of this module is to parse out any SQL or DB Schema information |
39 | from an Excel spreadsheet file. |
40 | |
41 | =cut |
42 | |
43 | use strict; |
44 | use vars qw[ $DEBUG $VERSION @EXPORT_OK ]; |
45 | $DEBUG = 0 unless defined $DEBUG; |
46 | |
47 | use Data::Dumper; |
48 | #use Spreadsheet::ParseExcel; |
49 | use Exporter; |
50 | use base qw(Exporter); |
51 | |
52 | @EXPORT_OK = qw(parse); |
53 | |
54 | |
55 | |
56 | |
57 | # ------------------------------------------------------------------- |
58 | sub parse { |
59 | my ( $translator, $data ) = @_; |
60 | my $parsed = { |
61 | table1 => { |
62 | "type" => undef, |
63 | "indices" => [ { } ], |
64 | "fields" => { }, |
65 | }, |
66 | }; |
67 | |
68 | |
69 | my $tr = new Spreadsheet::ParseExcel; |
70 | $tr->Parse($data); |
71 | my ($R, $C); |
72 | $R = 1; # For now we will assume all column names are in the first row |
73 | |
74 | my @parsed = map { return $tr->{Cells}[$R][$C] } ( $C = $tr->{MinCol} ; $C <= $tr->{MaxCol} ; $C++;) ; |
75 | |
76 | |
77 | for (my $i = 0; $i < @parsed; $i++) { |
78 | $parsed->{"table1"}->{"fields"}->{$parsed[$i]} = { |
79 | type => "field", |
80 | order => $i, |
81 | name => $parsed[$i], |
82 | |
83 | # Default datatype is "char" |
84 | data_type => "char", |
85 | |
86 | # default size is 8bits; something more reasonable? |
87 | size => 255, |
88 | null => 1, |
89 | default => "", |
90 | is_auto_inc => undef, |
91 | |
92 | # field field is the primary key |
93 | is_primary_key => ($i == 0) ? 1 : undef, |
94 | } |
95 | } |
96 | |
97 | |
98 | # Field 0 is primary key, by default, so add an index |
99 | for ($parsed->{"table1"}->{"indices"}->[0]) { |
100 | $_->{"type"} = "primary_key"; |
101 | $_->{"name"} = undef; |
102 | $_->{"fields"} = [ $parsed[0] ]; |
103 | } |
104 | |
105 | |
106 | |
107 | return $parsed; |
108 | |
109 | |
110 | } |
111 | |
112 | 1; |
113 | |
114 | |
115 | =pod |
116 | |
117 | =head1 AUTHOR |
118 | |
119 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>, |
120 | Chris Mungall |
121 | |
122 | =head1 SEE ALSO |
123 | |
124 | perl(1), Spreadsheet::ParseExcel. |
125 | |
126 | =cut |