1 package SQL::Translator::Producer::XML;
3 # -------------------------------------------------------------------
4 # $Id: XML.pm,v 1.10 2003-06-11 04:00:44 kycl4rk 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.10 $ =~ /(\d+)\.(\d+)/;
30 use SQL::Translator::Utils qw(header_comment);
33 my $sqlf_ns = 'http://sqlfairy.sourceforge.net/sqlfairy.xml';
35 # -------------------------------------------------------------------
37 my $translator = shift;
38 my $schema = $translator->schema;
39 my $args = $translator->producer_args;
41 my $io = IO::Scalar->new;
42 my $xml = XML::Writer->new(
45 PREFIX_MAP => { $sqlf_ns => 'sqlf' },
50 $xml->xmlDecl('UTF-8');
51 $xml->comment(header_comment('', ''));
52 $xml->startTag([ $sqlf_ns => 'schema' ]);
54 for my $table ( $schema->get_tables ) {
55 my $table_name = $table->name or next;
56 $xml->startTag ( [ $sqlf_ns => 'table' ] );
57 $xml->dataElement( [ $sqlf_ns => 'name' ], $table_name );
58 $xml->dataElement( [ $sqlf_ns => 'order' ], $table->order );
63 $xml->startTag( [ $sqlf_ns => 'fields' ] );
64 for my $field ( $table->get_fields ) {
65 $xml->startTag( [ $sqlf_ns => 'field' ] );
69 name data_type default_value is_auto_increment
70 is_primary_key is_nullable is_foreign_key order size
73 my $val = $field->$method || '';
74 $xml->dataElement( [ $sqlf_ns => $method ], $val )
76 ( !defined $val && $args->{'emit_empty_tags'} ) );
79 $xml->endTag( [ $sqlf_ns => 'field' ] );
82 $xml->endTag( [ $sqlf_ns => 'fields' ] );
87 $xml->startTag( [ $sqlf_ns => 'indices' ] );
88 for my $index ( $table->get_indices ) {
89 $xml->startTag( [ $sqlf_ns => 'index' ] );
91 for my $method ( qw[ fields name options type ] ) {
92 my $val = $index->$method || '';
93 $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val;
94 $xml->dataElement( [ $sqlf_ns => $method ], $val )
96 ( !defined $val && $args->{'emit_empty_tags'} ) );
99 $xml->endTag( [ $sqlf_ns => 'index' ] );
101 $xml->endTag( [ $sqlf_ns => 'indices' ] );
106 $xml->startTag( [ $sqlf_ns => 'constraints' ] );
107 for my $index ( $table->get_constraints ) {
108 $xml->startTag( [ $sqlf_ns => 'constraint' ] );
112 deferrable expression fields match_type name
113 options on_delete on_update reference_fields
117 my $val = $index->$method || '';
118 $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val;
119 $xml->dataElement( [ $sqlf_ns => $method ], $val )
121 ( !defined $val && $args->{'emit_empty_tags'} ) );
124 $xml->endTag( [ $sqlf_ns => 'constraint' ] );
126 $xml->endTag( [ $sqlf_ns => 'constraints' ] );
128 $xml->endTag( [ $sqlf_ns => 'table' ] );
131 $xml->endTag([ $sqlf_ns => 'schema' ]);
139 # -------------------------------------------------------------------
140 # The eyes of fire, the nostrils of air,
141 # The mouth of water, the beard of earth.
143 # -------------------------------------------------------------------
147 SQL::Translator::Producer::XML - XML output
151 use SQL::Translator::Producer::XML;
155 Meant to create some sort of usable XML output.
159 Takes the following optional C<producer_args>:
163 =item emit_empty_tags
165 If this is set to a true value, then tags corresponding to value-less
166 elements will be emitted. For example, take this schema:
168 CREATE TABLE random (
169 id int auto_increment PRIMARY KEY,
170 foo varchar(255) not null default '',
174 With C<emit_empty_tags> = 1, this will be dumped with XML similar to:
181 <is_auto_inc>1</is_auto_inc>
183 <is_primary_key>1</is_primary_key>
184 <data_type>int</data_type>
186 <constraints></constraints>
193 With C<emit_empty_tags> = 0, you'd get:
200 <is_auto_inc>1</is_auto_inc>
201 <is_primary_key>1</is_primary_key>
202 <data_type>int</data_type>
209 This can lead to dramatic size savings.
217 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>