package SQL::Translator::Schema::Trigger;
-# ----------------------------------------------------------------------
-# 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
on_table => 'foo', # table name
action => '...', # text of trigger
schema => $schema, # Schema object
+ scope => 'row', # or statement
);
=head1 DESCRIPTION
=cut
-use strict;
-use SQL::Translator::Utils 'parse_list_arg';
-
-use base 'SQL::Translator::Schema::Object';
-
-use Carp;
+use Moo 1.000003;
+use SQL::Translator::Utils qw(parse_list_arg ex2err throw);
+use SQL::Translator::Types qw(schema_obj enum);
+use List::MoreUtils qw(uniq);
+use Sub::Quote qw(quote_sub);
-use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
+extends 'SQL::Translator::Schema::Object';
-$VERSION = '1.59';
-
-# ----------------------------------------------------------------------
-
-__PACKAGE__->_attributes( qw/
- name schema perform_action_when database_events database_event
- fields table on_table action order
-/);
-
-=pod
+our $VERSION = '1.59';
=head2 new
=cut
-# ----------------------------------------------------------------------
-sub perform_action_when {
-
-=pod
+around BUILDARGS => sub {
+ my ($orig, $self, @args) = @_;
+ my $args = $self->$orig(@args);
+ if (exists $args->{on_table}) {
+ my $arg = delete $args->{on_table};
+ my $table = $args->{schema}->get_table($arg)
+ or die "Table named $arg doesn't exist";
+ $args->{table} = $table;
+ }
+ if (exists $args->{database_event}) {
+ $args->{database_events} = delete $args->{database_event};
+ }
+ return $args;
+};
=head2 perform_action_when
-Gets or sets whether the event happens "before" or "after" the
+Gets or sets whether the event happens "before" or "after" the
C<database_event>.
$trigger->perform_action_when('after');
=cut
- my $self = shift;
-
- if ( my $arg = shift ) {
- $arg = lc $arg;
- $arg =~ s/\s+/ /g;
- if ( $arg =~ m/^(before|after)$/i ) {
- $self->{'perform_action_when'} = $arg;
- }
- else {
- return
- $self->error("Invalid argument '$arg' to perform_action_when");
- }
- }
+has perform_action_when => (
+ is => 'rw',
+ coerce => quote_sub(q{ defined $_[0] ? lc $_[0] : $_[0] }),
+ isa => enum([qw(before after)], {
+ msg => "Invalid argument '%s' to perform_action_when",
+ allow_undef => 1,
+ }),
+);
- return $self->{'perform_action_when'};
-}
+around perform_action_when => \&ex2err;
-# ----------------------------------------------------------------------
sub database_event {
=pod
Obsolete please use database_events!
=cut
-
+
my $self = shift;
return $self->database_events( @_ );
}
-
-# ----------------------------------------------------------------------
-sub database_events {
-
-=pod
=head2 database_events
=cut
- my $self = shift;
- my @args = ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_;
-
- if ( @args ) {
- @args = map { s/\s+/ /g; lc $_ } @args;
+has database_events => (
+ is => 'rw',
+ coerce => quote_sub(q{ [ map { lc } ref $_[0] eq 'ARRAY' ? @{$_[0]} : ($_[0]) ] }),
+ isa => sub {
+ my @args = @{$_[0]};
my %valid = map { $_, 1 } qw[ insert update update_on delete ];
my @invalid = grep { !defined $valid{ $_ } } @args;
-
+
if ( @invalid ) {
- return $self->error(
+ throw(
sprintf("Invalid events '%s' in database_events",
join(', ', @invalid)
)
);
}
+ },
+);
- $self->{'database_events'} = [ @args ];
- }
-
- return wantarray
- ? @{ $self->{'database_events'} || [] }
- : $self->{'database_events'};
-}
+around database_events => sub {
+ my ($orig,$self) = (shift, shift);
-# ----------------------------------------------------------------------
-sub fields {
+ if (@_) {
+ ex2err($orig, $self, ref $_[0] eq 'ARRAY' ? $_[0] : \@_)
+ or return;
+ }
-=pod
+ return wantarray
+ ? @{ $self->$orig || [] }
+ : $self->$orig;
+};
=head2 fields
=cut
- my $self = shift;
+has fields => (
+ is => 'rw',
+ coerce => sub {
+ my @fields = uniq @{parse_list_arg($_[0])};
+ @fields ? \@fields : undef;
+ },
+);
+
+around fields => sub {
+ my $orig = shift;
+ my $self = shift;
my $fields = parse_list_arg( @_ );
+ $self->$orig($fields) if @$fields;
- if ( @$fields ) {
- my ( %unique, @unique );
- for my $f ( @$fields ) {
- next if $unique{ $f };
- $unique{ $f } = 1;
- push @unique, $f;
- }
-
- $self->{'fields'} = \@unique;
- }
-
- return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
-}
-
-# ----------------------------------------------------------------------
-sub table {
-
-=pod
+ return wantarray ? @{ $self->$orig || [] } : $self->$orig;
+};
=head2 table
=cut
- my ($self, $arg) = @_;
- if ( @_ == 2 ) {
- $self->error("Table attribute of a ".__PACKAGE__.
- " must be a SQL::Translator::Schema::Table")
- unless ref $arg and $arg->isa('SQL::Translator::Schema::Table');
- $self->{table} = $arg;
- }
- return $self->{table};
-}
+has table => ( is => 'rw', isa => schema_obj('Table'), weak_ref => 1 );
+
+around table => \&ex2err;
-# ----------------------------------------------------------------------
sub on_table {
=pod
return $self->table->name;
}
-# ----------------------------------------------------------------------
-sub action {
-
-=pod
-
=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 ...;
=cut
- my $self = shift;
- my $arg = shift || '';
- $self->{'action'} = $arg if $arg;
- return $self->{'action'};
-}
+has action => ( is => 'rw', default => quote_sub(q{ '' }) );
-# ----------------------------------------------------------------------
sub is_valid {
=pod
my $self = shift;
- for my $attr (
- qw[ name perform_action_when database_events on_table action ]
+ for my $attr (
+ qw[ name perform_action_when database_events on_table action ]
) {
return $self->error("Invalid: missing '$attr'") unless $self->$attr();
}
-
- return $self->error("Missing fields for UPDATE ON") if
+
+ return $self->error("Missing fields for UPDATE ON") if
$self->database_event eq 'update_on' && !$self->fields;
return 1;
}
-# ----------------------------------------------------------------------
-sub name {
-
-=pod
-
=head2 name
Get or set the trigger's name.
=cut
- my $self = shift;
- $self->{'name'} = shift if @_;
- return $self->{'name'} || '';
-}
-
-# ----------------------------------------------------------------------
-sub order {
-
-=pod
+has name => ( is => 'rw', default => quote_sub(q{ '' }) );
=head2 order
=cut
- my ( $self, $arg ) = @_;
+has order => ( is => 'rw', default => quote_sub(q{ 0 }) );
+
+around order => sub {
+ my ( $orig, $self, $arg ) = @_;
if ( defined $arg && $arg =~ /^\d+$/ ) {
- $self->{'order'} = $arg;
+ return $self->$orig($arg);
}
- return $self->{'order'} || 0;
-}
+ return $self->$orig;
+};
-# ----------------------------------------------------------------------
-sub schema {
+=head2 scope
+
+Get or set the trigger's scope (row or statement).
+
+ my $scope = $trigger->scope('statement');
+
+=cut
+
+has scope => (
+ is => 'rw',
+ isa => enum([qw(row statement)], {
+ msg => "Invalid scope '%s'", icase => 1, allow_undef => 1,
+ }),
+);
+
+around scope => \&ex2err;
-=pod
=head2 schema
=cut
- my $self = shift;
- if ( my $arg = shift ) {
- return $self->error('Not a schema object') unless
- UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
- $self->{'schema'} = $arg;
- }
+has schema => (is => 'rw', isa => schema_obj('Schema'), weak_ref => 1 );
- return $self->{'schema'};
-}
+around schema => \&ex2err;
-# ----------------------------------------------------------------------
sub compare_arrays {
=pod
Compare two arrays.
=cut
-
+
my ($first, $second) = @_;
no warnings; # silence spurious -w undef complaints
return 1;
}
-# ----------------------------------------------------------------------
-sub equals {
-
-=pod
-
=head2 equals
Determines if this trigger is the same as another
=cut
+around equals => sub {
+ my $orig = 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->$orig($other);
- return 0 unless $self->perform_action_when eq $other->perform_action_when;
+ my %names;
+ for my $name ( $self->name, $other->name ) {
+ $name = lc $name if $case_insensitive;
+ $names{ $name }++;
+ }
- return 0
- unless compare_arrays( $self->database_events, $other->database_events );
+ if ( keys %names > 1 ) {
+ return $self->error('Names not equal');
+ }
- return 0 unless $self->on_table eq $other->on_table;
+ if ( !$self->perform_action_when eq $other->perform_action_when ) {
+ return $self->error('perform_action_when differs');
+ }
- return 0 unless $self->action eq $other->action;
+ if (
+ !compare_arrays( [$self->database_events], [$other->database_events] )
+ ) {
+ return $self->error('database_events differ');
+ }
- return 0 unless $self->_compare_objects( scalar $self->extra,
- scalar $other->extra );
+ 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;
-}
+};
-# ----------------------------------------------------------------------
-sub DESTROY {
- my $self = shift;
- undef $self->{'schema'}; # destroy cyclical reference
-}
+# Must come after all 'has' declarations
+around new => \&ex2err;
1;
-# ----------------------------------------------------------------------
-
=pod
=head1 AUTHORS