[Bast-commits] r5243 - in DBIx-Class/0.08/branches/belongs_to_null_col_fix: . lib/DBIx/Class lib/DBIx/Class/Relationship t t/lib/DBICTest/Schema

groditi at dev.catalyst.perl.org groditi at dev.catalyst.perl.org
Tue Dec 16 22:42:10 GMT 2008


Author: groditi
Date: 2008-12-16 22:42:10 +0000 (Tue, 16 Dec 2008)
New Revision: 5243

Modified:
   DBIx-Class/0.08/branches/belongs_to_null_col_fix/Changes
   DBIx-Class/0.08/branches/belongs_to_null_col_fix/lib/DBIx/Class/Relationship.pm
   DBIx-Class/0.08/branches/belongs_to_null_col_fix/lib/DBIx/Class/Relationship/Accessor.pm
   DBIx-Class/0.08/branches/belongs_to_null_col_fix/lib/DBIx/Class/Relationship/BelongsTo.pm
   DBIx-Class/0.08/branches/belongs_to_null_col_fix/t/66relationship.t
   DBIx-Class/0.08/branches/belongs_to_null_col_fix/t/lib/DBICTest/Schema/CD.pm
Log:
rename option to undef_on_null_fk and make it default for belongs_to

Modified: DBIx-Class/0.08/branches/belongs_to_null_col_fix/Changes
===================================================================
--- DBIx-Class/0.08/branches/belongs_to_null_col_fix/Changes	2008-12-16 22:10:37 UTC (rev 5242)
+++ DBIx-Class/0.08/branches/belongs_to_null_col_fix/Changes	2008-12-16 22:42:10 UTC (rev 5243)
@@ -3,9 +3,10 @@
         - Classes submitted as result_class for a resultsource are now
           automatically loaded via ensure_loaded()
         - 'result_class' resultset attribute, identical to result_class()
-        - add 'any_null_means_no_value' option for relationship accessors of
-          type 'single'. This will prevent DBIC from querying the database
-          if one or more of the key columns IS NULL. Tests + docs (groditi)
+        - add 'null_on_fk' option for relationship accessors of type 'single'. 
+          This will prevent DBIC from querying the database if one or more of
+          the key columns IS NULL. Tests + docs (groditi)
+           - for 'belongs_to' rels, 'null_on_fk' defaults to true.
 
 0.08099_05 2008-10-30 21:30:00 (UTC)
         - Rewritte of Storage::DBI::connect_info(), extended with an

Modified: DBIx-Class/0.08/branches/belongs_to_null_col_fix/lib/DBIx/Class/Relationship/Accessor.pm
===================================================================
--- DBIx-Class/0.08/branches/belongs_to_null_col_fix/lib/DBIx/Class/Relationship/Accessor.pm	2008-12-16 22:10:37 UTC (rev 5242)
+++ DBIx-Class/0.08/branches/belongs_to_null_col_fix/lib/DBIx/Class/Relationship/Accessor.pm	2008-12-16 22:42:10 UTC (rev 5243)
@@ -30,8 +30,8 @@
         my $cond = $self->result_source->resolve_condition(
           $rel_info->{cond}, $rel, $self
         );
