*** empty log message ***
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / XML / SQLFairy.pm
CommitLineData
0a1ec87a 1package SQL::Translator::Parser::XML::SQLFairy;
2
3# -------------------------------------------------------------------
8571d198 4# $Id: SQLFairy.pm,v 1.6 2004-07-08 19:06:24 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
45project (SQL::Translator). The XML must be in the namespace
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
a5e624ac 67Leave the tag out all together to use the default in Schema::Field.
8571d198 68Use empty tags 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
a5e624ac 72 <sqlf:default_value></sqlf:default_value> <!-- Empty string -->
73 <sqlf:default_value>EMPTY_STRING</sqlf:default_value> <!-- Empty string -->
8571d198 74 <sqlf:default_value/> <!-- Empty string -->
a5e624ac 75 <sqlf:default_value>NULL</sqlf:default_value> <!-- NULL -->
0a1ec87a 76
0a1ec87a 77=head2 ARGS
78
79Doesn't take any extra parser args at the moment.
80
81=cut
82
83# -------------------------------------------------------------------
84
85use strict;
86
87use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
8571d198 88$VERSION = sprintf "%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/;
0a1ec87a 89$DEBUG = 0 unless defined $DEBUG;
90
91use Data::Dumper;
92use Exporter;
93use base qw(Exporter);
94@EXPORT_OK = qw(parse);
95
96use base qw/SQL::Translator::Parser/; # Doesnt do anything at the mo!
a5e624ac 97use SQL::Translator::Utils 'debug';
0a1ec87a 98use XML::XPath;
99use XML::XPath::XMLParser;
100
0a1ec87a 101sub parse {
102 my ( $translator, $data ) = @_;
a5e624ac 103 my $schema = $translator->schema;
104 local $DEBUG = $translator->debug;
105 my $xp = XML::XPath->new(xml => $data);
0a1ec87a 106
0a1ec87a 107 $xp->set_namespace("sqlf", "http://sqlfairy.sourceforge.net/sqlfairy.xml");
108
a5e624ac 109 #
0a1ec87a 110 # Work our way through the tables
111 #
112 my @nodes = $xp->findnodes('/sqlf:schema/sqlf:table');
113 for my $tblnode (
8571d198 114 sort {
59b2ec83 115 "".$xp->findvalue('sqlf:order|@order',$a)
8571d198 116 <=>
117 "".$xp->findvalue('sqlf:order|@order',$b)
a5e624ac 118 } @nodes
0a1ec87a 119 ) {
120 debug "Adding table:".$xp->findvalue('sqlf:name',$tblnode);
a5e624ac 121
0a1ec87a 122 my $table = $schema->add_table(
123 get_tagfields($xp, $tblnode, "sqlf:" => qw/name order/)
124 ) or die $schema->error;
125
a5e624ac 126 #
0a1ec87a 127 # Fields
128 #
129 my @nodes = $xp->findnodes('sqlf:fields/sqlf:field',$tblnode);
130 foreach (
8571d198 131 sort {
a5e624ac 132 ("".$xp->findvalue('sqlf:order',$a) || 0)
8571d198 133 <=>
134 ("".$xp->findvalue('sqlf:order',$b) || 0)
a5e624ac 135 } @nodes
0a1ec87a 136 ) {
137 my %fdata = get_tagfields($xp, $_, "sqlf:",
a5e624ac 138 qw/name data_type size default_value is_nullable
139 is_auto_increment is_primary_key is_foreign_key comments/
140 );
141
142 if (
8571d198 143 exists $fdata{'default_value'} and
a5e624ac 144 defined $fdata{'default_value'}
145 ) {
146 if ( $fdata{'default_value'} =~ /^\s*NULL\s*$/ ) {
147 $fdata{'default_value'}= undef;
0a1ec87a 148 }
a5e624ac 149 elsif ( $fdata{'default_value'} =~ /^\s*EMPTY_STRING\s*$/ ) {
150 $fdata{'default_value'} = "";
0a1ec87a 151 }
152 }
a5e624ac 153
19922fbc 154 my $field = $table->add_field( %fdata ) or die $table->error;
a5e624ac 155
156 $table->primary_key( $field->name ) if $fdata{'is_primary_key'};
157
158 #
159 # TODO:
8571d198 160 # - We should be able to make the table obj spot this when
a5e624ac 161 # we use add_field.
162 # - Deal with $field->extra
163 #
0a1ec87a 164 }
165
a5e624ac 166 #
0a1ec87a 167 # Constraints
168 #
169 @nodes = $xp->findnodes('sqlf:constraints/sqlf:constraint',$tblnode);
170 foreach (@nodes) {
171 my %data = get_tagfields($xp, $_, "sqlf:",
a5e624ac 172 qw/name type table fields reference_fields reference_table
173 match_type on_delete_do on_update_do/
174 );
19922fbc 175 $table->add_constraint( %data ) or die $table->error;
0a1ec87a 176 }
177
a5e624ac 178 #
0a1ec87a 179 # Indexes
180 #
181 @nodes = $xp->findnodes('sqlf:indices/sqlf:index',$tblnode);
182 foreach (@nodes) {
183 my %data = get_tagfields($xp, $_, "sqlf:",
a5e624ac 184 qw/name type fields options/);
19922fbc 185 $table->add_index( %data ) or die $table->error;
0a1ec87a 186 }
187
188 } # tables loop
189
19922fbc 190 #
191 # Views
192 #
193 @nodes = $xp->findnodes('/sqlf:schema/sqlf:view');
194 foreach (@nodes) {
195 my %data = get_tagfields($xp, $_, "sqlf:",
196 qw/name sql fields order/
197 );
198 $schema->add_view( %data ) or die $schema->error;
199 }
8571d198 200
19922fbc 201 #
202 # Triggers
203 #
204 @nodes = $xp->findnodes('/sqlf:schema/sqlf:trigger');
205 foreach (@nodes) {
206 my %data = get_tagfields($xp, $_, "sqlf:",
207 qw/name perform_action_when database_event fields on_table action order/
208 );
209 $schema->add_trigger( %data ) or die $schema->error;
210 }
8571d198 211
19922fbc 212 #
213 # Procedures
214 #
215 @nodes = $xp->findnodes('/sqlf:schema/sqlf:procedure');
216 foreach (@nodes) {
217 my %data = get_tagfields($xp, $_, "sqlf:",
218 qw/name sql parameters owner comments order/
219 );
220 $schema->add_procedure( %data ) or die $schema->error;
221 }
8571d198 222
0a1ec87a 223 return 1;
224}
225
a5e624ac 226# -------------------------------------------------------------------
227sub get_tagfields {
228#
8571d198 229# get_tagfields XP, NODE, NAMESPACE => qw/TAGNAMES/;
0a1ec87a 230# get_tagfields $node, "sqlf:" => qw/name type fields reference/;
231#
8571d198 232# Returns hash of data.
233# TODO - Add handling of an explicit NULL value.
a5e624ac 234#
235
0a1ec87a 236 my ($xp, $node, @names) = @_;
237 my (%data, $ns);
238 foreach (@names) {
239 if ( m/:$/ ) { $ns = $_; next; } # Set def namespace
240 my $thisns = (s/(^.*?:)// ? $1 : $ns);
a5e624ac 241
8571d198 242 my $is_attrib = m/^sql|comments|action$/ ? 0 : 1;
a5e624ac 243
8571d198 244 my $attrib_path = "\@$thisns$_";
245 my $tag_path = "$thisns$_";
246 if ( $xp->exists($attrib_path,$node) ) {
247 $data{$_} = "".$xp->findvalue($attrib_path,$node);
248 warn "Use of '$_' as an attribute is depricated."
249 ." Use a child tag instead."
250 ." To convert your file to the new version see the Docs.\n"
251 unless $is_attrib;
252 debug "Got $_=".( defined $data{ $_ } ? $data{ $_ } : 'UNDEF' );
253 }
254 elsif ( $xp->exists($tag_path,$node) ) {
255 $data{$_} = "".$xp->findvalue($tag_path,$node);
256 warn "Use of '$_' as a child tag is depricated."
257 ." Use an attribute instead."
258 ." To convert your file to the new version see the Docs.\n"
259 if $is_attrib;
a5e624ac 260 debug "Got $_=".( defined $data{ $_ } ? $data{ $_ } : 'UNDEF' );
0a1ec87a 261 }
262 }
a5e624ac 263
0a1ec87a 264 return wantarray ? %data : \%data;
265}
266
2671;
268
269# -------------------------------------------------------------------
270
271=pod
272
273=head1 BUGS
274
8571d198 275Ignores the order attribute for Constraints, Views, Indices,
276Views, Triggers and Procedures, using the tag order instead. (This is the order
277output by the SQLFairy XML producer).
0a1ec87a 278
279=head1 TODO
280
281=over 4
282
8571d198 283=item *
a5e624ac 284
8571d198 285Support options and extra attributes.
a5e624ac 286
8571d198 287=item *
0a1ec87a 288
8571d198 289Test foreign keys are parsed ok.
0a1ec87a 290
8571d198 291=item *
0a1ec87a 292
a5e624ac 293Control over defaulting of non-existant tags.
0a1ec87a 294
295=back
296
297=head1 AUTHOR
298
299Mark D. Addison E<lt>mark.addison@itn.co.ukE<gt>.
300
301=head1 SEE ALSO
302
303perl(1), SQL::Translator, SQL::Translator::Producer::XML::SQLFairy,
304SQL::Translator::Schema.
305
306=cut