fully implement naming=v8 and force_ascii
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / RelBuilder.pm
1 package DBIx::Class::Schema::Loader::RelBuilder;
2
3 use strict;
4 use warnings;
5 use base 'Class::Accessor::Grouped';
6 use mro 'c3';
7 use Carp::Clan qw/^DBIx::Class/;
8 use Scalar::Util 'weaken';
9 use DBIx::Class::Schema::Loader::Utils qw/split_name slurp_file/;
10 use Try::Tiny;
11 use List::MoreUtils qw/apply uniq any/;
12 use namespace::clean;
13 use Lingua::EN::Inflect::Phrase ();
14 use Lingua::EN::Tagger ();
15 use String::ToIdentifier::EN ();
16 use String::ToIdentifier::EN::Unicode ();
17 use Class::Unload ();
18 use Class::Inspector ();
19
20 our $VERSION = '0.07010';
21
22 # Glossary:
23 #
24 # remote_relname -- name of relationship from the local table referring to the remote table
25 # local_relname  -- name of relationship from the remote table referring to the local table
26 # remote_method  -- relationship type from remote table to local table, usually has_many
27
28 =head1 NAME
29
30 DBIx::Class::Schema::Loader::RelBuilder - Builds relationships for DBIx::Class::Schema::Loader
31
32 =head1 SYNOPSIS
33
34 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
35
36 =head1 DESCRIPTION
37
38 This class builds relationships for L<DBIx::Class::Schema::Loader>.  This
39 is module is not (yet) for external use.
40
41 =head1 METHODS
42
43 =head2 new
44
45 Arguments: $loader object
46
47 =head2 generate_code
48
49 Arguments: 
50     
51     [
52         [ local_moniker1 (scalar), fk_info1 (arrayref), uniq_info1 (arrayref) ]
53         [ local_moniker2 (scalar), fk_info2 (arrayref), uniq_info2 (arrayref) ]
54         ...
55     ]
56
57 This generates the code for the relationships of each table.
58
59 C<local_moniker> is the moniker name of the table which had the REFERENCES
60 statements.  The fk_info arrayref's contents should take the form:
61
62     [
63         {
64             local_table    => 'some_table',
65             local_moniker  => 'SomeTable',
66             local_columns  => [ 'col2', 'col3' ],
67             remote_table   => 'another_table_moniker',
68             remote_moniker => 'AnotherTableMoniker',
69             remote_columns => [ 'col5', 'col7' ],
70         },
71         {
72             local_table    => 'some_other_table',
73             local_moniker  => 'SomeOtherTable',
74             local_columns  => [ 'col1', 'col4' ],
75             remote_table   => 'yet_another_table_moniker',
76             remote_moniker => 'YetAnotherTableMoniker',
77             remote_columns => [ 'col1', 'col2' ],
78         },
79         # ...
80     ],
81
82 The uniq_info arrayref's contents should take the form:
83
84     [
85         [
86             uniq_constraint_name         => [ 'col1', 'col2' ],
87         ],
88         [
89             another_uniq_constraint_name => [ 'col1', 'col2' ],
90         ],
91     ],
92
93 This method will return the generated relationships as a hashref keyed on the
94 class names.  The values are arrayrefs of hashes containing method name and
95 arguments, like so:
96
97   {
98       'Some::Source::Class' => [
99           { method => 'belongs_to', arguments => [ 'col1', 'Another::Source::Class' ],
100           { method => 'has_many', arguments => [ 'anothers', 'Yet::Another::Source::Class', 'col15' ],
101       ],
102       'Another::Source::Class' => [
103           # ...
104       ],
105       # ...
106   }
107
108 =cut
109
110 __PACKAGE__->mk_group_accessors('simple', qw/
111     loader
112     schema
113     inflect_plural
114     inflect_singular
115     relationship_attrs
116     rel_collision_map
117     rel_name_map
118     _temp_classes
119     __tagger
120 /);
121
122 sub new {
123     my ($class, $loader) = @_;
124
125     # from old POD about this constructor:
126     # C<$schema_class> should be a schema class name, where the source
127     # classes have already been set up and registered.  Column info,
128     # primary key, and unique constraints will be drawn from this
129     # schema for all of the existing source monikers.
130
131     # Options inflect_plural and inflect_singular are optional, and
132     # are better documented in L<DBIx::Class::Schema::Loader::Base>.
133
134     my $self = {
135         loader             => $loader,
136         schema             => $loader->schema,
137         inflect_plural     => $loader->inflect_plural,
138         inflect_singular   => $loader->inflect_singular,
139         relationship_attrs => $loader->relationship_attrs,
140         rel_collision_map  => $loader->rel_collision_map,
141         rel_name_map       => $loader->rel_name_map,
142         _temp_classes      => [],
143     };
144
145     weaken $self->{loader}; #< don't leak
146
147     bless $self => $class;
148
149     # validate the relationship_attrs arg
150     if( defined $self->relationship_attrs ) {
151         ref $self->relationship_attrs eq 'HASH'
152             or croak "relationship_attrs must be a hashref";
153     }
154
155     return $self;
156 }
157
158
159 # pluralize a relationship name
160 sub _inflect_plural {
161     my ($self, $relname) = @_;
162
163     return '' if !defined $relname || $relname eq '';
164
165     my $result;
166     my $mapped = 0;
167
168     if( ref $self->inflect_plural eq 'HASH' ) {
169         if (exists $self->inflect_plural->{$relname}) {
170             $result = $self->inflect_plural->{$relname};
171             $mapped = 1;
172         }
173     }
174     elsif( ref $self->inflect_plural eq 'CODE' ) {
175         my $inflected = $self->inflect_plural->($relname);
176         if ($inflected) {
177             $result = $inflected;
178             $mapped = 1;
179         }
180     }
181
182     return ($result, $mapped) if $mapped;
183
184     return ($self->_to_PL($relname), 0);
185 }
186
187 # Singularize a relationship name
188 sub _inflect_singular {
189     my ($self, $relname) = @_;
190
191     return '' if !defined $relname || $relname eq '';
192
193     my $result;
194     my $mapped = 0;
195
196     if( ref $self->inflect_singular eq 'HASH' ) {
197         if (exists $self->inflect_singular->{$relname}) {
198             $result = $self->inflect_singular->{$relname};
199             $mapped = 1;
200         }
201     }
202     elsif( ref $self->inflect_singular eq 'CODE' ) {
203         my $inflected = $self->inflect_singular->($relname);
204         if ($inflected) {
205             $result = $inflected;
206             $mapped = 1;
207         }
208     }
209
210     return ($result, $mapped) if $mapped;
211
212     return ($self->_to_S($relname), 0);
213 }
214
215 sub _to_PL {
216     my ($self, $name) = @_;
217
218     $name =~ s/_/ /g;
219     my $plural = Lingua::EN::Inflect::Phrase::to_PL($name);
220     $plural =~ s/ /_/g;
221
222     return $plural;
223 }
224
225 sub _to_S {
226     my ($self, $name) = @_;
227
228     $name =~ s/_/ /g;
229     my $singular = Lingua::EN::Inflect::Phrase::to_S($name);
230     $singular =~ s/ /_/g;
231
232     return $singular;
233 }
234
235 sub _default_relationship_attrs { +{
236     has_many => {
237         cascade_delete => 0,
238         cascade_copy   => 0,
239     },
240     might_have => {
241         cascade_delete => 0,
242         cascade_copy   => 0,
243     },
244     belongs_to => {
245         on_delete => 'CASCADE',
246         on_update => 'CASCADE',
247         is_deferrable => 1,
248     },
249 } }
250
251 # accessor for options to be passed to each generated relationship
252 # type.  take single argument, the relationship type name, and returns
253 # either a hashref (if some options are set), or nothing
254 sub _relationship_attrs {
255     my ( $self, $reltype ) = @_;
256     my $r = $self->relationship_attrs;
257
258     my %composite = (
259         %{ $self->_default_relationship_attrs->{$reltype} || {} },
260         %{ $r->{all} || {} }
261     );
262
263     if( my $specific = $r->{$reltype} ) {
264         while( my ($k,$v) = each %$specific ) {
265             $composite{$k} = $v;
266         }
267     }
268     return \%composite;
269 }
270
271 sub _strip_id_postfix {
272     my ($self, $name) = @_;
273
274     $name =~ s/_?(?:id|ref|cd|code|num)\z//i;
275
276     return $name;
277 }
278
279 sub _array_eq {
280     my ($self, $a, $b) = @_;
281
282     return unless @$a == @$b;
283
284     for (my $i = 0; $i < @$a; $i++) {
285         return unless $a->[$i] eq $b->[$i];
286     }
287     return 1;
288 }
289
290 sub _remote_attrs {
291     my ($self, $local_moniker, $local_cols) = @_;
292
293     # get our base set of attrs from _relationship_attrs, if present
294     my $attrs = $self->_relationship_attrs('belongs_to') || {};
295
296     # If the referring column is nullable, make 'belongs_to' an
297     # outer join, unless explicitly set by relationship_attrs
298     my $nullable = grep { $self->schema->source($local_moniker)->column_info($_)->{is_nullable} } @$local_cols;
299     $attrs->{join_type} = 'LEFT' if $nullable && !defined $attrs->{join_type};
300
301     return $attrs;
302 }
303
304 sub _sanitize_name {
305     my ($self, $name) = @_;
306
307     $name = $self->loader->_to_identifier('relationships', $name, '_');
308
309     $name =~ s/\W+/_/g; # if naming >= 8 to_identifier takes care of it
310
311     return $name;
312 }
313
314 sub _normalize_name {
315     my ($self, $name) = @_;
316
317     $name = $self->_sanitize_name($name);
318
319     my @words = split_name $name;
320
321     return join '_', map lc, @words;
322 }
323
324 sub _remote_relname {
325     my ($self, $remote_table, $cond) = @_;
326
327     my $remote_relname;
328     # for single-column case, set the remote relname to the column
329     # name, to make filter accessors work, but strip trailing _id
330     if(scalar keys %{$cond} == 1) {
331         my ($col) = values %{$cond};
332         $col = $self->_strip_id_postfix($self->_normalize_name($col));
333         ($remote_relname) = $self->_inflect_singular($col);
334     }
335     else {
336         ($remote_relname) = $self->_inflect_singular($self->_normalize_name($remote_table));
337     }
338
339     return $remote_relname;
340 }
341
342 sub _resolve_relname_collision {
343     my ($self, $moniker, $cols, $relname) = @_;
344
345     return $relname if $relname eq 'id'; # this shouldn't happen, but just in case
346
347     my $table = $self->loader->moniker_to_table->{$moniker};
348
349     if ($self->loader->_is_result_class_method($relname, $table)) {
350         if (my $map = $self->rel_collision_map) {
351             for my $re (keys %$map) {
352                 if (my @matches = $relname =~ /$re/) {
353                     return sprintf $map->{$re}, @matches;
354                 }
355             }
356         }
357
358         my $new_relname = $relname;
359         while ($self->loader->_is_result_class_method($new_relname, $table)) {
360             $new_relname .= '_rel'
361         }
362
363         warn <<"EOF";
364 Relationship '$relname' in source '$moniker' for columns '@{[ join ',', @$cols ]}' collides with an inherited method. Renaming to '$new_relname'.
365 See "RELATIONSHIP NAME COLLISIONS" in perldoc DBIx::Class::Schema::Loader::Base .
366 EOF
367
368         return $new_relname;
369     }
370
371     return $relname;
372 }
373
374 sub generate_code {
375     my ($self, $tables) = @_;
376     
377     # make a copy to destroy
378     my @tables = @$tables;
379
380     my $all_code = {};
381
382     while (my ($local_moniker, $rels, $uniqs) = @{ shift @tables || [] }) {
383         my $local_class = $self->schema->class($local_moniker);
384
385         my %counters;
386         foreach my $rel (@$rels) {
387             next if !$rel->{remote_source};
388             $counters{$rel->{remote_source}}++;
389         }
390
391         foreach my $rel (@$rels) {
392             my $remote_moniker = $rel->{remote_source}
393                 or next;
394
395             my $remote_class   = $self->schema->class($remote_moniker);
396             my $remote_obj     = $self->schema->source($remote_moniker);
397             my $remote_cols    = $rel->{remote_columns} || [ $remote_obj->primary_columns ];
398
399             my $local_cols     = $rel->{local_columns};
400
401             if($#$local_cols != $#$remote_cols) {
402                 croak "Column count mismatch: $local_moniker (@$local_cols) "
403                     . "$remote_moniker (@$remote_cols)";
404             }
405
406             my %cond;
407             foreach my $i (0 .. $#$local_cols) {
408                 $cond{$remote_cols->[$i]} = $local_cols->[$i];
409             }
410
411             my ( $local_relname, $remote_relname, $remote_method ) =
412                 $self->_relnames_and_method( $local_moniker, $rel, \%cond,  $uniqs, \%counters );
413             my $local_method  = 'belongs_to';
414
415             ($remote_relname) = $self->_rel_name_map($remote_relname, $local_method, $local_class, $local_moniker, $local_cols, $remote_class, $remote_moniker, $remote_cols);
416             ($local_relname)  = $self->_rel_name_map($local_relname, $remote_method, $remote_class, $remote_moniker, $remote_cols, $local_class, $local_moniker, $local_cols);
417
418             $remote_relname   = $self->_resolve_relname_collision($local_moniker,  $local_cols,  $remote_relname);
419             $local_relname    = $self->_resolve_relname_collision($remote_moniker, $remote_cols, $local_relname);
420
421             push(@{$all_code->{$local_class}},
422                 { method => $local_method,
423                   args => [ $remote_relname,
424                             $remote_class,
425                             \%cond,
426                             $self->_remote_attrs($local_moniker, $local_cols),
427                   ],
428                   extra => {
429                       local_class    => $local_class,
430                       local_moniker  => $local_moniker,
431                       remote_moniker => $remote_moniker,
432                   },
433                 }
434             );
435
436             my %rev_cond = reverse %cond;
437             for (keys %rev_cond) {
438                 $rev_cond{"foreign.$_"} = "self.".$rev_cond{$_};
439                 delete $rev_cond{$_};
440             }
441
442             push(@{$all_code->{$remote_class}},
443                 { method => $remote_method,
444                   args => [ $local_relname,
445                             $local_class,
446                             \%rev_cond,
447                             $self->_relationship_attrs($remote_method),
448                   ],
449                   extra => {
450                       local_class    => $remote_class,
451                       local_moniker  => $remote_moniker,
452                       remote_moniker => $local_moniker,
453                   },
454                 }
455             );
456         }
457     }
458
459     # disambiguate rels with the same name
460     foreach my $class (keys %$all_code) {
461         my $dups = $self->_duplicates($all_code->{$class});
462
463         $self->_disambiguate($all_code->{$class}, $dups) if $dups;
464     }
465
466     $self->_cleanup;
467
468     return $all_code;
469 }
470
471 sub _duplicates {
472     my ($self, $rels) = @_;
473
474     my @rels = map [ $_->{args}[0] => $_ ], @$rels;
475     my %rel_names;
476     $rel_names{$_}++ foreach map $_->[0], @rels;
477
478     my @dups = grep $rel_names{$_} > 1, keys %rel_names;
479
480     my %dups;
481
482     foreach my $dup (@dups) {
483         $dups{$dup} = [ map $_->[1], grep { $_->[0] eq $dup } @rels ];
484     }
485
486     return if not %dups;
487
488     return \%dups;
489 }
490
491 sub _tagger {
492     my $self = shift;
493
494     $self->__tagger(Lingua::EN::Tagger->new) unless $self->__tagger;
495
496     return $self->__tagger;
497 }
498
499 sub _adjectives {
500     my ($self, @cols) = @_;
501
502     my @adjectives;
503
504     foreach my $col (@cols) {
505         my @words = split_name $col;
506
507         my $tagged = $self->_tagger->get_readable(join ' ', @words);
508
509         push @adjectives, $tagged =~ m{\G(\w+)/JJ\s+}g;
510     }
511
512     return @adjectives;
513 }
514
515 sub _name_to_identifier {
516     my ($self, $name) = @_;
517
518     my $to_identifier = $self->loader->naming->{force_ascii} ?
519         \&String::ToIdentifier::EN::to_identifier
520         : \&String::ToIdentifier::EN::Unicode::to_identifier;
521
522     return join '_', map lc, split_name $to_identifier->($name, '_');
523 }
524
525 sub _disambiguate {
526     my ($self, $all_rels, $dups) = @_;
527
528     DUP: foreach my $dup (keys %$dups) {
529         my @rels = @{ $dups->{$dup} };
530
531         # Check if there are rels to the same table name in different
532         # schemas/databases, if so qualify them.
533         my @tables = map $self->loader->moniker_to_table->{$_->{extra}{remote_moniker}},
534                         @rels;
535
536         # databases are different, prepend database
537         if ($tables[0]->can('database') && (uniq map $_->database||'', @tables) > 1) {
538             # If any rels are in the same database, we have to distinguish by
539             # both schema and database.
540             my %db_counts;
541             $db_counts{$_}++ for map $_->database, @tables;
542             my $use_schema = any { $_ > 1 } values %db_counts;
543
544             foreach my $i (0..$#rels) {
545                 my $rel   = $rels[$i];
546                 my $table = $tables[$i];
547
548                 $rel->{args}[0] = $self->_name_to_identifier($table->database)
549                     . ($use_schema ? ('_' . $self->name_to_identifier($table->schema)) : '')
550                     . '_' . $rel->{args}[0];
551             }
552             next DUP;
553         }
554         # schemas are different, prepend schema
555         elsif ((uniq map $_->schema||'', @tables) > 1) {
556             foreach my $i (0..$#rels) {
557                 my $rel   = $rels[$i];
558                 my $table = $tables[$i];
559
560                 $rel->{args}[0] = $self->_name_to_identifier($table->schema)
561                     . '_' . $rel->{args}[0];
562             }
563             next DUP;
564         }
565
566         foreach my $rel (@rels) {
567             next if $rel->{method} eq 'belongs_to';
568
569             my @to_cols = apply { s/^foreign\.//i }
570                 keys %{ $rel->{args}[2] };
571
572             my @adjectives = $self->_adjectives(@to_cols);
573
574             # If there are no adjectives, and there is only one might_have
575             # rel to that class, we hardcode 'active'.
576
577             my $to_class = $rel->{args}[1];
578
579             if ((not @adjectives)
580                 && (grep { $_->{method} eq 'might_have'
581                            && $_->{args}[1] eq $to_class } @$all_rels) == 1) {
582
583                 @adjectives = 'active';
584             }
585
586             if (@adjectives) {
587                 my $rel_name = join '_', sort(@adjectives), $rel->{args}[0];
588
589                 ($rel_name) = $rel->{method} eq 'might_have' ?
590                     $self->_inflect_singular($rel_name)
591                     :
592                     $self->_inflect_plural($rel_name);
593
594                 my ($local_class, $local_moniker, $remote_moniker)
595                     = @{ $rel->{extra} }
596                         {qw/local_class local_moniker remote_moniker/};
597
598                 my @from_cols = apply { s/^self\.//i }
599                     values %{ $rel->{args}[2] };
600
601                 ($rel_name) = $self->_rel_name_map($rel_name, $rel->{method}, $local_class, $local_moniker, \@from_cols, $to_class, $remote_moniker, \@to_cols);
602
603                 $rel_name = $self->_resolve_relname_collision($local_moniker, \@from_cols, $rel_name);
604
605                 $rel->{args}[0] = $rel_name;
606             }
607         }
608     }
609
610     # Check again for duplicates, since the heuristics above may not have resolved them all.
611
612     if ($dups = $self->_duplicates($all_rels)) {
613         foreach my $dup (keys %$dups) {
614             # sort by method
615             my @rels = map $_->[1], sort { $a->[0] <=> $b->[0] } map [
616                 ($_->{method} eq 'belongs_to' ? 3 : $_->{method} eq 'has_many' ? 2 : 1), $_
617             ], @{ $dups->{$dup} };
618
619             my $rel_num = 2;
620
621             foreach my $rel (@rels[1 .. $#rels]) {
622                 my $inflect_type = $rel->{method} eq 'has_many' ?
623                     'inflect_plural'
624                     :
625                     'inflect_singular';
626
627                 my $inflect_method = "_$inflect_type";
628
629                 my $relname_new_uninflected = $rel->{args}[0] . "_$rel_num";
630
631                 $rel_num++;
632
633                 my ($local_class, $local_moniker, $remote_moniker)
634                     = @{ $rel->{extra} }
635                         {qw/local_class local_moniker remote_moniker/};
636
637                 my @from_cols = apply { s/^self\.//i }
638                     values %{ $rel->{args}[2] };
639
640                 my @to_cols = apply { s/^foreign\.//i }
641                     keys %{ $rel->{args}[2] };
642
643                 my $to_class = $rel->{args}[1];
644
645                 my ($relname_new, $inflect_mapped) =
646                     $self->$inflect_method($relname_new_uninflected);
647
648                 my $rel_name_mapped;
649
650                 ($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);
651                 
652                 my $mapped = $inflect_mapped || $rel_name_mapped;
653
654                 warn <<"EOF" unless $mapped;
655 Could not find a proper name for relationship '$relname_new' in source
656 '$local_moniker' for columns '@{[ join ',', @from_cols ]}'. Supply a value in
657 '$inflect_type' or 'rel_name_map' for '$relname_new_uninflected' to name this
658 relationship.
659 EOF
660
661                 $relname_new = $self->_resolve_relname_collision($local_moniker, \@from_cols, $relname_new);
662
663                 $rel->{args}[0] = $relname_new;
664             }
665         }
666     }
667 }
668
669 sub _relnames_and_method {
670     my ( $self, $local_moniker, $rel, $cond, $uniqs, $counters ) = @_;
671
672     my $remote_moniker  = $rel->{remote_source};
673     my $remote_obj      = $self->schema->source( $remote_moniker );
674     my $remote_class    = $self->schema->class(  $remote_moniker );
675     my $remote_relname  = $self->_remote_relname( $rel->{remote_table}, $cond);
676
677     my $local_cols      = $rel->{local_columns};
678     my $local_table     = $rel->{local_table};
679     my $local_class     = $self->schema->class($local_moniker);
680     my $local_source    = $self->schema->source($local_moniker);
681
682     my $local_relname_uninflected = $self->_normalize_name($local_table);
683     my ($local_relname) = $self->_inflect_plural($self->_normalize_name($local_table));
684
685     my $remote_method = 'has_many';
686
687     # If the local columns have a UNIQUE constraint, this is a one-to-one rel
688     if ($self->_array_eq([ $local_source->primary_columns ], $local_cols) ||
689             grep { $self->_array_eq($_->[1], $local_cols) } @$uniqs) {
690         $remote_method   = 'might_have';
691         ($local_relname) = $self->_inflect_singular($local_relname_uninflected);
692     }
693
694     # If more than one rel between this pair of tables, use the local
695     # col names to distinguish, unless the rel was created previously.
696     if ($counters->{$remote_moniker} > 1) {
697         my $relationship_exists = 0;
698
699         if (-f (my $existing_remote_file = $self->loader->get_dump_filename($remote_class))) {
700             my $class = "${remote_class}Temporary";
701
702             if (not Class::Inspector->loaded($class)) {
703                 my $code = slurp_file $existing_remote_file;
704
705                 $code =~ s/(?<=package $remote_class)/Temporary/g;
706
707                 $code =~ s/__PACKAGE__->meta->make_immutable[^;]*;//g;
708
709                 eval $code;
710                 die $@ if $@;
711
712                 push @{ $self->_temp_classes }, $class;
713             }
714
715             if ($class->has_relationship($local_relname)) {
716                 my $rel_cols = [ sort { $a cmp $b } apply { s/^foreign\.//i }
717                     (keys %{ $class->relationship_info($local_relname)->{cond} }) ];
718
719                 $relationship_exists = 1 if $self->_array_eq([ sort @$local_cols ], $rel_cols);
720             }
721         }
722
723         if (not $relationship_exists) {
724             my $colnames = q{_} . $self->_normalize_name(join '_', @$local_cols);
725             $remote_relname .= $colnames if keys %$cond > 1;
726
727             $local_relname = $self->_strip_id_postfix($self->_normalize_name($local_table . $colnames));
728
729             $local_relname_uninflected = $local_relname;
730             ($local_relname) = $self->_inflect_plural($local_relname);
731
732             # if colnames were added and this is a might_have, re-inflect
733             if ($remote_method eq 'might_have') {
734                 ($local_relname) = $self->_inflect_singular($local_relname_uninflected);
735             }
736         }
737     }
738
739     return ($local_relname, $remote_relname, $remote_method);
740 }
741
742 sub _rel_name_map {
743     my ($self, $relname, $method, $local_class, $local_moniker, $local_cols,
744         $remote_class, $remote_moniker, $remote_cols) = @_;
745
746     my $info = {
747         name           => $relname,
748         type           => $method,
749         local_class    => $local_class,
750         local_moniker  => $local_moniker,
751         local_columns  => $local_cols,
752         remote_class   => $remote_class,
753         remote_moniker => $remote_moniker,
754         remote_columns => $remote_cols,
755     };
756
757     my $new_name = $relname;
758
759     my $map = $self->rel_name_map;
760     my $mapped = 0;
761
762     if ('HASH' eq ref($map)) {
763         my $name = $info->{name};
764         my $moniker = $info->{local_moniker};
765         if ($map->{$moniker} and 'HASH' eq ref($map->{$moniker})
766             and $map->{$moniker}{$name}
767         ) {
768             $new_name = $map->{$moniker}{$name};
769             $mapped   = 1;
770         }
771         elsif ($map->{$name} and not 'HASH' eq ref($map->{$name})) {
772             $new_name = $map->{$name};
773             $mapped   = 1;
774         }
775     }
776     elsif ('CODE' eq ref($map)) {
777         my $name = $map->($info);
778         if ($name) {
779             $new_name = $name;
780             $mapped   = 1;
781         }
782     }
783
784     return ($new_name, $mapped);
785 }
786
787 sub _cleanup {
788     my $self = shift;
789
790     for my $class (@{ $self->_temp_classes }) {
791         Class::Unload->unload($class);
792     }
793
794     $self->_temp_classes([]);
795 }
796
797 =head1 AUTHOR
798
799 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
800
801 =head1 LICENSE
802
803 This library is free software; you can redistribute it and/or modify it under
804 the same terms as Perl itself.
805
806 =cut
807
808 1;
809 # vim:et sts=4 sw=4 tw=0: