From: Mark Addison Date: Mon, 20 Oct 2003 13:15:23 +0000 (+0000) Subject: Added Views, Procedures and Triggers to bring it inline with the current Schema features. X-Git-Tag: v0.04~38 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1e3867bf80283bfd5dd243e8c6b678bc179010a6;p=dbsrgits%2FSQL-Translator.git Added Views, Procedures and Triggers to bring it inline with the current Schema features. --- diff --git a/lib/SQL/Translator/Producer/XML/SQLFairy.pm b/lib/SQL/Translator/Producer/XML/SQLFairy.pm index d13a665..2f98873 100644 --- a/lib/SQL/Translator/Producer/XML/SQLFairy.pm +++ b/lib/SQL/Translator/Producer/XML/SQLFairy.pm @@ -1,7 +1,7 @@ package SQL::Translator::Producer::XML::SQLFairy; # ------------------------------------------------------------------- -# $Id: SQLFairy.pm,v 1.6 2003-10-20 11:50:38 dlc Exp $ +# $Id: SQLFairy.pm,v 1.7 2003-10-20 13:15:23 grommit Exp $ # ------------------------------------------------------------------- # Copyright (C) 2003 Ken Y. Clark , # darren chamberlain , @@ -78,7 +78,7 @@ Creates XML output of a schema. use strict; use vars qw[ $VERSION @EXPORT_OK ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/; use Exporter; use base qw(Exporter); @@ -169,7 +169,32 @@ sub produce { $xml->endTag( [ $Namespace => 'table' ] ); } + + # + # Views + # + for my $foo ( $schema->get_views ) { + xml_obj($xml, $foo, tag => "view", + methods => [qw/name sql fields order/], end_tag => 1 ); + } + + # + # Tiggers + # + for my $foo ( $schema->get_triggers ) { + xml_obj($xml, $foo, tag => "trigger", + methods => [qw/name perform_action_when database_event fields on_table + action order/], end_tag => 1 ); + } + # + # Procedures + # + for my $foo ( $schema->get_procedures ) { + xml_obj($xml, $foo, tag => "procedure", + methods => [qw/name sql parameters owner comments order/], end_tag=>1 ); + } + $xml->endTag([ $Namespace => 'schema' ]); $xml->end; diff --git a/t/17sqlfxml-producer.t b/t/17sqlfxml-producer.t index c4bac65..27b824d 100644 --- a/t/17sqlfxml-producer.t +++ b/t/17sqlfxml-producer.t @@ -33,16 +33,17 @@ if ($@ && $@ =~ m!locate Test/Differences.pm in!) { plan skip_all => "You need Test::Differences for this test."; } use Test::Differences; -plan tests => 9; +plan tests => 18; use SQL::Translator; use SQL::Translator::Producer::XML::SQLFairy; -my ($obj,$ans,$xml); # # emit_empty_tags => 0 # +{ +my ($obj,$ans,$xml); $ans = < @@ -152,9 +153,13 @@ print "XML:\n$xml" if DEBUG; $xml =~ s/^([^\n]*\n){7}//m; eq_or_diff $xml, $ans ,"XML looks right"; +} # end emit_empty_tags=>0 + # # emit_empty_tags => 1 # +{ +my ($obj,$ans,$xml); $ans = < @@ -253,7 +258,6 @@ $ans = < EOXML -undef $obj; $obj = SQL::Translator->new( debug => DEBUG, trace => TRACE, @@ -270,27 +274,29 @@ print "XML emit_empty_tags=>1:\n$xml" if DEBUG; $xml =~ s/^([^\n]*\n){7}//m; eq_or_diff $xml, $ans ,"XML looks right"; - +} # end emit_empty_tags => 1 # # attrib_values => 1 # +{ +my ($obj,$ans,$xml); $ans = < - - - - + + + + - - + + @@ -312,3 +318,161 @@ print "XML attrib_values=>1:\n$xml" if DEBUG; $xml =~ s/^([^\n]*\n){7}//m; eq_or_diff $xml, $ans ,"XML looks right"; +} # end attrib_values => 1 + +# +# View +# +# Thanks to Ken for the schema setup lifted from 13schema.t +{ +my ($obj,$ans,$xml); + +$ans = < + + + + name,age + foo_view + 1 + select name, age from person + + +EOXML + + $obj = SQL::Translator->new( + debug => DEBUG, + trace => TRACE, + show_warnings => 1, + add_drop_table => 1, + from => "MySQL", + to => "XML-SQLFairy", + ); + my $s = $obj->schema; + my $name = 'foo_view'; + my $sql = 'select name, age from person'; + my $fields = 'name, age'; + my $v = $s->add_view( + name => $name, + sql => $sql, + fields => $fields, + schema => $s, + ) or die $s->error; + + # As we have created a Schema we give translate a dummy string so that + # it will run the produce. + lives_ok {$xml =$obj->translate("FOO");} "Translate (View) ran"; + ok("$xml" ne "" ,"Produced something!"); + print "XML attrib_values=>1:\n$xml" if DEBUG; + # Strip sqlf header with its variable date so we diff safely + $xml =~ s/^([^\n]*\n){7}//m; + eq_or_diff $xml, $ans ,"XML looks right"; +} # end View + +# +# Trigger +# +# Thanks to Ken for the schema setup lifted from 13schema.t +{ +my ($obj,$ans,$xml); + +$ans = < + + + + update modified=timestamp(); + insert + foo_trigger + foo + 1 + after + + +EOXML + + $obj = SQL::Translator->new( + debug => DEBUG, + trace => TRACE, + show_warnings => 1, + add_drop_table => 1, + from => "MySQL", + to => "XML-SQLFairy", + ); + my $s = $obj->schema; + my $name = 'foo_trigger'; + my $perform_action_when = 'after'; + my $database_event = 'insert'; + my $on_table = 'foo'; + my $action = 'update modified=timestamp();'; + my $t = $s->add_trigger( + name => $name, + perform_action_when => $perform_action_when, + database_event => $database_event, + on_table => $on_table, + action => $action, + ) or die $s->error; + + # As we have created a Schema we give translate a dummy string so that + # it will run the produce. + lives_ok {$xml =$obj->translate("FOO");} "Translate (Trigger) ran"; + ok("$xml" ne "" ,"Produced something!"); + print "XML attrib_values=>1:\n$xml" if DEBUG; + # Strip sqlf header with its variable date so we diff safely + $xml =~ s/^([^\n]*\n){7}//m; + eq_or_diff $xml, $ans ,"XML looks right"; +} # end Trigger + +# +# Procedure +# +# Thanks to Ken for the schema setup lifted from 13schema.t +{ +my ($obj,$ans,$xml); + +$ans = < + + + + Go Sox! + foo_proc + 1 + Nomar + foo,bar + select foo from bar + + +EOXML + + $obj = SQL::Translator->new( + debug => DEBUG, + trace => TRACE, + show_warnings => 1, + add_drop_table => 1, + from => "MySQL", + to => "XML-SQLFairy", + ); + my $s = $obj->schema; + my $name = 'foo_proc'; + my $sql = 'select foo from bar'; + my $parameters = 'foo, bar'; + my $owner = 'Nomar'; + my $comments = 'Go Sox!'; + my $p = $s->add_procedure( + name => $name, + sql => $sql, + parameters => $parameters, + owner => $owner, + comments => $comments, + ) or die $s->error; + + # As we have created a Schema we give translate a dummy string so that + # it will run the produce. + lives_ok {$xml =$obj->translate("FOO");} "Translate (Procedure) ran"; + ok("$xml" ne "" ,"Produced something!"); + print "XML attrib_values=>1:\n$xml" if DEBUG; + # Strip sqlf header with its variable date so we diff safely + $xml =~ s/^([^\n]*\n){7}//m; + eq_or_diff $xml, $ans ,"XML looks right"; +} # end Procedure