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 (); |
cc4f11a2 |
10 | use DBIx::Class::Schema::Loader::Utils 'split_name'; |
1ad8e8c3 |
11 | use File::Slurp 'slurp'; |
12 | use Try::Tiny; |
13 | use Class::Unload (); |
14 | use List::MoreUtils 'apply'; |
15 | use namespace::clean; |
996be9ee |
16 | |
f671b630 |
17 | our $VERSION = '0.07002'; |
32f784fc |
18 | |
996be9ee |
19 | =head1 NAME |
20 | |
21 | DBIx::Class::Schema::Loader::RelBuilder - Builds relationships for DBIx::Class::Schema::Loader |
22 | |
23 | =head1 SYNOPSIS |
24 | |
19b7d71c |
25 | See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>. |
996be9ee |
26 | |
27 | =head1 DESCRIPTION |
28 | |
29 | This class builds relationships for L<DBIx::Class::Schema::Loader>. This |
30 | is module is not (yet) for external use. |
31 | |
32 | =head1 METHODS |
33 | |
34 | =head2 new |
35 | |
4c7b5f46 |
36 | Arguments: $base object |
996be9ee |
37 | |
38 | =head2 generate_code |
39 | |
e8ad6491 |
40 | Arguments: local_moniker (scalar), fk_info (arrayref) |
41 | |
42 | This generates the code for the relationships of a given table. |
43 | |
44 | C<local_moniker> is the moniker name of the table which had the REFERENCES |
45 | statements. The fk_info arrayref's contents should take the form: |
46 | |
47 | [ |
48 | { |
49 | local_columns => [ 'col2', 'col3' ], |
50 | remote_columns => [ 'col5', 'col7' ], |
51 | remote_moniker => 'AnotherTableMoniker', |
52 | }, |
53 | { |
54 | local_columns => [ 'col1', 'col4' ], |
55 | remote_columns => [ 'col1', 'col2' ], |
56 | remote_moniker => 'YetAnotherTableMoniker', |
57 | }, |
58 | # ... |
59 | ], |
60 | |
61 | This method will return the generated relationships as a hashref keyed on the |
62 | class names. The values are arrayrefs of hashes containing method name and |
63 | arguments, like so: |
996be9ee |
64 | |
65 | { |
66 | 'Some::Source::Class' => [ |
b97c2c1e |
67 | { method => 'belongs_to', arguments => [ 'col1', 'Another::Source::Class' ], |
68 | { method => 'has_many', arguments => [ 'anothers', 'Yet::Another::Source::Class', 'col15' ], |
996be9ee |
69 | ], |
70 | 'Another::Source::Class' => [ |
71 | # ... |
72 | ], |
73 | # ... |
74 | } |
8f9d7ce5 |
75 | |
996be9ee |
76 | =cut |
77 | |
1ad8e8c3 |
78 | __PACKAGE__->mk_group_accessors('simple', qw/ |
79 | base |
80 | schema |
81 | inflect_plural |
82 | inflect_singular |
83 | relationship_attrs |
84 | _temp_classes |
85 | /); |
4c7b5f46 |
86 | |
996be9ee |
87 | sub new { |
4c7b5f46 |
88 | my ( $class, $base ) = @_; |
89 | |
90 | # from old POD about this constructor: |
91 | # C<$schema_class> should be a schema class name, where the source |
92 | # classes have already been set up and registered. Column info, |
93 | # primary key, and unique constraints will be drawn from this |
94 | # schema for all of the existing source monikers. |
95 | |
96 | # Options inflect_plural and inflect_singular are optional, and |
97 | # are better documented in L<DBIx::Class::Schema::Loader::Base>. |
996be9ee |
98 | |
99 | my $self = { |
4c7b5f46 |
100 | base => $base, |
101 | schema => $base->schema, |
102 | inflect_plural => $base->inflect_plural, |
103 | inflect_singular => $base->inflect_singular, |
104 | relationship_attrs => $base->relationship_attrs, |
1ad8e8c3 |
105 | _temp_classes => [], |
996be9ee |
106 | }; |
107 | |
1ad8e8c3 |
108 | weaken $self->{base}; #< don't leak |
109 | |
110 | bless $self => $class; |
4c7b5f46 |
111 | |
c8c27020 |
112 | # validate the relationship_attrs arg |
1ad8e8c3 |
113 | if( defined $self->relationship_attrs ) { |
114 | ref $self->relationship_attrs eq 'HASH' |
c8c27020 |
115 | or croak "relationship_attrs must be a hashref"; |
116 | } |
996be9ee |
117 | |
1ad8e8c3 |
118 | return $self; |
996be9ee |
119 | } |
120 | |
121 | |
122 | # pluralize a relationship name |
123 | sub _inflect_plural { |
ecf930e6 |
124 | my ($self, $relname) = @_; |
996be9ee |
125 | |
39ef3bfe |
126 | return '' if !defined $relname || $relname eq ''; |
127 | |
1ad8e8c3 |
128 | if( ref $self->inflect_plural eq 'HASH' ) { |
129 | return $self->inflect_plural->{$relname} |
130 | if exists $self->inflect_plural->{$relname}; |
996be9ee |
131 | } |
1ad8e8c3 |
132 | elsif( ref $self->inflect_plural eq 'CODE' ) { |
133 | my $inflected = $self->inflect_plural->($relname); |
996be9ee |
134 | return $inflected if $inflected; |
135 | } |
136 | |
ecf930e6 |
137 | return $self->_to_PL($relname); |
996be9ee |
138 | } |
139 | |
140 | # Singularize a relationship name |
141 | sub _inflect_singular { |
ecf930e6 |
142 | my ($self, $relname) = @_; |
996be9ee |
143 | |
39ef3bfe |
144 | return '' if !defined $relname || $relname eq ''; |
145 | |
1ad8e8c3 |
146 | if( ref $self->inflect_singular eq 'HASH' ) { |
147 | return $self->inflect_singular->{$relname} |
148 | if exists $self->inflect_singular->{$relname}; |
996be9ee |
149 | } |
1ad8e8c3 |
150 | elsif( ref $self->inflect_singular eq 'CODE' ) { |
151 | my $inflected = $self->inflect_singular->($relname); |
996be9ee |
152 | return $inflected if $inflected; |
153 | } |
154 | |
ecf930e6 |
155 | return $self->_to_S($relname); |
c496748b |
156 | } |
157 | |
158 | sub _to_PL { |
159 | my ($self, $name) = @_; |
160 | |
161 | $name =~ s/_/ /g; |
39b22ca9 |
162 | my $plural = Lingua::EN::Inflect::Phrase::to_PL($name); |
c496748b |
163 | $plural =~ s/ /_/g; |
164 | |
165 | return $plural; |
166 | } |
167 | |
c496748b |
168 | sub _to_S { |
169 | my ($self, $name) = @_; |
170 | |
39b22ca9 |
171 | $name =~ s/_/ /g; |
172 | my $singular = Lingua::EN::Inflect::Phrase::to_S($name); |
173 | $singular =~ s/ /_/g; |
174 | |
175 | return $singular; |
996be9ee |
176 | } |
177 | |
53ef681d |
178 | sub _default_relationship_attrs { +{ |
179 | has_many => { |
180 | cascade_delete => 0, |
181 | cascade_copy => 0, |
182 | }, |
183 | might_have => { |
184 | cascade_delete => 0, |
185 | cascade_copy => 0, |
186 | }, |
187 | belongs_to => { |
188 | on_delete => 'CASCADE', |
189 | on_update => 'CASCADE', |
aa0867ee |
190 | is_deferrable => 1, |
53ef681d |
191 | }, |
192 | } } |
193 | |
c8c27020 |
194 | # accessor for options to be passed to each generated relationship |
195 | # type. take single argument, the relationship type name, and returns |
196 | # either a hashref (if some options are set), or nothing |
197 | sub _relationship_attrs { |
198 | my ( $self, $reltype ) = @_; |
1ad8e8c3 |
199 | my $r = $self->relationship_attrs; |
c8c27020 |
200 | |
53ef681d |
201 | my %composite = ( |
202 | %{ $self->_default_relationship_attrs->{$reltype} || {} }, |
203 | %{ $r->{all} || {} } |
204 | ); |
205 | |
c8c27020 |
206 | if( my $specific = $r->{$reltype} ) { |
207 | while( my ($k,$v) = each %$specific ) { |
208 | $composite{$k} = $v; |
209 | } |
210 | } |
211 | return \%composite; |
212 | } |
213 | |
26f1c8c9 |
214 | sub _array_eq { |
ecf930e6 |
215 | my ($self, $a, $b) = @_; |
26f1c8c9 |
216 | |
217 | return unless @$a == @$b; |
218 | |
219 | for (my $i = 0; $i < @$a; $i++) { |
220 | return unless $a->[$i] eq $b->[$i]; |
221 | } |
222 | return 1; |
223 | } |
224 | |
c39e403e |
225 | sub _remote_attrs { |
c496748b |
226 | my ($self, $local_moniker, $local_cols) = @_; |
c39e403e |
227 | |
c496748b |
228 | # get our base set of attrs from _relationship_attrs, if present |
229 | my $attrs = $self->_relationship_attrs('belongs_to') || {}; |
c8c27020 |
230 | |
c496748b |
231 | # If the referring column is nullable, make 'belongs_to' an |
232 | # outer join, unless explicitly set by relationship_attrs |
1ad8e8c3 |
233 | my $nullable = grep { $self->schema->source($local_moniker)->column_info($_)->{is_nullable} } @$local_cols; |
c496748b |
234 | $attrs->{join_type} = 'LEFT' if $nullable && !defined $attrs->{join_type}; |
c39e403e |
235 | |
c496748b |
236 | return $attrs; |
c39e403e |
237 | } |
238 | |
414c61a0 |
239 | sub _sanitize_name { |
240 | my ($self, $name) = @_; |
241 | |
242 | if (ref $name) { |
243 | # scalar ref for weird table name (like one containing a '.') |
244 | ($name = $$name) =~ s/\W+/_/g; |
245 | } |
246 | else { |
247 | # remove 'schema.' prefix if any |
248 | $name =~ s/^[^.]+\.//; |
249 | } |
250 | |
251 | return $name; |
252 | } |
253 | |
19b7d71c |
254 | sub _normalize_name { |
255 | my ($self, $name) = @_; |
256 | |
414c61a0 |
257 | $name = $self->_sanitize_name($name); |
258 | |
cc4f11a2 |
259 | my @words = split_name $name; |
19b7d71c |
260 | |
261 | return join '_', map lc, @words; |
262 | } |
263 | |
f2fc8d01 |
264 | sub _remote_relname { |
265 | my ($self, $remote_table, $cond) = @_; |
266 | |
267 | my $remote_relname; |
268 | # for single-column case, set the remote relname to the column |
269 | # name, to make filter accessors work, but strip trailing _id |
270 | if(scalar keys %{$cond} == 1) { |
271 | my ($col) = values %{$cond}; |
19b7d71c |
272 | $col = $self->_normalize_name($col); |
f2fc8d01 |
273 | $col =~ s/_id$//; |
274 | $remote_relname = $self->_inflect_singular($col); |
275 | } |
276 | else { |
19b7d71c |
277 | $remote_relname = $self->_inflect_singular($self->_normalize_name($remote_table)); |
f2fc8d01 |
278 | } |
279 | |
280 | return $remote_relname; |
281 | } |
282 | |
996be9ee |
283 | sub generate_code { |
26f1c8c9 |
284 | my ($self, $local_moniker, $rels, $uniqs) = @_; |
996be9ee |
285 | |
286 | my $all_code = {}; |
287 | |
1ad8e8c3 |
288 | my $local_class = $self->schema->class($local_moniker); |
057fbb08 |
289 | |
e8ad6491 |
290 | my %counters; |
291 | foreach my $rel (@$rels) { |
292 | next if !$rel->{remote_source}; |
293 | $counters{$rel->{remote_source}}++; |
294 | } |
295 | |
296 | foreach my $rel (@$rels) { |
057fbb08 |
297 | my $remote_moniker = $rel->{remote_source} |
298 | or next; |
299 | |
1ad8e8c3 |
300 | my $remote_class = $self->schema->class($remote_moniker); |
301 | my $remote_obj = $self->schema->source($remote_moniker); |
057fbb08 |
302 | my $remote_cols = $rel->{remote_columns} || [ $remote_obj->primary_columns ]; |
303 | |
304 | my $local_cols = $rel->{local_columns}; |
e8ad6491 |
305 | |
306 | if($#$local_cols != $#$remote_cols) { |
307 | croak "Column count mismatch: $local_moniker (@$local_cols) " |
308 | . "$remote_moniker (@$remote_cols)"; |
996be9ee |
309 | } |
310 | |
e8ad6491 |
311 | my %cond; |
312 | foreach my $i (0 .. $#$local_cols) { |
313 | $cond{$remote_cols->[$i]} = $local_cols->[$i]; |
314 | } |
996be9ee |
315 | |
057fbb08 |
316 | my ( $local_relname, $remote_relname, $remote_method ) = |
39ef3bfe |
317 | $self->_relnames_and_method( $local_moniker, $rel, \%cond, $uniqs, \%counters ); |
7dba7c70 |
318 | |
e8ad6491 |
319 | push(@{$all_code->{$local_class}}, |
320 | { method => 'belongs_to', |
321 | args => [ $remote_relname, |
322 | $remote_class, |
323 | \%cond, |
c39e403e |
324 | $self->_remote_attrs($local_moniker, $local_cols), |
e8ad6491 |
325 | ], |
996be9ee |
326 | } |
e8ad6491 |
327 | ); |
328 | |
057fbb08 |
329 | my %rev_cond = reverse %cond; |
330 | for (keys %rev_cond) { |
331 | $rev_cond{"foreign.$_"} = "self.".$rev_cond{$_}; |
332 | delete $rev_cond{$_}; |
333 | } |
334 | |
e8ad6491 |
335 | push(@{$all_code->{$remote_class}}, |
26f1c8c9 |
336 | { method => $remote_method, |
e8ad6491 |
337 | args => [ $local_relname, |
338 | $local_class, |
339 | \%rev_cond, |
c8c27020 |
340 | $self->_relationship_attrs($remote_method), |
e8ad6491 |
341 | ], |
342 | } |
343 | ); |
996be9ee |
344 | } |
345 | |
346 | return $all_code; |
347 | } |
348 | |
39ef3bfe |
349 | sub _relnames_and_method { |
057fbb08 |
350 | my ( $self, $local_moniker, $rel, $cond, $uniqs, $counters ) = @_; |
e9c09ed9 |
351 | |
057fbb08 |
352 | my $remote_moniker = $rel->{remote_source}; |
1ad8e8c3 |
353 | my $remote_obj = $self->schema->source( $remote_moniker ); |
354 | my $remote_class = $self->schema->class( $remote_moniker ); |
ecf930e6 |
355 | my $remote_relname = $self->_remote_relname( $remote_obj->from, $cond); |
fa6f8d4e |
356 | |
1ad8e8c3 |
357 | my $local_cols = $rel->{local_columns}; |
358 | my $local_table = $self->schema->source($local_moniker)->from; |
359 | my $local_class = $self->schema->class($local_moniker); |
360 | my $local_source = $self->schema->source($local_moniker); |
057fbb08 |
361 | |
1ad8e8c3 |
362 | my $local_relname_uninflected = $self->_normalize_name($local_table); |
363 | my $local_relname = $self->_inflect_plural($self->_normalize_name($local_table)); |
fa6f8d4e |
364 | |
057fbb08 |
365 | my $remote_method = 'has_many'; |
366 | |
367 | # If the local columns have a UNIQUE constraint, this is a one-to-one rel |
ecf930e6 |
368 | if ($self->_array_eq([ $local_source->primary_columns ], $local_cols) || |
369 | grep { $self->_array_eq($_->[1], $local_cols) } @$uniqs) { |
057fbb08 |
370 | $remote_method = 'might_have'; |
c496748b |
371 | $local_relname = $self->_inflect_singular($local_relname_uninflected); |
057fbb08 |
372 | } |
fa6f8d4e |
373 | |
1ad8e8c3 |
374 | # If more than one rel between this pair of tables, use the local |
375 | # col names to distinguish, unless the rel was created previously. |
376 | if ($counters->{$remote_moniker} > 1) { |
377 | my $relationship_exists = 0; |
378 | |
379 | if (-f (my $existing_remote_file = $self->{base}->get_dump_filename($remote_class))) { |
380 | my $class = "${remote_class}Temporary"; |
381 | |
382 | if (not do { no strict 'refs'; %{$class . '::'} }) { |
383 | my $code = slurp $existing_remote_file; |
384 | |
385 | $code =~ s/(?<=package $remote_class)/Temporary/g; |
386 | |
387 | $code =~ s/__PACKAGE__->meta->make_immutable;//g; |
388 | |
389 | eval $code; |
390 | die $@ if $@; |
391 | |
392 | push @{ $self->_temp_classes }, $class; |
393 | } |
394 | |
395 | if ($class->has_relationship($local_relname)) { |
396 | my $rel_cols = [ sort { $a cmp $b } apply { s/^foreign\.//i } |
397 | (keys %{ $class->relationship_info($local_relname)->{cond} }) ]; |
398 | |
399 | $relationship_exists = 1 if $self->_array_eq([ sort @$local_cols ], $rel_cols); |
400 | } |
401 | } |
402 | |
403 | if (not $relationship_exists) { |
404 | my $colnames = q{_} . $self->_normalize_name(join '_', @$local_cols); |
405 | $remote_relname .= $colnames if keys %$cond > 1; |
406 | |
407 | $local_relname = $self->_normalize_name($local_table . $colnames); |
408 | $local_relname =~ s/_id$//; |
409 | |
410 | $local_relname_uninflected = $local_relname; |
411 | $local_relname = $self->_inflect_plural($local_relname); |
412 | |
413 | # if colnames were added and this is a might_have, re-inflect |
414 | if ($remote_method eq 'might_have') { |
415 | $local_relname = $self->_inflect_singular($local_relname_uninflected); |
416 | } |
417 | } |
418 | } |
419 | |
057fbb08 |
420 | return ( $local_relname, $remote_relname, $remote_method ); |
fa6f8d4e |
421 | } |
422 | |
1ad8e8c3 |
423 | sub cleanup { |
424 | my $self = shift; |
425 | |
426 | for my $class (@{ $self->_temp_classes }) { |
427 | Class::Unload->unload($class); |
428 | } |
429 | |
430 | $self->_temp_classes([]); |
431 | } |
432 | |
be80bba7 |
433 | =head1 AUTHOR |
434 | |
9cc8e7e1 |
435 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
be80bba7 |
436 | |
437 | =head1 LICENSE |
438 | |
439 | This library is free software; you can redistribute it and/or modify it under |
440 | the same terms as Perl itself. |
441 | |
442 | =cut |
443 | |
996be9ee |
444 | 1; |
19b7d71c |
445 | # vim:et sts=4 sw=4 tw=0: |