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