Added a rule to MySQL parser to disregard "DROP...;" statements, filled out
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / XML.pm
1 package SQL::Translator::Producer::XML;
2
3 # -------------------------------------------------------------------
4 # $Id: XML.pm,v 1.4 2002-11-28 04:21:07 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002 Ken Y. Clark <kclark@cpan.org>,
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 =head1 NAME
25
26 SQL::Translator::Producer::XML - XML output
27
28 =head1 SYNOPSIS
29
30   use SQL::Translator::Producer::XML;
31
32 =head1 DESCRIPTION
33
34 Meant to create some sort of usable XML output.
35
36 =cut
37
38 use strict;
39 use vars qw[ $VERSION $XML ];
40 $VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/;
41
42 # -------------------------------------------------------------------
43 sub produce {
44     my ( $translator, $data ) = @_;
45     my $indent = 0;
46     aggregate( '<schema>', $indent );
47     
48     $indent++;
49     for my $table ( 
50         map  { $_->[1] }
51         sort { $a->[0] <=> $b->[0] }
52         map  { [ $_->{'order'}, $_ ] }
53         values %$data
54     ) { 
55         aggregate( '<table>', $indent );
56         $indent++;
57
58         aggregate( "<name>$table->{'table_name'}</name>", $indent );
59         aggregate( "<order>$table->{'order'}</order>", $indent );
60
61         #
62         # Fields
63         #
64         aggregate( '<fields>', $indent );
65         for my $field ( 
66             map  { $_->[1] }
67             sort { $a->[0] <=> $b->[0] }
68             map  { [ $_->{'order'}, $_ ] }
69             values %{ $table->{'fields'} }
70         ) {
71             aggregate( '<field>', ++$indent );
72             $indent++;
73
74             for my $key ( keys %$field ) {
75                 my $val = defined $field->{ $key } ? $field->{ $key } : '';
76                    $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val;
77                 aggregate( "<$key>$val</$key>", $indent );
78             }
79
80             $indent--;
81             aggregate( "</field>", $indent-- );
82         }
83         aggregate( "</fields>", $indent );
84
85         #
86         # Indices
87         #
88         aggregate( '<indices>', $indent );
89         for my $index ( @{ $table->{'indices'} } ) {
90             aggregate( '<index>', ++$indent );
91             $indent++;
92
93             for my $key ( keys %$index ) {
94                 my $val = defined $index->{ $key } ? $index->{ $key } : '';
95                    $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val;
96                 aggregate( "<$key>$val</$key>", $indent );
97             }
98
99             $indent--;
100             aggregate( "</index>", $indent-- );
101         }
102         aggregate( "</indices>", $indent );
103
104         $indent--;
105         aggregate( "</table>", $indent );
106     }
107
108     $indent--;
109     aggregate( '</schema>', $indent );
110
111     return $XML;
112 }
113
114 # -------------------------------------------------------------------
115 sub aggregate {
116     my ( $text, $indent ) = @_;
117     $XML .= ('  ' x $indent) . "$text\n";
118 }
119
120 1;
121
122 # -------------------------------------------------------------------
123 # The eyes of fire, the nostrils of air,
124 # The mouth of water, the beard of earth.
125 # William Blake
126 # -------------------------------------------------------------------
127
128 =pod
129
130 =head1 AUTHOR
131
132 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
133
134 =head1 SEE ALSO
135
136 XML::Dumper;
137
138 =cut