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