-        if( exists $rel_info->{attrs}->{any_null_means_no_value}
-              && $rel_info->{attrs}->{any_null_means_no_value} ){
+        if( exists $rel_info->{attrs}->{undef_on_null_fk}
+              && $rel_info->{attrs}->{undef_on_null_fk} ){
           return if grep { not defined } values %$cond;
         }
         my $val = $self->find_related($rel, {}, {});

Modified: DBIx-Class/0.08/branches/belongs_to_null_col_fix/lib/DBIx/Class/Relationship/BelongsTo.pm
===================================================================
--- DBIx-Class/0.08/branches/belongs_to_null_col_fix/lib/DBIx/Class/Relationship/BelongsTo.pm	2008-12-16 22:10:37 UTC (rev 5242)
+++ DBIx-Class/0.08/branches/belongs_to_null_col_fix/lib/DBIx/Class/Relationship/BelongsTo.pm	2008-12-16 22:42:10 UTC (rev 5243)
@@ -13,6 +13,8 @@
   # assume a foreign key contraint unless defined otherwise
   $attrs->{is_foreign_key_constraint} = 1 
     if not exists $attrs->{is_foreign_key_constraint};
+  $attrs->{undef_on_null_fk} = 1
+    if not exists $attrs->{undef_on_null_fk};
 
   # no join condition or just a column name
   if (!ref $cond) {

Modified: DBIx-Class/0.08/branches/belongs_to_null_col_fix/lib/DBIx/Class/Relationship.pm
===================================================================
--- DBIx-Class/0.08/branches/belongs_to_null_col_fix/lib/DBIx/Class/Relationship.pm	2008-12-16 22:10:37 UTC (rev 5242)
+++ DBIx-Class/0.08/branches/belongs_to_null_col_fix/lib/DBIx/Class/Relationship.pm	2008-12-16 22:42:10 UTC (rev 5243)
@@ -208,12 +208,10 @@
 relationship. To turn them on, pass C<< cascade_delete => 1 >>
 in the $attr hashref.
 
-By default, DBIC will attempt to query the related table for a row when the
-relationship accessor is called even if a foreign key member column IS NULL,
-which can be wasteful. To avoid this query from being performed, pass
-C<< any_null_means_no_value => 1 >> in the C<$attr> hashref. This only applies
-to accessors of type 'single' (when your accessor and foreign key have
-different names e.g. 'cd_id', and 'cd').
+By default, DBIC will return undef and avoid querying the database if a
+C<belongs_to> accessor is called when any part of the foreign key IS NULL. To
+disable this behavior, pass C<< undef_on_null_fk => 0 >> in the C<$attr>
+hashref.
 
 NOTE: If you are used to L<Class::DBI> relationships, this is the equivalent
 of C<has_a>.

Modified: DBIx-Class/0.08/branches/belongs_to_null_col_fix/t/66relationship.t
===================================================================
--- DBIx-Class/0.08/branches/belongs_to_null_col_fix/t/66relationship.t	2008-12-16 22:10:37 UTC (rev 5242)
+++ DBIx-Class/0.08/branches/belongs_to_null_col_fix/t/66relationship.t	2008-12-16 22:42:10 UTC (rev 5243)
@@ -8,7 +8,7 @@
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 73;
+plan tests => 74;
 
 # has_a test
 my $cd = $schema->resultset("CD")->find(4);
@@ -49,7 +49,9 @@
   $schema->storage->debugcb(sub { $queries++; });
   $schema->storage->debug(1);
   $big_flop_cd->genre; #should not trigger a select query
-  is($queries, 0, 'No Select made for belongs_to if key IS NULL');
+  is($queries, 0, 'No SELECT made for belongs_to if key IS NULL');
+  $big_flop_cd->genre_inefficient; #should trigger a select query
+  is($queries, 1, 'SELECT made for belongs_to if key IS NULL when undef_on_null_fk disabled');
   $schema->storage->debug(0);
   $schema->storage->debugcb(undef);
 }

Modified: DBIx-Class/0.08/branches/belongs_to_null_col_fix/t/lib/DBICTest/Schema/CD.pm
===================================================================
--- DBIx-Class/0.08/branches/belongs_to_null_col_fix/t/lib/DBICTest/Schema/CD.pm	2008-12-16 22:10:37 UTC (rev 5242)
+++ DBIx-Class/0.08/branches/belongs_to_null_col_fix/t/lib/DBICTest/Schema/CD.pm	2008-12-16 22:42:10 UTC (rev 5243)
@@ -67,10 +67,22 @@
         join_type => 'left',
         on_delete => 'SET NULL',
         on_update => 'CASCADE',
-        any_null_means_no_value => 1,
     },
 );
 
+#This second relationship was added to test the short-circuiting of pointless
+#queries provided by undef_on_null_fk. the relevant test in 66relationship.t
+__PACKAGE__->belongs_to('genre_inefficient', 'DBICTest::Schema::Genre',
+    { 'foreign.genreid' => 'self.genreid' },
+    {
+        join_type => 'left',
+        on_delete => 'SET NULL',
+        on_update => 'CASCADE',
+        undef_on_null_fk => 0,
+    },
+);
+
+
 #__PACKAGE__->add_relationship('genre', 'DBICTest::Schema::Genre',
 #    { 'foreign.genreid' => 'self.genreid' },
 #    { 'accessor' => 'single' }




More information about the Bast-commits mailing list