1 package SQL::Translator::Producer::XML::SQLFairy;
3 # -------------------------------------------------------------------
4 # $Id: SQLFairy.pm,v 1.19 2004-11-05 16:37:00 grommit Exp $
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>.
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.
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.
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
24 # -------------------------------------------------------------------
30 SQL::Translator::Producer::XML::SQLFairy - SQLFairy's default XML format
36 my $t = SQL::Translator->new(
39 filename => 'schema.sql',
47 Creates XML output of a schema, in the flavor of XML used natively by the
48 SQLFairy project (L<SQL::Translator>). This format is detailed here.
50 The XML lives in the C<http://sqlfairy.sourceforge.net/sqlfairy.xml> namespace.
51 With a root element of <schema>.
53 Objects in the schema are mapped to tags of the same name as the objects class
56 The attributes of the objects (e.g. $field->name) are mapped to attributes of
57 the tag, except for sql, comments and action, which get mapped to child data
60 List valued attributes (such as the list of fields in an index)
61 get mapped to comma seperated lists of values in the attribute.
63 Child objects, such as a tables fields, get mapped to child tags wrapped in a
64 set of container tags using the plural of their contained classes name.
66 An objects's extra attribute (a hash of arbitary data) is
67 mapped to a tag called extra, with the hash of data as attributes, sorted into
72 <schema name="" database=""
73 xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
76 <table name="Story" order="1">
78 <field name="id" data_type="BIGINT" size="20"
79 is_nullable="0" is_auto_increment="1" is_primary_key="1"
80 is_foreign_key="0" order="3">
81 <extra ZEROFILL="1" />
84 <field name="created" data_type="datetime" size="0"
85 is_nullable="1" is_auto_increment="0" is_primary_key="0"
86 is_foreign_key="0" order="1">
93 <index name="foobar" type="NORMAL" fields="foo,bar" options="" />
99 <view name="email_list" fields="email" order="1">
100 <sql>SELECT email FROM Basic WHERE email IS NOT NULL</sql>
106 To see a complete example of the XML translate one of your schema :)
108 $ sqlt -f MySQL -t XML-SQLFairy schema.sql
116 Set to true to use the default namespace prefix of 'sqlf', instead of using
117 the default namespace for
118 C<http://sqlfairy.sourceforge.net/sqlfairy.xml namespace>
122 <!-- add_prefix=0 -->
125 <!-- add_prefix=1 -->
126 <sqlf:field name="foo" />
130 Set to the namespace prefix you want to use for the
131 C<http://sqlfairy.sourceforge.net/sqlfairy.xml namespace>
135 <!-- prefix='foo' -->
136 <foo:field name="foo" />
140 If true (the default) inserts newlines around the XML, otherwise the schema is
145 When using newlines the number of whitespace characters to use as the indent.
146 Default is 2, set to 0 to turn off indenting.
152 The previous version of the SQLFairy XML allowed the attributes of the the
153 schema objects to be written as either xml attributes or as data elements, in
154 any combination. The old producer could produce attribute only or data element
155 only versions. While this allowed for lots of flexibility in writing the XML
156 the result is a great many possible XML formats, not so good for DTD writing,
157 XPathing etc! So we have moved to a fixed version described above.
159 This version of the producer will now only produce the new style XML.
160 To convert your old format files simply pass them through the translator :)
162 $ sqlt -f XML-SQLFairy -t XML-SQLFairy schema-old.xml > schema-new.xml
167 use vars qw[ $VERSION @EXPORT_OK ];
168 $VERSION = sprintf "%d.%02d", q$Revision: 1.19 $ =~ /(\d+)\.(\d+)/;
171 use base qw(Exporter);
172 @EXPORT_OK = qw(produce);
175 use SQL::Translator::Utils qw(header_comment debug);
177 # Will someone fix XML::Writer already?
183 # Which schema object attributes (methods) to write as xml elements rather than
184 # as attributes. e.g. <comments>blah, blah...</comments>
185 my @MAP_AS_ELEMENTS = qw/sql comments action extra/;
189 my $Namespace = 'http://sqlfairy.sourceforge.net/sqlfairy.xml';
194 my $translator = shift;
195 my $schema = $translator->schema;
196 $PArgs = $translator->producer_args;
197 my $newlines = defined $PArgs->{newlines} ? $PArgs->{newlines} : 1;
198 my $indent = defined $PArgs->{indent} ? $PArgs->{indent} : 2;
199 my $io = IO::Scalar->new;
201 # Setup the XML::Writer and set the namespace
203 $prefix = $Name if $PArgs->{add_prefix};
204 $prefix = $PArgs->{prefix} if $PArgs->{prefix};
205 my $xml = XML::Writer->new(
208 PREFIX_MAP => { $Namespace => $prefix },
209 DATA_MODE => $newlines,
210 DATA_INDENT => $indent,
214 $xml->xmlDecl('UTF-8');
215 $xml->comment(header_comment('', ''));
216 xml_obj($xml, $schema,
217 tag => "schema", methods => [qw/name database extra/], end_tag => 0 );
222 $xml->startTag( [ $Namespace => "tables" ] );
223 for my $table ( $schema->get_tables ) {
224 debug "Table:",$table->name;
225 xml_obj($xml, $table,
227 methods => [qw/name order extra/],
234 xml_obj_children( $xml, $table,
237 name data_type size is_nullable default_value is_auto_increment
238 is_primary_key is_foreign_key extra comments order
245 xml_obj_children( $xml, $table,
247 collection_tag => "indices",
248 methods => [qw/name type fields options extra/],
254 xml_obj_children( $xml, $table,
257 name type fields reference_table reference_fields
258 on_delete on_update match_type expression options deferrable
263 $xml->endTag( [ $Namespace => 'table' ] );
265 $xml->endTag( [ $Namespace => 'tables' ] );
270 xml_obj_children( $xml, $schema,
272 methods => [qw/name sql fields order extra/],
278 xml_obj_children( $xml, $schema,
280 methods => [qw/name database_event action on_table perform_action_when
281 fields order extra/],
287 xml_obj_children( $xml, $schema,
289 methods => [qw/name sql parameters owner comments order extra/],
292 $xml->endTag([ $Namespace => 'schema' ]);
300 # Takes and XML::Write object, Schema::* parent object, the tag name,
301 # the collection name and a list of methods (of the children) to write as XML.
302 # The collection name defaults to the name with an s on the end and is used to
303 # work out the method to get the children with. eg a name of 'foo' gives a
304 # collection of foos and gets the members using ->get_foos.
306 sub xml_obj_children {
307 my ($xml,$parent) = (shift,shift);
309 my ($name,$collection_name,$methods)
310 = @args{qw/tag collection_tag methods/};
311 $collection_name ||= "${name}s";
312 my $meth = "get_$collection_name";
314 my @kids = $parent->$meth;
316 $xml->startTag( [ $Namespace => $collection_name ] );
317 for my $obj ( @kids ) {
324 $xml->endTag( [ $Namespace => $collection_name ] );
328 # Takes an XML::Writer, Schema::* object and list of method names
329 # and writes the obect out as XML. All methods values are written as attributes
330 # except for the methods listed in @MAP_AS_ELEMENTS which get written as child
333 # The attributes/tags are written in the same order as the method names are
337 # - Should the Namespace be passed in instead of global? Pass in the same
338 # as Writer ie [ NS => TAGNAME ]
340 my $elements_re = join("|", @MAP_AS_ELEMENTS);
341 $elements_re = qr/^($elements_re)$/;
343 my ($xml, $obj, %args) = @_;
344 my $tag = $args{'tag'} || '';
345 my $end_tag = $args{'end_tag'} || '';
346 my @meths = @{ $args{'methods'} };
349 # Use array to ensure consistant (ie not hash) ordering of attribs
350 # The order comes from the meths list passed in.
353 foreach ( grep { defined $obj->$_ } @meths ) {
354 my $what = m/$elements_re/ ? \@tags : \@attr;
355 my $val = $_ eq 'extra'
358 $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val;
359 push @$what, $_ => $val;
361 my $child_tags = @tags;
362 $end_tag && !$child_tags
363 ? $xml->emptyTag( [ $Namespace => $tag ], @attr )
364 : $xml->startTag( [ $Namespace => $tag ], @attr );
365 while ( my ($name,$val) = splice @tags,0,2 ) {
366 if ( ref $val eq 'HASH' ) {
367 $xml->emptyTag( [ $Namespace => $name ],
368 map { ($_, $val->{$_}) } sort keys %$val );
371 $xml->dataElement( [ $Namespace => $name ], $val );
374 $xml->endTag( [ $Namespace => $tag ] ) if $child_tags && $end_tag;
379 # -------------------------------------------------------------------
380 # The eyes of fire, the nostrils of air,
381 # The mouth of water, the beard of earth.
383 # -------------------------------------------------------------------
389 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
390 Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
391 Mark Addison E<lt>mark.addison@itn.co.ukE<gt>.
395 L<perl(1)>, L<SQL::Translator>, L<SQL::Translator::Parser::XML::SQLFairy>,
396 L<SQL::Translator::Schema>, L<XML::Writer>.