X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FSchema%2FLoader%2FRelBuilder.pm;h=05a1c9f38b9bdc7eb513ba43bf75fa9002044942;hb=eeeab5406ba5610482bfa3220a918363c35ed3e1;hp=4dee45e17c02586f5d2b1927eca6e580e5a6dc0d;hpb=fcf328c7653af502233df3b9a692f38c36c681ec;p=dbsrgits%2FDBIx-Class-Schema-Loader.git diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm index 4dee45e..05a1c9f 100644 --- a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm +++ b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm @@ -6,16 +6,18 @@ use base 'Class::Accessor::Grouped'; use mro 'c3'; use Carp::Clan qw/^DBIx::Class/; use Scalar::Util 'weaken'; +use DBIx::Class::Schema::Loader::Utils qw/split_name slurp_file array_eq/; +use Try::Tiny; +use List::MoreUtils qw/apply uniq any/; +use namespace::clean; use Lingua::EN::Inflect::Phrase (); use Lingua::EN::Tagger (); -use DBIx::Class::Schema::Loader::Utils qw/split_name slurp_file/; -use Try::Tiny; +use String::ToIdentifier::EN (); +use String::ToIdentifier::EN::Unicode (); use Class::Unload (); use Class::Inspector (); -use List::MoreUtils 'apply'; -use namespace::clean; -our $VERSION = '0.07010'; +our $VERSION = '0.07012'; # Glossary: # @@ -46,10 +48,11 @@ Arguments: $loader object Arguments: - { - local_moniker (scalar) => [ fk_info (arrayref), uniq_info (arrayref) ] + [ + [ local_moniker1 (scalar), fk_info1 (arrayref), uniq_info1 (arrayref) ] + [ local_moniker2 (scalar), fk_info2 (arrayref), uniq_info2 (arrayref) ] ... - } + ] This generates the code for the relationships of each table. @@ -58,14 +61,20 @@ statements. The fk_info arrayref's contents should take the form: [ { - local_columns => [ 'col2', 'col3' ], - remote_columns => [ 'col5', 'col7' ], + local_table => 'some_table', + local_moniker => 'SomeTable', + local_columns => [ 'col2', 'col3' ], + remote_table => 'another_table_moniker', remote_moniker => 'AnotherTableMoniker', + remote_columns => [ 'col5', 'col7' ], }, { - local_columns => [ 'col1', 'col4' ], - remote_columns => [ 'col1', 'col2' ], + local_table => 'some_other_table', + local_moniker => 'SomeOtherTable', + local_columns => [ 'col1', 'col4' ], + remote_table => 'yet_another_table_moniker', remote_moniker => 'YetAnotherTableMoniker', + remote_columns => [ 'col1', 'col2' ], }, # ... ], @@ -139,8 +148,8 @@ sub new { # validate the relationship_attrs arg if( defined $self->relationship_attrs ) { - ref $self->relationship_attrs eq 'HASH' - or croak "relationship_attrs must be a hashref"; + ref $self->relationship_attrs eq 'HASH' + or croak "relationship_attrs must be a hashref"; } return $self; @@ -252,22 +261,19 @@ sub _relationship_attrs { ); if( my $specific = $r->{$reltype} ) { - while( my ($k,$v) = each %$specific ) { - $composite{$k} = $v; - } + while( my ($k,$v) = each %$specific ) { + $composite{$k} = $v; + } } return \%composite; } -sub _array_eq { - my ($self, $a, $b) = @_; +sub _strip_id_postfix { + my ($self, $name) = @_; - return unless @$a == @$b; + $name =~ s/_?(?:id|ref|cd|code|num)\z//i; - for (my $i = 0; $i < @$a; $i++) { - return unless $a->[$i] eq $b->[$i]; - } - return 1; + return $name; } sub _remote_attrs { @@ -287,14 +293,9 @@ sub _remote_attrs { sub _sanitize_name { my ($self, $name) = @_; - if (ref $name) { - # scalar ref for weird table name (like one containing a '.') - ($name = $$name) =~ s/\W+/_/g; - } - else { - # remove 'schema.' prefix if any - $name =~ s/^[^.]+\.//; - } + $name = $self->loader->_to_identifier('relationships', $name, '_'); + + $name =~ s/\W+/_/g; # if naming >= 8 to_identifier takes care of it return $name; } @@ -304,7 +305,7 @@ sub _normalize_name { $name = $self->_sanitize_name($name); - my @words = split_name $name; + my @words = split_name $name, $self->loader->_get_naming_v('relationships'); return join '_', map lc, @words; } @@ -317,8 +318,7 @@ sub _remote_relname { # name, to make filter accessors work, but strip trailing _id if(scalar keys %{$cond} == 1) { my ($col) = values %{$cond}; - $col = $self->_normalize_name($col); - $col =~ s/_id$//; + $col = $self->_strip_id_postfix($self->_normalize_name($col)); ($remote_relname) = $self->_inflect_singular($col); } else { @@ -333,7 +333,7 @@ sub _resolve_relname_collision { return $relname if $relname eq 'id'; # this shouldn't happen, but just in case - my $table = $self->loader->tables->{$moniker}; + my $table = $self->loader->moniker_to_table->{$moniker}; if ($self->loader->_is_result_class_method($relname, $table)) { if (my $map = $self->rel_collision_map) { @@ -350,8 +350,7 @@ sub _resolve_relname_collision { } warn <<"EOF"; -Relationship '$relname' in source '$moniker' for columns '@{[ join ',', @$cols ]}' collides with an inherited method. -Renaming to '$new_relname'. +Relationship '$relname' in source '$moniker' for columns '@{[ join ',', @$cols ]}' collides with an inherited method. Renaming to '$new_relname'. See "RELATIONSHIP NAME COLLISIONS" in perldoc DBIx::Class::Schema::Loader::Base . EOF @@ -502,12 +501,57 @@ sub _adjectives { return @adjectives; } +sub _name_to_identifier { + my ($self, $name) = @_; + + my $to_identifier = $self->loader->naming->{force_ascii} ? + \&String::ToIdentifier::EN::to_identifier + : \&String::ToIdentifier::EN::Unicode::to_identifier; + + return join '_', map lc, split_name $to_identifier->($name, '_'); +} + sub _disambiguate { my ($self, $all_rels, $dups) = @_; - foreach my $dup (keys %$dups) { + DUP: foreach my $dup (keys %$dups) { my @rels = @{ $dups->{$dup} }; + # Check if there are rels to the same table name in different + # schemas/databases, if so qualify them. + my @tables = map $self->loader->moniker_to_table->{$_->{extra}{remote_moniker}}, + @rels; + + # databases are different, prepend database + if ($tables[0]->can('database') && (uniq map $_->database||'', @tables) > 1) { + # If any rels are in the same database, we have to distinguish by + # both schema and database. + my %db_counts; + $db_counts{$_}++ for map $_->database, @tables; + my $use_schema = any { $_ > 1 } values %db_counts; + + foreach my $i (0..$#rels) { + my $rel = $rels[$i]; + my $table = $tables[$i]; + + $rel->{args}[0] = $self->_name_to_identifier($table->database) + . ($use_schema ? ('_' . $self->name_to_identifier($table->schema)) : '') + . '_' . $rel->{args}[0]; + } + next DUP; + } + # schemas are different, prepend schema + elsif ((uniq map $_->schema||'', @tables) > 1) { + foreach my $i (0..$#rels) { + my $rel = $rels[$i]; + my $table = $tables[$i]; + + $rel->{args}[0] = $self->_name_to_identifier($table->schema) + . '_' . $rel->{args}[0]; + } + next DUP; + } + foreach my $rel (@rels) { next if $rel->{method} eq 'belongs_to'; @@ -617,10 +661,10 @@ sub _relnames_and_method { 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_relname = $self->_remote_relname( $rel->{remote_table}, $cond); my $local_cols = $rel->{local_columns}; - my $local_table = $self->schema->source($local_moniker)->from; + my $local_table = $rel->{local_table}; my $local_class = $self->schema->class($local_moniker); my $local_source = $self->schema->source($local_moniker); @@ -630,8 +674,8 @@ sub _relnames_and_method { 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) { + if (array_eq([ $local_source->primary_columns ], $local_cols) || + grep { array_eq($_->[1], $local_cols) } @$uniqs) { $remote_method = 'might_have'; ($local_relname) = $self->_inflect_singular($local_relname_uninflected); } @@ -661,7 +705,7 @@ sub _relnames_and_method { my $rel_cols = [ sort { $a cmp $b } apply { s/^foreign\.//i } (keys %{ $class->relationship_info($local_relname)->{cond} }) ]; - $relationship_exists = 1 if $self->_array_eq([ sort @$local_cols ], $rel_cols); + $relationship_exists = 1 if array_eq([ sort @$local_cols ], $rel_cols); } } @@ -669,8 +713,7 @@ sub _relnames_and_method { my $colnames = q{_} . $self->_normalize_name(join '_', @$local_cols); $remote_relname .= $colnames if keys %$cond > 1; - $local_relname = $self->_normalize_name($local_table . $colnames); - $local_relname =~ s/_id$//; + $local_relname = $self->_strip_id_postfix($self->_normalize_name($local_table . $colnames)); $local_relname_uninflected = $local_relname; ($local_relname) = $self->_inflect_plural($local_relname);