1 package DBIx::Class::Schema::Loader::RelBuilder;
6 use Carp::Clan qw/^DBIx::Class/;
7 use Lingua::EN::Inflect::Phrase ();
8 use DBIx::Class::Schema::Loader::Utils 'split_name';
10 our $VERSION = '0.07001';
14 DBIx::Class::Schema::Loader::RelBuilder - Builds relationships for DBIx::Class::Schema::Loader
18 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
22 This class builds relationships for L<DBIx::Class::Schema::Loader>. This
23 is module is not (yet) for external use.
29 Arguments: schema_class (scalar), inflect_plural, inflect_singular
31 C<$schema_class> should be a schema class name, where the source
32 classes have already been set up and registered. Column info, primary
33 key, and unique constraints will be drawn from this schema for all
34 of the existing source monikers.
36 Options inflect_plural and inflect_singular are optional, and are better documented
37 in L<DBIx::Class::Schema::Loader::Base>.
41 Arguments: local_moniker (scalar), fk_info (arrayref)
43 This generates the code for the relationships of a given table.
45 C<local_moniker> is the moniker name of the table which had the REFERENCES
46 statements. The fk_info arrayref's contents should take the form:
50 local_columns => [ 'col2', 'col3' ],
51 remote_columns => [ 'col5', 'col7' ],
52 remote_moniker => 'AnotherTableMoniker',
55 local_columns => [ 'col1', 'col4' ],
56 remote_columns => [ 'col1', 'col2' ],
57 remote_moniker => 'YetAnotherTableMoniker',
62 This method will return the generated relationships as a hashref keyed on the
63 class names. The values are arrayrefs of hashes containing method name and
67 'Some::Source::Class' => [
68 { method => 'belongs_to', arguments => [ 'col1', 'Another::Source::Class' ],
69 { method => 'has_many', arguments => [ 'anothers', 'Yet::Another::Source::Class', 'col15' ],
71 'Another::Source::Class' => [
80 my ( $class, $schema, $inflect_pl, $inflect_singular, $rel_attrs ) = @_;
84 inflect_plural => $inflect_pl,
85 inflect_singular => $inflect_singular,
86 relationship_attrs => $rel_attrs,
89 # validate the relationship_attrs arg
90 if( defined $self->{relationship_attrs} ) {
91 ref($self->{relationship_attrs}) eq 'HASH'
92 or croak "relationship_attrs must be a hashref";
95 return bless $self => $class;
99 # pluralize a relationship name
100 sub _inflect_plural {
101 my ($self, $relname) = @_;
103 return '' if !defined $relname || $relname eq '';
105 if( ref $self->{inflect_plural} eq 'HASH' ) {
106 return $self->{inflect_plural}->{$relname}
107 if exists $self->{inflect_plural}->{$relname};
109 elsif( ref $self->{inflect_plural} eq 'CODE' ) {
110 my $inflected = $self->{inflect_plural}->($relname);
111 return $inflected if $inflected;
114 return $self->_to_PL($relname);
117 # Singularize a relationship name
118 sub _inflect_singular {
119 my ($self, $relname) = @_;
121 return '' if !defined $relname || $relname eq '';
123 if( ref $self->{inflect_singular} eq 'HASH' ) {
124 return $self->{inflect_singular}->{$relname}
125 if exists $self->{inflect_singular}->{$relname};
127 elsif( ref $self->{inflect_singular} eq 'CODE' ) {
128 my $inflected = $self->{inflect_singular}->($relname);
129 return $inflected if $inflected;
132 return $self->_to_S($relname);
136 my ($self, $name) = @_;
139 my $plural = Lingua::EN::Inflect::Phrase::to_PL($name);
146 my ($self, $name) = @_;
149 my $singular = Lingua::EN::Inflect::Phrase::to_S($name);
150 $singular =~ s/ /_/g;
155 sub _default_relationship_attrs { +{
165 on_delete => 'CASCADE',
166 on_update => 'CASCADE',
167 # is_deferrable => 1,
171 # accessor for options to be passed to each generated relationship
172 # type. take single argument, the relationship type name, and returns
173 # either a hashref (if some options are set), or nothing
174 sub _relationship_attrs {
175 my ( $self, $reltype ) = @_;
176 my $r = $self->{relationship_attrs};
179 %{ $self->_default_relationship_attrs->{$reltype} || {} },
183 if( my $specific = $r->{$reltype} ) {
184 while( my ($k,$v) = each %$specific ) {
192 my ($self, $a, $b) = @_;
194 return unless @$a == @$b;
196 for (my $i = 0; $i < @$a; $i++) {
197 return unless $a->[$i] eq $b->[$i];
203 my ($self, $local_moniker, $local_cols) = @_;
205 # get our base set of attrs from _relationship_attrs, if present
206 my $attrs = $self->_relationship_attrs('belongs_to') || {};
208 # If the referring column is nullable, make 'belongs_to' an
209 # outer join, unless explicitly set by relationship_attrs
210 my $nullable = grep { $self->{schema}->source($local_moniker)->column_info($_)->{is_nullable} } @$local_cols;
211 $attrs->{join_type} = 'LEFT' if $nullable && !defined $attrs->{join_type};
217 my ($self, $name) = @_;
220 # scalar ref for weird table name (like one containing a '.')
221 ($name = $$name) =~ s/\W+/_/g;
224 # remove 'schema.' prefix if any
225 $name =~ s/^[^.]+\.//;
231 sub _normalize_name {
232 my ($self, $name) = @_;
234 $name = $self->_sanitize_name($name);
236 my @words = split_name $name;
238 return join '_', map lc, @words;
241 sub _remote_relname {
242 my ($self, $remote_table, $cond) = @_;
245 # for single-column case, set the remote relname to the column
246 # name, to make filter accessors work, but strip trailing _id
247 if(scalar keys %{$cond} == 1) {
248 my ($col) = values %{$cond};
249 $col = $self->_normalize_name($col);
251 $remote_relname = $self->_inflect_singular($col);
254 $remote_relname = $self->_inflect_singular($self->_normalize_name($remote_table));
257 return $remote_relname;
261 my ($self, $local_moniker, $rels, $uniqs) = @_;
265 my $local_class = $self->{schema}->class($local_moniker);
268 foreach my $rel (@$rels) {
269 next if !$rel->{remote_source};
270 $counters{$rel->{remote_source}}++;
273 foreach my $rel (@$rels) {
274 my $remote_moniker = $rel->{remote_source}
277 my $remote_class = $self->{schema}->class($remote_moniker);
278 my $remote_obj = $self->{schema}->source($remote_moniker);
279 my $remote_cols = $rel->{remote_columns} || [ $remote_obj->primary_columns ];
281 my $local_cols = $rel->{local_columns};
283 if($#$local_cols != $#$remote_cols) {
284 croak "Column count mismatch: $local_moniker (@$local_cols) "
285 . "$remote_moniker (@$remote_cols)";
289 foreach my $i (0 .. $#$local_cols) {
290 $cond{$remote_cols->[$i]} = $local_cols->[$i];
293 my ( $local_relname, $remote_relname, $remote_method ) =
294 $self->_relnames_and_method( $local_moniker, $rel, \%cond, $uniqs, \%counters );
296 push(@{$all_code->{$local_class}},
297 { method => 'belongs_to',
298 args => [ $remote_relname,
301 $self->_remote_attrs($local_moniker, $local_cols),
306 my %rev_cond = reverse %cond;
307 for (keys %rev_cond) {
308 $rev_cond{"foreign.$_"} = "self.".$rev_cond{$_};
309 delete $rev_cond{$_};
312 push(@{$all_code->{$remote_class}},
313 { method => $remote_method,
314 args => [ $local_relname,
317 $self->_relationship_attrs($remote_method),
326 sub _relnames_and_method {
327 my ( $self, $local_moniker, $rel, $cond, $uniqs, $counters ) = @_;
329 my $remote_moniker = $rel->{remote_source};
330 my $remote_obj = $self->{schema}->source( $remote_moniker );
331 my $remote_class = $self->{schema}->class( $remote_moniker );
332 my $remote_relname = $self->_remote_relname( $remote_obj->from, $cond);
334 my $local_cols = $rel->{local_columns};
335 my $local_table = $self->{schema}->source($local_moniker)->from;
337 # If more than one rel between this pair of tables, use the local
338 # col names to distinguish
339 my ($local_relname, $local_relname_uninflected);
340 if ( $counters->{$remote_moniker} > 1) {
341 my $colnames = q{_} . $self->_normalize_name(join '_', @$local_cols);
342 $remote_relname .= $colnames if keys %$cond > 1;
344 $local_relname = $self->_normalize_name($local_table . $colnames);
345 $local_relname =~ s/_id$//;
347 $local_relname_uninflected = $local_relname;
348 $local_relname = $self->_inflect_plural($local_relname);
350 $local_relname_uninflected = $self->_normalize_name($local_table);
351 $local_relname = $self->_inflect_plural($self->_normalize_name($local_table));
354 my $remote_method = 'has_many';
356 # If the local columns have a UNIQUE constraint, this is a one-to-one rel
357 my $local_source = $self->{schema}->source($local_moniker);
358 if ($self->_array_eq([ $local_source->primary_columns ], $local_cols) ||
359 grep { $self->_array_eq($_->[1], $local_cols) } @$uniqs) {
360 $remote_method = 'might_have';
361 $local_relname = $self->_inflect_singular($local_relname_uninflected);
364 return ( $local_relname, $remote_relname, $remote_method );
369 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
373 This library is free software; you can redistribute it and/or modify it under
374 the same terms as Perl itself.
379 # vim:et sts=4 sw=4 tw=0: