on_table now stores a known table obj in the table() attribute, which can also be used directly
Updated tests to match
package SQL::Translator::Schema::Trigger;
# ----------------------------------------------------------------------
-# $Id: Trigger.pm,v 1.8 2005-08-10 16:46:55 duality72 Exp $
+# $Id: Trigger.pm,v 1.9 2006-06-07 16:37:33 schiffbruechige Exp $
# ----------------------------------------------------------------------
# Copyright (C) 2002-4 SQLFairy Authors
#
use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
-$VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/;
# ----------------------------------------------------------------------
__PACKAGE__->_attributes( qw/
- name perform_action_when database_event fields on_table action schema
+ name schema perform_action_when database_event fields table on_table action
order
/);
}
# ----------------------------------------------------------------------
+sub table {
+
+=pod
+
+=head2 table
+
+Gets or set the table on which the trigger works, as a L<SQL::Translator::Schema::Table> object.
+ $trigger->table($triggered_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};
+}
+
+# ----------------------------------------------------------------------
sub on_table {
=pod
=head2 on_table
-Gets or set the table name on which the trigger works.
-
- $trigger->table('foo');
+Gets or set the table name on which the trigger works, as a string.
+ $trigger->on_table('foo');
=cut
- my $self = shift;
- my $arg = shift || '';
- $self->{'on_table'} = $arg if $arg;
- return $self->{'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;
}
# ----------------------------------------------------------------------
#
{
my $s = SQL::Translator::Schema->new(name => 'TrigTest');
+ $s->add_table(name=>'foo') or die "Couldn't create table: ", $s->error;
my $name = 'foo_trigger';
my $perform_action_when = 'after';
my $database_event = 'insert';
qq[Perform action when is "$perform_action_when"] );
is( $t->database_event, $database_event,
qq[Database event is "$database_event"] );
- is( $t->on_table, $on_table, qq[Table is "$on_table"] );
+ isa_ok( $t->table, 'SQL::Translator::Schema::Table', qq[table is a Table"] );
is( $t->action, $action, qq[Action is "$action"] );
my @triggs = $s->get_triggers;
name => 'foo_trigger',
perform_action_when => 'after',
database_event => 'insert',
- on_table => 'foo',
+ on_table => 'Basic',
action => 'update modified=timestamp();',
extra => {
foo => "bar",
local $SIG{__WARN__} = sub {
CORE::warn(@_)
- unless $_[0] =~ m#XML/Writer#;
+ unless $_[0] =~ m!XML/Writer!;
};
# Testing 1,2,3,4...
$ans = <<EOXML;
<schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
<extra />
- <tables></tables>
+ <tables>
+ <table name="Basic" order="2">
+ <extra />
+ <fields></fields>
+ <indices></indices>
+ <constraints></constraints>
+ <comments></comments>
+ </table>
+ </tables>
<views></views>
<triggers>
- <trigger name="foo_trigger" database_event="insert" on_table="foo" perform_action_when="after" order="1">
+ <trigger name="foo_trigger" database_event="insert" on_table="Basic" perform_action_when="after" order="1">
<action>update modified=timestamp();</action>
<extra hello="world" />
</trigger>
my $name = 'foo_trigger';
my $perform_action_when = 'after';
my $database_event = 'insert';
- my $on_table = 'foo';
my $action = 'update modified=timestamp();';
+ my $table = $s->add_table( name => "Basic" ) or die $s->error;
my $t = $s->add_trigger(
name => $name,
perform_action_when => $perform_action_when,
database_event => $database_event,
- on_table => $on_table,
+ table => $table,
action => $action,
extra => { hello => "world" },
) or die $s->error;
<schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
<extra />
<tables>
- <table name="Basic" order="2">
+ <table name="Basic" order="3">
<extra />
<fields>
<field name="foo" data_type="integer" size="10" is_nullable="1" is_auto_increment="0" is_primary_key="0" is_foreign_key="0" order="5">
producer_type: SQL::Translator::Producer::YAML
show_warnings: 1
trace: 0
- version: 0.07
+ version: SUPPRESSED
};
# Parse the test schema
lives_ok { $out = $obj->translate; } "Translate ran";
is $obj->error, '' ,"No errors";
ok $out ne "" ,"Produced something!";
+# Somewhat hackishly modify the yaml with a regex to avoid
+# failing randomly on every change of version.
+$out =~ s/version: .*/version: SUPPRESSED/;
eq_or_diff $out, $ans_yaml ,"Output looks right";
</views>
<triggers>
- <trigger name="foo_trigger" database_event="insert" on_table="foo"
+ <trigger name="foo_trigger" database_event="insert" on_table="Basic"
perform_action_when="after" order="1">
<action>update modified=timestamp();</action>
<extra foo="bar" hello="world" bar="baz" />