1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
package SQL::Translator::Producer::XML::SQLFairy;
=pod
=head1 NAME
SQL::Translator::Producer::XML::SQLFairy - SQLFairy's default XML format
=head1 SYNOPSIS
use SQL::Translator;
my $t = SQL::Translator->new(
from => 'MySQL',
to => 'XML-SQLFairy',
filename => 'schema.sql',
show_warnings => 1,
);
print $t->translate;
=head1 DESCRIPTION
Creates XML output of a schema, in the flavor of XML used natively by the
SQLFairy project (L<SQL::Translator>). This format is detailed here.
The XML lives in the C<http://sqlfairy.sourceforge.net/sqlfairy.xml> namespace.
With a root element of <schema>.
Objects in the schema are mapped to tags of the same name as the objects class
(all lowercase).
The attributes of the objects (e.g. $field->name) are mapped to attributes of
the tag, except for sql, comments and action, which get mapped to child data
elements.
List valued attributes (such as the list of fields in an index)
get mapped to comma separated lists of values in the attribute.
Child objects, such as a tables fields, get mapped to child tags wrapped in a
set of container tags using the plural of their contained classes name.
An objects' extra attribute (a hash of arbitrary data) is
mapped to a tag called extra, with the hash of data as attributes, sorted into
alphabetical order.
e.g.
<schema name="" database=""
xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
<tables>
<table name="Story" order="1">
<fields>
<field name="id" data_type="BIGINT" size="20"
is_nullable="0" is_auto_increment="1" is_primary_key="1"
is_foreign_key="0" order="3">
<extra ZEROFILL="1" />
<comments></comments>
</field>
<field name="created" data_type="datetime" size="0"
is_nullable="1" is_auto_increment="0" is_primary_key="0"
is_foreign_key="0" order="1">
<extra />
<comments></comments>
</field>
...
</fields>
<indices>
<index name="foobar" type="NORMAL" fields="foo,bar" options="" />
</indices>
</table>
</tables>
<views>
<view name="email_list" fields="email" order="1">
<sql>SELECT email FROM Basic WHERE email IS NOT NULL</sql>
</view>
</views>
</schema>
To see a complete example of the XML translate one of your schema :)
$ sqlt -f MySQL -t XML-SQLFairy schema.sql
=head1 ARGS
=over 4
=item add_prefix
Set to true to use the default namespace prefix of 'sqlf', instead of using
the default namespace for
C<http://sqlfairy.sourceforge.net/sqlfairy.xml namespace>
e.g.
<!-- add_prefix=0 -->
<field name="foo" />
<!-- add_prefix=1 -->
<sqlf:field name="foo" />
=item prefix
Set to the namespace prefix you want to use for the
C<http://sqlfairy.sourceforge.net/sqlfairy.xml namespace>
e.g.
<!-- prefix='foo' -->
<foo:field name="foo" />
=item newlines
If true (the default) inserts newlines around the XML, otherwise the schema is
written on one line.
=item indent
When using newlines the number of whitespace characters to use as the indent.
Default is 2, set to 0 to turn off indenting.
=back
=head1 LEGACY FORMAT
The previous version of the SQLFairy XML allowed the attributes of the the
schema objects to be written as either xml attributes or as data elements, in
any combination. The old producer could produce attribute only or data element
only versions. While this allowed for lots of flexibility in writing the XML
the result is a great many possible XML formats, not so good for DTD writing,
XPathing etc! So we have moved to a fixed version described above.
This version of the producer will now only produce the new style XML.
To convert your old format files simply pass them through the translator :)
$ sqlt -f XML-SQLFairy -t XML-SQLFairy schema-old.xml > schema-new.xml
=cut
use strict;
use warnings;
our @EXPORT_OK;
our $VERSION = '1.59';
use Exporter;
use base qw(Exporter);
@EXPORT_OK = qw(produce);
use IO::Scalar;
use SQL::Translator::Utils qw(header_comment debug);
BEGIN {
# Will someone fix XML::Writer already?
local $^W = 0;
require XML::Writer;
import XML::Writer;
}
# Which schema object attributes (methods) to write as xml elements rather than
# as attributes. e.g. <comments>blah, blah...</comments>
my @MAP_AS_ELEMENTS = qw/sql comments action extra/;
my $Namespace = 'http://sqlfairy.sourceforge.net/sqlfairy.xml';
my $Name = 'sqlf';
my $PArgs = {};
my $no_comments;
sub produce {
my $translator = shift;
my $schema = $translator->schema;
$no_comments = $translator->no_comments;
$PArgs = $translator->producer_args;
my $newlines = defined $PArgs->{newlines} ? $PArgs->{newlines} : 1;
my $indent = defined $PArgs->{indent} ? $PArgs->{indent} : 2;
my $io = IO::Scalar->new;
# Setup the XML::Writer and set the namespace
my $prefix = "";
$prefix = $Name if $PArgs->{add_prefix};
$prefix = $PArgs->{prefix} if $PArgs->{prefix};
my $xml = XML::Writer->new(
OUTPUT => $io,
NAMESPACES => 1,
PREFIX_MAP => { $Namespace => $prefix },
DATA_MODE => $newlines,
DATA_INDENT => $indent,
);
# Start the document
$xml->xmlDecl('UTF-8');
$xml->comment(header_comment('', ''))
unless $no_comments;
xml_obj($xml, $schema,
tag => "schema", methods => [qw/name database extra/], end_tag => 0 );
#
# Table
#
$xml->startTag( [ $Namespace => "tables" ] );
for my $table ( $schema->get_tables ) {
debug "Table:",$table->name;
xml_obj($xml, $table,
tag => "table",
methods => [qw/name order extra/],
end_tag => 0
);
#
# Fields
#
xml_obj_children( $xml, $table,
tag => 'field',
methods =>[qw/
name data_type size is_nullable default_value is_auto_increment
is_primary_key is_foreign_key extra comments order
/],
);
#
# Indices
#
xml_obj_children( $xml, $table,
tag => 'index',
collection_tag => "indices",
methods => [qw/name type fields options extra/],
);
#
# Constraints
#
xml_obj_children( $xml, $table,
tag => 'constraint',
methods => [qw/
name type fields reference_table reference_fields
on_delete on_update match_type expression options deferrable
extra
/],
);
#
# Comments
#
xml_obj_children( $xml, $table,
tag => 'comment',
collection_tag => "comments",
methods => [qw/
comments
/],
);
$xml->endTag( [ $Namespace => 'table' ] );
}
$xml->endTag( [ $Namespace => 'tables' ] );
#
# Views
#
xml_obj_children( $xml, $schema,
tag => 'view',
methods => [qw/name sql fields order extra/],
);
#
# Tiggers
#
xml_obj_children( $xml, $schema,
tag => 'trigger',
methods => [qw/name database_events action on_table perform_action_when
fields order extra/],
);
#
# Procedures
#
xml_obj_children( $xml, $schema,
tag => 'procedure',
methods => [qw/name sql parameters owner comments order extra/],
);
$xml->endTag([ $Namespace => 'schema' ]);
$xml->end;
return $io;
}
#
# Takes and XML::Write object, Schema::* parent object, the tag name,
# the collection name and a list of methods (of the children) to write as XML.
# The collection name defaults to the name with an s on the end and is used to
# work out the method to get the children with. eg a name of 'foo' gives a
# collection of foos and gets the members using ->get_foos.
#
sub xml_obj_children {
my ($xml,$parent) = (shift,shift);
my %args = @_;
my ($name,$collection_name,$methods)
= @args{qw/tag collection_tag methods/};
$collection_name ||= "${name}s";
my $meth;
if ( $collection_name eq 'comments' ) {
$meth = 'comments';
} else {
$meth = "get_$collection_name";
}
my @kids = $parent->$meth;
#@kids || return;
$xml->startTag( [ $Namespace => $collection_name ] );
for my $obj ( @kids ) {
if ( $collection_name eq 'comments' ){
$xml->dataElement( [ $Namespace => 'comment' ], $obj );
} else {
xml_obj($xml, $obj,
tag => "$name",
end_tag => 1,
methods => $methods,
);
}
}
$xml->endTag( [ $Namespace => $collection_name ] );
}
#
# Takes an XML::Writer, Schema::* object and list of method names
# and writes the obect out as XML. All methods values are written as attributes
# except for the methods listed in @MAP_AS_ELEMENTS which get written as child
# data elements.
#
# The attributes/tags are written in the same order as the method names are
# passed.
#
# TODO
# - Should the Namespace be passed in instead of global? Pass in the same
# as Writer ie [ NS => TAGNAME ]
#
my $elements_re = join("|", @MAP_AS_ELEMENTS);
$elements_re = qr/^($elements_re)$/;
sub xml_obj {
my ($xml, $obj, %args) = @_;
my $tag = $args{'tag'} || '';
my $end_tag = $args{'end_tag'} || '';
my @meths = @{ $args{'methods'} };
my $empty_tag = 0;
# Use array to ensure consistant (ie not hash) ordering of attribs
# The order comes from the meths list passed in.
my @tags;
my @attr;
foreach ( grep { defined $obj->$_ } @meths ) {
my $what = m/$elements_re/ ? \@tags : \@attr;
my $val = $_ eq 'extra'
? { $obj->$_ }
: $obj->$_;
$val = ref $val eq 'ARRAY' ? join(',', @$val) : $val;
push @$what, $_ => $val;
};
my $child_tags = @tags;
$end_tag && !$child_tags
? $xml->emptyTag( [ $Namespace => $tag ], @attr )
: $xml->startTag( [ $Namespace => $tag ], @attr );
while ( my ($name,$val) = splice @tags,0,2 ) {
if ( ref $val eq 'HASH' ) {
$xml->emptyTag( [ $Namespace => $name ],
map { ($_, $val->{$_}) } sort keys %$val );
}
else {
$xml->dataElement( [ $Namespace => $name ], $val );
}
}
$xml->endTag( [ $Namespace => $tag ] ) if $child_tags && $end_tag;
}
1;
# -------------------------------------------------------------------
# The eyes of fire, the nostrils of air,
# The mouth of water, the beard of earth.
# William Blake
# -------------------------------------------------------------------
=pod
=head1 AUTHORS
Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
Mark Addison E<lt>mark.addison@itn.co.ukE<gt>.
=head1 SEE ALSO
L<perl(1)>, L<SQL::Translator>, L<SQL::Translator::Parser::XML::SQLFairy>,
L<SQL::Translator::Schema>, L<XML::Writer>.
=cut
|