From: Ken Youens-Clark Date: Tue, 28 Feb 2012 22:11:29 +0000 (-0500) Subject: Added "tables" and "options" methods to Schema::View X-Git-Tag: v0.11011~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e6c5fb6e892a94c4244dd8ee93848d6fb1b00308;p=dbsrgits%2FSQL-Translator.git Added "tables" and "options" methods to Schema::View Updated MySQL parser tests --- diff --git a/lib/SQL/Translator/Parser/MySQL.pm b/lib/SQL/Translator/Parser/MySQL.pm index e830df8..40ef243 100644 --- a/lib/SQL/Translator/Parser/MySQL.pm +++ b/lib/SQL/Translator/Parser/MySQL.pm @@ -1059,15 +1059,16 @@ sub parse { my $view = $result->{'views'}{ $view_name }; my @flds = map { $_->{'alias'} || $_->{'name'} } @{ $view->{'select'}{'columns'} || [] }; + my @from = map { $_->{'alias'} || $_->{'name'} } + @{ $view->{'from'}{'tables'} || [] }; $schema->add_view( name => $view_name, sql => $view->{'sql'}, order => $view->{'order'}, fields => \@flds, -# from => $view->{'from'}{'tables'}, -# where => $view->{'from'}{'where'}, -# options => $view->{'options'} + tables => \@from, + options => $view->{'options'} ); } diff --git a/lib/SQL/Translator/Schema/View.pm b/lib/SQL/Translator/Schema/View.pm index 462a9e8..faae06c 100644 --- a/lib/SQL/Translator/Schema/View.pm +++ b/lib/SQL/Translator/Schema/View.pm @@ -34,7 +34,7 @@ our ( $TABLE_COUNT, $VIEW_COUNT ); our $VERSION = '1.59'; __PACKAGE__->_attributes( qw/ - name sql fields schema order + name sql fields schema order tables options /); =pod @@ -85,6 +85,76 @@ names and keep them in order by the first occurrence of a field name. return wantarray ? @flds : \@flds; } +sub tables { + +=pod + +=head2 tables + +Gets and set the tables the SELECT mentions. Accepts a string, list or +arrayref; returns an array or array reference. Will unique the table +names and keep them in order by the first occurrence of a field name. + + $view->tables('foo'); + $view->tables('foo', 'bar'); + $view->tables( 'foo, bar' ); + $view->tables( [ 'foo', 'bar' ] ); + $view->tables( qw[ foo bar ] ); + + my @tables = $view->tables; + +=cut + + my $self = shift; + my $tables = parse_list_arg( @_ ); + + if ( @$tables ) { + my ( %unique, @unique ); + for my $t ( @$tables ) { + next if $unique{ $t }++; + push @unique, $t; + } + + $self->{'tables'} = \@unique; + } + + my @tbls = @{ $self->{'tables'} || [] }; + + return wantarray ? @tbls : \@tbls; +} + +sub options { + +=pod + +=head2 options + +Gets and sets a list of options on the view. + + $view->options('ALGORITHM=UNDEFINED'); + + my @options = $view->options; + +=cut + + my $self = shift; + my $options = parse_list_arg( @_ ); + + if ( @$options ) { + my ( %unique, @unique ); + for my $o ( @$options, @{ $self->{'options'} || [] } ) { + next if $unique{ $o }++; + push @unique, $o; + } + + $self->{'options'} = \@unique; + } + + my @opts = @{ $self->{'options'} || [] }; + + return wantarray ? @opts : \@opts; +} + sub is_valid { =pod diff --git a/t/02mysql-parser.t b/t/02mysql-parser.t index 46f3277..dbcde4e 100644 --- a/t/02mysql-parser.t +++ b/t/02mysql-parser.t @@ -508,7 +508,7 @@ BEGIN { # charset table option # { - my $tr = SQL::Translator->new(parser_args => {mysql_parser_version => 50003}); + my $tr = SQL::Translator->new(parser_args => {mysql_parser_version => 50013}); my $data = parse($tr, q[ DELIMITER ;; @@ -527,6 +527,7 @@ BEGIN { /*!50001 CREATE ALGORITHM=UNDEFINED */ /*!50013 DEFINER=`cmdomain`@`localhost` SQL SECURITY DEFINER */ + /*!50014 DEFINER=`BOGUS` */ /*! VIEW `vs_asset` AS select `a`.`asset_id` AS `asset_id`,`a`.`fq_name` AS `fq_name`, `cfgmgmt_mig`.`ap_extract_folder`(`a`.`fq_name`) AS `folder_name`, @@ -662,7 +663,6 @@ BEGIN { is( $view1->name, 'vs_asset', 'Found "vs_asset" view' ); is( $view2->name, 'vs_asset2', 'Found "vs_asset2" view' ); is( $view3->name, 'vs_asset3', 'Found "vs_asset3" view' ); - like($view1->sql, qr/ALGORITHM=UNDEFINED/, "Detected algorithm"); like($view1->sql, qr/vs_asset/, "Detected view vs_asset"); # KYC - commenting this out as I don't understand why this string @@ -677,6 +677,18 @@ BEGIN { 'First view has correct fields' ); + my @options = $view1->options; + + is_deeply( + \@options, + [ + 'ALGORITHM=UNDEFINED', + 'DEFINER=`cmdomain`@`localhost`', + 'SQL SECURITY DEFINER', + ], + 'Only version 50013 options parsed', + ); + my @procs = $schema->get_procedures; is( scalar @procs, 2, 'Right number of procedures (2)' ); my $proc1 = shift @procs;