X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FSchema%2FTrigger.pm;h=e52ea6e2b3b2829df5df2a0a1b6937a9bc4f0730;hb=935800450f88b0500c4fa7c3b174cd22b5f9eb56;hp=1c2a68a5db4a2fde47dd1488b37d122df41a8496;hpb=d4f84dd192edc7a64a0b1a9923f1bafc0bc5ef9d;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Schema/Trigger.pm b/lib/SQL/Translator/Schema/Trigger.pm index 1c2a68a..e52ea6e 100644 --- a/lib/SQL/Translator/Schema/Trigger.pm +++ b/lib/SQL/Translator/Schema/Trigger.pm @@ -1,25 +1,5 @@ package SQL::Translator::Schema::Trigger; -# ---------------------------------------------------------------------- -# $Id: Trigger.pm 1444 2009-02-07 15:53:56Z ashberlin $ -# ---------------------------------------------------------------------- -# Copyright (C) 2002-2009 SQLFairy Authors -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License as -# published by the Free Software Foundation; version 2. -# -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA -# 02111-1307 USA -# ------------------------------------------------------------------- - =pod =head1 NAME @@ -30,14 +10,13 @@ SQL::Translator::Schema::Trigger - SQL::Translator trigger object use SQL::Translator::Schema::Trigger; my $trigger = SQL::Translator::Schema::Trigger->new( - name => 'foo', - perform_action_when => 'before', # or after - database_event => 'insert', # Obsolete please use database_events! - database_events => [qw/update instert/], # or update, update_on, delete - fields => [], # fields if event is "update" - on_table => 'foo', # table name - action => '...', # text of trigger - schema => $schema, # Schema object + name => 'foo', + perform_action_when => 'before', # or after + database_events => [qw/update insert/], # also update, update_on, delete + fields => [], # if event is "update" + on_table => 'foo', # table name + action => '...', # text of trigger + schema => $schema, # Schema object ); =head1 DESCRIPTION @@ -55,13 +34,15 @@ use base 'SQL::Translator::Schema::Object'; use Carp; -use vars qw($TABLE_COUNT $VIEW_COUNT); +use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT); + +$VERSION = '1.59'; # ---------------------------------------------------------------------- __PACKAGE__->_attributes( qw/ - name schema perform_action_when database_event database_events fields table on_table action - order + name schema perform_action_when database_events database_event + fields table on_table action order /); =pod @@ -112,23 +93,15 @@ sub database_event { =head2 database_event -Obosolete please use database_events! +Obsolete please use database_events! =cut - my $self = shift; - - - if ( my $arg = shift ) { - $self->database_events( [$arg] ); - } - - return $self->error("Please use database_events the trigger has more then one events!") - if (scalar @{$self->{'database_events'}} > 1); - - carp 'database_event is obsolete please use database_events'; - return (@{ $self->{'database_events'} })[0]; + my $self = shift; + + return $self->database_events( @_ ); } + # ---------------------------------------------------------------------- sub database_events { @@ -142,24 +115,28 @@ Gets or sets the events that triggers the trigger. =cut - my $self = shift; - - if ( my $arg = shift ) { - if (ref $arg eq "ARRAY"){ - map { - $_ = lc; - $_ =~ s/\s+/ /g; - return $self->error("Invalid event '$_' in database_events") unless ( $_ =~ /^(insert|update|update_on|delete)$/ ); - } @$arg ; - @{ $self->{'database_events'} } = @$arg; - - }else{ - return $self->error("Invalid argument to database_events"); - }; - - } - - return $self->{'database_events'}; + my $self = shift; + my @args = ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_; + + if ( @args ) { + @args = map { s/\s+/ /g; lc $_ } @args; + my %valid = map { $_, 1 } qw[ insert update update_on delete ]; + my @invalid = grep { !defined $valid{ $_ } } @args; + + if ( @invalid ) { + return $self->error( + sprintf("Invalid events '%s' in database_events", + join(', ', @invalid) + ) + ); + } + + $self->{'database_events'} = [ @args ]; + } + + return wantarray + ? @{ $self->{'database_events'} || [] } + : $self->{'database_events'}; } # ---------------------------------------------------------------------- @@ -249,9 +226,9 @@ sub action { =head2 action -Gets or set the actions of the trigger. +Gets or set the action of the trigger. - $trigger->actions( + $trigger->action( q[ BEGIN select ...; @@ -284,9 +261,9 @@ Determine whether the trigger is valid or not. my $self = shift; for my $attr ( - qw[ name perform_action_when database_event on_table action ] + qw[ name perform_action_when database_events on_table action ] ) { - return $self->error("No $attr") unless $self->$attr(); + return $self->error("Invalid: missing '$attr'") unless $self->$attr(); } return $self->error("Missing fields for UPDATE ON") if @@ -369,18 +346,23 @@ sub compare_arrays { Compare two arrays. =cut - - my ($first, $second) = @_; - no warnings; # silence spurious -w undef complaints - - return 0 unless (ref $first eq 'ARRAY' and ref $second eq 'ARRAY' ) ; - return 0 unless @$first == @$second; - my @first = sort @$first; - my @second = sort @$second; - for (my $i = 0; $i < scalar @first; $i++) { - return 0 if @first[$i] ne @second[$i]; - } - return 1; + + my ($first, $second) = @_; + no warnings; # silence spurious -w undef complaints + + return 0 unless (ref $first eq 'ARRAY' and ref $second eq 'ARRAY' ) ; + + return 0 unless @$first == @$second; + + my @first = sort @$first; + + my @second = sort @$second; + + for (my $i = 0; $i < scalar @first; $i++) { + return 0 if @first[$i] ne @second[$i]; + } + + return 1; } # ---------------------------------------------------------------------- @@ -392,22 +374,50 @@ sub equals { Determines if this trigger is the same as another - my $isIdentical = $trigger1->equals( $trigger2 ); + my $is_identical = $trigger1->equals( $trigger2 ); =cut - my $self = shift; - my $other = shift; + my $self = shift; + my $other = shift; my $case_insensitive = shift; return 0 unless $self->SUPER::equals($other); - return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name; - #return 0 unless $self->is_valid eq $other->is_valid; - return 0 unless $self->perform_action_when eq $other->perform_action_when; - return 0 unless compare_arrays($self->database_events,$other->database_events) ; - return 0 unless $self->on_table eq $other->on_table; - return 0 unless $self->action eq $other->action; - return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra); + + my %names; + for my $name ( $self->name, $other->name ) { + $name = lc $name if $case_insensitive; + $names{ $name }++; + } + + if ( keys %names > 1 ) { + return $self->error('Names not equal'); + } + + if ( !$self->perform_action_when eq $other->perform_action_when ) { + return $self->error('perform_action_when differs'); + } + + if ( + !compare_arrays( [$self->database_events], [$other->database_events] ) + ) { + return $self->error('database_events differ'); + } + + if ( $self->on_table ne $other->on_table ) { + return $self->error('on_table differs'); + } + + if ( $self->action ne $other->action ) { + return $self->error('action differs'); + } + + if ( + !$self->_compare_objects( scalar $self->extra, scalar $other->extra ) + ) { + return $self->error('extras differ'); + } + return 1; } @@ -423,8 +433,9 @@ sub DESTROY { =pod -=head1 AUTHOR +=head1 AUTHORS -Ken Y. Clark Ekclark@cpan.orgE. +Anonymous, +Ken Youens-Clark Ekclark@cpan.orgE. =cut