X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FParser%2FXML%2FSQLFairy.pm;h=fc128f116a3e64008e86c8a892ce306f5ba025b5;hb=0c04c5a2210135419771878dc7e341a1cba52cca;hp=7a1213698690d42ec516ffd814ccd371ff6e4dbc;hpb=b178940934ec79968ed16511ec2644f3736c92f2;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Parser/XML/SQLFairy.pm b/lib/SQL/Translator/Parser/XML/SQLFairy.pm index 7a12136..fc128f1 100644 --- a/lib/SQL/Translator/Parser/XML/SQLFairy.pm +++ b/lib/SQL/Translator/Parser/XML/SQLFairy.pm @@ -1,25 +1,5 @@ package SQL::Translator::Parser::XML::SQLFairy; -# ------------------------------------------------------------------- -# $Id: SQLFairy.pm,v 1.12 2004-11-05 15:03:09 grommit Exp $ -# ------------------------------------------------------------------- -# Copyright (C) 2003 Mark Addison , -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License as -# published by the Free Software Foundation; version 2. -# -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA -# 02111-1307 USA -# ------------------------------------------------------------------- - =head1 NAME SQL::Translator::Parser::XML::SQLFairy - parser for SQL::Translator's XML. @@ -95,31 +75,32 @@ To convert your old format files simply pass them through the translator :) =cut -# ------------------------------------------------------------------- - use strict; +use warnings; -use vars qw[ $DEBUG $VERSION @EXPORT_OK ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/; +our ( $DEBUG, @EXPORT_OK ); +our $VERSION = '1.59'; $DEBUG = 0 unless defined $DEBUG; use Data::Dumper; +use Carp::Clan qw/^SQL::Translator/; use Exporter; use base qw(Exporter); @EXPORT_OK = qw(parse); use base qw/SQL::Translator::Parser/; # Doesnt do anything at the mo! use SQL::Translator::Utils 'debug'; -use XML::XPath; -use XML::XPath::XMLParser; +use XML::LibXML 1.69; +use XML::LibXML::XPathContext; sub parse { my ( $translator, $data ) = @_; my $schema = $translator->schema; local $DEBUG = $translator->debug; - my $xp = XML::XPath->new(xml => $data); + my $doc = XML::LibXML->new->parse_string($data); + my $xp = XML::LibXML::XPathContext->new($doc); - $xp->set_namespace("sqlf", "http://sqlfairy.sourceforge.net/sqlfairy.xml"); + $xp->registerNs("sqlf", "http://sqlfairy.sourceforge.net/sqlfairy.xml"); # # Work our way through the tables @@ -129,9 +110,9 @@ sub parse { ); for my $tblnode ( sort { - "".$xp->findvalue('sqlf:order|@order',$a) + ("".$xp->findvalue('sqlf:order|@order',$a) || 0) <=> - "".$xp->findvalue('sqlf:order|@order',$b) + ("".$xp->findvalue('sqlf:order|@order',$b) || 0) } @nodes ) { debug "Adding table:".$xp->findvalue('sqlf:name',$tblnode); @@ -186,7 +167,7 @@ sub parse { foreach (@nodes) { my %data = get_tagfields($xp, $_, "sqlf:", qw/name type table fields reference_fields reference_table - match_type on_delete_do on_update_do extra/ + match_type on_delete on_update extra/ ); $table->add_constraint( %data ) or die $table->error; } @@ -201,6 +182,16 @@ sub parse { $table->add_index( %data ) or die $table->error; } + + # + # Comments + # + @nodes = $xp->findnodes('sqlf:comments/sqlf:comment',$tblnode); + foreach (@nodes) { + my $data = $_->string_value; + $table->comments( $data ); + } + } # tables loop # @@ -224,9 +215,26 @@ sub parse { ); foreach (@nodes) { my %data = get_tagfields($xp, $_, "sqlf:", qw/ - name perform_action_when database_event fields on_table action order - extra + name perform_action_when database_event database_events fields + on_table action order extra /); + + # back compat + if (my $evt = $data{database_event} and $translator->{show_warnings}) { + carp 'The database_event tag is deprecated - please use ' . + 'database_events (which can take one or more comma separated ' . + 'event names)'; + $data{database_events} = join (', ', + $data{database_events} || (), + $evt, + ); + } + + # split into arrayref + if (my $evts = $data{database_events}) { + $data{database_events} = [split (/\s*,\s*/, $evts) ]; + } + $schema->add_trigger( %data ) or die $schema->error; } @@ -246,7 +254,6 @@ sub parse { return 1; } -# ------------------------------------------------------------------- sub get_tagfields { # # get_tagfields XP, NODE, NAMESPACE => qw/TAGNAMES/; @@ -264,27 +271,26 @@ sub get_tagfields { my $is_attrib = m/^(sql|comments|action|extra)$/ ? 0 : 1; - my $attrib_path = "\@$thisns$_"; + my $attrib_path = "\@$_"; my $tag_path = "$thisns$_"; - if ( $xp->exists($attrib_path,$node) ) { - $data{$_} = "".$xp->findvalue($attrib_path,$node); + if ( my $found = $xp->find($attrib_path,$node) ) { + $data{$_} = "".$found->to_literal; warn "Use of '$_' as an attribute is depricated." ." Use a child tag instead." ." To convert your file to the new version see the Docs.\n" unless $is_attrib; debug "Got $_=".( defined $data{ $_ } ? $data{ $_ } : 'UNDEF' ); } - elsif ( $xp->exists($tag_path,$node) ) { + elsif ( $found = $xp->find($tag_path,$node) ) { if ($_ eq "extra") { my %extra; - my $extra_nodes = $xp->find($tag_path,$node); - foreach ( $extra_nodes->pop->getAttributes ) { + foreach ( $found->pop->getAttributes ) { $extra{$_->getName} = $_->getData; } $data{$_} = \%extra; } else { - $data{$_} = "".$xp->findvalue($tag_path,$node); + $data{$_} = "".$found->to_literal; } warn "Use of '$_' as a child tag is depricated." ." Use an attribute instead." @@ -299,15 +305,13 @@ sub get_tagfields { 1; -# ------------------------------------------------------------------- - =pod =head1 BUGS -Ignores the order attribute for Constraints, Views, Indices, -Views, Triggers and Procedures, using the tag order instead. (This is the order -output by the SQLFairy XML producer). +Ignores the order attribute for Constraints, Views, Indices, Views, Triggers +and Procedures, using the tag order instead. (This is the order output by the +SQLFairy XML producer). =head1 SEE ALSO @@ -334,6 +338,7 @@ Control over defaulting. =head1 AUTHOR -Mark D. Addison Emark.addison@itn.co.ukE. +Mark D. Addison Emark.addison@itn.co.ukE, +Jonathan Yu Efrequency@cpan.orgE =cut