Commit | Line | Data |
046f18e5 |
1 | package SQL::Translator::Parser::xSV; |
2 | |
3 | #----------------------------------------------------- |
4 | # $Id: xSV.pm,v 1.1 2002-03-25 14:27:23 dlc Exp $ |
5 | #----------------------------------------------------- |
6 | # Copyright (C) 2002 Ken Y. Clark <kycl4rk@users.sourceforge.net>, |
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 | |
24 | use strict; |
25 | use vars qw($VERSION @EXPORT); |
26 | $VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/; |
27 | |
28 | use Exporter; |
29 | use Text::ParseWords qw(quotewords); |
30 | |
31 | use base qw(Exporter); |
32 | @EXPORT = qw(parse); |
33 | |
34 | # Passed a SQL::Translator instance and a string containing the data |
35 | sub parse { |
36 | my ($tr, $data) = @_; |
37 | |
38 | # Skeleton structure, mostly empty |
39 | my $parsed = { |
40 | table1 => { |
41 | "type" => undef, |
42 | "indeces" => [ { } ], |
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 |
73 | for ($parsed->{"table1"}->{"indeces"}->[0]) { |
74 | $_->{"type"} = "primary_key"; |
75 | $_->{"name"} = undef; |
76 | $_->{"fields"} = [ $parsed[0] ]; |
77 | } |
78 | |
79 | return $parsed; |
80 | } |
81 | |
82 | |
83 | 1; |
84 | __END__ |