Commit | Line | Data |
c957e92d |
1 | package SQL::Translator::Producer::SqlfXML; |
16dc9970 |
2 | |
d529894e |
3 | # ------------------------------------------------------------------- |
c957e92d |
4 | # $Id: SqlfXML.pm,v 1.1 2003-08-06 17:14:09 grommit Exp $ |
d529894e |
5 | # ------------------------------------------------------------------- |
abfa405a |
6 | # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>, |
7 | # darren chamberlain <darren@cpan.org>, |
8 | # Chris Mungall <cjm@fruitfly.org> |
16dc9970 |
9 | # |
d529894e |
10 | # This program is free software; you can redistribute it and/or |
11 | # modify it under the terms of the GNU General Public License as |
12 | # published by the Free Software Foundation; version 2. |
13 | # |
14 | # This program is distributed in the hope that it will be useful, but |
15 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | # General Public License for more details. |
18 | # |
19 | # You should have received a copy of the GNU General Public License |
20 | # along with this program; if not, write to the Free Software |
21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
22 | # 02111-1307 USA |
23 | # ------------------------------------------------------------------- |
24 | |
16dc9970 |
25 | use strict; |
fb6b0318 |
26 | use vars qw[ $VERSION ]; |
c957e92d |
27 | $VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/; |
5ee19df8 |
28 | |
fb6b0318 |
29 | use IO::Scalar; |
5ee19df8 |
30 | use SQL::Translator::Utils qw(header_comment); |
fb6b0318 |
31 | use XML::Writer; |
32 | |
03afabda |
33 | my $namespace = 'http://sqlfairy.sourceforge.net/sqlfairy.xml'; |
34 | my $name = 'sqlt'; |
16dc9970 |
35 | |
61745327 |
36 | # ------------------------------------------------------------------- |
4b603a3f |
37 | sub produce { |
a1d94525 |
38 | my $translator = shift; |
39 | my $schema = $translator->schema; |
40 | my $args = $translator->producer_args; |
c6a7dcb1 |
41 | |
42 | my $io = IO::Scalar->new; |
03afabda |
43 | my $xml = XML::Writer->new( |
c6a7dcb1 |
44 | OUTPUT => $io, |
45 | NAMESPACES => 1, |
03afabda |
46 | PREFIX_MAP => { $namespace => $name }, |
c6a7dcb1 |
47 | DATA_MODE => 1, |
48 | DATA_INDENT => 2, |
49 | ); |
50 | |
51 | $xml->xmlDecl('UTF-8'); |
fb6b0318 |
52 | $xml->comment(header_comment('', '')); |
03afabda |
53 | $xml->startTag([ $namespace => 'schema' ]); |
61745327 |
54 | |
c6a7dcb1 |
55 | for my $table ( $schema->get_tables ) { |
56 | my $table_name = $table->name or next; |
03afabda |
57 | $xml->startTag ( [ $namespace => 'table' ] ); |
58 | $xml->dataElement( [ $namespace => 'name' ], $table_name ); |
59 | $xml->dataElement( [ $namespace => 'order' ], $table->order ); |
61745327 |
60 | |
61 | # |
62 | # Fields |
63 | # |
03afabda |
64 | $xml->startTag( [ $namespace => 'fields' ] ); |
c6a7dcb1 |
65 | for my $field ( $table->get_fields ) { |
03afabda |
66 | $xml->startTag( [ $namespace => 'field' ] ); |
c6a7dcb1 |
67 | |
68 | for my $method ( |
69 | qw[ |
70 | name data_type default_value is_auto_increment |
71 | is_primary_key is_nullable is_foreign_key order size |
72 | ] |
73 | ) { |
74 | my $val = $field->$method || ''; |
03afabda |
75 | $xml->dataElement( [ $namespace => $method ], $val ) |
c6a7dcb1 |
76 | if ( defined $val || |
77 | ( !defined $val && $args->{'emit_empty_tags'} ) ); |
61745327 |
78 | } |
79 | |
03afabda |
80 | $xml->endTag( [ $namespace => 'field' ] ); |
61745327 |
81 | } |
c6a7dcb1 |
82 | |
03afabda |
83 | $xml->endTag( [ $namespace => 'fields' ] ); |
61745327 |
84 | |
85 | # |
86 | # Indices |
87 | # |
03afabda |
88 | $xml->startTag( [ $namespace => 'indices' ] ); |
c6a7dcb1 |
89 | for my $index ( $table->get_indices ) { |
03afabda |
90 | $xml->startTag( [ $namespace => 'index' ] ); |
61745327 |
91 | |
c6a7dcb1 |
92 | for my $method ( qw[ fields name options type ] ) { |
93 | my $val = $index->$method || ''; |
94 | $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val; |
03afabda |
95 | $xml->dataElement( [ $namespace => $method ], $val ) |
c6a7dcb1 |
96 | if ( defined $val || |
97 | ( !defined $val && $args->{'emit_empty_tags'} ) ); |
98 | } |
99 | |
03afabda |
100 | $xml->endTag( [ $namespace => 'index' ] ); |
c6a7dcb1 |
101 | } |
03afabda |
102 | $xml->endTag( [ $namespace => 'indices' ] ); |
c6a7dcb1 |
103 | |
104 | # |
105 | # Constraints |
106 | # |
03afabda |
107 | $xml->startTag( [ $namespace => 'constraints' ] ); |
c6a7dcb1 |
108 | for my $index ( $table->get_constraints ) { |
03afabda |
109 | $xml->startTag( [ $namespace => 'constraint' ] ); |
c6a7dcb1 |
110 | |
111 | for my $method ( |
112 | qw[ |
113 | deferrable expression fields match_type name |
114 | options on_delete on_update reference_fields |
115 | reference_table type |
116 | ] |
117 | ) { |
118 | my $val = $index->$method || ''; |
61745327 |
119 | $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val; |
03afabda |
120 | $xml->dataElement( [ $namespace => $method ], $val ) |
c6a7dcb1 |
121 | if ( defined $val || |
122 | ( !defined $val && $args->{'emit_empty_tags'} ) ); |
61745327 |
123 | } |
124 | |
03afabda |
125 | $xml->endTag( [ $namespace => 'constraint' ] ); |
61745327 |
126 | } |
03afabda |
127 | $xml->endTag( [ $namespace => 'constraints' ] ); |
61745327 |
128 | |
03afabda |
129 | $xml->endTag( [ $namespace => 'table' ] ); |
61745327 |
130 | } |
131 | |
03afabda |
132 | $xml->endTag([ $namespace => 'schema' ]); |
fb6b0318 |
133 | $xml->end; |
61745327 |
134 | |
fb6b0318 |
135 | return $io; |
16dc9970 |
136 | } |
137 | |
138 | 1; |
d529894e |
139 | |
140 | # ------------------------------------------------------------------- |
16dc9970 |
141 | # The eyes of fire, the nostrils of air, |
142 | # The mouth of water, the beard of earth. |
143 | # William Blake |
d529894e |
144 | # ------------------------------------------------------------------- |
16dc9970 |
145 | |
5ee19df8 |
146 | =head1 NAME |
147 | |
c957e92d |
148 | SQL::Translator::Producer::SqlfXML - XML output |
5ee19df8 |
149 | |
150 | =head1 SYNOPSIS |
151 | |
c957e92d |
152 | use SQL::Translator; |
153 | |
154 | my $translator = SQL::Translator->new( |
155 | show_warnings => 1, |
156 | add_drop_table => 1, |
157 | ); |
158 | print = $obj->translate( |
159 | from => "MySQL", |
160 | to => "SqlfXML", |
161 | filename => "fooschema.sql", |
162 | ); |
5ee19df8 |
163 | |
164 | =head1 DESCRIPTION |
165 | |
03afabda |
166 | Creates XML output of a schema. |
16dc9970 |
167 | |
168 | =head1 AUTHOR |
169 | |
03afabda |
170 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>, darren chamberlain E<lt>darren@cpan.orgE<gt> |
16dc9970 |
171 | |
172 | =head1 SEE ALSO |
173 | |
c957e92d |
174 | perl(1), SQL::Translator, SQL::Translator::Parser::SqlfXML, |
175 | SQL::Translator::Schema, XML::Writer. |