2 role SQL::Translator::Producer::XML {
3 use MooseX::Types::Moose qw(HashRef);
5 #use SQL::Translator::Utils qw(header_comment debug);
7 # Will someone fix XML::Writer already?
13 # Which schema object attributes (methods) to write as xml elements rather than
14 # as attributes. e.g. <comments>blah, blah...</comments>
15 my @MAP_AS_ELEMENTS = qw/sql comments action extra/;
17 my $Namespace = 'http://sqlfairy.sourceforge.net/sqlfairy.xml';
23 my $translator = $self->translator;
24 my $schema = $translator->schema;
25 $no_comments = $translator->no_comments;
26 # $PArgs = $translator->producer_args;
27 my $newlines = defined $PArgs->{newlines} ? $PArgs->{newlines} : 1;
28 my $indent = defined $PArgs->{indent} ? $PArgs->{indent} : 2;
29 my $io = IO::Scalar->new;
31 # Setup the XML::Writer and set the namespace
33 $prefix = $Name if $PArgs->{add_prefix};
34 $prefix = $PArgs->{prefix} if $PArgs->{prefix};
35 my $xml = XML::Writer->new(
38 PREFIX_MAP => { $Namespace => $prefix },
39 DATA_MODE => $newlines,
40 DATA_INDENT => $indent,
44 $xml->xmlDecl('UTF-8');
46 # $xml->comment(header_comment('', ''))
47 # unless $no_comments;
49 xml_obj($xml, $schema,
50 { tag => "schema", methods => [qw/name extra/], end_tag => 0 });
55 $xml->startTag( [ $Namespace => "tables" ] );
56 for my $table ( $schema->get_tables ) {
57 # debug "Table:",$table->name;
60 methods => [qw/name extra/],
67 xml_obj_children( $xml, $table,
70 name data_type size is_nullable default_value is_auto_increment
71 is_primary_key is_foreign_key extra comments
78 xml_obj_children( $xml, $table,
80 collection_tag => "indices",
81 methods => [qw/name type fields extra/], }
87 xml_obj_children( $xml, $table,
88 { tag => 'constraint',
90 name type fields reference_table reference_fields
91 on_delete on_update match_type expression options deferrable
94 # methods => [qw/name type expression options deferrable extra/], }
100 xml_obj_children( $xml, $table,
102 methods => [qw/ comments /], }
105 $xml->endTag( [ $Namespace => 'table' ] );
107 $xml->endTag( [ $Namespace => 'tables' ] );
112 xml_obj_children( $xml, $schema,
114 methods => [qw/name sql fields extra/], }
120 xml_obj_children( $xml, $schema,
122 methods => [qw/name database_events action on_table perform_action_when fields extra/], }
128 xml_obj_children( $xml, $schema,
129 { tag => 'procedure',
130 methods => [qw/name sql parameters owner comments extra/], }
133 $xml->endTag([ $Namespace => 'schema' ]);
141 # Takes and XML::Write object, Schema::* parent object, the tag name,
142 # the collection name and a list of methods (of the children) to write as XML.
143 # The collection name defaults to the name with an s on the end and is used to
144 # work out the method to get the children with. eg a name of 'foo' gives a
145 # collection of foos and gets the members using ->get_foos.
147 #sub xml_obj_children {
148 method xml_obj_children($xml: $parent, HashRef $args?) {
149 # my ($xml,$parent) = (shift,shift);
152 my ($name,$collection_name,$methods)
153 = @{$args}{qw/tag collection_tag methods/};
154 $collection_name ||= "${name}s";
157 if ( $collection_name eq 'comments' ) {
160 $meth = "get_$collection_name";
163 my @kids = $parent->$meth;
165 $xml->startTag( [ $Namespace => $collection_name ] );
167 for my $obj ( @kids ) {
168 if ( $collection_name eq 'comments' ){
169 $xml->dataElement( [ $Namespace => 'comment' ], $obj );
174 methods => $methods, }
178 $xml->endTag( [ $Namespace => $collection_name ] );
182 # Takes an XML::Writer, Schema::* object and list of method names
183 # and writes the obect out as XML. All methods values are written as attributes
184 # except for the methods listed in @MAP_AS_ELEMENTS which get written as child
187 # The attributes/tags are written in the same order as the method names are
191 # - Should the Namespace be passed in instead of global? Pass in the same
192 # as Writer ie [ NS => TAGNAME ]
194 my $elements_re = join("|", @MAP_AS_ELEMENTS);
195 $elements_re = qr/^($elements_re)$/;
197 method xml_obj($xml: $obj, HashRef $args?) {
198 my $tag = $args->{'tag'} || '';
199 my $end_tag = $args->{'end_tag'} || '';
200 my @meths = @{ $args->{'methods'} };
203 # Use array to ensure consistant (ie not hash) ordering of attribs
204 # The order comes from the meths list passed in.
207 foreach ( grep { defined $obj->$_ } @meths ) {
208 my $what = m/$elements_re/ ? \@tags : \@attr;
209 my $val = $_ eq 'extra'
212 $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val;
213 push @$what, $_ => $val;
215 my $child_tags = @tags;
216 $end_tag && !$child_tags
217 ? $xml->emptyTag( [ $Namespace => $tag ], @attr )
218 : $xml->startTag( [ $Namespace => $tag ], @attr );
219 while ( my ($name,$val) = splice @tags,0,2 ) {
220 if ( ref $val eq 'HASH' ) {
221 $xml->emptyTag( [ $Namespace => $name ],
222 map { ($_, $val->{$_}) } keys %$val ); ### SORT
225 $xml->dataElement( [ $Namespace => $name ], $val );
228 $xml->endTag( [ $Namespace => $tag ] ) if $child_tags && $end_tag;