From: Robert Buels Date: Sat, 20 Feb 2010 06:18:19 +0000 (-0800) Subject: rearrange relbuilder methods and interface to backcompat X-Git-Tag: 0.06000~90^2~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=057fbb08b497a9054b86f00f4c8e47ea0e8feedf;p=dbsrgits%2FDBIx-Class-Schema-Loader.git rearrange relbuilder methods and interface to backcompat --- diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm index ef387bd..72a567e 100644 --- a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm +++ b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm @@ -156,22 +156,6 @@ sub _array_eq { return 1; } -sub _uniq_fk_rel { - my ($self, $local_moniker, $local_relname, $local_cols, $uniqs) = @_; - - my $remote_method = 'has_many'; - - # If the local columns have a UNIQUE constraint, this is a one-to-one rel - my $local_source = $self->{schema}->source($local_moniker); - 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); - } - - return ($remote_method, $local_relname); -} - sub _remote_attrs { my ($self, $local_moniker, $local_cols) = @_; @@ -211,9 +195,8 @@ sub generate_code { my $all_code = {}; - my $local_table = $self->{schema}->source($local_moniker)->from; my $local_class = $self->{schema}->class($local_moniker); - + my %counters; foreach my $rel (@$rels) { next if !$rel->{remote_source}; @@ -221,14 +204,14 @@ sub generate_code { } foreach my $rel (@$rels) { - next if !$rel->{remote_source}; - my $local_cols = $rel->{local_columns}; - my $remote_cols = $rel->{remote_columns}; - my $remote_moniker = $rel->{remote_source}; - my $remote_obj = $self->{schema}->source($remote_moniker); - my $remote_class = $self->{schema}->class($remote_moniker); - my $remote_table = $remote_obj->from; - $remote_cols ||= [ $remote_obj->primary_columns ]; + my $remote_moniker = $rel->{remote_source} + or next; + + my $remote_class = $self->{schema}->class($remote_moniker); + my $remote_obj = $self->{schema}->source($remote_moniker); + my $remote_cols = $rel->{remote_columns} || [ $remote_obj->primary_columns ]; + + my $local_cols = $rel->{local_columns}; if($#$local_cols != $#$remote_cols) { croak "Column count mismatch: $local_moniker (@$local_cols) " @@ -240,35 +223,8 @@ sub generate_code { $cond{$remote_cols->[$i]} = $local_cols->[$i]; } - my $local_relname; - my $remote_relname = $self->_remote_relname($remote_table, \%cond); - - # If more than one rel between this pair of tables, use the local - # col names to distinguish - if($counters{$remote_moniker} > 1) { - my $colnames = q{_} . join(q{_}, @$local_cols); - $remote_relname .= $colnames if keys %cond > 1; - - my $is_singular = - ($self->_uniq_fk_rel($local_moniker, 'dummy', $local_cols, $uniqs))[0] ne 'has_many'; - - $local_relname = $self->_multi_rel_local_relname( - $remote_class, $local_table, $local_cols, $is_singular - ); - } else { - $local_relname = $self->_inflect_plural(lc $local_table); - } - - my %rev_cond = reverse %cond; - - for (keys %rev_cond) { - $rev_cond{"foreign.$_"} = "self.".$rev_cond{$_}; - delete $rev_cond{$_}; - } - - my ($remote_method); - - ($remote_method, $local_relname) = $self->_uniq_fk_rel($local_moniker, $local_relname, $local_cols, $uniqs); + my ( $local_relname, $remote_relname, $remote_method ) = + $self->_relnames_and_methods( $local_moniker, $rel, \%cond, $uniqs, \%counters ); push(@{$all_code->{$local_class}}, { method => 'belongs_to', @@ -280,6 +236,12 @@ sub generate_code { } ); + my %rev_cond = reverse %cond; + for (keys %rev_cond) { + $rev_cond{"foreign.$_"} = "self.".$rev_cond{$_}; + delete $rev_cond{$_}; + } + push(@{$all_code->{$remote_class}}, { method => $remote_method, args => [ $local_relname, @@ -294,24 +256,49 @@ sub generate_code { return $all_code; } -sub _multi_rel_local_relname { - my ($self, $remote_class, $local_table, $local_cols, $is_singular) = @_; +sub _relnames_and_methods { + my ( $self, $local_moniker, $rel, $cond, $uniqs, $counters ) = @_; - my $inflect = $is_singular ? '_inflect_singular' : '_inflect_plural'; - $inflect = $self->can($inflect); + 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 $colnames = q{_} . join(q{_}, @$local_cols); - my $old_relname = #< TODO: remove me after 0.05003 release - my $local_relname = lc($local_table) . $colnames; - my $stripped_id = $local_relname =~ s/_id$//; #< strip off any trailing _id - $local_relname = $self->$inflect( $local_relname ); + my $local_cols = $rel->{local_columns}; + my $local_table = $self->{schema}->source($local_moniker)->from; + + # If more than one rel between this pair of tables, use the local + # col names to distinguish + my $local_relname; + if ( $counters->{$remote_moniker} > 1) { + my $colnames = q{_} . join(q{_}, @$local_cols); + $remote_relname .= $colnames if keys %$cond > 1; + + my $old_relname = #< TODO: remove me after 0.05003 release + my $local_relname = lc($local_table) . $colnames; + my $stripped_id = $local_relname =~ s/_id$//; #< strip off any trailing _id + $local_relname = $self->_inflect_plural( $local_relname ); + + # TODO: remove me after 0.05003 release + $old_relname = $self->_inflect_plural( $old_relname ); + warn __PACKAGE__." $VERSION: warning, stripping trailing _id from ${remote_class} relation '$old_relname', renaming to '$local_relname'. This behavior is new as of 0.05003.\n" + if $stripped_id; + + } else { + $local_relname = $self->_inflect_plural(lc $local_table); + } - # TODO: remove me after 0.05003 release - $old_relname = $self->$inflect( $old_relname ); - warn __PACKAGE__." $VERSION: warning, stripping trailing _id from ${remote_class} relation '$old_relname', renaming to '$local_relname'. This behavior is new as of 0.05003.\n" - if $stripped_id; + my $remote_method = 'has_many'; + + # If the local columns have a UNIQUE constraint, this is a one-to-one rel + my $local_source = $self->{schema}->source($local_moniker); + 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); + } - return $local_relname; + return ( $local_relname, $remote_relname, $remote_method ); } =head1 AUTHOR diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_040.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_040.pm index f3e46ed..f96ba8a 100644 --- a/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_040.pm +++ b/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_040.pm @@ -6,38 +6,36 @@ use Class::C3; use base 'DBIx::Class::Schema::Loader::RelBuilder'; -sub _uniq_fk_rel { - my ($self, $local_moniker, $local_relname, $local_cols, $uniqs) = @_; - - return ('has_many', $local_relname); -} - -sub _remote_attrs { } - -sub _remote_relname { - my ($self, $remote_table, $cond) = @_; - - my $remote_relname; - # for single-column case, set the remote relname to the column - # name, to make filter accessors work - if(scalar keys %{$cond} == 1) { - $remote_relname = $self->_inflect_singular(values %{$cond}); - } - else { - $remote_relname = $self->_inflect_singular(lc $remote_table); +sub _relnames_and_methods { + my ( $self, $local_moniker, $rel, $cond, $uniqs, $counters ) = @_; + + my $remote_moniker = $rel->{remote_source}; + my $remote_table = $self->{schema}->source( $remote_moniker )->from; + + my $local_table = $self->{schema}->source($local_moniker)->from; + my $local_cols = $rel->{local_columns}; + + # for single-column case, set the remote relname to just the column name + my $remote_relname = + scalar keys %{$cond} == 1 + ? $self->_inflect_singular( values %$cond ) + : $self->_inflect_singular( lc $remote_table ); + + # If more than one rel between this pair of tables, use the local + # col names to distinguish + my $local_relname; + if ($counters->{$remote_moniker} > 1) { + my $colnames = '_' . join( '_', @$local_cols ); + $remote_relname .= $colnames if keys %$cond > 1; + $local_relname = $self->_inflect_plural( lc($local_table) . $colnames ); + } else { + $local_relname = $self->_inflect_plural(lc $local_table); } - return $remote_relname; + return ( $local_relname, $remote_relname, 'has_many' ); } -sub _multi_rel_local_relname { - my ($self, $remote_class, $local_table, $local_cols) = @_; - - my $colnames = q{_} . join(q{_}, @$local_cols); - my $local_relname = $self->_inflect_plural( lc($local_table) . $colnames ); - - return $local_relname; -} +sub _remote_attrs { } 1;