From: Arthur Axel 'fREW' Schmidt Date: Sun, 13 May 2012 19:36:32 +0000 (-0500) Subject: 100% conversion to sources X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e9bab9d06cc9d7311e499d01963651b43590712c;p=dbsrgits%2FDBIx-Class.git 100% conversion to sources --- diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index 3c4d9e8..d364393 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -1536,6 +1536,16 @@ sub compose_connection { return $schema; } +sub r_sources { + my $self = shift; + + my %sref = %{$self->source_registrations}; + + return { + map { $sref{$_} => $_ } keys %sref + } +} + sub source_tree { my $self = shift; my $args = shift; @@ -1555,19 +1565,15 @@ sub source_tree { grep { !$limit_sources{$_} } $self->sources; - my %tables; + my %r_sources = %{$self->r_sources}; + my %sources; foreach my $moniker (sort keys %table_monikers) { my $source = $self->source($moniker); - my $table_name = $source->name; - - # FIXME - this isn't the right way to do it, but sqlt does not - # support quoting properly to be signaled about this - $table_name = $$table_name if ref $table_name eq 'SCALAR'; # It's possible to have multiple DBIC sources using the same table - next if $tables{$moniker}; + next if $sources{$moniker}; - $tables{$moniker}{source} = $source; + $sources{$moniker}{source} = $source; foreach my $rel (sort $source->relationships) { my $rel_info = $source->relationship_info($rel); @@ -1583,11 +1589,7 @@ sub source_tree { # related sources might be excluded via a {sources} filter or might be views next unless exists $table_monikers{$relsource->source_name}; - my $rel_table = $relsource->name; - - # FIXME - this isn't the right way to do it, but sqlt does not - # support quoting properly to be signaled about this - $rel_table = $$rel_table if ref $rel_table eq 'SCALAR'; + my $rel_moniker = $r_sources{$relsource}; # Force the order of @cond to match the order of ->add_columns my $idx; @@ -1617,18 +1619,18 @@ sub source_tree { \@keys, [$source->primary_columns]); } - $tables{$moniker}{foreign_table_deps}{$rel_table}++ + $sources{$moniker}{foreign_table_deps}{$rel_moniker}++ if $fk_constraint && @keys # calculate dependencies: do not consider deferrable constraints and # self-references for dependency calculations && !$rel_info->{attrs}{is_deferrable} - && $rel_table && $rel_table ne $table_name + && $rel_moniker && $rel_moniker ne $moniker } } return { - map { $_ => $self->_resolve_deps ($_, \%tables) } (keys %tables) + map { $_ => $self->_resolve_deps ($_, \%sources) } (keys %sources) } } diff --git a/t/schema/source-tree.t b/t/schema/source-tree.t index f6616a5..4cef159 100644 --- a/t/schema/source-tree.t +++ b/t/schema/source-tree.t @@ -7,68 +7,89 @@ use DBICTest; my $schema = DBICTest->init_schema( no_connect => 1, no_deploy => 1 ); +use Devel::Dwarn; + is_deeply($schema->source_tree, { Artist => {}, ArtistSubclass => {}, ArtistUndirectedMap => { - artist => 1 + Artist => 1 }, Artwork => { - cd => 1 + CD => 1, + Genre => 1, + Track => 1 }, Artwork_to_Artist => { - artist => 1, - cd_artwork => 1 + Artist => 1, + Artwork => 1, + CD => 1, + Genre => 1, + Track => 1 }, BindType => {}, Bookmark => { - link => 1 + Link => 1 }, BooksInLibrary => { - owners => 1 + Owners => 1 }, CD => { - genre => 1, - track => 1 + Genre => 1, + Track => 1 }, CD_to_Producer => { - cd => 1, - producer => 1 + CD => 1, + Genre => 1, + Producer => 1, + Track => 1 }, Collection => {}, CollectionObject => { - collection => 1, - typed_object => 1 + Collection => 1, + TypedObject => 1 }, CustomSql => {}, Dummy => {}, Employee => { - encoded => 1 + Encoded => 1 }, Encoded => {}, Event => {}, EventTZ => {}, ForceForeign => { - artist => 1 + Artist => 1 }, FourKeys => {}, FourKeys_to_TwoKeys => { - fourkeys => 1, - twokeys => 1 + Artist => 1, + CD => 1, + FourKeys => 1, + Genre => 1, + Track => 1, + TwoKeys => 1 }, Genre => {}, Image => { - cd_artwork => 1 + Artwork => 1, + CD => 1, + Genre => 1, + Track => 1 }, LinerNotes => { - cd => 1 + CD => 1, + Genre => 1, + Track => 1 }, Link => {}, LyricVersion => { - lyrics => 1 + CD => 1, + Lyrics => 1, + Track => 1 }, Lyrics => { - track => 1 + CD => 1, + Track => 1 }, Money => {}, NoPrimaryKey => {}, @@ -77,23 +98,27 @@ is_deeply($schema->source_tree, { Producer => {}, SelfRef => {}, SelfRefAlias => { - self_ref => 1 + SelfRef => 1 }, SequenceTest => {}, Serialized => {}, SourceNameArtists => {}, Tag => { - cd => 1 + CD => 1, + Genre => 1, + Track => 1 }, TimestampPrimaryKey => {}, Track => { - cd => 1 + CD => 1 }, TreeLike => {}, TwoKeyTreeLike => {}, TwoKeys => { - artist => 1, - cd => 1 + Artist => 1, + CD => 1, + Genre => 1, + Track => 1 }, TypedObject => {} }, 'got correct source tree'); @@ -104,63 +129,78 @@ my $sans_TwoKeys = { Artist => {}, ArtistSubclass => {}, ArtistUndirectedMap => { - artist => 1 + Artist => 1 }, Artwork => { - cd => 1 + CD => 1, + Genre => 1, + Track => 1 }, Artwork_to_Artist => { - artist => 1, - cd_artwork => 1 + Artist => 1, + Artwork => 1, + CD => 1, + Genre => 1, + Track => 1 }, BindType => {}, Bookmark => { - link => 1 + Link => 1 }, BooksInLibrary => { - owners => 1 + Owners => 1 }, CD => { - genre => 1, - track => 1 + Genre => 1, + Track => 1 }, CD_to_Producer => { - cd => 1, - producer => 1 + CD => 1, + Genre => 1, + Producer => 1, + Track => 1 }, Collection => {}, CollectionObject => { - collection => 1, - typed_object => 1 + Collection => 1, + TypedObject => 1 }, CustomSql => {}, Dummy => {}, Employee => { - encoded => 1 + Encoded => 1 }, Encoded => {}, Event => {}, EventTZ => {}, ForceForeign => { - artist => 1 + Artist => 1 }, FourKeys => {}, FourKeys_to_TwoKeys => { - fourkeys => 1 + FourKeys => 1 }, Genre => {}, Image => { - cd_artwork => 1 + Artwork => 1, + CD => 1, + Genre => 1, + Track => 1 }, LinerNotes => { - cd => 1 + CD => 1, + Genre => 1, + Track => 1 }, Link => {}, LyricVersion => { - lyrics => 1 + CD => 1, + Lyrics => 1, + Track => 1 }, Lyrics => { - track => 1 + CD => 1, + Track => 1 }, Money => {}, NoPrimaryKey => {}, @@ -169,17 +209,19 @@ my $sans_TwoKeys = { Producer => {}, SelfRef => {}, SelfRefAlias => { - self_ref => 1 + SelfRef => 1 }, SequenceTest => {}, Serialized => {}, SourceNameArtists => {}, Tag => { - cd => 1 + CD => 1, + Genre => 1, + Track => 1 }, TimestampPrimaryKey => {}, Track => { - cd => 1 + CD => 1 }, TreeLike => {}, TwoKeyTreeLike => {}, @@ -198,6 +240,6 @@ is_deeply( 'got correct source tree with limit_sources => { ... }', ); -# We probably also want a "collapsed" tree +# We probably also want a "collapsed" tree thingy done_testing;