1 package SQL::Translator::Producer::XML;
3 # -------------------------------------------------------------------
4 # $Id: XML.pm,v 1.8 2003-05-06 12:47:27 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>
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.
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.
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
23 # -------------------------------------------------------------------
26 use vars qw[ $VERSION ];
27 $VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/;
30 use SQL::Translator::Utils qw(header_comment);
33 my $sqlf_ns = 'http://sqlfairy.sourceforge.net/sqlfairy.xml';
35 # -------------------------------------------------------------------
37 my ( $translator, $data ) = @_;
38 #my $prargs = $translator->producer_args;
40 my $io = IO::Scalar->new;
42 my $xml = XML::Writer->new(OUTPUT => $io,
44 PREFIX_MAP => { $sqlf_ns => 'sqlf' },
49 $xml->xmlDecl("UTF-8");
50 $xml->comment(header_comment('', ''));
51 $xml->startTag([ $sqlf_ns => "schema" ]);
55 sort { $a->[0] <=> $b->[0] }
56 map { [ $_->{'order'}, $_ ] }
59 $xml->startTag([ $sqlf_ns => "table" ]);
61 $xml->dataElement([ $sqlf_ns => "name" ], $table->{'table_name'});
62 $xml->dataElement([ $sqlf_ns => "order" ], $table->{'order'});
67 $xml->startTag([ $sqlf_ns => "fields" ]);
70 sort { $a->[0] <=> $b->[0] }
71 map { [ $_->{'order'}, $_ ] }
72 values %{ $table->{'fields'} }
74 $xml->startTag([ $sqlf_ns => "field" ]);
76 for my $key ( keys %$field ) {
77 my $val = defined $field->{ $key } ? $field->{ $key } : '';
78 $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val;
79 $xml->dataElement([ $sqlf_ns => $key ], $val)
80 if ($val || (!$val && $prargs->{'emit_empty_tags'}));
83 $xml->endTag([ $sqlf_ns => "field" ]);
85 $xml->endTag([ $sqlf_ns => "fields" ]);
90 $xml->startTag([ $sqlf_ns => "indices" ]);
91 for my $index (@{$table->{'indices'}}) {
92 $xml->startTag([ $sqlf_ns => "index" ]);
94 for my $key (keys %$index) {
95 my $val = defined $index->{ $key } ? $index->{ $key } : '';
96 $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val;
97 $xml->dataElement([ $sqlf_ns => $key ], $val);
100 $xml->endTag([ $sqlf_ns => "index" ]);
102 $xml->endTag([ $sqlf_ns => "indices" ]);
104 $xml->endTag([ $sqlf_ns => "table" ]);
107 $xml->endTag([ $sqlf_ns => "schema" ]);
116 # -------------------------------------------------------------------
117 # The eyes of fire, the nostrils of air,
118 # The mouth of water, the beard of earth.
120 # -------------------------------------------------------------------
124 SQL::Translator::Producer::XML - XML output
128 use SQL::Translator::Producer::XML;
132 Meant to create some sort of usable XML output.
136 Takes the following optional C<producer_args>:
140 =item emit_empty_tags
142 If this is set to a true value, then tags corresponding to value-less
143 elements will be emitted. For example, take this schema:
145 CREATE TABLE random (
146 id int auto_increment PRIMARY KEY,
147 foo varchar(255) not null default '',
151 With C<emit_empty_tags> = 1, this will be dumped with XML similar to:
158 <is_auto_inc>1</is_auto_inc>
160 <is_primary_key>1</is_primary_key>
161 <data_type>int</data_type>
163 <constraints></constraints>
170 With C<emit_empty_tags> = 0, you'd get:
177 <is_auto_inc>1</is_auto_inc>
178 <is_primary_key>1</is_primary_key>
179 <data_type>int</data_type>
186 This can lead to dramatic size savings.
194 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>