From: Ben Faga Date: Wed, 8 Jun 2005 15:31:06 +0000 (+0000) Subject: Fixed small error that was causing a test to fail. X-Git-Tag: v0.11008~549 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=da8ab5248666d12675b8c680e4b3277cb80bd0f3;p=dbsrgits%2FSQL-Translator.git Fixed small error that was causing a test to fail. --- diff --git a/lib/SQL/Translator/Schema.pm b/lib/SQL/Translator/Schema.pm index a8a775f..4d7869e 100644 --- a/lib/SQL/Translator/Schema.pm +++ b/lib/SQL/Translator/Schema.pm @@ -3,7 +3,7 @@ package SQL::Translator::Schema; # vim: sw=4: ts=4: # ---------------------------------------------------------------------- -# $Id: Schema.pm,v 1.22 2005-06-07 16:55:41 kycl4rk Exp $ +# $Id: Schema.pm,v 1.23 2005-06-08 15:31:06 mwz444 Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -60,9 +60,9 @@ use SQL::Translator::Utils 'parse_list_arg'; use base 'SQL::Translator::Schema::Object'; use vars qw[ $VERSION $TABLE_ORDER $VIEW_ORDER $TRIGGER_ORDER $PROC_ORDER ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.22 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.23 $ =~ /(\d+)\.(\d+)/; -__PACKAGE__->_attributes( qw/name database translator/ ); +__PACKAGE__->_attributes(qw/name database translator/); # ---------------------------------------------------------------------- sub as_graph { @@ -75,10 +75,9 @@ Returns the schema as an L object. =cut - my $self = @_; + my $self = shift; return SQL::Translator::Schema::Graph->new( - translator => $self->translator - ); + translator => $self->translator ); } # ---------------------------------------------------------------------- @@ -105,24 +104,25 @@ not be created. if ( UNIVERSAL::isa( $_[0], $table_class ) ) { $table = shift; - $table->schema( $self ); + $table->schema($self); } else { my %args = @_; $args{'schema'} = $self; - $table = $table_class->new( \%args ) or return - $self->error( $table_class->error ); + $table = $table_class->new( \%args ) + or return $self->error( $table_class->error ); } $table->order( ++$TABLE_ORDER ); + # We know we have a name as the Table->new above errors if none given. my $table_name = $table->name; - if ( defined $self->{'tables'}{ $table_name } ) { + if ( defined $self->{'tables'}{$table_name} ) { return $self->error(qq[Can't create table: "$table_name" exists]); } else { - $self->{'tables'}{ $table_name } = $table; + $self->{'tables'}{$table_name} = $table; } return $table; @@ -145,8 +145,8 @@ can be set to 1 to also drop all triggers on the table, default is 0. =cut - my $self = shift; - my $table_class = 'SQL::Translator::Schema::Table'; + my $self = shift; + my $table_class = 'SQL::Translator::Schema::Table'; my $table_name; if ( UNIVERSAL::isa( $_[0], $table_class ) ) { @@ -155,20 +155,20 @@ can be set to 1 to also drop all triggers on the table, default is 0. else { $table_name = shift; } - my %args = @_; + my %args = @_; my $cascade = $args{'cascade'}; - if ( ! exists $self->{'tables'}{ $table_name } ) { + if ( !exists $self->{'tables'}{$table_name} ) { return $self->error(qq[Can't drop table: $table_name" doesn't exist]); } - my $table = delete $self->{'tables'}{ $table_name }; + my $table = delete $self->{'tables'}{$table_name}; + + if ($cascade) { - if ( $cascade ) { # Drop all triggers on this table - $self->drop_trigger() for (grep { $_->on_table eq $table_name } - @{ $self->{'triggers'}} - ); + $self->drop_trigger() + for ( grep { $_->on_table eq $table_name } @{ $self->{'triggers'} } ); } return $table; } @@ -197,31 +197,31 @@ procedure will not be created. if ( UNIVERSAL::isa( $_[0], $procedure_class ) ) { $procedure = shift; - $procedure->schema( $self ); + $procedure->schema($self); } else { my %args = @_; $args{'schema'} = $self; return $self->error('No procedure name') unless $args{'name'}; - $procedure = $procedure_class->new( \%args ) or - return $self->error( $procedure_class->error ); + $procedure = $procedure_class->new( \%args ) + or return $self->error( $procedure_class->error ); } $procedure->order( ++$PROC_ORDER ); - my $procedure_name = $procedure->name or return - $self->error('No procedure name'); + my $procedure_name = $procedure->name + or return $self->error('No procedure name'); - if ( defined $self->{'procedures'}{ $procedure_name } ) { + if ( defined $self->{'procedures'}{$procedure_name} ) { return $self->error( - qq[Can't create procedure: "$procedure_name" exists] - ); + qq[Can't create procedure: "$procedure_name" exists] ); } else { - $self->{'procedures'}{ $procedure_name } = $procedure; + $self->{'procedures'}{$procedure_name} = $procedure; } return $procedure; } + # ---------------------------------------------------------------------- sub drop_procedure { @@ -238,8 +238,8 @@ object. =cut - my $self = shift; - my $proc_class = 'SQL::Translator::Schema::Procedure'; + my $self = shift; + my $proc_class = 'SQL::Translator::Schema::Procedure'; my $proc_name; if ( UNIVERSAL::isa( $_[0], $proc_class ) ) { @@ -249,11 +249,12 @@ object. $proc_name = shift; } - if ( ! exists $self->{'procedures'}{ $proc_name } ) { - return $self->error(qq[Can't drop procedure: $proc_name" doesn't exist]); + if ( !exists $self->{'procedures'}{$proc_name} ) { + return $self->error( + qq[Can't drop procedure: $proc_name" doesn't exist]); } - my $proc = delete $self->{'procedures'}{ $proc_name }; + my $proc = delete $self->{'procedures'}{$proc_name}; return $proc; } @@ -282,28 +283,29 @@ not be created. if ( UNIVERSAL::isa( $_[0], $trigger_class ) ) { $trigger = shift; - $trigger->schema( $self ); + $trigger->schema($self); } else { my %args = @_; $args{'schema'} = $self; return $self->error('No trigger name') unless $args{'name'}; - $trigger = $trigger_class->new( \%args ) or - return $self->error( $trigger_class->error ); + $trigger = $trigger_class->new( \%args ) + or return $self->error( $trigger_class->error ); } $trigger->order( ++$TRIGGER_ORDER ); my $trigger_name = $trigger->name or return $self->error('No trigger name'); - if ( defined $self->{'triggers'}{ $trigger_name } ) { + if ( defined $self->{'triggers'}{$trigger_name} ) { return $self->error(qq[Can't create trigger: "$trigger_name" exists]); } else { - $self->{'triggers'}{ $trigger_name } = $trigger; + $self->{'triggers'}{$trigger_name} = $trigger; } return $trigger; } + # ---------------------------------------------------------------------- sub drop_trigger { @@ -319,8 +321,8 @@ trigger name or an C object. =cut - my $self = shift; - my $trigger_class = 'SQL::Translator::Schema::Trigger'; + my $self = shift; + my $trigger_class = 'SQL::Translator::Schema::Trigger'; my $trigger_name; if ( UNIVERSAL::isa( $_[0], $trigger_class ) ) { @@ -330,11 +332,12 @@ trigger name or an C object. $trigger_name = shift; } - if ( ! exists $self->{'triggers'}{ $trigger_name } ) { - return $self->error(qq[Can't drop trigger: $trigger_name" doesn't exist]); + if ( !exists $self->{'triggers'}{$trigger_name} ) { + return $self->error( + qq[Can't drop trigger: $trigger_name" doesn't exist]); } - my $trigger = delete $self->{'triggers'}{ $trigger_name }; + my $trigger = delete $self->{'triggers'}{$trigger_name}; return $trigger; } @@ -357,13 +360,13 @@ not be created. =cut - my $self = shift; + my $self = shift; my $view_class = 'SQL::Translator::Schema::View'; my $view; if ( UNIVERSAL::isa( $_[0], $view_class ) ) { $view = shift; - $view->schema( $self ); + $view->schema($self); } else { my %args = @_; @@ -375,11 +378,11 @@ not be created. $view->order( ++$VIEW_ORDER ); my $view_name = $view->name or return $self->error('No view name'); - if ( defined $self->{'views'}{ $view_name } ) { + if ( defined $self->{'views'}{$view_name} ) { return $self->error(qq[Can't create view: "$view_name" exists]); } else { - $self->{'views'}{ $view_name } = $view; + $self->{'views'}{$view_name} = $view; } return $view; @@ -400,8 +403,8 @@ name or an C object. =cut - my $self = shift; - my $view_class = 'SQL::Translator::Schema::View'; + my $self = shift; + my $view_class = 'SQL::Translator::Schema::View'; my $view_name; if ( UNIVERSAL::isa( $_[0], $view_class ) ) { @@ -411,11 +414,11 @@ name or an C object. $view_name = shift; } - if ( ! exists $self->{'views'}{ $view_name } ) { + if ( !exists $self->{'views'}{$view_name} ) { return $self->error(qq[Can't drop view: $view_name" doesn't exist]); } - my $view = delete $self->{'views'}{ $view_name }; + my $view = delete $self->{'views'}{$view_name}; return $view; } @@ -475,11 +478,11 @@ Returns a procedure by the name provided. =cut - my $self = shift; + my $self = shift; my $procedure_name = shift or return $self->error('No procedure name'); - return $self->error( qq[Table "$procedure_name" does not exist] ) unless - exists $self->{'procedures'}{ $procedure_name }; - return $self->{'procedures'}{ $procedure_name }; + return $self->error(qq[Table "$procedure_name" does not exist]) + unless exists $self->{'procedures'}{$procedure_name}; + return $self->{'procedures'}{$procedure_name}; } # ---------------------------------------------------------------------- @@ -495,14 +498,13 @@ Returns all the procedures as an array or array reference. =cut - my $self = shift; - my @procedures = - map { $_->[1] } - sort { $a->[0] <=> $b->[0] } - map { [ $_->order, $_ ] } - values %{ $self->{'procedures'} }; + my $self = shift; + my @procedures = + map { $_->[1] } + sort { $a->[0] <=> $b->[0] } + map { [ $_->order, $_ ] } values %{ $self->{'procedures'} }; - if ( @procedures ) { + if (@procedures) { return wantarray ? @procedures : \@procedures; } else { @@ -524,11 +526,11 @@ Returns a table by the name provided. =cut - my $self = shift; + my $self = shift; my $table_name = shift or return $self->error('No table name'); - return $self->error( qq[Table "$table_name" does not exist] ) unless - exists $self->{'tables'}{ $table_name }; - return $self->{'tables'}{ $table_name }; + return $self->error(qq[Table "$table_name" does not exist]) + unless exists $self->{'tables'}{$table_name}; + return $self->{'tables'}{$table_name}; } # ---------------------------------------------------------------------- @@ -545,13 +547,12 @@ Returns all the tables as an array or array reference. =cut my $self = shift; - my @tables = - map { $_->[1] } - sort { $a->[0] <=> $b->[0] } - map { [ $_->order, $_ ] } - values %{ $self->{'tables'} }; + my @tables = + map { $_->[1] } + sort { $a->[0] <=> $b->[0] } + map { [ $_->order, $_ ] } values %{ $self->{'tables'} }; - if ( @tables ) { + if (@tables) { return wantarray ? @tables : \@tables; } else { @@ -573,11 +574,11 @@ Returns a trigger by the name provided. =cut - my $self = shift; + my $self = shift; my $trigger_name = shift or return $self->error('No trigger name'); - return $self->error( qq[Table "$trigger_name" does not exist] ) unless - exists $self->{'triggers'}{ $trigger_name }; - return $self->{'triggers'}{ $trigger_name }; + return $self->error(qq[Table "$trigger_name" does not exist]) + unless exists $self->{'triggers'}{$trigger_name}; + return $self->{'triggers'}{$trigger_name}; } # ---------------------------------------------------------------------- @@ -593,14 +594,13 @@ Returns all the triggers as an array or array reference. =cut - my $self = shift; - my @triggers = - map { $_->[1] } - sort { $a->[0] <=> $b->[0] } - map { [ $_->order, $_ ] } - values %{ $self->{'triggers'} }; + my $self = shift; + my @triggers = + map { $_->[1] } + sort { $a->[0] <=> $b->[0] } + map { [ $_->order, $_ ] } values %{ $self->{'triggers'} }; - if ( @triggers ) { + if (@triggers) { return wantarray ? @triggers : \@triggers; } else { @@ -622,11 +622,11 @@ Returns a view by the name provided. =cut - my $self = shift; + my $self = shift; my $view_name = shift or return $self->error('No view name'); - return $self->error('View "$view_name" does not exist') unless - exists $self->{'views'}{ $view_name }; - return $self->{'views'}{ $view_name }; + return $self->error('View "$view_name" does not exist') + unless exists $self->{'views'}{$view_name}; + return $self->{'views'}{$view_name}; } # ---------------------------------------------------------------------- @@ -643,13 +643,12 @@ Returns all the views as an array or array reference. =cut my $self = shift; - my @views = - map { $_->[1] } - sort { $a->[0] <=> $b->[0] } - map { [ $_->order, $_ ] } - values %{ $self->{'views'} }; + my @views = + map { $_->[1] } + sort { $a->[0] <=> $b->[0] } + map { [ $_->order, $_ ] } values %{ $self->{'views'} }; - if ( @views ) { + if (@views) { return wantarray ? @views : \@views; } else { @@ -691,31 +690,30 @@ A list of fields to skip in the joins my $self = shift; my %args = @_; my $join_pk_only = $args{'join_pk_only'} || 0; - my %skip_fields = map { s/^\s+|\s+$//g; $_, 1 } @{ - parse_list_arg( $args{'skip_fields'} ) - }; + my %skip_fields = + map { s/^\s+|\s+$//g; $_, 1 } @{ parse_list_arg( $args{'skip_fields'} ) }; my ( %common_keys, %pk ); for my $table ( $self->get_tables ) { for my $field ( $table->get_fields ) { my $field_name = $field->name or next; - next if $skip_fields{ $field_name }; - $pk{ $field_name } = 1 if $field->is_primary_key; - push @{ $common_keys{ $field_name } }, $table->name; + next if $skip_fields{$field_name}; + $pk{$field_name} = 1 if $field->is_primary_key; + push @{ $common_keys{$field_name} }, $table->name; } - } - + } + for my $field ( keys %common_keys ) { - next if $join_pk_only and !defined $pk{ $field }; + next if $join_pk_only and !defined $pk{$field}; - my @table_names = @{ $common_keys{ $field } }; + my @table_names = @{ $common_keys{$field} }; next unless scalar @table_names > 1; for my $i ( 0 .. $#table_names ) { - my $table1 = $self->get_table( $table_names[ $i ] ) or next; + my $table1 = $self->get_table( $table_names[$i] ) or next; for my $j ( 1 .. $#table_names ) { - my $table2 = $self->get_table( $table_names[ $j ] ) or next; + my $table2 = $self->get_table( $table_names[$j] ) or next; next if $table1->name eq $table2->name; $table1->add_constraint( @@ -769,7 +767,7 @@ Get the SQL::Translator instance that instantiated the parser. sub DESTROY { my $self = shift; undef $_ for values %{ $self->{'tables'} }; - undef $_ for values %{ $self->{'views'} }; + undef $_ for values %{ $self->{'views'} }; } 1; @@ -783,3 +781,4 @@ sub DESTROY { Ken Youens-Clark Ekclark@cpan.orgE. =cut +