From: Christopher H. Laco Date: Fri, 7 Apr 2006 01:13:50 +0000 (+0000) Subject: Added source_name to ResultSource/ResultSourceProxy X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bab77431c4723e1625740c08c9c53742b689cbdb;p=dbsrgits%2FDBIx-Class-Historic.git Added source_name to ResultSource/ResultSourceProxy Changed load_classes to load/use source_name for the moniker Added small tests for source_name/remove_columns --- diff --git a/Changes b/Changes index f31680c..b1d2c80 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,8 @@ Revision history for DBIx::Class - added remove_column(s) to ResultSource/ResultSourceProxy - added add_column alias to ResultSourceProxy + - added source_name to ResultSource + - load_classes now uses source_name and sets it if necessary 0.06001 - Added fix for quoting with single table diff --git a/lib/DBIx/Class/ResultSource.pm b/lib/DBIx/Class/ResultSource.pm index 3174957..1caf03c 100644 --- a/lib/DBIx/Class/ResultSource.pm +++ b/lib/DBIx/Class/ResultSource.pm @@ -15,7 +15,7 @@ __PACKAGE__->mk_group_accessors('simple' => qw/_ordered_columns schema from _relationships/); __PACKAGE__->mk_group_accessors('component_class' => qw/resultset_class - result_class/); + result_class source_name/); =head1 NAME @@ -493,7 +493,7 @@ sub has_relationship { =back -Returns an array of hash references of relationship information for +Returns an array of hash references of relationship information for the other side of the specified relationship name. =cut @@ -508,13 +508,13 @@ sub reverse_relationship_info { my @cond = keys(%{$rel_info->{cond}}); my @refkeys = map {/^\w+\.(\w+)$/} @cond; my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond; - + # Get the related result source for this relationship my $othertable = $self->related_source($rel); # Get all the relationships for that source that related to this source # whose foreign column set are our self columns on $rel and whose self - # columns are our foreign columns on $rel. + # columns are our foreign columns on $rel. my @otherrels = $othertable->relationships(); my $otherrelationship; foreach my $otherrel (@otherrels) { @@ -539,7 +539,7 @@ sub reverse_relationship_info { my @other_cond = keys(%$othercond); my @other_refkeys = map {/^\w+\.(\w+)$/} @other_cond; my @other_keys = map {$othercond->{$_} =~ /^\w+\.(\w+)$/} @other_cond; - next if (!$self->compare_relationship_keys(\@refkeys, \@other_keys) || + next if (!$self->compare_relationship_keys(\@refkeys, \@other_keys) || !$self->compare_relationship_keys(\@other_refkeys, \@keys)); $ret->{$otherrel} = $otherrel_info; } @@ -853,6 +853,26 @@ sub resultset { ); } +=head2 source_name + +=over 4 + +=item Arguments: $source_name + +=back + +Set the name of the result source when it is loaded into a schema. +This is usefull if you want to refer to a result source by a name other than +its class name. + + package ArchivedBooks; + use base qw/DBIx::Class/; + __PACKAGE__->table('books_archive'); + __PACKAGE__->source_name('Books'); + + # from your schema... + $schema->resultset('Books')->find(1); + =head2 throw_exception See L. diff --git a/lib/DBIx/Class/ResultSourceProxy.pm b/lib/DBIx/Class/ResultSourceProxy.pm index d1f5b80..2743bf7 100644 --- a/lib/DBIx/Class/ResultSourceProxy.pm +++ b/lib/DBIx/Class/ResultSourceProxy.pm @@ -8,6 +8,7 @@ use base qw/DBIx::Class/; sub iterator_class { shift->result_source_instance->resultset_class(@_) } sub resultset_class { shift->result_source_instance->resultset_class(@_) } +sub source_name { shift->result_source_instance->source_name(@_) } sub resultset_attributes { shift->result_source_instance->resultset_attributes(@_); diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index 0b9b969..3c96aef 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -20,7 +20,7 @@ DBIx::Class::Schema - composable schemas package Library::Schema; use base qw/DBIx::Class::Schema/; - + # load Library::Schema::CD, Library::Schema::Book, Library::Schema::DVD __PACKAGE__->load_classes(qw/CD Book DVD/); @@ -36,7 +36,7 @@ DBIx::Class::Schema - composable schemas $password, { AutoCommit => 0 }, ); - + my $schema2 = Library::Schema->connect($coderef_returning_dbh); # fetch objects using Library::Schema::DVD @@ -219,15 +219,15 @@ Example: sub load_classes { my ($class, @params) = @_; - + my %comps_for; - + if (@params) { foreach my $param (@params) { if (ref $param eq 'ARRAY') { # filter out commented entries my @modules = grep { $_ !~ /^#/ } @$param; - + push (@{$comps_for{$class}}, @modules); } elsif (ref $param eq 'HASH') { @@ -267,7 +267,10 @@ sub load_classes { die $@ unless $@ =~ /Can't locate.+$comp_class\.pm\sin\s\@INC/; warn $@ if $@; } - push(@to_register, [ $comp, $comp_class ]); + + $comp_class->source_name($comp) unless $comp_class->source_name; + + push(@to_register, [ $comp_class->source_name, $comp_class ]); } } } diff --git a/t/lib/DBICTest/Schema.pm b/t/lib/DBICTest/Schema.pm index d3f086d..e882ee7 100644 --- a/t/lib/DBICTest/Schema.pm +++ b/t/lib/DBICTest/Schema.pm @@ -1,4 +1,4 @@ -package # hide from PAUSE +package # hide from PAUSE DBICTest::Schema; use base qw/DBIx::Class::Schema/; @@ -25,6 +25,7 @@ __PACKAGE__->load_classes(qw/ '#dummy', 'SelfRef', 'ArtistUndirectedMap', + 'ArtistSourceName', 'Producer', 'CD_to_Producer', ), diff --git a/t/lib/DBICTest/Schema/ArtistSourceName.pm b/t/lib/DBICTest/Schema/ArtistSourceName.pm new file mode 100644 index 0000000..c4c8a8b --- /dev/null +++ b/t/lib/DBICTest/Schema/ArtistSourceName.pm @@ -0,0 +1,8 @@ +package # hide from PAUSE + DBICTest::Schema::ArtistSourceName; + +use base 'DBICTest::Schema::Artist'; + +__PACKAGE__->source_name('SourceNameArtists'); + +1; diff --git a/t/run/01core.tl b/t/run/01core.tl index c41ef17..d2fcd24 100644 --- a/t/run/01core.tl +++ b/t/run/01core.tl @@ -1,7 +1,7 @@ sub run_tests { my $schema = shift; -plan tests => 41; +plan tests => 46; my @art = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'}); @@ -161,6 +161,21 @@ is($typeinfo->{data_type}, 'INTEGER', 'column_info ok'); $schema->source("Artist")->column_info('artistid'); ok($schema->source("Artist")->{_columns_info_loaded} == 1, 'Columns info flag set'); +# source_name should be set for normal modules +is($schema->source('CD')->source_name, 'CD', 'source_name is set to moniker'); + +# test the result source that uses source_name +ok($schema->source('SourceNameArtists'), 'SourceNameArtists result source exists'); + +my @artsn = $schema->resultset("SourceNameArtists")->search({ }, { order_by => 'name DESC'}); +cmp_ok(@artsn, '==', 4, "Four artists returned"); + + +# test removed columns +is_deeply([$schema->source('CD')->columns], [qw/cdid artist title year/]); +$schema->source('CD')->remove_columns('year'); +is_deeply([$schema->source('CD')->columns], [qw/cdid artist title/]); + } 1;