Fixed bug where it was truncating table name needlessly.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / xSV.pm
CommitLineData
046f18e5 1package SQL::Translator::Parser::xSV;
2
49e1eb70 3# -------------------------------------------------------------------
d529894e 4# $Id: xSV.pm,v 1.3 2002-11-22 03:03:40 kycl4rk Exp $
49e1eb70 5# -------------------------------------------------------------------
d529894e 6# Copyright (C) 2002 Ken Y. Clark <kclark@cpan.org>,
046f18e5 7# darren chamberlain <darren@cpan.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
24use strict;
25use vars qw($VERSION @EXPORT);
d529894e 26$VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/;
046f18e5 27
28use Exporter;
29use Text::ParseWords qw(quotewords);
30
31use base qw(Exporter);
32@EXPORT = qw(parse);
33
34# Passed a SQL::Translator instance and a string containing the data
35sub parse {
36 my ($tr, $data) = @_;
37
38 # Skeleton structure, mostly empty
39 my $parsed = {
40 table1 => {
41 "type" => undef,
49e1eb70 42 "indices" => [ { } ],
046f18e5 43 "fields" => { },
44 },
45 };
46
47 # Discard all but the first line
48 $data = (split m,$/,, $data)[0];
49
50 my @parsed = quotewords(',\s*', 0, $data);
51
52 for (my $i = 0; $i < @parsed; $i++) {
53 $parsed->{"table1"}->{"fields"}->{$parsed[$i]} = {
54 type => "field",
55 order => $i,
56 name => $parsed[$i],
57
58 # Default datatype is "char"
59 data_type => "char",
60
61 # default size is 8bits; something more reasonable?
62 size => 255,
63 null => 1,
64 default => "",
65 is_auto_inc => undef,
66
67 # field field is the primary key
68 is_primary_key => ($i == 0) ? 1 : undef,
69 }
70 }
71
72 # Field 0 is primary key, by default, so add an index
49e1eb70 73 for ($parsed->{"table1"}->{"indices"}->[0]) {
046f18e5 74 $_->{"type"} = "primary_key";
75 $_->{"name"} = undef;
76 $_->{"fields"} = [ $parsed[0] ];
77 }
78
79 return $parsed;
80}
81
046f18e5 821;
83__END__