Commit | Line | Data |
c957e92d |
1 | package SQL::Translator::Producer::SqlfXML; |
16dc9970 |
2 | |
d529894e |
3 | # ------------------------------------------------------------------- |
d0c12b9f |
4 | # $Id: SqlfXML.pm,v 1.3 2003-08-08 12:30:20 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; |
d0c12b9f |
26 | use warnings; |
fb6b0318 |
27 | use vars qw[ $VERSION ]; |
d0c12b9f |
28 | $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/; |
29 | |
30 | use Exporter; |
31 | use base qw(Exporter); |
32 | our @EXPORT_OK = qw(produce); |
5ee19df8 |
33 | |
fb6b0318 |
34 | use IO::Scalar; |
5ee19df8 |
35 | use SQL::Translator::Utils qw(header_comment); |
fb6b0318 |
36 | use XML::Writer; |
37 | |
03afabda |
38 | my $namespace = 'http://sqlfairy.sourceforge.net/sqlfairy.xml'; |
39 | my $name = 'sqlt'; |
16dc9970 |
40 | |
d0c12b9f |
41 | { |
42 | our ($translator,$args,$schema); |
43 | |
44 | sub debug { $translator->debug(@_,"\n"); } # Shortcut. |
45 | |
4b603a3f |
46 | sub produce { |
d0c12b9f |
47 | $translator = shift; |
48 | $args = $translator->producer_args; |
49 | $schema = $translator->schema; |
c6a7dcb1 |
50 | |
d0c12b9f |
51 | my $io = IO::Scalar->new; |
03afabda |
52 | my $xml = XML::Writer->new( |
c6a7dcb1 |
53 | OUTPUT => $io, |
54 | NAMESPACES => 1, |
03afabda |
55 | PREFIX_MAP => { $namespace => $name }, |
c6a7dcb1 |
56 | DATA_MODE => 1, |
57 | DATA_INDENT => 2, |
58 | ); |
59 | |
60 | $xml->xmlDecl('UTF-8'); |
fb6b0318 |
61 | $xml->comment(header_comment('', '')); |
03afabda |
62 | $xml->startTag([ $namespace => 'schema' ]); |
61745327 |
63 | |
d0c12b9f |
64 | # |
65 | # Table |
66 | # |
c6a7dcb1 |
67 | for my $table ( $schema->get_tables ) { |
d0c12b9f |
68 | debug "Table:",$table->name; |
69 | $xml->startTag( [ $namespace => 'table' ] ); |
70 | xml_objAttr($xml,$table, qw/name order/); |
71 | |
61745327 |
72 | # |
73 | # Fields |
74 | # |
03afabda |
75 | $xml->startTag( [ $namespace => 'fields' ] ); |
c6a7dcb1 |
76 | for my $field ( $table->get_fields ) { |
d0c12b9f |
77 | debug " Field:",$field->name; |
03afabda |
78 | $xml->startTag( [ $namespace => 'field' ] ); |
d0c12b9f |
79 | xml_objAttr($xml,$field, qw/ |
80 | name data_type default_value is_auto_increment |
81 | is_primary_key is_nullable is_foreign_key order size |
82 | /); |
03afabda |
83 | $xml->endTag( [ $namespace => 'field' ] ); |
61745327 |
84 | } |
03afabda |
85 | $xml->endTag( [ $namespace => 'fields' ] ); |
61745327 |
86 | |
87 | # |
88 | # Indices |
89 | # |
03afabda |
90 | $xml->startTag( [ $namespace => 'indices' ] ); |
c6a7dcb1 |
91 | for my $index ( $table->get_indices ) { |
d0c12b9f |
92 | debug "Index:",$index->name; |
03afabda |
93 | $xml->startTag( [ $namespace => 'index' ] ); |
d0c12b9f |
94 | xml_objAttr($xml,$index, qw/fields name options type/); |
03afabda |
95 | $xml->endTag( [ $namespace => 'index' ] ); |
c6a7dcb1 |
96 | } |
03afabda |
97 | $xml->endTag( [ $namespace => 'indices' ] ); |
c6a7dcb1 |
98 | |
99 | # |
100 | # Constraints |
101 | # |
03afabda |
102 | $xml->startTag( [ $namespace => 'constraints' ] ); |
c6a7dcb1 |
103 | for my $index ( $table->get_constraints ) { |
d0c12b9f |
104 | debug "Constraint:",$index->name; |
03afabda |
105 | $xml->startTag( [ $namespace => 'constraint' ] ); |
d0c12b9f |
106 | xml_objAttr($xml,$index, qw/ |
c6a7dcb1 |
107 | deferrable expression fields match_type name |
108 | options on_delete on_update reference_fields |
109 | reference_table type |
d0c12b9f |
110 | /); |
03afabda |
111 | $xml->endTag( [ $namespace => 'constraint' ] ); |
61745327 |
112 | } |
03afabda |
113 | $xml->endTag( [ $namespace => 'constraints' ] ); |
61745327 |
114 | |
03afabda |
115 | $xml->endTag( [ $namespace => 'table' ] ); |
61745327 |
116 | } |
117 | |
03afabda |
118 | $xml->endTag([ $namespace => 'schema' ]); |
fb6b0318 |
119 | $xml->end; |
61745327 |
120 | |
fb6b0318 |
121 | return $io; |
16dc9970 |
122 | } |
123 | |
d0c12b9f |
124 | # Takes an xml writer, a Schema:: object and a list of methods and adds the |
125 | # XML for those methods. |
126 | sub xml_objAttr { |
127 | my ($xml, $obj, @methods) = @_; |
128 | for my $method (@methods) { |
129 | my $val = $obj->$method; |
130 | debug " ".ref($obj)."->$method=", |
131 | (defined $val ? "'$val'" : "<UNDEF>"); |
132 | next unless $args->{emit_empty_tags} || defined $val; |
133 | $val = "" if not defined $val; |
134 | $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val; |
135 | debug " Adding Attr:".$method."='",$val,"'"; |
136 | $xml->dataElement( [ $namespace => $method ], $val ); |
137 | } |
138 | } |
139 | |
140 | } # End of our scoped bit |
141 | |
16dc9970 |
142 | 1; |
d529894e |
143 | |
144 | # ------------------------------------------------------------------- |
16dc9970 |
145 | # The eyes of fire, the nostrils of air, |
146 | # The mouth of water, the beard of earth. |
147 | # William Blake |
d529894e |
148 | # ------------------------------------------------------------------- |
16dc9970 |
149 | |
5ee19df8 |
150 | =head1 NAME |
151 | |
c957e92d |
152 | SQL::Translator::Producer::SqlfXML - XML output |
5ee19df8 |
153 | |
154 | =head1 SYNOPSIS |
155 | |
c957e92d |
156 | use SQL::Translator; |
157 | |
158 | my $translator = SQL::Translator->new( |
159 | show_warnings => 1, |
160 | add_drop_table => 1, |
161 | ); |
162 | print = $obj->translate( |
163 | from => "MySQL", |
164 | to => "SqlfXML", |
165 | filename => "fooschema.sql", |
166 | ); |
5ee19df8 |
167 | |
168 | =head1 DESCRIPTION |
169 | |
03afabda |
170 | Creates XML output of a schema. |
16dc9970 |
171 | |
d0c12b9f |
172 | =head1 TODO |
173 | |
16dc9970 |
174 | =head1 AUTHOR |
175 | |
d0c12b9f |
176 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>, |
177 | darren chamberlain E<lt>darren@cpan.orgE<gt>, |
178 | mark addison E<lt>mark.addison@itn.co.ukE<gt>, |
16dc9970 |
179 | |
180 | =head1 SEE ALSO |
181 | |
c957e92d |
182 | perl(1), SQL::Translator, SQL::Translator::Parser::SqlfXML, |
183 | SQL::Translator::Schema, XML::Writer. |