When using attrib_values the attribs are now written in alphabetical order
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / XML / SQLFairy.pm
CommitLineData
0a689100 1package SQL::Translator::Producer::XML::SQLFairy;
2
3# -------------------------------------------------------------------
446dfcbd 4# $Id: SQLFairy.pm,v 1.8 2003-10-21 14:53:08 grommit Exp $
0a689100 5# -------------------------------------------------------------------
6# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7# darren chamberlain <darren@cpan.org>,
8# Chris Mungall <cjm@fruitfly.org>,
9# Mark Addison <mark.addison@itn.co.uk>.
10#
11# This program is free software; you can redistribute it and/or
12# modify it under the terms of the GNU General Public License as
13# published by the Free Software Foundation; version 2.
14#
15# This program is distributed in the hope that it will be useful, but
16# WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18# General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23# 02111-1307 USA
24# -------------------------------------------------------------------
25
26=pod
27
28=head1 NAME
29
a7d50b44 30SQL::Translator::Producer::XML::SQLFairy - SQLFairy's default XML format
0a689100 31
32=head1 SYNOPSIS
33
34 use SQL::Translator;
35
36 my $t = SQL::Translator->new(
37 from => 'MySQL',
a7d50b44 38 to => 'XML-SQLFairy',
0a689100 39 filename => 'schema.sql',
40 show_warnings => 1,
41 add_drop_table => 1,
42 );
43
44 print $t->translate;
45
46=head1 ARGS
47
48Takes the following extra producer args.
49
50=over 4
51
52=item * emit_empty_tags
53
54Default is false, set to true to emit <foo></foo> style tags for undef values
55in the schema.
56
57=item * attrib_values
58
59Set true to use attributes for values of the schema objects instead of tags.
60
61 <!-- attrib_values => 0 -->
62 <table>
63 <name>foo</name>
64 <order>1</order>
65 </table>
66
67 <!-- attrib_values => 1 -->
68 <table name="foo" order="1">
69 </table>
70
71=back
72
73=head1 DESCRIPTION
74
75Creates XML output of a schema.
76
77=cut
78
79use strict;
80use vars qw[ $VERSION @EXPORT_OK ];
446dfcbd 81$VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/;
0a689100 82
83use Exporter;
84use base qw(Exporter);
85@EXPORT_OK = qw(produce);
86
87use IO::Scalar;
88use SQL::Translator::Utils qw(header_comment debug);
89use XML::Writer;
90
91my $Namespace = 'http://sqlfairy.sourceforge.net/sqlfairy.xml';
92my $Name = 'sqlt';
375f0be1 93my $PArgs = {};
0a689100 94
95sub produce {
96 my $translator = shift;
97 my $schema = $translator->schema;
98 $PArgs = $translator->producer_args;
99 my $io = IO::Scalar->new;
100 my $xml = XML::Writer->new(
101 OUTPUT => $io,
102 NAMESPACES => 1,
103 PREFIX_MAP => { $Namespace => $Name },
104 DATA_MODE => 1,
105 DATA_INDENT => 2,
106 );
107
108 $xml->xmlDecl('UTF-8');
109 $xml->comment(header_comment('', ''));
1caf2bb2 110 #$xml->startTag([ $Namespace => 'schema' ]);
111 xml_obj($xml, $schema,
112 tag => "schema", methods => [qw/name database/], end_tag => 0 );
0a689100 113
114 #
115 # Table
116 #
117 for my $table ( $schema->get_tables ) {
118 debug "Table:",$table->name;
119 xml_obj($xml, $table,
120 tag => "table", methods => [qw/name order/], end_tag => 0 );
121
122 #
123 # Fields
124 #
125 $xml->startTag( [ $Namespace => 'fields' ] );
126 for my $field ( $table->get_fields ) {
127 debug " Field:",$field->name;
128 xml_obj($xml, $field,
129 tag =>"field",
130 end_tag => 1,
131 methods =>[qw/name data_type default_value is_auto_increment
13d08089 132 is_primary_key is_nullable is_foreign_key order size
133 comments
0a689100 134 /],
135 );
136 }
137 $xml->endTag( [ $Namespace => 'fields' ] );
138
139 #
140 # Indices
141 #
142 $xml->startTag( [ $Namespace => 'indices' ] );
143 for my $index ( $table->get_indices ) {
144 debug "Index:",$index->name;
145 xml_obj($xml, $index,
146 tag => "index",
147 end_tag => 1,
148 methods =>[qw/fields name options type/],
149 );
150 }
151 $xml->endTag( [ $Namespace => 'indices' ] );
152
153 #
154 # Constraints
155 #
156 $xml->startTag( [ $Namespace => 'constraints' ] );
157 for my $index ( $table->get_constraints ) {
158 debug "Constraint:",$index->name;
159 xml_obj($xml, $index,
160 tag => "constraint",
161 end_tag => 1,
162 methods =>[qw/
163 deferrable expression fields match_type name
164 options on_delete on_update reference_fields
165 reference_table type/],
166 );
167 }
168 $xml->endTag( [ $Namespace => 'constraints' ] );
169
170 $xml->endTag( [ $Namespace => 'table' ] );
171 }
1e3867bf 172
173 #
174 # Views
175 #
176 for my $foo ( $schema->get_views ) {
177 xml_obj($xml, $foo, tag => "view",
178 methods => [qw/name sql fields order/], end_tag => 1 );
179 }
180
181 #
182 # Tiggers
183 #
184 for my $foo ( $schema->get_triggers ) {
185 xml_obj($xml, $foo, tag => "trigger",
186 methods => [qw/name perform_action_when database_event fields on_table
187 action order/], end_tag => 1 );
188 }
0a689100 189
1e3867bf 190 #
191 # Procedures
192 #
193 for my $foo ( $schema->get_procedures ) {
194 xml_obj($xml, $foo, tag => "procedure",
195 methods => [qw/name sql parameters owner comments order/], end_tag=>1 );
196 }
197
0a689100 198 $xml->endTag([ $Namespace => 'schema' ]);
199 $xml->end;
200
201 return $io;
202}
203
204# -------------------------------------------------------------------
1caf2bb2 205#
206# TODO
207# - Doc this sub
208# - Should the Namespace be passed in instead of global? Pass in the same
209# as Writer ie [ NS => TAGNAME ]
210#
0a689100 211sub xml_obj {
212 my ($xml, $obj, %args) = @_;
213 my $tag = $args{'tag'} || '';
214 my $end_tag = $args{'end_tag'} || '';
215 my $attrib_values = $PArgs->{'attrib_values'} || '';
216 my @meths = @{ $args{'methods'} };
217 my $empty_tag = 0;
218
219 if ( $attrib_values and $end_tag ) {
220 $empty_tag = 1;
221 $end_tag = 0;
222 }
223
224 if ( $attrib_values ) {
225 my %attr = map {
226 my $val = $obj->$_;
227 ($_ => ref($val) eq 'ARRAY' ? join(', ', @$val) : $val);
228 } @meths;
229 foreach ( keys %attr ) { delete $attr{$_} unless defined $attr{$_}; }
446dfcbd 230 # Convert to array to ensure consistant (ie not hash) ordering of
231 # attribs
232 my @attr = map { ($_ => $attr{$_}) } sort keys %attr;
233 $empty_tag ? $xml->emptyTag( [ $Namespace => $tag ], @attr )
234 : $xml->startTag( [ $Namespace => $tag ], @attr );
0a689100 235 }
236 else {
237 $xml->startTag( [ $Namespace => $tag ] );
238 xml_objAttr( $xml, $obj, @meths );
239 }
240
241 $xml->endTag( [ $Namespace => $tag ] ) if $end_tag;
242}
243
244# -------------------------------------------------------------------
245# Takes an XML writer, a Schema::* object and a list of methods and
246# adds the XML for those methods.
247#
248sub xml_objAttr {
249 my ($xml, $obj, @methods) = @_;
250 my $emit_empty = $PArgs->{'emit_empty_tags'};
251
3b80481c 252 for my $method ( sort @methods ) {
0a689100 253 my $val = $obj->$method;
254 debug " ".ref($obj)."->$method=",
255 (defined $val ? "'$val'" : "<UNDEF>");
256 next unless $emit_empty || defined $val;
257 $val = '' if not defined $val;
258 $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val;
259 debug " Adding Attr:".$method."='",$val,"'";
260 $xml->dataElement( [ $Namespace => $method ], $val );
261 }
262}
263
2641;
265
266# -------------------------------------------------------------------
267# The eyes of fire, the nostrils of air,
268# The mouth of water, the beard of earth.
269# William Blake
270# -------------------------------------------------------------------
271
272=pod
273
274=head1 AUTHORS
275
276Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
277Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
278Mark Addison E<lt>mark.addison@itn.co.ukE<gt>.
279
280=head1 SEE ALSO
281
a7d50b44 282perl(1), SQL::Translator, SQL::Translator::Parser::XML::SQLFairy,
0a689100 283SQL::Translator::Schema, XML::Writer.
284
285=cut