1 package SQL::Translator::Parser::XML::SQLFairy;
5 SQL::Translator::Parser::XML::SQLFairy - parser for SQL::Translator's XML.
11 my $translator = SQL::Translator->new( show_warnings => 1 );
13 my $out = $obj->translate(
14 from => 'XML-SQLFairy',
16 filename => 'schema.xml',
17 ) or die $translator->error;
23 This parser handles the flavor of XML used natively by the SQLFairy
24 project (L<SQL::Translator>). The XML must be in the namespace
25 "http://sqlfairy.sourceforge.net/sqlfairy.xml."
26 See L<SQL::Translator::Producer::XML::SQLFairy> for details of this format.
28 You do not need to specify every attribute of the Schema objects as any missing
29 from the XML will be set to their default values. e.g. A field could be written
32 <sqlf:field name="email" data_type="varchar" size="255" />
36 <sqlf:field name="email" data_type="varchar" size="255" is_nullable="1"
37 is_auto_increment="0" is_primary_key="0" is_foreign_key="0" order="4">
38 <sqlf:comments></sqlf:comments>
41 If you do not explicitly set the order of items using order attributes on the
42 tags then the order the tags appear in the XML will be used.
46 Leave the attribute out all together to use the default in L<Schema::Field>.
47 Use empty quotes or 'EMPTY_STRING' for a zero lenth string. 'NULL' for an
48 explicit null (currently sets default_value to undef in the
51 <sqlf:field default_value="" /> <!-- Empty string -->
52 <sqlf:field default_value="EMPTY_STRING" /> <!-- Empty string -->
53 <sqlf:field default_value="NULL" /> <!-- NULL -->
57 Doesn't take any extra parser args at the moment.
61 The previous version of the SQLFairy XML allowed the attributes of the the
62 schema objects to be written as either xml attributes or as data elements, in
63 any combination. While this allows for lots of flexibility in writing the XML
64 the result is a great many possible XML formats, not so good for DTD writing,
65 XPathing etc! So we have moved to a fixed version described in
66 L<SQL::Translator::Producer::XML::SQLFairy>.
68 This version of the parser will still parse the old formats and emmit warnings
69 when it sees them being used but they should be considered B<heavily
72 To convert your old format files simply pass them through the translator :)
74 $ sqlt -f XML-SQLFairy -t XML-SQLFairy schema-old.xml > schema-new.xml
78 # -------------------------------------------------------------------
82 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
84 $DEBUG = 0 unless defined $DEBUG;
87 use Carp::Clan qw/^SQL::Translator/;
89 use base qw(Exporter);
90 @EXPORT_OK = qw(parse);
92 use base qw/SQL::Translator::Parser/; # Doesnt do anything at the mo!
93 use SQL::Translator::Utils 'debug';
95 use XML::LibXML::XPathContext;
98 my ( $translator, $data ) = @_;
99 my $schema = $translator->schema;
100 local $DEBUG = $translator->debug;
101 my $doc = XML::LibXML->new->parse_string($data);
102 my $xp = XML::LibXML::XPathContext->new($doc);
104 $xp->registerNs("sqlf", "http://sqlfairy.sourceforge.net/sqlfairy.xml");
107 # Work our way through the tables
109 my @nodes = $xp->findnodes(
110 '/sqlf:schema/sqlf:table|/sqlf:schema/sqlf:tables/sqlf:table'
114 ("".$xp->findvalue('sqlf:order|@order',$a) || 0)
116 ("".$xp->findvalue('sqlf:order|@order',$b) || 0)
119 debug "Adding table:".$xp->findvalue('sqlf:name',$tblnode);
121 my $table = $schema->add_table(
122 get_tagfields($xp, $tblnode, "sqlf:" => qw/name order extra/)
123 ) or die $schema->error;
128 my @nodes = $xp->findnodes('sqlf:fields/sqlf:field',$tblnode);
131 ("".$xp->findvalue('sqlf:order',$a) || 0)
133 ("".$xp->findvalue('sqlf:order',$b) || 0)
136 my %fdata = get_tagfields($xp, $_, "sqlf:",
137 qw/name data_type size default_value is_nullable extra
138 is_auto_increment is_primary_key is_foreign_key comments/
142 exists $fdata{'default_value'} and
143 defined $fdata{'default_value'}
145 if ( $fdata{'default_value'} =~ /^\s*NULL\s*$/ ) {
146 $fdata{'default_value'}= undef;
148 elsif ( $fdata{'default_value'} =~ /^\s*EMPTY_STRING\s*$/ ) {
149 $fdata{'default_value'} = "";
153 my $field = $table->add_field( %fdata ) or die $table->error;
155 $table->primary_key( $field->name ) if $fdata{'is_primary_key'};
159 # - We should be able to make the table obj spot this when
167 @nodes = $xp->findnodes('sqlf:constraints/sqlf:constraint',$tblnode);
169 my %data = get_tagfields($xp, $_, "sqlf:",
170 qw/name type table fields reference_fields reference_table
171 match_type on_delete on_update extra/
173 $table->add_constraint( %data ) or die $table->error;
179 @nodes = $xp->findnodes('sqlf:indices/sqlf:index',$tblnode);
181 my %data = get_tagfields($xp, $_, "sqlf:",
182 qw/name type fields options extra/);
183 $table->add_index( %data ) or die $table->error;
190 @nodes = $xp->findnodes('sqlf:comments/sqlf:comment',$tblnode);
192 my $data = $_->string_value;
193 $table->comments( $data );
201 @nodes = $xp->findnodes(
202 '/sqlf:schema/sqlf:view|/sqlf:schema/sqlf:views/sqlf:view'
205 my %data = get_tagfields($xp, $_, "sqlf:",
206 qw/name sql fields order extra/
208 $schema->add_view( %data ) or die $schema->error;
214 @nodes = $xp->findnodes(
215 '/sqlf:schema/sqlf:trigger|/sqlf:schema/sqlf:triggers/sqlf:trigger'
218 my %data = get_tagfields($xp, $_, "sqlf:", qw/
219 name perform_action_when database_event database_events fields
220 on_table action order extra
224 if (my $evt = $data{database_event} and $translator->{show_warnings}) {
225 carp 'The database_event tag is deprecated - please use ' .
226 'database_events (which can take one or more comma separated ' .
228 $data{database_events} = join (', ',
229 $data{database_events} || (),
234 # split into arrayref
235 if (my $evts = $data{database_events}) {
236 $data{database_events} = [split (/\s*,\s*/, $evts) ];
239 $schema->add_trigger( %data ) or die $schema->error;
245 @nodes = $xp->findnodes(
246 '/sqlf:schema/sqlf:procedure|/sqlf:schema/sqlf:procedures/sqlf:procedure'
249 my %data = get_tagfields($xp, $_, "sqlf:",
250 qw/name sql parameters owner comments order extra/
252 $schema->add_procedure( %data ) or die $schema->error;
258 # -------------------------------------------------------------------
261 # get_tagfields XP, NODE, NAMESPACE => qw/TAGNAMES/;
262 # get_tagfields $node, "sqlf:" => qw/name type fields reference/;
264 # Returns hash of data.
265 # TODO - Add handling of an explicit NULL value.
268 my ($xp, $node, @names) = @_;
271 if ( m/:$/ ) { $ns = $_; next; } # Set def namespace
272 my $thisns = (s/(^.*?:)// ? $1 : $ns);
274 my $is_attrib = m/^(sql|comments|action|extra)$/ ? 0 : 1;
276 my $attrib_path = "\@$_";
277 my $tag_path = "$thisns$_";
278 if ( my $found = $xp->find($attrib_path,$node) ) {
279 $data{$_} = "".$found->to_literal;
280 warn "Use of '$_' as an attribute is depricated."
281 ." Use a child tag instead."
282 ." To convert your file to the new version see the Docs.\n"
284 debug "Got $_=".( defined $data{ $_ } ? $data{ $_ } : 'UNDEF' );
286 elsif ( $found = $xp->find($tag_path,$node) ) {
289 foreach ( $found->pop->getAttributes ) {
290 $extra{$_->getName} = $_->getData;
295 $data{$_} = "".$found->to_literal;
297 warn "Use of '$_' as a child tag is depricated."
298 ." Use an attribute instead."
299 ." To convert your file to the new version see the Docs.\n"
301 debug "Got $_=".( defined $data{ $_ } ? $data{ $_ } : 'UNDEF' );
305 return wantarray ? %data : \%data;
310 # -------------------------------------------------------------------
316 Ignores the order attribute for Constraints, Views, Indices, Views, Triggers
317 and Procedures, using the tag order instead. (This is the order output by the
318 SQLFairy XML producer).
322 L<perl>, L<SQL::Translator>, L<SQL::Translator::Producer::XML::SQLFairy>,
323 L<SQL::Translator::Schema>.
331 Support options attribute.
335 Test foreign keys are parsed ok.
339 Control over defaulting.
345 Mark D. Addison E<lt>mark.addison@itn.co.ukE<gt>,
346 Jonathan Yu E<lt>frequency@cpan.orgE<gt>