Attempt to be more robust.lib/SQL/Translator/Validator.pm
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / xSV.pm
1 package SQL::Translator::Parser::xSV;
2
3 # -------------------------------------------------------------------
4 # $Id: xSV.pm,v 1.5 2003-04-17 13:42:45 dlc Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7 #                    darren chamberlain <darren@cpan.org>,
8 #                    Chris Mungall <cjm@fruitfly.org>
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License as
12 # published by the Free Software Foundation; version 2.
13 #
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 # 02111-1307  USA
23 # -------------------------------------------------------------------
24
25 use strict;
26 use vars qw($VERSION @EXPORT);
27 $VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/;
28
29 use Exporter;
30 use Text::ParseWords qw(quotewords);
31
32 use base qw(Exporter);
33 @EXPORT = qw(parse);
34
35 # Passed a SQL::Translator instance and a string containing the data
36 sub parse {
37     my ($tr, $data) = @_;
38
39     # Skeleton structure, mostly empty
40     my $parsed = {
41         table1 => {
42             "type" => undef,
43             "indices" => [ { } ],
44             "fields" => { },
45         },
46     };
47
48     # Discard all but the first line
49     $data = (split m,$/,, $data)[0];
50
51     my @parsed = quotewords(',\s*', 0, $data);
52
53     for (my $i = 0; $i < @parsed; $i++) {
54         $parsed->{"table1"}->{"fields"}->{$parsed[$i]} = {
55             type           => "field",
56             order          => $i,
57             name           => $parsed[$i],
58
59             # Default datatype is "char"
60             data_type      => "char",
61
62             # default size is 8bits; something more reasonable?
63             size           => [ 255 ],
64             null           => 1,
65             default        => "",
66             is_auto_inc    => undef,
67
68             # field field is the primary key
69             is_primary_key => ($i == 0) ? 1 : undef,
70         }
71     }
72
73     # Field 0 is primary key, by default, so add an index
74     for ($parsed->{"table1"}->{"indices"}->[0]) {
75         $_->{"type"} = "primary_key";
76         $_->{"name"} = undef;
77         $_->{"fields"} = [ $parsed[0] ];
78     }
79
80     return $parsed;
81 }
82
83 1;
84 __END__