From: Rafael Kitover Date: Tue, 14 Jun 2011 04:37:54 +0000 (-0400) Subject: add rel_name_map option X-Git-Tag: 0.07011~73 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=59388920507cf773795544ab948421ec58e20d1f;p=dbsrgits%2FDBIx-Class-Schema-Loader.git add rel_name_map option --- diff --git a/Changes b/Changes index b5bee8c..1fd64b5 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,6 @@ Revision history for Perl extension DBIx::Class::Schema::Loader + - add rel_name_map option - fix the decimal data type for MS Access over ODBC - fix enum/set detection for MySQL (RT#68717) - fix is_nullable detection on MS Access diff --git a/lib/DBIx/Class/Schema/Loader.pm b/lib/DBIx/Class/Schema/Loader.pm index abceb51..4338034 100644 --- a/lib/DBIx/Class/Schema/Loader.pm +++ b/lib/DBIx/Class/Schema/Loader.pm @@ -488,6 +488,8 @@ arcanez: Justin Hunter ash: Ash Berlin +Ben Tilly + Caelum: Rafael Kitover TSUNODA Kazuya diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index e4b1265..e5199ba 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -92,6 +92,7 @@ __PACKAGE__->mk_group_accessors('simple', qw/ preserve_case col_collision_map rel_collision_map + rel_name_map real_dump_directory result_components_map result_roles_map @@ -351,6 +352,43 @@ passed, the code is called with arguments of column_info => hashref of column info (data_type, is_nullable, etc), } +=head2 rel_name_map + +Similar in idea to moniker_map, but different in the details. It can be +a hashref or a code ref. + +If it is a hashref, keys can be either the default relationship name, or the +moniker. The keys that are the default relationship name should map to the +name you want to change the relationship to. Keys that are monikers should map +to hashes mapping relationship names to their translation. You can do both at +once, and the more specific moniker version will be picked up first. So, for +instance, you could have + + { + bar => "baz", + Foo => { + bar => "blat", + }, + } + +and relationships that would have been named C will now be named C +except that in the table whose moniker is C it will be named C. + +If it is a coderef, the argument passed will be a hashref of this form: + + { + name => default relationship name, + type => the relationship type eg: C, + local_class => name of the DBIC class we are building, + local_moniker => moniker of the DBIC class we are building, + local_columns => columns in this table in the relationship, + remote_class => name of the DBIC class we are related to, + remote_moniker => moniker of the DBIC class we are related to, + remote_columns => columns in the other table in the relationship, + } + +DBICSL will try to use the value returned as the relationship name. + =head2 inflect_plural Just like L above (can be hash/code-ref, falls back to default @@ -784,6 +822,13 @@ sub new { } } + if (defined(my $rel_name_map = $self->rel_name_map)) { + my $reftype = ref $rel_name_map; + if ($reftype ne 'HASH' && $reftype ne 'CODE') { + croak "Invalid type $reftype for option 'rel_name_map', must be HASH or CODE"; + } + } + $self; } @@ -1839,8 +1884,7 @@ EOF } } -# use the same logic to run moniker_map, col_accessor_map, and -# relationship_name_map +# use the same logic to run moniker_map, col_accessor_map sub _run_user_map { my ( $self, $map, $default_code, $ident, @extra ) = @_; diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm index 4cfd707..64d9366 100644 --- a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm +++ b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm @@ -106,6 +106,7 @@ __PACKAGE__->mk_group_accessors('simple', qw/ inflect_singular relationship_attrs rel_collision_map + rel_name_map _temp_classes __tagger /); @@ -129,6 +130,7 @@ sub new { inflect_singular => $base->inflect_singular, relationship_attrs => $base->relationship_attrs, rel_collision_map => $base->rel_collision_map, + rel_name_map => $base->rel_name_map, _temp_classes => [], }; @@ -152,16 +154,26 @@ sub _inflect_plural { return '' if !defined $relname || $relname eq ''; + my $result; + my $mapped = 0; + if( ref $self->inflect_plural eq 'HASH' ) { - return $self->inflect_plural->{$relname} - if exists $self->inflect_plural->{$relname}; + if (exists $self->inflect_plural->{$relname}) { + $result = $self->inflect_plural->{$relname}; + $mapped = 1; + } } elsif( ref $self->inflect_plural eq 'CODE' ) { my $inflected = $self->inflect_plural->($relname); - return $inflected if $inflected; + if ($inflected) { + $result = $inflected; + $mapped = 1; + } } - return $self->_to_PL($relname); + return ($result, $mapped) if $mapped; + + return ($self->_to_PL($relname), 0); } # Singularize a relationship name @@ -170,16 +182,26 @@ sub _inflect_singular { return '' if !defined $relname || $relname eq ''; + my $result; + my $mapped = 0; + if( ref $self->inflect_singular eq 'HASH' ) { - return $self->inflect_singular->{$relname} - if exists $self->inflect_singular->{$relname}; + if (exists $self->inflect_singular->{$relname}) { + $result = $self->inflect_singular->{$relname}; + $mapped = 1; + } } elsif( ref $self->inflect_singular eq 'CODE' ) { my $inflected = $self->inflect_singular->($relname); - return $inflected if $inflected; + if ($inflected) { + $result = $inflected; + $mapped = 1; + } } - return $self->_to_S($relname); + return ($result, $mapped) if $mapped; + + return ($self->_to_S($relname), 0); } sub _to_PL { @@ -298,10 +320,10 @@ sub _remote_relname { my ($col) = values %{$cond}; $col = $self->_normalize_name($col); $col =~ s/_id$//; - $remote_relname = $self->_inflect_singular($col); + ($remote_relname) = $self->_inflect_singular($col); } else { - $remote_relname = $self->_inflect_singular($self->_normalize_name($remote_table)); + ($remote_relname) = $self->_inflect_singular($self->_normalize_name($remote_table)); } return $remote_relname; @@ -379,19 +401,25 @@ sub generate_code { my ( $local_relname, $remote_relname, $remote_method ) = $self->_relnames_and_method( $local_moniker, $rel, \%cond, $uniqs, \%counters ); + my $local_method = 'belongs_to'; + + ($remote_relname) = $self->_rel_name_map($remote_relname, $local_method, $local_class, $local_moniker, $local_cols, $remote_class, $remote_moniker, $remote_cols); + ($local_relname) = $self->_rel_name_map($local_relname, $remote_method, $remote_class, $remote_moniker, $remote_cols, $local_class, $local_moniker, $local_cols); - $remote_relname = $self->_resolve_relname_collision($local_moniker, $local_cols, $remote_relname); - $local_relname = $self->_resolve_relname_collision($remote_moniker, $remote_cols, $local_relname); + $remote_relname = $self->_resolve_relname_collision($local_moniker, $local_cols, $remote_relname); + $local_relname = $self->_resolve_relname_collision($remote_moniker, $remote_cols, $local_relname); push(@{$all_code->{$local_class}}, - { method => 'belongs_to', + { method => $local_method, args => [ $remote_relname, $remote_class, \%cond, $self->_remote_attrs($local_moniker, $local_cols), ], extra => { - moniker => $local_moniker, + local_class => $local_class, + local_moniker => $local_moniker, + remote_moniker => $remote_moniker, }, } ); @@ -410,7 +438,9 @@ sub generate_code { $self->_relationship_attrs($remote_method), ], extra => { - moniker => $remote_moniker, + local_class => $remote_class, + local_moniker => $remote_moniker, + remote_moniker => $local_moniker, }, } ); @@ -502,17 +532,21 @@ sub _disambiguate { if (@adjectives) { my $rel_name = join '_', sort(@adjectives), $rel->{args}[0]; - $rel_name = $rel->{method} eq 'might_have' ? + ($rel_name) = $rel->{method} eq 'might_have' ? $self->_inflect_singular($rel_name) : $self->_inflect_plural($rel_name); - my $moniker = $rel->{extra}{moniker}; + my ($local_class, $local_moniker, $remote_moniker) + = @{ $rel->{extra} } + {qw/local_class local_moniker remote_moniker/}; my @from_cols = apply { s/^self\.//i } values %{ $rel->{args}[2] }; - $rel_name = $self->_resolve_relname_collision($moniker, \@from_cols, $rel_name); + ($rel_name) = $self->_rel_name_map($rel_name, $rel->{method}, $local_class, $local_moniker, \@from_cols, $to_class, $remote_moniker, \@to_cols); + + $rel_name = $self->_resolve_relname_collision($local_moniker, \@from_cols, $rel_name); $rel->{args}[0] = $rel_name; } @@ -538,24 +572,39 @@ sub _disambiguate { my $inflect_method = "_$inflect_type"; - my $relname_new_uninflected = - $self->_inflect_singular($rel->{args}[0]) . "_$rel_num"; + my $relname_new_uninflected = $rel->{args}[0] . "_$rel_num"; $rel_num++; - my $relname_new = $self->$inflect_method($relname_new_uninflected); - - my $moniker = $rel->{extra}{moniker}; + my ($local_class, $local_moniker, $remote_moniker) + = @{ $rel->{extra} } + {qw/local_class local_moniker remote_moniker/}; my @from_cols = apply { s/^self\.//i } values %{ $rel->{args}[2] }; - warn <<"EOF"; -Could not find a proper name for relationship '$relname_new' in source '$moniker' for columns '@{[ join ',', @from_cols ]}'. -Supply a value in '$inflect_type' for '$relname_new_uninflected' to name this relationship. + my @to_cols = apply { s/^foreign\.//i } + keys %{ $rel->{args}[2] }; + + my $to_class = $rel->{args}[1]; + + my ($relname_new, $inflect_mapped) = + $self->$inflect_method($relname_new_uninflected); + + my $rel_name_mapped; + + ($relname_new, $rel_name_mapped) = $self->_rel_name_map($relname_new, $rel->{method}, $local_class, $local_moniker, \@from_cols, $to_class, $remote_moniker, \@to_cols); + + my $mapped = $inflect_mapped || $rel_name_mapped; + + warn <<"EOF" unless $mapped; +Could not find a proper name for relationship '$relname_new' in source +'$local_moniker' for columns '@{[ join ',', @from_cols ]}'. Supply a value in +'$inflect_type' or 'rel_name_map' for '$relname_new_uninflected' to name this +relationship. EOF - $relname_new = $self->_resolve_relname_collision($moniker, \@from_cols, $relname_new); + $relname_new = $self->_resolve_relname_collision($local_moniker, \@from_cols, $relname_new); $rel->{args}[0] = $relname_new; } @@ -566,26 +615,26 @@ EOF sub _relnames_and_method { my ( $self, $local_moniker, $rel, $cond, $uniqs, $counters ) = @_; - my $remote_moniker = $rel->{remote_source}; - my $remote_obj = $self->schema->source( $remote_moniker ); - my $remote_class = $self->schema->class( $remote_moniker ); - my $remote_relname = $self->_remote_relname( $remote_obj->from, $cond); + my $remote_moniker = $rel->{remote_source}; + my $remote_obj = $self->schema->source( $remote_moniker ); + my $remote_class = $self->schema->class( $remote_moniker ); + my $remote_relname = $self->_remote_relname( $remote_obj->from, $cond); - my $local_cols = $rel->{local_columns}; - my $local_table = $self->schema->source($local_moniker)->from; - my $local_class = $self->schema->class($local_moniker); - my $local_source = $self->schema->source($local_moniker); + my $local_cols = $rel->{local_columns}; + my $local_table = $self->schema->source($local_moniker)->from; + my $local_class = $self->schema->class($local_moniker); + my $local_source = $self->schema->source($local_moniker); my $local_relname_uninflected = $self->_normalize_name($local_table); - my $local_relname = $self->_inflect_plural($self->_normalize_name($local_table)); + my ($local_relname) = $self->_inflect_plural($self->_normalize_name($local_table)); my $remote_method = 'has_many'; # If the local columns have a UNIQUE constraint, this is a one-to-one rel if ($self->_array_eq([ $local_source->primary_columns ], $local_cols) || grep { $self->_array_eq($_->[1], $local_cols) } @$uniqs) { - $remote_method = 'might_have'; - $local_relname = $self->_inflect_singular($local_relname_uninflected); + $remote_method = 'might_have'; + ($local_relname) = $self->_inflect_singular($local_relname_uninflected); } # If more than one rel between this pair of tables, use the local @@ -625,16 +674,61 @@ sub _relnames_and_method { $local_relname =~ s/_id$//; $local_relname_uninflected = $local_relname; - $local_relname = $self->_inflect_plural($local_relname); + ($local_relname) = $self->_inflect_plural($local_relname); # if colnames were added and this is a might_have, re-inflect if ($remote_method eq 'might_have') { - $local_relname = $self->_inflect_singular($local_relname_uninflected); + ($local_relname) = $self->_inflect_singular($local_relname_uninflected); } } } - return ( $local_relname, $remote_relname, $remote_method ); + return ($local_relname, $remote_relname, $remote_method); +} + +sub _rel_name_map { + my ($self, $relname, $method, $local_class, $local_moniker, $local_cols, + $remote_class, $remote_moniker, $remote_cols) = @_; + + my $info = { + name => $relname, + type => $method, + local_class => $local_class, + local_moniker => $local_moniker, + local_columns => $local_cols, + remote_class => $remote_class, + remote_moniker => $remote_moniker, + remote_columns => $remote_cols, + }; + + my $new_name = $relname; + + my $map = $self->rel_name_map; + my $mapped = 0; + + if ('HASH' eq ref($map)) { + my $name = $info->{name}; + my $moniker = $info->{local_moniker}; + if ($map->{$moniker} and 'HASH' eq ref($map->{$moniker}) + and $map->{$moniker}{$name} + ) { + $new_name = $map->{$moniker}{$name}; + $mapped = 1; + } + elsif ($map->{$name} and not 'HASH' eq ref($map->{$name})) { + $new_name = $map->{$name}; + $mapped = 1; + } + } + elsif ('CODE' eq ref($map)) { + my $name = $map->($info); + if ($name) { + $new_name = $name; + $mapped = 1; + } + } + + return ($new_name, $mapped); } sub _cleanup { diff --git a/t/45relationships.t b/t/45relationships.t index f3ae061..b2eb18a 100644 --- a/t/45relationships.t +++ b/t/45relationships.t @@ -1,11 +1,13 @@ use strict; -use Test::More tests => 6; +use Test::More tests => 12; use Test::Exception; use lib qw(t/lib); use make_dbictest_db; use DBIx::Class::Schema::Loader; +my $schema_counter = 0; + # test skip_relationships my $regular = schema_with(); is( ref($regular->source('Bar')->relationship_info('fooref')), 'HASH', @@ -16,6 +18,82 @@ is_deeply( $skip_rel->source('Bar')->relationship_info('fooref'), undef, 'skip_relationships blocks generation of fooref rel', ); +# test hashref as rel_name_map +my $hash_relationship = schema_with( + rel_name_map => { + fooref => "got_fooref", + bars => "ignored", + Foo => { + bars => "got_bars", + fooref => "ignored", + }, + } +); +is( ref($hash_relationship->source('Foo')->relationship_info('got_bars')), + 'HASH', + 'single level hash in rel_name_map picked up correctly' + ); +is( ref($hash_relationship->source('Bar')->relationship_info('got_fooref')), + 'HASH', + 'double level hash in rel_name_map picked up correctly' + ); + +# test coderef as rel_name_map +my $code_relationship = schema_with( + rel_name_map => sub { + my ($args) = @_; + + if ($args->{local_moniker} eq 'Foo') { + is_deeply( + $args, + { + name => 'bars', + type => 'has_many', + local_class => + "DBICTest::Schema::${schema_counter}::Result::Foo", + local_moniker => 'Foo', + local_columns => ['fooid'], + remote_class => + "DBICTest::Schema::${schema_counter}::Result::Bar", + remote_moniker => 'Bar', + remote_columns => ['fooref'], + }, + 'correct args for Foo passed' + ); + return 'bars_caught'; + } + elsif ($args->{local_moniker} eq 'Bar') { + is_deeply( + $args, + { + name => 'fooref', + type => 'belongs_to', + local_class => + "DBICTest::Schema::${schema_counter}::Result::Bar", + local_moniker => 'Bar', + local_columns => ['fooref'], + remote_class => + "DBICTest::Schema::${schema_counter}::Result::Foo", + remote_moniker => 'Foo', + remote_columns => ['fooid'], + }, + 'correct args for Foo passed' + ); + + return 'fooref_caught'; + } + } + ); +is( ref($code_relationship->source('Foo')->relationship_info('bars_caught')), + 'HASH', + 'rel_name_map overrode local_info correctly' + ); +is( ref($code_relationship->source('Bar')->relationship_info('fooref_caught')), + 'HASH', + 'rel_name_map overrode remote_info correctly' + ); + + # test relationship_attrs throws_ok { @@ -46,9 +124,7 @@ throws_ok { ); } - #### generates a new schema with the given opts every time it's called -my $schema_counter = 0; sub schema_with { $schema_counter++; DBIx::Class::Schema::Loader::make_schema_at(