X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FParser%2FSqlfXML.pm;h=cb4fc48267cae63112f2faef4307fd936e5a0f0f;hb=b35303534de4c377a4c9dea7a5efb8dfcf759537;hp=6919b3ae87883613dc0db2c4212e442d5109aa08;hpb=c957e92dd6b604eed3167580716f418b9dec42f8;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Parser/SqlfXML.pm b/lib/SQL/Translator/Parser/SqlfXML.pm index 6919b3a..cb4fc48 100644 --- a/lib/SQL/Translator/Parser/SqlfXML.pm +++ b/lib/SQL/Translator/Parser/SqlfXML.pm @@ -1,7 +1,7 @@ package SQL::Translator::Parser::SqlfXML; # ------------------------------------------------------------------- -# $Id: SqlfXML.pm,v 1.1 2003-08-06 17:14:08 grommit Exp $ +# $Id: SqlfXML.pm,v 1.3 2003-08-07 14:49:24 grommit Exp $ # ------------------------------------------------------------------- # Copyright (C) 2003 Mark Addison , # @@ -49,13 +49,25 @@ To see and example of the XML translate one of your schema :) e.g. $ sql_translator.pl --from MySQL --to SqftXML foo_schema.sql +==head1 default_value + +Leave the tag out all together to use the default in Schema::Field. Use empty +tags or EMPTY_STRING for a zero lenth string. NULL for an explicit null +(currently sets default_value to undef Schema::Field). + + + EMPTY_STRING + NULL + + + =cut use strict; use warnings; use vars qw[ $DEBUG $VERSION @EXPORT_OK ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/; $DEBUG = 0 unless defined $DEBUG; use Data::Dumper; @@ -103,6 +115,14 @@ sub parse { my %fdata = get_tagfields($xp, $_, "sqlf:", qw/name data_type size default_value is_nullable is_auto_increment is_primary_key is_foreign_key comments/); + if (exists $fdata{default_value} and defined $fdata{default_value}){ + if ( $fdata{default_value} =~ /^\s*NULL\s*$/ ) { + $fdata{default_value}= undef; + } + elsif ( $fdata{default_value} =~ /^\s*EMPTY_STRING\s*$/ ) { + $fdata{default_value} = ""; + } + } my $field = $table->add_field(%fdata) or die $schema->error; $table->primary_key($field->name) if $fdata{'is_primary_key'}; # TODO We should be able to make the table obj spot this when we @@ -136,12 +156,18 @@ sub parse { # get_tagfields XPNODE, NAMESPACE => qw/TAGNAMES/; # get_tagfields $node, "sqlf:" => qw/name type fields reference/; +# +# Returns hash of data. If a tag isn't in the file it is not in this +# hash. +# TODO Add handling of and explicit NULL value. sub get_tagfields { my ($xp, $node, @names) = @_; my (%data, $ns); foreach (@names) { if ( m/:$/ ) { $ns = $_; next; } # Set def namespace - $data{$_} = "".$xp->findvalue( (s/(^.*?:)// ? $1 : $ns).$_, $node ); + my $path = (s/(^.*?:)// ? $1 : $ns).$_; + $data{$_} = $xp->findvalue($path,$node) if $xp->exists($path,$node); + debug "Got $_=".(defined $data{$_} ? $data{$_} : "UNDEF"); } return wantarray ? %data : \%data; } @@ -152,11 +178,20 @@ __END__ =pod +=head1 BUGS + +B e.g. Will be parsed as "" and hence also +false. This is a bit counter intuative for some tags as seeing + you might think that it was set when it fact it wouldn't +be. So for now it is safest not to use them until their handling by the parser +is sorted out. + =head1 TODO * Support sqf:options. * Test forign keys are parsed ok. - * Control over defaulting and parsing of empty vs non-existant tags. + * Sort out sane handling of vs vs it not being there. + * Control over defaulting of non-existant tags. =head1 AUTHOR