1 package DBIx::Class::Schema::Loader::RelBuilder;
6 use Carp::Clan qw/^DBIx::Class/;
9 use Lingua::EN::Inflect::Phrase ();
10 use DBIx::Class::Schema::Loader::Utils 'split_name';
12 our $VERSION = '0.07001';
16 DBIx::Class::Schema::Loader::RelBuilder - Builds relationships for DBIx::Class::Schema::Loader
20 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
24 This class builds relationships for L<DBIx::Class::Schema::Loader>. This
25 is module is not (yet) for external use.
31 Arguments: $base object
35 Arguments: local_moniker (scalar), fk_info (arrayref)
37 This generates the code for the relationships of a given table.
39 C<local_moniker> is the moniker name of the table which had the REFERENCES
40 statements. The fk_info arrayref's contents should take the form:
44 local_columns => [ 'col2', 'col3' ],
45 remote_columns => [ 'col5', 'col7' ],
46 remote_moniker => 'AnotherTableMoniker',
49 local_columns => [ 'col1', 'col4' ],
50 remote_columns => [ 'col1', 'col2' ],
51 remote_moniker => 'YetAnotherTableMoniker',
56 This method will return the generated relationships as a hashref keyed on the
57 class names. The values are arrayrefs of hashes containing method name and
61 'Some::Source::Class' => [
62 { method => 'belongs_to', arguments => [ 'col1', 'Another::Source::Class' ],
63 { method => 'has_many', arguments => [ 'anothers', 'Yet::Another::Source::Class', 'col15' ],
65 'Another::Source::Class' => [
75 my ( $class, $base ) = @_;
77 # from old POD about this constructor:
78 # C<$schema_class> should be a schema class name, where the source
79 # classes have already been set up and registered. Column info,
80 # primary key, and unique constraints will be drawn from this
81 # schema for all of the existing source monikers.
83 # Options inflect_plural and inflect_singular are optional, and
84 # are better documented in L<DBIx::Class::Schema::Loader::Base>.
88 schema => $base->schema,
89 inflect_plural => $base->inflect_plural,
90 inflect_singular => $base->inflect_singular,
91 relationship_attrs => $base->relationship_attrs,
94 Scalar::Util::weaken $self->{base}; #< don't leak
96 # validate the relationship_attrs arg
97 if( defined $self->{relationship_attrs} ) {
98 ref $self->{relationship_attrs} eq 'HASH'
99 or croak "relationship_attrs must be a hashref";
102 return bless $self => $class;
106 # pluralize a relationship name
107 sub _inflect_plural {
108 my ($self, $relname) = @_;
110 return '' if !defined $relname || $relname eq '';
112 if( ref $self->{inflect_plural} eq 'HASH' ) {
113 return $self->{inflect_plural}->{$relname}
114 if exists $self->{inflect_plural}->{$relname};
116 elsif( ref $self->{inflect_plural} eq 'CODE' ) {
117 my $inflected = $self->{inflect_plural}->($relname);
118 return $inflected if $inflected;
121 return $self->_to_PL($relname);
124 # Singularize a relationship name
125 sub _inflect_singular {
126 my ($self, $relname) = @_;
128 return '' if !defined $relname || $relname eq '';
130 if( ref $self->{inflect_singular} eq 'HASH' ) {
131 return $self->{inflect_singular}->{$relname}
132 if exists $self->{inflect_singular}->{$relname};
134 elsif( ref $self->{inflect_singular} eq 'CODE' ) {
135 my $inflected = $self->{inflect_singular}->($relname);
136 return $inflected if $inflected;
139 return $self->_to_S($relname);
143 my ($self, $name) = @_;
146 my $plural = Lingua::EN::Inflect::Phrase::to_PL($name);
153 my ($self, $name) = @_;
156 my $singular = Lingua::EN::Inflect::Phrase::to_S($name);
157 $singular =~ s/ /_/g;
162 sub _default_relationship_attrs { +{
172 on_delete => 'CASCADE',
173 on_update => 'CASCADE',
178 # accessor for options to be passed to each generated relationship
179 # type. take single argument, the relationship type name, and returns
180 # either a hashref (if some options are set), or nothing
181 sub _relationship_attrs {
182 my ( $self, $reltype ) = @_;
183 my $r = $self->{relationship_attrs};
186 %{ $self->_default_relationship_attrs->{$reltype} || {} },
190 if( my $specific = $r->{$reltype} ) {
191 while( my ($k,$v) = each %$specific ) {
199 my ($self, $a, $b) = @_;
201 return unless @$a == @$b;
203 for (my $i = 0; $i < @$a; $i++) {
204 return unless $a->[$i] eq $b->[$i];
210 my ($self, $local_moniker, $local_cols) = @_;
212 # get our base set of attrs from _relationship_attrs, if present
213 my $attrs = $self->_relationship_attrs('belongs_to') || {};
215 # If the referring column is nullable, make 'belongs_to' an
216 # outer join, unless explicitly set by relationship_attrs
217 my $nullable = grep { $self->{schema}->source($local_moniker)->column_info($_)->{is_nullable} } @$local_cols;
218 $attrs->{join_type} = 'LEFT' if $nullable && !defined $attrs->{join_type};
224 my ($self, $name) = @_;
227 # scalar ref for weird table name (like one containing a '.')
228 ($name = $$name) =~ s/\W+/_/g;
231 # remove 'schema.' prefix if any
232 $name =~ s/^[^.]+\.//;
238 sub _normalize_name {
239 my ($self, $name) = @_;
241 $name = $self->_sanitize_name($name);
243 my @words = split_name $name;
245 return join '_', map lc, @words;
248 sub _remote_relname {
249 my ($self, $remote_table, $cond) = @_;
252 # for single-column case, set the remote relname to the column
253 # name, to make filter accessors work, but strip trailing _id
254 if(scalar keys %{$cond} == 1) {
255 my ($col) = values %{$cond};
256 $col = $self->_normalize_name($col);
258 $remote_relname = $self->_inflect_singular($col);
261 $remote_relname = $self->_inflect_singular($self->_normalize_name($remote_table));
264 return $remote_relname;
268 my ($self, $local_moniker, $rels, $uniqs) = @_;
272 my $local_class = $self->{schema}->class($local_moniker);
275 foreach my $rel (@$rels) {
276 next if !$rel->{remote_source};
277 $counters{$rel->{remote_source}}++;
280 foreach my $rel (@$rels) {
281 my $remote_moniker = $rel->{remote_source}
284 my $remote_class = $self->{schema}->class($remote_moniker);
285 my $remote_obj = $self->{schema}->source($remote_moniker);
286 my $remote_cols = $rel->{remote_columns} || [ $remote_obj->primary_columns ];
288 my $local_cols = $rel->{local_columns};
290 if($#$local_cols != $#$remote_cols) {
291 croak "Column count mismatch: $local_moniker (@$local_cols) "
292 . "$remote_moniker (@$remote_cols)";
296 foreach my $i (0 .. $#$local_cols) {
297 $cond{$remote_cols->[$i]} = $local_cols->[$i];
300 my ( $local_relname, $remote_relname, $remote_method ) =
301 $self->_relnames_and_method( $local_moniker, $rel, \%cond, $uniqs, \%counters );
303 push(@{$all_code->{$local_class}},
304 { method => 'belongs_to',
305 args => [ $remote_relname,
308 $self->_remote_attrs($local_moniker, $local_cols),
313 my %rev_cond = reverse %cond;
314 for (keys %rev_cond) {
315 $rev_cond{"foreign.$_"} = "self.".$rev_cond{$_};
316 delete $rev_cond{$_};
319 push(@{$all_code->{$remote_class}},
320 { method => $remote_method,
321 args => [ $local_relname,
324 $self->_relationship_attrs($remote_method),
333 sub _relnames_and_method {
334 my ( $self, $local_moniker, $rel, $cond, $uniqs, $counters ) = @_;
336 my $remote_moniker = $rel->{remote_source};
337 my $remote_obj = $self->{schema}->source( $remote_moniker );
338 my $remote_class = $self->{schema}->class( $remote_moniker );
339 my $remote_relname = $self->_remote_relname( $remote_obj->from, $cond);
341 my $local_cols = $rel->{local_columns};
342 my $local_table = $self->{schema}->source($local_moniker)->from;
344 # If more than one rel between this pair of tables, use the local
345 # col names to distinguish
346 my ($local_relname, $local_relname_uninflected);
347 if ( $counters->{$remote_moniker} > 1) {
348 my $colnames = q{_} . $self->_normalize_name(join '_', @$local_cols);
349 $remote_relname .= $colnames if keys %$cond > 1;
351 $local_relname = $self->_normalize_name($local_table . $colnames);
352 $local_relname =~ s/_id$//;
354 $local_relname_uninflected = $local_relname;
355 $local_relname = $self->_inflect_plural($local_relname);
357 $local_relname_uninflected = $self->_normalize_name($local_table);
358 $local_relname = $self->_inflect_plural($self->_normalize_name($local_table));
361 my $remote_method = 'has_many';
363 # If the local columns have a UNIQUE constraint, this is a one-to-one rel
364 my $local_source = $self->{schema}->source($local_moniker);
365 if ($self->_array_eq([ $local_source->primary_columns ], $local_cols) ||
366 grep { $self->_array_eq($_->[1], $local_cols) } @$uniqs) {
367 $remote_method = 'might_have';
368 $local_relname = $self->_inflect_singular($local_relname_uninflected);
371 return ( $local_relname, $remote_relname, $remote_method );
376 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
380 This library is free software; you can redistribute it and/or modify it under
381 the same terms as Perl itself.
386 # vim:et sts=4 sw=4 tw=0: