[Bast-commits] r5241 - 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 21:27:06 GMT 2008
Author: groditi
Date: 2008-12-16 21:27:06 +0000 (Tue, 16 Dec 2008)
New Revision: 5241
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/t/66relationship.t
DBIx-Class/0.08/branches/belongs_to_null_col_fix/t/lib/DBICTest/Schema/CD.pm
Log:
Introduce 'any_null_means_no_value' option to eliminate wasteful queries. The option is off by default and must be explicitly turned on. Tests, + docs included
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 20:30:01 UTC (rev 5240)
+++ DBIx-Class/0.08/branches/belongs_to_null_col_fix/Changes 2008-12-16 21:27:06 UTC (rev 5241)
@@ -3,6 +3,9 @@
- 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)
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 20:30:01 UTC (rev 5240)
+++ DBIx-Class/0.08/branches/belongs_to_null_col_fix/lib/DBIx/Class/Relationship/Accessor.pm 2008-12-16 21:27:06 UTC (rev 5241)
@@ -18,7 +18,7 @@
my ($class, $rel, $acc_type) = @_;
my %meth;
if ($acc_type eq 'single') {
- my $rel_cond = $class->relationship_info($rel)->{cond};
+ my $rel_info = $class->relationship_info($rel);
$meth{$rel} = sub {
my $self = shift;
if (@_) {
@@ -28,9 +28,12 @@
return $self->{_relationship_data}{$rel};
} else {
my $cond = $self->result_source->resolve_condition(
- $rel_cond, $rel, $self
+ $rel_info->{cond}, $rel, $self
);
- return if grep { not defined } values %$cond;
+ if( exists $rel_info->{attrs}->{any_null_means_no_value}
+ && $rel_info->{attrs}->{any_null_means_no_value} ){
+ return if grep { not defined } values %$cond;
+ }
my $val = $self->find_related($rel, {}, {});
return unless $val;
return $self->{_relationship_data}{$rel} = $val;
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 20:30:01 UTC (rev 5240)
+++ DBIx-Class/0.08/branches/belongs_to_null_col_fix/lib/DBIx/Class/Relationship.pm 2008-12-16 21:27:06 UTC (rev 5241)
@@ -208,6 +208,13 @@
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').
+
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 20:30:01 UTC (rev 5240)
+++ DBIx-Class/0.08/branches/belongs_to_null_col_fix/t/66relationship.t 2008-12-16 21:27:06 UTC (rev 5241)
@@ -8,7 +8,7 @@
my $schema = DBICTest->init_schema();
-plan tests => 72;
+plan tests => 73;
# has_a test
my $cd = $schema->resultset("CD")->find(4);
@@ -41,8 +41,19 @@
} );
}
-is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
+my $big_flop_cd = ($artist->search_related('cds'))[3];
+is( $big_flop_cd->title, 'Big Flop', 'create_related ok' );
+{ # make sure we are not making pointless select queries when a FK IS NULL
+ my $queries = 0;
+ $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');
+ $schema->storage->debug(0);
+ $schema->storage->debugcb(undef);
+}
+
my( $rs_from_list ) = $artist->search_related_rs('cds');
is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'search_related_rs in list context returns rs' );
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 20:30:01 UTC (rev 5240)
+++ DBIx-Class/0.08/branches/belongs_to_null_col_fix/t/lib/DBICTest/Schema/CD.pm 2008-12-16 21:27:06 UTC (rev 5241)
@@ -67,7 +67,7 @@
join_type => 'left',
on_delete => 'SET NULL',
on_update => 'CASCADE',
-
+ any_null_means_no_value => 1,
},
);
More information about the Bast-commits
mailing list