1 package SQL::Translator::Producer::XML::SQLFairy;
3 # -------------------------------------------------------------------
4 # $Id: SQLFairy.pm,v 1.13 2004-07-08 19:34:29 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',
48 Creates XML output of a schema, in SQLFairy format XML.
50 The XML lives in the 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.
55 The attributes of the objects (e.g. $field->name) are mapped to attributes of
56 the tag, except for sql, comments and action, which get mapped to child data
59 List valued attributes (such as the list of fields in an index)
60 get mapped to a comma seperated list of values in the attribute.
62 Child objects, such as a tables fields, get mapped to child tags wrapped in a
63 set of container tags using the plural of their contained classes name.
67 <schema name="" database=""
68 xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
70 <table name="Story" order="1">
73 <field name="created" data_type="datetime" size="0"
74 is_nullable="1" is_auto_increment="0" is_primary_key="0"
75 is_foreign_key="0" 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">
87 <index name="foobar" type="NORMAL" fields="foo,bar" options="" />
92 <view name="email_list" fields="email" order="1">
93 <sql>SELECT email FROM Basic WHERE email IS NOT NULL</sql>
98 To see a complete example of the XML translate one of your schema :)
100 $ sqlt -f MySQL -t XML-SQLFairy schema.sql
104 Doesn't take any extra arguments.
108 The previous version of the SQLFairy XML allowed the attributes of the the
109 schema objects to be written as either xml attributes or as data elements, in
110 any combination. The old producer could produce attribute only or data element
111 only versions. While this allowed for lots of flexibility in writing the XML
112 the result is a great many possible XML formats, not so good for DTD writing,
113 XPathing etc! So we have moved to a fixed version described above.
115 This version of the producer will now only produce the new style XML.
116 To convert your old format files simply pass them through the translator;
118 sqlt -f XML-SQLFairy -t XML-SQLFairy schema-old.xml > schema-new.xml
123 use vars qw[ $VERSION @EXPORT_OK ];
124 $VERSION = sprintf "%d.%02d", q$Revision: 1.13 $ =~ /(\d+)\.(\d+)/;
127 use base qw(Exporter);
128 @EXPORT_OK = qw(produce);
131 use SQL::Translator::Utils qw(header_comment debug);
133 # Will someone fix XML::Writer already?
139 my $Namespace = 'http://sqlfairy.sourceforge.net/sqlfairy.xml';
144 my $translator = shift;
145 my $schema = $translator->schema;
146 $PArgs = $translator->producer_args;
147 my $io = IO::Scalar->new;
148 my $xml = XML::Writer->new(
151 PREFIX_MAP => { $Namespace => $Name },
156 $xml->xmlDecl('UTF-8');
157 $xml->comment(header_comment('', ''));
158 #$xml->startTag([ $Namespace => 'schema' ]);
159 xml_obj($xml, $schema,
160 tag => "schema", methods => [qw/name database/], end_tag => 0 );
165 for my $table ( $schema->get_tables ) {
166 debug "Table:",$table->name;
167 xml_obj($xml, $table,
168 tag => "table", methods => [qw/name order/], end_tag => 0 );
173 $xml->startTag( [ $Namespace => 'fields' ] );
174 for my $field ( $table->get_fields ) {
175 debug " Field:",$field->name;
176 xml_obj($xml, $field,
179 methods =>[qw/name data_type size is_nullable default_value
180 is_auto_increment is_primary_key is_foreign_key comments order
184 $xml->endTag( [ $Namespace => 'fields' ] );
189 $xml->startTag( [ $Namespace => 'indices' ] );
190 for my $index ( $table->get_indices ) {
191 debug "Index:",$index->name;
192 xml_obj($xml, $index,
195 methods =>[qw/ name type fields options/],
198 $xml->endTag( [ $Namespace => 'indices' ] );
203 $xml->startTag( [ $Namespace => 'constraints' ] );
204 for my $index ( $table->get_constraints ) {
205 debug "Constraint:",$index->name;
206 xml_obj($xml, $index,
210 name type fields reference_table reference_fields
211 on_delete on_update match_type expression options deferrable
215 $xml->endTag( [ $Namespace => 'constraints' ] );
217 $xml->endTag( [ $Namespace => 'table' ] );
223 for my $foo ( $schema->get_views ) {
224 xml_obj($xml, $foo, tag => "view",
225 methods => [qw/name sql fields order/], end_tag => 1 );
231 for my $foo ( $schema->get_triggers ) {
232 xml_obj($xml, $foo, tag => "trigger",
233 methods => [qw/name database_event action on_table perform_action_when
234 fields order/], end_tag => 1 );
240 for my $foo ( $schema->get_procedures ) {
241 xml_obj($xml, $foo, tag => "procedure",
242 methods => [qw/name sql parameters owner comments order/], end_tag=>1 );
245 $xml->endTag([ $Namespace => 'schema' ]);
251 # -------------------------------------------------------------------
253 # Takes an XML Write, Schema::* object and list of method names
254 # and writes the obect out as XML. All methods values are written as attributes
255 # except for comments, sql and action which get written as child data elements.
257 # The attributes, tags are written in the same order as the method names are
261 # - Should the Namespace be passed in instead of global? Pass in the same
262 # as Writer ie [ NS => TAGNAME ]
265 my ($xml, $obj, %args) = @_;
266 my $tag = $args{'tag'} || '';
267 my $end_tag = $args{'end_tag'} || '';
268 my @meths = @{ $args{'methods'} };
271 # Use array to ensure consistant (ie not hash) ordering of attribs
272 # The order comes from the meths list passed in.
275 foreach ( grep { defined $obj->$_ } @meths ) {
276 my $what = m/^sql|comments|action$/ ? \@tags : \@attr;
278 $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val;
279 push @$what, $_ => $val;
281 my $child_tags = @tags;
282 $end_tag && !$child_tags
283 ? $xml->emptyTag( [ $Namespace => $tag ], @attr )
284 : $xml->startTag( [ $Namespace => $tag ], @attr );
285 while ( my ($name,$val) = splice @tags,0,2 ) {
286 $xml->dataElement( [ $Namespace => $name ], $val );
288 $xml->endTag( [ $Namespace => $tag ] ) if $child_tags && $end_tag;
293 # -------------------------------------------------------------------
294 # The eyes of fire, the nostrils of air,
295 # The mouth of water, the beard of earth.
297 # -------------------------------------------------------------------
303 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
304 Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
305 Mark Addison E<lt>mark.addison@itn.co.ukE<gt>.
309 perl(1), SQL::Translator, SQL::Translator::Parser::XML::SQLFairy,
310 SQL::Translator::Schema, XML::Writer.