| SQL-Translator documentation | Contained in the SQL-Translator distribution. |
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_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
);
SQL::Translator::Schema::Trigger is the trigger object.
Object constructor.
my $schema = SQL::Translator::Schema::Trigger->new;
Gets or sets whether the event happens "before" or "after" the
database_event.
$trigger->perform_action_when('after');
Obsolete please use database_events!
Gets or sets the events that triggers the trigger.
my $ok = $trigger->database_events('insert');
Gets and set which fields to monitor for database_event.
$view->fields('id');
$view->fields('id', 'name');
$view->fields( 'id, name' );
$view->fields( [ 'id', 'name' ] );
$view->fields( qw[ id name ] );
my @fields = $view->fields;
Gets or set the table on which the trigger works, as a SQL::Translator::Schema::Table object. $trigger->table($triggered_table);
Gets or set the table name on which the trigger works, as a string. $trigger->on_table('foo');
Gets or set the action of the trigger.
$trigger->action(
q[
BEGIN
select ...;
update ...;
END
]
);
Determine whether the trigger is valid or not.
my $ok = $trigger->is_valid;
Get or set the trigger's name.
my $name = $trigger->name('foo');
Get or set the trigger's order.
my $order = $trigger->order(3);
Get or set the trigger's schema object.
$trigger->schema( $schema ); my $schema = $trigger->schema;
Compare two arrays.
Determines if this trigger is the same as another
my $is_identical = $trigger1->equals( $trigger2 );
Anonymous, Ken Youens-Clark <kclark@cpan.org>.
| SQL-Translator documentation | Contained in the SQL-Translator distribution. |
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 # -------------------------------------------------------------------
use strict; use SQL::Translator::Utils 'parse_list_arg'; use base 'SQL::Translator::Schema::Object'; use Carp; use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT); $VERSION = '1.59'; # ---------------------------------------------------------------------- __PACKAGE__->_attributes( qw/ name schema perform_action_when database_events database_event fields table on_table action order /);
# ---------------------------------------------------------------------- sub perform_action_when {
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");
}
}
return $self->{'perform_action_when'};
}
# ----------------------------------------------------------------------
sub database_event {
my $self = shift;
return $self->database_events( @_ );
}
# ----------------------------------------------------------------------
sub 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'};
}
# ----------------------------------------------------------------------
sub fields {
my $self = shift;
my $fields = parse_list_arg( @_ );
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 {
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};
}
# ----------------------------------------------------------------------
sub on_table {
my ($self, $arg) = @_;
if ( @_ == 2 ) {
my $table = $self->schema->get_table($arg);
die "Table named $arg doesn't exist"
if !$table;
$self->table($table);
}
return $self->table->name;
}
# ----------------------------------------------------------------------
sub action {
my $self = shift;
my $arg = shift || '';
$self->{'action'} = $arg if $arg;
return $self->{'action'};
}
# ----------------------------------------------------------------------
sub is_valid {
my $self = shift;
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
$self->database_event eq 'update_on' && !$self->fields;
return 1;
}
# ----------------------------------------------------------------------
sub name {
my $self = shift;
$self->{'name'} = shift if @_;
return $self->{'name'} || '';
}
# ----------------------------------------------------------------------
sub order {
my ( $self, $arg ) = @_;
if ( defined $arg && $arg =~ /^\d+$/ ) {
$self->{'order'} = $arg;
}
return $self->{'order'} || 0;
}
# ----------------------------------------------------------------------
sub schema {
my $self = shift;
if ( my $arg = shift ) {
return $self->error('Not a schema object') unless
UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
$self->{'schema'} = $arg;
}
return $self->{'schema'};
}
# ----------------------------------------------------------------------
sub compare_arrays {
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;
}
# ----------------------------------------------------------------------
sub equals {
my $self = shift;
my $other = shift;
my $case_insensitive = shift;
return 0 unless $self->SUPER::equals($other);
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;
}
# ----------------------------------------------------------------------
sub DESTROY {
my $self = shift;
undef $self->{'schema'}; # destroy cyclical reference
}
1;
# ----------------------------------------------------------------------