Added parsing of field.extra
Mark Addison [Fri, 9 Jul 2004 00:50:47 +0000 (00:50 +0000)]
lib/SQL/Translator/Parser/XML/SQLFairy.pm
t/16xml-parser.t
t/data/xml/schema.xml

index baa2cbb..f8af966 100644 (file)
@@ -1,7 +1,7 @@
 package SQL::Translator::Parser::XML::SQLFairy;
 
 # -------------------------------------------------------------------
-# $Id: SQLFairy.pm,v 1.7 2004-07-08 19:34:29 grommit Exp $
+# $Id: SQLFairy.pm,v 1.8 2004-07-09 00:50:06 grommit Exp $
 # -------------------------------------------------------------------
 # Copyright (C) 2003 Mark Addison <mark.addison@itn.co.uk>,
 #
@@ -103,7 +103,7 @@ To convert your old format files simply pass them through the translator;
 use strict;
 
 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/;
 $DEBUG   = 0 unless defined $DEBUG;
 
 use Data::Dumper;
@@ -153,7 +153,7 @@ sub parse {
             } @nodes
         ) {
             my %fdata = get_tagfields($xp, $_, "sqlf:",
-                qw/name data_type size default_value is_nullable 
+                qw/name data_type size default_value is_nullable extra
                 is_auto_increment is_primary_key is_foreign_key comments/
             );
 
@@ -177,7 +177,6 @@ sub parse {
             # TODO:
             # - We should be able to make the table obj spot this when
             #   we use add_field.
-            # - Deal with $field->extra
             #
         }
 
@@ -257,7 +256,7 @@ sub get_tagfields {
         if ( m/:$/ ) { $ns = $_; next; }  # Set def namespace
         my $thisns = (s/(^.*?:)// ? $1 : $ns);
 
-        my $is_attrib = m/^sql|comments|action$/ ? 0 : 1;
+        my $is_attrib = m/^(sql|comments|action|extra)$/ ? 0 : 1;
 
         my $attrib_path = "\@$thisns$_";
         my $tag_path    = "$thisns$_";
@@ -270,7 +269,17 @@ sub get_tagfields {
             debug "Got $_=".( defined $data{ $_ } ? $data{ $_ } : 'UNDEF' );
         }
         elsif ( $xp->exists($tag_path,$node) ) {
-            $data{$_} = "".$xp->findvalue($tag_path,$node);
+            if ($_ eq "extra") {
+                my %extra;
+                my $extra_nodes = $xp->find($tag_path,$node);
+                foreach ( $extra_nodes->pop->getAttributes ) {
+                    $extra{$_->getName} = $_->getData;
+                }
+                $data{$_} = \%extra;
+            }
+            else {
+                $data{$_} = "".$xp->findvalue($tag_path,$node);
+            }
             warn "Use of '$_' as a child tag is depricated."
                 ." Use an attribute instead."
                 ." To convert your file to the new version see the Docs.\n"
@@ -300,7 +309,7 @@ output by the SQLFairy XML producer).
 
 =item *
 
-Support options and extra attributes.
+Support options attribute.
 
 =item *
 
index bc2b7a5..ba3d6a0 100644 (file)
@@ -66,6 +66,7 @@ schema_ok( $scma, {
                     size => 10,
                     is_primary_key => 1,
                     is_auto_increment => 1,
+                    extra => { ZEROFILL => 1 },
                 },
                 {
                     name => "title",
@@ -87,6 +88,11 @@ schema_ok( $scma, {
                     is_unique => 1,
                     default_value => undef,
                     is_nullable => 1,
+                    extra => {
+                        foo => "bar",
+                        hello => "world",
+                        bar => "baz",
+                    }
                 },
                 {
                     name => "explicitnulldef",
@@ -105,6 +111,7 @@ schema_ok( $scma, {
                     data_type => "varchar",
                     default_value => "",
                     is_nullable => 1,
+                    comments => "Hello emptytagdef",
                 },
             ],
             constraints => [
index d702eca..aec20ff 100644 (file)
@@ -4,51 +4,61 @@ Created by SQL::Translator::Producer::SqlfXML
 Created on Fri Aug 15 15:08:18 2003
 
  -->
-<sqlt:schema xmlns:sqlt="http://sqlfairy.sourceforge.net/sqlfairy.xml">
+<schema xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
 
-  <sqlt:table order="1" name="Basic">
-    <sqlt:fields>
-      <sqlt:field
-          name="id" 
+  <table order="1" name="Basic">
+    <fields>
+      <field
+          name="id"
           is_primary_key="1" is_foreign_key="0"
           size="10" data_type="int" is_auto_increment="1" order="1"
-          is_nullable="0" />
-      <sqlt:field 
-          name="title" 
+          is_nullable="0">
+          <extra ZEROFILL="1" />
+      </field>
+      <field
+          name="title"
           is_primary_key="0" is_foreign_key="0"
           size="100" is_auto_increment="0" data_type="varchar"
           order="2" default_value="hello" is_nullable="0" />
-      <sqlt:field 
+      <field
           name="description"
           size="0" data_type="text" order="3" default_value="" />
-      <sqlt:field name="email" size="255" data_type="varchar" order="4" />
-      <sqlt:field name="explicitnulldef" size="0" data_type="varchar" order="5" />
-      <sqlt:field name="explicitemptystring" size="0"
+      <field name="email" size="255" data_type="varchar" order="4">
+          <extra foo="bar" hello="world" bar="baz" />
+      </field>
+      <field name="explicitnulldef" size="0" data_type="varchar" order="5" />
+      <field name="explicitemptystring" size="0"
           data_type="varchar" order="6" default_value="" />
-      <sqlt:field name="emptytagdef" size="0"
+      <field name="emptytagdef" size="0"
           data_type="varchar" order="7" default_value="" >
-          <comments>Hello World</comments>
-      </sqlt:field>
-    </sqlt:fields>
-    <sqlt:indices>
-      <sqlt:index name="titleindex" fields="title" type="NORMAL" />
-    </sqlt:indices>
-    <sqlt:constraints>
-      <sqlt:constraint name="" type="PRIMARY KEY" fields="id"
+          <comments>Hello emptytagdef</comments>
+      </field>
+    </fields>
+
+    <indices>
+      <index name="titleindex" fields="title" type="NORMAL" />
+    </indices>
+
+    <constraints>
+      <constraint name="" type="PRIMARY KEY" fields="id"
           reference_table="" options="" deferrable="1" match_type=""
           expression="" on_update="" on_delete="" />
-      <sqlt:constraint name="emailuniqueindex" type="UNIQUE" fields="email" />
-    </sqlt:constraints>
-  </sqlt:table>
-  <sqlt:view name="email_list" fields="email" order="1">
-      <sqlt:sql>SELECT email FROM Basic WHERE email IS NOT NULL</sqlt:sql>
-  </sqlt:view>
-  <sqlt:trigger name="foo_trigger" database_event="insert" on_table="foo"
+      <constraint name="emailuniqueindex" type="UNIQUE" fields="email" />
+    </constraints>
+  </table>
+
+  <view name="email_list" fields="email" order="1">
+      <sql>SELECT email FROM Basic WHERE email IS NOT NULL</sql>
+  </view>
+
+  <trigger name="foo_trigger" database_event="insert" on_table="foo"
       perform_action_when="after" order="1">
-      <sqlt:action>update modified=timestamp();</sqlt:action>
-  </sqlt:trigger>
-  <sqlt:procedure name="foo_proc" order="1" owner="Nomar" parameters="foo,bar">
-      <sqlt:sql>select foo from bar</sqlt:sql>
-      <sqlt:comments>Go Sox!</sqlt:comments>
-  </sqlt:procedure>
-</sqlt:schema>
+      <action>update modified=timestamp();</action>
+  </trigger>
+
+  <procedure name="foo_proc" order="1" owner="Nomar" parameters="foo,bar">
+      <sql>select foo from bar</sql>
+      <comments>Go Sox!</comments>
+  </procedure>
+
+</schema>