1 package SQL::Translator::Parser::XML::SQLFairy;
3 # -------------------------------------------------------------------
4 # $Id: SQLFairy.pm,v 1.9 2004-08-19 14:08:59 grommit Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Mark Addison <mark.addison@itn.co.uk>,
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.
30 use SQL::Translator::Parser::XML::SQLFairy;
32 my $translator = SQL::Translator->new(
33 from => 'XML-SQLFairy',
35 filename => 'schema.xml',
40 print $obj->translate;
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.
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
53 <sqlf:field name="email" data_type="varchar" size="255" />
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>
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.
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
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 -->
79 Doesn't take any extra parser args at the moment.
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>.
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
95 To convert your old format files simply pass them through the translator;
97 sqlt -f XML-SQLFairy -t XML-SQLFairy schema-old.xml > schema-new.xml
101 # -------------------------------------------------------------------
105 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
106 $VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/;
107 $DEBUG = 0 unless defined $DEBUG;
111 use base qw(Exporter);
112 @EXPORT_OK = qw(parse);
114 use base qw/SQL::Translator::Parser/; # Doesnt do anything at the mo!
115 use SQL::Translator::Utils 'debug';
117 use XML::XPath::XMLParser;
120 my ( $translator, $data ) = @_;
121 my $schema = $translator->schema;
122 local $DEBUG = $translator->debug;
123 my $xp = XML::XPath->new(xml => $data);
125 $xp->set_namespace("sqlf", "http://sqlfairy.sourceforge.net/sqlfairy.xml");
128 # Work our way through the tables
130 my @nodes = $xp->findnodes(
131 '/sqlf:schema/sqlf:table|/sqlf:schema/sqlf:tables/sqlf:table'
135 "".$xp->findvalue('sqlf:order|@order',$a)
137 "".$xp->findvalue('sqlf:order|@order',$b)
140 debug "Adding table:".$xp->findvalue('sqlf:name',$tblnode);
142 my $table = $schema->add_table(
143 get_tagfields($xp, $tblnode, "sqlf:" => qw/name order/)
144 ) or die $schema->error;
149 my @nodes = $xp->findnodes('sqlf:fields/sqlf:field',$tblnode);
152 ("".$xp->findvalue('sqlf:order',$a) || 0)
154 ("".$xp->findvalue('sqlf:order',$b) || 0)
157 my %fdata = get_tagfields($xp, $_, "sqlf:",
158 qw/name data_type size default_value is_nullable extra
159 is_auto_increment is_primary_key is_foreign_key comments/
163 exists $fdata{'default_value'} and
164 defined $fdata{'default_value'}
166 if ( $fdata{'default_value'} =~ /^\s*NULL\s*$/ ) {
167 $fdata{'default_value'}= undef;
169 elsif ( $fdata{'default_value'} =~ /^\s*EMPTY_STRING\s*$/ ) {
170 $fdata{'default_value'} = "";
174 my $field = $table->add_field( %fdata ) or die $table->error;
176 $table->primary_key( $field->name ) if $fdata{'is_primary_key'};
180 # - We should be able to make the table obj spot this when
188 @nodes = $xp->findnodes('sqlf:constraints/sqlf:constraint',$tblnode);
190 my %data = get_tagfields($xp, $_, "sqlf:",
191 qw/name type table fields reference_fields reference_table
192 match_type on_delete_do on_update_do/
194 $table->add_constraint( %data ) or die $table->error;
200 @nodes = $xp->findnodes('sqlf:indices/sqlf:index',$tblnode);
202 my %data = get_tagfields($xp, $_, "sqlf:",
203 qw/name type fields options/);
204 $table->add_index( %data ) or die $table->error;
212 @nodes = $xp->findnodes(
213 '/sqlf:schema/sqlf:view|/sqlf:schema/sqlf:views/sqlf:view'
216 my %data = get_tagfields($xp, $_, "sqlf:",
217 qw/name sql fields order/
219 $schema->add_view( %data ) or die $schema->error;
225 @nodes = $xp->findnodes(
226 '/sqlf:schema/sqlf:trigger|/sqlf:schema/sqlf:triggers/sqlf:trigger'
229 my %data = get_tagfields($xp, $_, "sqlf:",
230 qw/name perform_action_when database_event fields on_table action order/
232 $schema->add_trigger( %data ) or die $schema->error;
238 @nodes = $xp->findnodes(
239 '/sqlf:schema/sqlf:procedure|/sqlf:schema/sqlf:procedures/sqlf:procedure'
242 my %data = get_tagfields($xp, $_, "sqlf:",
243 qw/name sql parameters owner comments order/
245 $schema->add_procedure( %data ) or die $schema->error;
251 # -------------------------------------------------------------------
254 # get_tagfields XP, NODE, NAMESPACE => qw/TAGNAMES/;
255 # get_tagfields $node, "sqlf:" => qw/name type fields reference/;
257 # Returns hash of data.
258 # TODO - Add handling of an explicit NULL value.
261 my ($xp, $node, @names) = @_;
264 if ( m/:$/ ) { $ns = $_; next; } # Set def namespace
265 my $thisns = (s/(^.*?:)// ? $1 : $ns);
267 my $is_attrib = m/^(sql|comments|action|extra)$/ ? 0 : 1;
269 my $attrib_path = "\@$thisns$_";
270 my $tag_path = "$thisns$_";
271 if ( $xp->exists($attrib_path,$node) ) {
272 $data{$_} = "".$xp->findvalue($attrib_path,$node);
273 warn "Use of '$_' as an attribute is depricated."
274 ." Use a child tag instead."
275 ." To convert your file to the new version see the Docs.\n"
277 debug "Got $_=".( defined $data{ $_ } ? $data{ $_ } : 'UNDEF' );
279 elsif ( $xp->exists($tag_path,$node) ) {
282 my $extra_nodes = $xp->find($tag_path,$node);
283 foreach ( $extra_nodes->pop->getAttributes ) {
284 $extra{$_->getName} = $_->getData;
289 $data{$_} = "".$xp->findvalue($tag_path,$node);
291 warn "Use of '$_' as a child tag is depricated."
292 ." Use an attribute instead."
293 ." To convert your file to the new version see the Docs.\n"
295 debug "Got $_=".( defined $data{ $_ } ? $data{ $_ } : 'UNDEF' );
299 return wantarray ? %data : \%data;
304 # -------------------------------------------------------------------
310 Ignores the order attribute for Constraints, Views, Indices,
311 Views, Triggers and Procedures, using the tag order instead. (This is the order
312 output by the SQLFairy XML producer).
320 Support options attribute.
324 Test foreign keys are parsed ok.
328 Control over defaulting of non-existant tags.
334 Mark D. Addison E<lt>mark.addison@itn.co.ukE<gt>.
338 perl(1), SQL::Translator, SQL::Translator::Producer::XML::SQLFairy,
339 SQL::Translator::Schema.