PATCH: add_trigger()

[prev] [thread] [next] [Date index for 2004/09/30]

From: Drew Taylor
Subject: PATCH: add_trigger()
Date: 19:59 on 30 Sep 2004
I'm using an older version of C::DBI (0.93) and just ran into a
problem when trying to add a trigger to a class. Basically I had a
brain cramp and was using 'before_insert' instead of 'before_create'.
So of course my trigger was silently added, but never run. It took me
a few minutes to realize my stupidity.

It would be very nice to have checking in add_trigger() which makes
sure you're using a valid trigger. Version 0.96 doesn't have this
functionality, so a stab at it is below. I don't have a handy recent
perl available to test the patch so I apologize if I've done something
stupid. I did update the docs but not t/11-triggers.t (but I will if
Tony is interested).

Drew

--- Class-DBI.orig.pm   Thu Sep 30 15:55:17 2004
+++ Class-DBI.pm        Thu Sep 30 15:55:03 2004
@@ -104,6 +104,13 @@
 __PACKAGE__->mk_classdata('__driver');
 __PACKAGE__->__data_type({});

+__PACKAGE__->mk_classdata('__valid_triggers');
+__PACKAGE__->__valid_triggers({ map{$_=>undef} qw(before_create after_create
+                                                 before_update after_update
+                                                 before_delete after_delete
+                                                 select create)
+                             });
+
 __PACKAGE__->mk_classdata('iterator_class');
 __PACKAGE__->iterator_class('Class::DBI::Iterator');
 __PACKAGE__->__grouper(Class::DBI::ColumnGrouper->new());
@@ -1000,9 +1007,23 @@
        $self->_carp(
                "$name trigger deprecated: use before_$name or
after_$name instead")
                if ($name eq "create" or $name eq "delete");
+       return $self->_croak("Invalid trigger [$name]")
+         unless $self->__is_valid_trigger($name);
+
        $self->SUPER::add_trigger($name => @args);
 }

+sub __is_valid_trigger {
+    my ($self, $name) = @_;
+    my %valid = %{ $self->__valid_triggers };
+    foreach my $col ($self->all_columns) {
+       $valid{$col} = undef;
+    }
+
+    return 0 unless exists $valid{$name};
+    return 1;
+}
+
 #----------------------------------------------------------------------
 # Inflation
 #----------------------------------------------------------------------
@@ -1785,7 +1806,8 @@
 You can create any number of triggers for each point, but you cannot
 specify the order in which they will be run. Each will be passed the
 object being dealt with (whose values you may change if required),
-and return values will be ignored.
+and return values will be ignored. If you specify an invalid trigger
+in the call to add_trigger() an error will be thrown.

 All triggers are passed the object they are being fired for.
 Some triggers are also passed extra parameters as name-value pairs.

        -- 
        ----------------------------------------------------------------
 Drew Taylor                 *  Web development & consulting
 Email: drew@xxxxxxxxxx.xxx  *  Site implementation & hosting
 Web  : www.drewtaylor.com   *  perl/mod_perl/DBI/mysql/postgres
 ----------------------------------------------------------------

(message missing)

PATCH: add_trigger()
Drew Taylor 19:59 on 30 Sep 2004

Re: PATCH: add_trigger()
Tony Bowden 21:20 on 30 Sep 2004

Re: PATCH: add_trigger()
Edward J. Sabol 21:42 on 30 Sep 2004

Re: PATCH: add_trigger()
Drew Taylor 22:15 on 30 Sep 2004

Re: PATCH: add_trigger()
Edward J. Sabol 22:21 on 30 Sep 2004

Re: PATCH: add_trigger()
Drew Taylor 13:51 on 01 Oct 2004

Re: PATCH: add_trigger()
Tony Bowden 14:06 on 01 Oct 2004

Re: PATCH: add_trigger()
Edward J. Sabol 15:16 on 01 Oct 2004

Re: PATCH: add_trigger()
Drew Taylor 14:12 on 01 Oct 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52