Doc tweaks
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / XML / SQLFairy.pm
CommitLineData
0a1ec87a 1package SQL::Translator::Parser::XML::SQLFairy;
2
3# -------------------------------------------------------------------
91f28468 4# $Id: SQLFairy.pm,v 1.10 2004-08-19 20:41:31 grommit Exp $
0a1ec87a 5# -------------------------------------------------------------------
6# Copyright (C) 2003 Mark Addison <mark.addison@itn.co.uk>,
7#
8# This program is free software; you can redistribute it and/or
9# modify it under the terms of the GNU General Public License as
10# published by the Free Software Foundation; version 2.
11#
12# This program is distributed in the hope that it will be useful, but
13# WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15# General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20# 02111-1307 USA
21# -------------------------------------------------------------------
22
23=head1 NAME
24
8571d198 25SQL::Translator::Parser::XML::SQLFairy - parser for SQL::Translator's XML.
0a1ec87a 26
27=head1 SYNOPSIS
28
29 use SQL::Translator;
30 use SQL::Translator::Parser::XML::SQLFairy;
31
32 my $translator = SQL::Translator->new(
33 from => 'XML-SQLFairy',
34 to => 'MySQL',
35 filename => 'schema.xml',
36 show_warnings => 1,
37 add_drop_table => 1,
38 );
39
40 print $obj->translate;
41
42=head1 DESCRIPTION
43
44This parser handles the flavor of XML used natively by the SQLFairy
91f28468 45project (L<SQL::Translator>). The XML must be in the namespace
0a1ec87a 46"http://sqlfairy.sourceforge.net/sqlfairy.xml."
8571d198 47See L<SQL::Translator::Producer::XML::SQLFairy> for details of this format.
0a1ec87a 48
8571d198 49You do not need to specify every attribute of the Schema objects as any missing
50from the XML will be set to their default values. e.g. A field could be written
51using only;
0a1ec87a 52
8571d198 53 <sqlf:field name="email" data_type="varchar" size="255" />
0a1ec87a 54
8571d198 55Instead of the full;
0a1ec87a 56
8571d198 57 <sqlf:field name="email" data_type="varchar" size="255" is_nullable="1"
58 is_auto_increment="0" is_primary_key="0" is_foreign_key="0" order="4">
59 <sqlf:comments></sqlf:comments>
60 </sqlf:field>
0a1ec87a 61
8571d198 62If you do not explicitly set the order of items using order attributes on the
63tags then the order the tags appear in the XML will be used.
0a1ec87a 64
65=head2 default_value
66
91f28468 67Leave the attribute out all together to use the default in L<Schema::Field>.
68Use empty quotes or 'EMPTY_STRING' for a zero lenth string. 'NULL' for an
a5e624ac 69explicit null (currently sets default_value to undef in the
70Schema::Field obj).
0a1ec87a 71
91f28468 72 <sqlf:field default_value="" /> <!-- Empty string -->
73 <sqlf:field default_value="EMPTY_STRING" /> <!-- Empty string -->
74 <sqlf:field default_value="NULL" /> <!-- NULL -->
0a1ec87a 75
0a1ec87a 76=head2 ARGS
77
78Doesn't take any extra parser args at the moment.
79
4a268a6c 80=head1 LEGACY FORMAT
81
82The previous version of the SQLFairy XML allowed the attributes of the the
83schema objects to be written as either xml attributes or as data elements, in
84any combination. While this allows for lots of flexibility in writing the XML
85the result is a great many possible XML formats, not so good for DTD writing,
86XPathing etc! So we have moved to a fixed version described in
87L<SQL::Translator::Producer::XML::SQLFairy>.
88
89This version of the parser will still parse the old formats and emmit warnings
91f28468 90when it sees them being used but they should be considered B<heavily
91depreciated>.
4a268a6c 92
91f28468 93To convert your old format files simply pass them through the translator :)
4a268a6c 94
91f28468 95 $ sqlt -f XML-SQLFairy -t XML-SQLFairy schema-old.xml > schema-new.xml
4a268a6c 96
0a1ec87a 97=cut
98
99# -------------------------------------------------------------------
100
101use strict;
102
103use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
91f28468 104$VERSION = sprintf "%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/;
0a1ec87a 105$DEBUG = 0 unless defined $DEBUG;
106
107use Data::Dumper;
108use Exporter;
109use base qw(Exporter);
110@EXPORT_OK = qw(parse);
111
112use base qw/SQL::Translator::Parser/; # Doesnt do anything at the mo!
a5e624ac 113use SQL::Translator::Utils 'debug';
0a1ec87a 114use XML::XPath;
115use XML::XPath::XMLParser;
116
0a1ec87a 117sub parse {
118 my ( $translator, $data ) = @_;
a5e624ac 119 my $schema = $translator->schema;
120 local $DEBUG = $translator->debug;
121 my $xp = XML::XPath->new(xml => $data);
0a1ec87a 122
0a1ec87a 123 $xp->set_namespace("sqlf", "http://sqlfairy.sourceforge.net/sqlfairy.xml");
124
a5e624ac 125 #
0a1ec87a 126 # Work our way through the tables
127 #
87c5565e 128 my @nodes = $xp->findnodes(
129 '/sqlf:schema/sqlf:table|/sqlf:schema/sqlf:tables/sqlf:table'
130 );
0a1ec87a 131 for my $tblnode (
8571d198 132 sort {
59b2ec83 133 "".$xp->findvalue('sqlf:order|@order',$a)
8571d198 134 <=>
135 "".$xp->findvalue('sqlf:order|@order',$b)
a5e624ac 136 } @nodes
0a1ec87a 137 ) {
138 debug "Adding table:".$xp->findvalue('sqlf:name',$tblnode);
a5e624ac 139
0a1ec87a 140 my $table = $schema->add_table(
141 get_tagfields($xp, $tblnode, "sqlf:" => qw/name order/)
142 ) or die $schema->error;
143
a5e624ac 144 #
0a1ec87a 145 # Fields
146 #
147 my @nodes = $xp->findnodes('sqlf:fields/sqlf:field',$tblnode);
148 foreach (
8571d198 149 sort {
a5e624ac 150 ("".$xp->findvalue('sqlf:order',$a) || 0)
8571d198 151 <=>
152 ("".$xp->findvalue('sqlf:order',$b) || 0)
a5e624ac 153 } @nodes
0a1ec87a 154 ) {
155 my %fdata = get_tagfields($xp, $_, "sqlf:",
94ed484b 156 qw/name data_type size default_value is_nullable extra
a5e624ac 157 is_auto_increment is_primary_key is_foreign_key comments/
158 );
159
160 if (
8571d198 161 exists $fdata{'default_value'} and
a5e624ac 162 defined $fdata{'default_value'}
163 ) {
164 if ( $fdata{'default_value'} =~ /^\s*NULL\s*$/ ) {
165 $fdata{'default_value'}= undef;
0a1ec87a 166 }
a5e624ac 167 elsif ( $fdata{'default_value'} =~ /^\s*EMPTY_STRING\s*$/ ) {
168 $fdata{'default_value'} = "";
0a1ec87a 169 }
170 }
a5e624ac 171
19922fbc 172 my $field = $table->add_field( %fdata ) or die $table->error;
a5e624ac 173
174 $table->primary_key( $field->name ) if $fdata{'is_primary_key'};
175
176 #
177 # TODO:
8571d198 178 # - We should be able to make the table obj spot this when
a5e624ac 179 # we use add_field.
a5e624ac 180 #
0a1ec87a 181 }
182
a5e624ac 183 #
0a1ec87a 184 # Constraints
185 #
186 @nodes = $xp->findnodes('sqlf:constraints/sqlf:constraint',$tblnode);
187 foreach (@nodes) {
188 my %data = get_tagfields($xp, $_, "sqlf:",
a5e624ac 189 qw/name type table fields reference_fields reference_table
190 match_type on_delete_do on_update_do/
191 );
19922fbc 192 $table->add_constraint( %data ) or die $table->error;
0a1ec87a 193 }
194
a5e624ac 195 #
0a1ec87a 196 # Indexes
197 #
198 @nodes = $xp->findnodes('sqlf:indices/sqlf:index',$tblnode);
199 foreach (@nodes) {
200 my %data = get_tagfields($xp, $_, "sqlf:",
a5e624ac 201 qw/name type fields options/);
19922fbc 202 $table->add_index( %data ) or die $table->error;
0a1ec87a 203 }
204
205 } # tables loop
206
19922fbc 207 #
208 # Views
209 #
87c5565e 210 @nodes = $xp->findnodes(
211 '/sqlf:schema/sqlf:view|/sqlf:schema/sqlf:views/sqlf:view'
212 );
19922fbc 213 foreach (@nodes) {
214 my %data = get_tagfields($xp, $_, "sqlf:",
215 qw/name sql fields order/
216 );
217 $schema->add_view( %data ) or die $schema->error;
218 }
8571d198 219
19922fbc 220 #
221 # Triggers
222 #
87c5565e 223 @nodes = $xp->findnodes(
224 '/sqlf:schema/sqlf:trigger|/sqlf:schema/sqlf:triggers/sqlf:trigger'
225 );
19922fbc 226 foreach (@nodes) {
227 my %data = get_tagfields($xp, $_, "sqlf:",
228 qw/name perform_action_when database_event fields on_table action order/
229 );
230 $schema->add_trigger( %data ) or die $schema->error;
231 }
8571d198 232
19922fbc 233 #
234 # Procedures
235 #
87c5565e 236 @nodes = $xp->findnodes(
237 '/sqlf:schema/sqlf:procedure|/sqlf:schema/sqlf:procedures/sqlf:procedure'
238 );
19922fbc 239 foreach (@nodes) {
240 my %data = get_tagfields($xp, $_, "sqlf:",
241 qw/name sql parameters owner comments order/
242 );
243 $schema->add_procedure( %data ) or die $schema->error;
244 }
8571d198 245
0a1ec87a 246 return 1;
247}
248
a5e624ac 249# -------------------------------------------------------------------
250sub get_tagfields {
251#
8571d198 252# get_tagfields XP, NODE, NAMESPACE => qw/TAGNAMES/;
0a1ec87a 253# get_tagfields $node, "sqlf:" => qw/name type fields reference/;
254#
8571d198 255# Returns hash of data.
256# TODO - Add handling of an explicit NULL value.
a5e624ac 257#
258
0a1ec87a 259 my ($xp, $node, @names) = @_;
260 my (%data, $ns);
261 foreach (@names) {
262 if ( m/:$/ ) { $ns = $_; next; } # Set def namespace
263 my $thisns = (s/(^.*?:)// ? $1 : $ns);
a5e624ac 264
94ed484b 265 my $is_attrib = m/^(sql|comments|action|extra)$/ ? 0 : 1;
a5e624ac 266
8571d198 267 my $attrib_path = "\@$thisns$_";
268 my $tag_path = "$thisns$_";
269 if ( $xp->exists($attrib_path,$node) ) {
270 $data{$_} = "".$xp->findvalue($attrib_path,$node);
271 warn "Use of '$_' as an attribute is depricated."
272 ." Use a child tag instead."
273 ." To convert your file to the new version see the Docs.\n"
274 unless $is_attrib;
275 debug "Got $_=".( defined $data{ $_ } ? $data{ $_ } : 'UNDEF' );
276 }
277 elsif ( $xp->exists($tag_path,$node) ) {
94ed484b 278 if ($_ eq "extra") {
279 my %extra;
280 my $extra_nodes = $xp->find($tag_path,$node);
281 foreach ( $extra_nodes->pop->getAttributes ) {
282 $extra{$_->getName} = $_->getData;
283 }
284 $data{$_} = \%extra;
285 }
286 else {
287 $data{$_} = "".$xp->findvalue($tag_path,$node);
288 }
8571d198 289 warn "Use of '$_' as a child tag is depricated."
290 ." Use an attribute instead."
291 ." To convert your file to the new version see the Docs.\n"
292 if $is_attrib;
a5e624ac 293 debug "Got $_=".( defined $data{ $_ } ? $data{ $_ } : 'UNDEF' );
0a1ec87a 294 }
295 }
a5e624ac 296
0a1ec87a 297 return wantarray ? %data : \%data;
298}
299
3001;
301
302# -------------------------------------------------------------------
303
304=pod
305
306=head1 BUGS
307
8571d198 308Ignores the order attribute for Constraints, Views, Indices,
309Views, Triggers and Procedures, using the tag order instead. (This is the order
310output by the SQLFairy XML producer).
0a1ec87a 311
91f28468 312=head1 SEE ALSO
313
314L<perl>, L<SQL::Translator>, L<SQL::Translator::Producer::XML::SQLFairy>,
315L<SQL::Translator::Schema>.
316
0a1ec87a 317=head1 TODO
318
319=over 4
320
8571d198 321=item *
a5e624ac 322
94ed484b 323Support options attribute.
a5e624ac 324
8571d198 325=item *
0a1ec87a 326
8571d198 327Test foreign keys are parsed ok.
0a1ec87a 328
8571d198 329=item *
0a1ec87a 330
91f28468 331Control over defaulting.
0a1ec87a 332
333=back
334
335=head1 AUTHOR
336
337Mark D. Addison E<lt>mark.addison@itn.co.ukE<gt>.
338
0a1ec87a 339=cut