9bc310e228d2555eeb30dcfc871044fb48c3428b
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / YAML.pm
1 package SQL::Translator::Parser::YAML;
2
3 # -------------------------------------------------------------------
4 # $Id: YAML.pm,v 1.4 2004-01-25 18:10:55 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 darren chamberlain <darren@cpan.org>,
7 #   Ken Y. Clark <kclark@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);
26 $VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/;
27
28 use SQL::Translator::Schema;
29 use SQL::Translator::Utils qw(header_comment);
30 use Data::Dumper;
31 use YAML qw(Load);
32
33 sub parse {
34     my ($translator, $data) = @_;
35     $data = Load($data);
36     $data = $data->{'schema'};
37
38     warn Dumper( $data ) if $translator->debug;
39
40     my $schema = $translator->schema;
41
42     #
43     # Tables
44     #
45     my @tables = 
46         map   { $data->{'tables'}{ $_->[1] } }
47         sort  { $a->[0] <=> $b->[0] }
48         map   { [ $data->{'tables'}{ $_ }{'order'}, $_ ] }
49         keys %{ $data->{'tables'} }
50     ;
51
52     for my $tdata ( @tables ) {
53         my $table = $schema->add_table(
54             name  => $tdata->{'name'},
55         ) or die $schema->error;
56
57         my @fields = 
58             map   { $tdata->{'fields'}{ $_->[1] } }
59             sort  { $a->[0] <=> $b->[0] }
60             map   { [ $tdata->{'fields'}{ $_ }{'order'}, $_ ] }
61             keys %{ $tdata->{'fields'} }
62         ;
63
64         for my $fdata ( @fields ) {
65             $table->add_field( %$fdata ) or die $table->error;
66             $table->primary_key( $fdata->{'name'} ) 
67                 if $fdata->{'is_primary_key'};
68         }
69
70         for my $idata ( @{ $tdata->{'indices'} || [] } ) {
71             $table->add_index( %$idata ) or die $table->error;
72         }
73
74         for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
75             $table->add_constraint( %$cdata ) or die $table->error;
76         }
77     }
78
79     #
80     # Views
81     #
82     my @views = 
83         map   { $data->{'views'}{ $_->[1] } }
84         sort  { $a->[0] <=> $b->[0] }
85         map   { [ $data->{'views'}{ $_ }{'order'}, $_ ] }
86         keys %{ $data->{'views'} }
87     ;
88
89     for my $vdata ( @views ) {
90         $schema->add_view( %$vdata ) or die $schema->error;
91     }
92
93     #
94     # Triggers
95     #
96     my @triggers = 
97         map   { $data->{'triggers'}{ $_->[1] } }
98         sort  { $a->[0] <=> $b->[0] }
99         map   { [ $data->{'triggers'}{ $_ }{'order'}, $_ ] }
100         keys %{ $data->{'triggers'} }
101     ;
102
103     for my $tdata ( @triggers ) {
104         $schema->add_trigger( %$tdata ) or die $schema->error;
105     }
106
107     #
108     # Procedures
109     #
110     my @procedures = 
111         map   { $data->{'procedures'}{ $_->[1] } }
112         sort  { $a->[0] <=> $b->[0] }
113         map   { [ $data->{'procedures'}{ $_ }{'order'}, $_ ] }
114         keys %{ $data->{'procedures'} }
115     ;
116
117     for my $tdata ( @procedures ) {
118         $schema->add_procedure( %$tdata ) or die $schema->error;
119     }
120
121     return 1;
122 }
123
124 1;
125
126 __END__
127
128 =head1 NAME
129
130 SQL::Translator::Parser::YAML - Parse a YAML representation of a schema
131
132 =head1 SYNOPSIS
133
134     use SQL::Translator;
135
136     my $translator = SQL::Translator->new(parser => "YAML");
137
138 =head1 DESCRIPTION
139
140 C<SQL::Translator::Parser::YAML> parses a schema serialized with YAML.
141
142 =head1 AUTHOR
143
144 Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
145 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.