Added Utils package with debug method, shared between MySQL and SQLite producers.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / XML.pm
index 7621a62..e71b281 100644 (file)
 package SQL::Translator::Producer::XML;
 
-#-----------------------------------------------------
-# $Id: XML.pm,v 1.1.1.1 2002-03-01 02:26:25 kycl4rk Exp $
+# -------------------------------------------------------------------
+# $Id: XML.pm,v 1.5 2003-01-27 17:04:48 dlc Exp $
+# -------------------------------------------------------------------
+# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
+#                    darren chamberlain <darren@cpan.org>,
+#                    Chris Mungall <cjm@fruitfly.org>
 #
-# File       : SQL/Translator/Producer/XML.pm
-# Programmer : Ken Y. Clark, kclark@logsoft.com
-# Created    : 2002/02/27
-# Purpose    : XML output
-#-----------------------------------------------------
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; version 2.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+# 02111-1307  USA
+# -------------------------------------------------------------------
 
-use strict;
-use SQL::Translator::Producer;
-use vars qw( $VERSION );
-$VERSION = (qw$Revision: 1.1.1.1 $)[-1];
+=head1 NAME
 
-use XML::Dumper;
+SQL::Translator::Producer::XML - XML output
 
-use base qw[ SQL::Translator::Producer ];
+=head1 SYNOPSIS
 
-sub to { 'XML' }
+  use SQL::Translator::Producer::XML;
 
-sub translate {
-    my ( $self, $data ) = @_;
-    my $dumper = XML::Dumper->new;
-    return $dumper->pl2xml( $data );
+=head1 DESCRIPTION
+
+Meant to create some sort of usable XML output.
+
+=cut
+
+use strict;
+use vars qw[ $VERSION $XML ];
+$VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/;
+
+# -------------------------------------------------------------------
+sub produce {
+    my ( $translator, $data ) = @_;
+    my $indent = 0;
+    aggregate( '<schema>', $indent );
+    
+    $indent++;
+    for my $table ( 
+        map  { $_->[1] }
+        sort { $a->[0] <=> $b->[0] }
+        map  { [ $_->{'order'}, $_ ] }
+        values %$data
+    ) { 
+        aggregate( '<table>', $indent );
+        $indent++;
+
+        aggregate( "<name>$table->{'table_name'}</name>", $indent );
+        aggregate( "<order>$table->{'order'}</order>", $indent );
+
+        #
+        # Fields
+        #
+        aggregate( '<fields>', $indent );
+        for my $field ( 
+            map  { $_->[1] }
+            sort { $a->[0] <=> $b->[0] }
+            map  { [ $_->{'order'}, $_ ] }
+            values %{ $table->{'fields'} }
+        ) {
+            aggregate( '<field>', ++$indent );
+            $indent++;
+
+            for my $key ( keys %$field ) {
+                my $val = defined $field->{ $key } ? $field->{ $key } : '';
+                   $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val;
+                aggregate( "<$key>$val</$key>", $indent );
+            }
+
+            $indent--;
+            aggregate( "</field>", $indent-- );
+        }
+        aggregate( "</fields>", $indent );
+
+        #
+        # Indices
+        #
+        aggregate( '<indices>', $indent );
+        for my $index ( @{ $table->{'indices'} } ) {
+            aggregate( '<index>', ++$indent );
+            $indent++;
+
+            for my $key ( keys %$index ) {
+                my $val = defined $index->{ $key } ? $index->{ $key } : '';
+                   $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val;
+                aggregate( "<$key>$val</$key>", $indent );
+            }
+
+            $indent--;
+            aggregate( "</index>", $indent-- );
+        }
+        aggregate( "</indices>", $indent );
+
+        $indent--;
+        aggregate( "</table>", $indent );
+    }
+
+    $indent--;
+    aggregate( '</schema>', $indent );
+
+    return $XML;
+}
+
+# -------------------------------------------------------------------
+sub aggregate {
+    my ( $text, $indent ) = @_;
+    $XML .= ('  ' x $indent) . "$text\n";
 }
 
 1;
 
-#-----------------------------------------------------
+# -------------------------------------------------------------------
 # The eyes of fire, the nostrils of air,
 # The mouth of water, the beard of earth.
 # William Blake
-#-----------------------------------------------------
-
-=head1 NAME
-
-SQL::Translator::Producer::XML - XML output
-
-=head1 SYNOPSIS
-
-  use SQL::Translator::Producer::XML;
-
-=head1 DESCRIPTION
+# -------------------------------------------------------------------
 
-Blah blah blah.
+=pod
 
 =head1 AUTHOR
 
-Ken Y. Clark, kclark@logsoft.com
+Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
 
 =head1 SEE ALSO
 
-perl(1).
+XML::Dumper;
 
 =cut