1 package SQL::Translator::Parser::XML::SQLFairy;
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 The SQLFairy Authors,
5 # Copyright (C) 2003 Mark Addison <mark.addison@itn.co.uk>,
6 # Copyright (C) 2009 Jonathan Yu <frequency@cpan.org>
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.
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.
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
21 # -------------------------------------------------------------------
25 SQL::Translator::Parser::XML::SQLFairy - parser for SQL::Translator's XML.
31 my $translator = SQL::Translator->new( show_warnings => 1 );
33 my $out = $obj->translate(
34 from => 'XML-SQLFairy',
36 filename => 'schema.xml',
37 ) or die $translator->error;
43 This parser handles the flavor of XML used natively by the SQLFairy
44 project (L<SQL::Translator>). The XML must be in the namespace
45 "http://sqlfairy.sourceforge.net/sqlfairy.xml."
46 See L<SQL::Translator::Producer::XML::SQLFairy> for details of this format.
48 You do not need to specify every attribute of the Schema objects as any missing
49 from the XML will be set to their default values. e.g. A field could be written
52 <sqlf:field name="email" data_type="varchar" size="255" />
56 <sqlf:field name="email" data_type="varchar" size="255" is_nullable="1"
57 is_auto_increment="0" is_primary_key="0" is_foreign_key="0" order="4">
58 <sqlf:comments></sqlf:comments>
61 If you do not explicitly set the order of items using order attributes on the
62 tags then the order the tags appear in the XML will be used.
66 Leave the attribute out all together to use the default in L<Schema::Field>.
67 Use empty quotes or 'EMPTY_STRING' for a zero lenth string. 'NULL' for an
68 explicit null (currently sets default_value to undef in the
71 <sqlf:field default_value="" /> <!-- Empty string -->
72 <sqlf:field default_value="EMPTY_STRING" /> <!-- Empty string -->
73 <sqlf:field default_value="NULL" /> <!-- NULL -->
77 Doesn't take any extra parser args at the moment.
81 The previous version of the SQLFairy XML allowed the attributes of the the
82 schema objects to be written as either xml attributes or as data elements, in
83 any combination. While this allows for lots of flexibility in writing the XML
84 the result is a great many possible XML formats, not so good for DTD writing,
85 XPathing etc! So we have moved to a fixed version described in
86 L<SQL::Translator::Producer::XML::SQLFairy>.
88 This version of the parser will still parse the old formats and emmit warnings
89 when it sees them being used but they should be considered B<heavily
92 To convert your old format files simply pass them through the translator :)
94 $ sqlt -f XML-SQLFairy -t XML-SQLFairy schema-old.xml > schema-new.xml
98 # -------------------------------------------------------------------
102 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
104 $DEBUG = 0 unless defined $DEBUG;
107 use Carp::Clan qw/^SQL::Translator/;
109 use base qw(Exporter);
110 @EXPORT_OK = qw(parse);
112 use base qw/SQL::Translator::Parser/; # Doesnt do anything at the mo!
113 use SQL::Translator::Utils 'debug';
115 use XML::LibXML::XPathContext;
118 my ( $translator, $data ) = @_;
119 my $schema = $translator->schema;
120 local $DEBUG = $translator->debug;
121 my $doc = XML::LibXML->new->parse_string($data);
122 my $xp = XML::LibXML::XPathContext->new($doc);
124 $xp->registerNs("sqlf", "http://sqlfairy.sourceforge.net/sqlfairy.xml");
127 # Work our way through the tables
129 my @nodes = $xp->findnodes(
130 '/sqlf:schema/sqlf:table|/sqlf:schema/sqlf:tables/sqlf:table'
134 ("".$xp->findvalue('sqlf:order|@order',$a) || 0)
136 ("".$xp->findvalue('sqlf:order|@order',$b) || 0)
139 debug "Adding table:".$xp->findvalue('sqlf:name',$tblnode);
141 my $table = $schema->add_table(
142 get_tagfields($xp, $tblnode, "sqlf:" => qw/name order extra/)
143 ) or die $schema->error;
148 my @nodes = $xp->findnodes('sqlf:fields/sqlf:field',$tblnode);
151 ("".$xp->findvalue('sqlf:order',$a) || 0)
153 ("".$xp->findvalue('sqlf:order',$b) || 0)
156 my %fdata = get_tagfields($xp, $_, "sqlf:",
157 qw/name data_type size default_value is_nullable extra
158 is_auto_increment is_primary_key is_foreign_key comments/
162 exists $fdata{'default_value'} and
163 defined $fdata{'default_value'}
165 if ( $fdata{'default_value'} =~ /^\s*NULL\s*$/ ) {
166 $fdata{'default_value'}= undef;
168 elsif ( $fdata{'default_value'} =~ /^\s*EMPTY_STRING\s*$/ ) {
169 $fdata{'default_value'} = "";
173 my $field = $table->add_field( %fdata ) or die $table->error;
175 $table->primary_key( $field->name ) if $fdata{'is_primary_key'};
179 # - We should be able to make the table obj spot this when
187 @nodes = $xp->findnodes('sqlf:constraints/sqlf:constraint',$tblnode);
189 my %data = get_tagfields($xp, $_, "sqlf:",
190 qw/name type table fields reference_fields reference_table
191 match_type on_delete on_update extra/
193 $table->add_constraint( %data ) or die $table->error;
199 @nodes = $xp->findnodes('sqlf:indices/sqlf:index',$tblnode);
201 my %data = get_tagfields($xp, $_, "sqlf:",
202 qw/name type fields options extra/);
203 $table->add_index( %data ) or die $table->error;
210 @nodes = $xp->findnodes('sqlf:comments/sqlf:comment',$tblnode);
212 my $data = $_->string_value;
213 $table->comments( $data );
221 @nodes = $xp->findnodes(
222 '/sqlf:schema/sqlf:view|/sqlf:schema/sqlf:views/sqlf:view'
225 my %data = get_tagfields($xp, $_, "sqlf:",
226 qw/name sql fields order extra/
228 $schema->add_view( %data ) or die $schema->error;
234 @nodes = $xp->findnodes(
235 '/sqlf:schema/sqlf:trigger|/sqlf:schema/sqlf:triggers/sqlf:trigger'
238 my %data = get_tagfields($xp, $_, "sqlf:", qw/
239 name perform_action_when database_event database_events fields
240 on_table action order extra
244 if (my $evt = $data{database_event} and $translator->{show_warnings}) {
245 carp 'The database_event tag is deprecated - please use ' .
246 'database_events (which can take one or more comma separated ' .
248 $data{database_events} = join (', ',
249 $data{database_events} || (),
254 # split into arrayref
255 if (my $evts = $data{database_events}) {
256 $data{database_events} = [split (/\s*,\s*/, $evts) ];
259 $schema->add_trigger( %data ) or die $schema->error;
265 @nodes = $xp->findnodes(
266 '/sqlf:schema/sqlf:procedure|/sqlf:schema/sqlf:procedures/sqlf:procedure'
269 my %data = get_tagfields($xp, $_, "sqlf:",
270 qw/name sql parameters owner comments order extra/
272 $schema->add_procedure( %data ) or die $schema->error;
278 # -------------------------------------------------------------------
281 # get_tagfields XP, NODE, NAMESPACE => qw/TAGNAMES/;
282 # get_tagfields $node, "sqlf:" => qw/name type fields reference/;
284 # Returns hash of data.
285 # TODO - Add handling of an explicit NULL value.
288 my ($xp, $node, @names) = @_;
291 if ( m/:$/ ) { $ns = $_; next; } # Set def namespace
292 my $thisns = (s/(^.*?:)// ? $1 : $ns);
294 my $is_attrib = m/^(sql|comments|action|extra)$/ ? 0 : 1;
296 my $attrib_path = "\@$_";
297 my $tag_path = "$thisns$_";
298 if ( my $found = $xp->find($attrib_path,$node) ) {
299 $data{$_} = "".$found->to_literal;
300 warn "Use of '$_' as an attribute is depricated."
301 ." Use a child tag instead."
302 ." To convert your file to the new version see the Docs.\n"
304 debug "Got $_=".( defined $data{ $_ } ? $data{ $_ } : 'UNDEF' );
306 elsif ( $found = $xp->find($tag_path,$node) ) {
309 foreach ( $found->pop->getAttributes ) {
310 $extra{$_->getName} = $_->getData;
315 $data{$_} = "".$found->to_literal;
317 warn "Use of '$_' as a child tag is depricated."
318 ." Use an attribute instead."
319 ." To convert your file to the new version see the Docs.\n"
321 debug "Got $_=".( defined $data{ $_ } ? $data{ $_ } : 'UNDEF' );
325 return wantarray ? %data : \%data;
330 # -------------------------------------------------------------------
336 Ignores the order attribute for Constraints, Views, Indices, Views, Triggers
337 and Procedures, using the tag order instead. (This is the order output by the
338 SQLFairy XML producer).
342 L<perl>, L<SQL::Translator>, L<SQL::Translator::Producer::XML::SQLFairy>,
343 L<SQL::Translator::Schema>.
351 Support options attribute.
355 Test foreign keys are parsed ok.
359 Control over defaulting.
365 Mark D. Addison E<lt>mark.addison@itn.co.ukE<gt>,
366 Jonathan Yu E<lt>frequency@cpan.orgE<gt>