Commit | Line | Data |
996be9ee |
1 | package DBIx::Class::Schema::Loader::RelBuilder; |
2 | |
3 | use strict; |
4 | use warnings; |
fa994d3c |
5 | use Carp::Clan qw/^DBIx::Class/; |
996be9ee |
6 | use Lingua::EN::Inflect::Number (); |
7 | |
3ddb05da |
8 | our $VERSION = '0.04999_03'; |
32f784fc |
9 | |
996be9ee |
10 | =head1 NAME |
11 | |
12 | DBIx::Class::Schema::Loader::RelBuilder - Builds relationships for DBIx::Class::Schema::Loader |
13 | |
14 | =head1 SYNOPSIS |
15 | |
16 | See L<DBIx::Class::Schema::Loader> |
17 | |
18 | =head1 DESCRIPTION |
19 | |
20 | This class builds relationships for L<DBIx::Class::Schema::Loader>. This |
21 | is module is not (yet) for external use. |
22 | |
23 | =head1 METHODS |
24 | |
25 | =head2 new |
26 | |
e8ad6491 |
27 | Arguments: schema_class (scalar), inflect_plural, inflect_singular |
996be9ee |
28 | |
29 | C<$schema_class> should be a schema class name, where the source |
30 | classes have already been set up and registered. Column info, primary |
31 | key, and unique constraints will be drawn from this schema for all |
32 | of the existing source monikers. |
33 | |
996be9ee |
34 | Options inflect_plural and inflect_singular are optional, and are better documented |
35 | in L<DBIx::Class::Schema::Loader::Base>. |
36 | |
37 | =head2 generate_code |
38 | |
e8ad6491 |
39 | Arguments: local_moniker (scalar), fk_info (arrayref) |
40 | |
41 | This generates the code for the relationships of a given table. |
42 | |
43 | C<local_moniker> is the moniker name of the table which had the REFERENCES |
44 | statements. The fk_info arrayref's contents should take the form: |
45 | |
46 | [ |
47 | { |
48 | local_columns => [ 'col2', 'col3' ], |
49 | remote_columns => [ 'col5', 'col7' ], |
50 | remote_moniker => 'AnotherTableMoniker', |
51 | }, |
52 | { |
53 | local_columns => [ 'col1', 'col4' ], |
54 | remote_columns => [ 'col1', 'col2' ], |
55 | remote_moniker => 'YetAnotherTableMoniker', |
56 | }, |
57 | # ... |
58 | ], |
59 | |
60 | This method will return the generated relationships as a hashref keyed on the |
61 | class names. The values are arrayrefs of hashes containing method name and |
62 | arguments, like so: |
996be9ee |
63 | |
64 | { |
65 | 'Some::Source::Class' => [ |
b97c2c1e |
66 | { method => 'belongs_to', arguments => [ 'col1', 'Another::Source::Class' ], |
67 | { method => 'has_many', arguments => [ 'anothers', 'Yet::Another::Source::Class', 'col15' ], |
996be9ee |
68 | ], |
69 | 'Another::Source::Class' => [ |
70 | # ... |
71 | ], |
72 | # ... |
73 | } |
8f9d7ce5 |
74 | |
996be9ee |
75 | =cut |
76 | |
77 | sub new { |
e8ad6491 |
78 | my ( $class, $schema, $inflect_pl, $inflect_singular ) = @_; |
996be9ee |
79 | |
80 | my $self = { |
81 | schema => $schema, |
996be9ee |
82 | inflect_plural => $inflect_pl, |
83 | inflect_singular => $inflect_singular, |
84 | }; |
85 | |
86 | bless $self => $class; |
87 | |
88 | $self; |
89 | } |
90 | |
91 | |
92 | # pluralize a relationship name |
93 | sub _inflect_plural { |
94 | my ($self, $relname) = @_; |
95 | |
96 | if( ref $self->{inflect_plural} eq 'HASH' ) { |
97 | return $self->{inflect_plural}->{$relname} |
98 | if exists $self->{inflect_plural}->{$relname}; |
99 | } |
100 | elsif( ref $self->{inflect_plural} eq 'CODE' ) { |
101 | my $inflected = $self->{inflect_plural}->($relname); |
102 | return $inflected if $inflected; |
103 | } |
104 | |
d3af7a07 |
105 | return Lingua::EN::Inflect::Number::to_PL($relname); |
996be9ee |
106 | } |
107 | |
108 | # Singularize a relationship name |
109 | sub _inflect_singular { |
110 | my ($self, $relname) = @_; |
111 | |
112 | if( ref $self->{inflect_singular} eq 'HASH' ) { |
113 | return $self->{inflect_singular}->{$relname} |
114 | if exists $self->{inflect_singular}->{$relname}; |
115 | } |
116 | elsif( ref $self->{inflect_singular} eq 'CODE' ) { |
117 | my $inflected = $self->{inflect_singular}->($relname); |
118 | return $inflected if $inflected; |
119 | } |
120 | |
d3af7a07 |
121 | return Lingua::EN::Inflect::Number::to_S($relname); |
996be9ee |
122 | } |
123 | |
26f1c8c9 |
124 | sub _array_eq { |
125 | my ($a, $b) = @_; |
126 | |
127 | return unless @$a == @$b; |
128 | |
129 | for (my $i = 0; $i < @$a; $i++) { |
130 | return unless $a->[$i] eq $b->[$i]; |
131 | } |
132 | return 1; |
133 | } |
134 | |
996be9ee |
135 | sub generate_code { |
26f1c8c9 |
136 | my ($self, $local_moniker, $rels, $uniqs) = @_; |
996be9ee |
137 | |
138 | my $all_code = {}; |
139 | |
e8ad6491 |
140 | my $local_table = $self->{schema}->source($local_moniker)->from; |
141 | my $local_class = $self->{schema}->class($local_moniker); |
996be9ee |
142 | |
e8ad6491 |
143 | my %counters; |
144 | foreach my $rel (@$rels) { |
145 | next if !$rel->{remote_source}; |
146 | $counters{$rel->{remote_source}}++; |
147 | } |
148 | |
149 | foreach my $rel (@$rels) { |
150 | next if !$rel->{remote_source}; |
151 | my $local_cols = $rel->{local_columns}; |
152 | my $remote_cols = $rel->{remote_columns}; |
153 | my $remote_moniker = $rel->{remote_source}; |
154 | my $remote_obj = $self->{schema}->source($remote_moniker); |
155 | my $remote_class = $self->{schema}->class($remote_moniker); |
156 | my $remote_table = $remote_obj->from; |
157 | $remote_cols ||= [ $remote_obj->primary_columns ]; |
158 | |
159 | if($#$local_cols != $#$remote_cols) { |
160 | croak "Column count mismatch: $local_moniker (@$local_cols) " |
161 | . "$remote_moniker (@$remote_cols)"; |
996be9ee |
162 | } |
163 | |
e8ad6491 |
164 | my %cond; |
165 | foreach my $i (0 .. $#$local_cols) { |
166 | $cond{$remote_cols->[$i]} = $local_cols->[$i]; |
167 | } |
996be9ee |
168 | |
e8ad6491 |
169 | my $local_relname; |
e8ad6491 |
170 | my $remote_relname; |
996be9ee |
171 | |
54700b71 |
172 | # for single-column case, set the remote relname to the column |
173 | # name, to make filter accessors work |
e8ad6491 |
174 | if(scalar keys %cond == 1) { |
175 | my ($col) = keys %cond; |
176 | $remote_relname = $self->_inflect_singular($cond{$col}); |
177 | } |
178 | else { |
179 | $remote_relname = $self->_inflect_singular(lc $remote_table); |
180 | } |
996be9ee |
181 | |
54700b71 |
182 | # If more than one rel between this pair of tables, use the local |
183 | # col names to distinguish |
184 | if($counters{$remote_moniker} > 1) { |
185 | my $colnames = q{_} . join(q{_}, @$local_cols); |
186 | $local_relname = $self->_inflect_plural( |
187 | lc($local_table) . $colnames |
188 | ); |
189 | $remote_relname .= $colnames if keys %cond > 1; |
190 | } else { |
191 | $local_relname = $self->_inflect_plural(lc $local_table); |
192 | } |
996be9ee |
193 | |
e8ad6491 |
194 | my %rev_cond = reverse %cond; |
996be9ee |
195 | |
e8ad6491 |
196 | for (keys %rev_cond) { |
197 | $rev_cond{"foreign.$_"} = "self.".$rev_cond{$_}; |
198 | delete $rev_cond{$_}; |
199 | } |
996be9ee |
200 | |
26f1c8c9 |
201 | my $remote_method = 'has_many'; |
202 | |
203 | # If the local columns have a UNIQUE constraint, this is a one-to-one rel |
97ec773a |
204 | my $local_source = $self->{schema}->source($local_moniker); |
205 | if (_array_eq([ $local_source->primary_columns ], $local_cols) || |
26f1c8c9 |
206 | grep { _array_eq($_->[1], $local_cols) } @$uniqs) { |
207 | $remote_method = 'might_have'; |
208 | $local_relname = $self->_inflect_singular($local_relname); |
209 | } |
210 | |
7dba7c70 |
211 | # If the referring column is nullable, make 'belongs_to' an outer join: |
7dba7c70 |
212 | my $nullable = grep { $local_source->column_info($_)->{is_nullable} } |
213 | @$local_cols; |
214 | |
e8ad6491 |
215 | push(@{$all_code->{$local_class}}, |
216 | { method => 'belongs_to', |
217 | args => [ $remote_relname, |
218 | $remote_class, |
219 | \%cond, |
7dba7c70 |
220 | $nullable ? { join_type => 'LEFT OUTER' } : () |
e8ad6491 |
221 | ], |
996be9ee |
222 | } |
e8ad6491 |
223 | ); |
224 | |
225 | push(@{$all_code->{$remote_class}}, |
26f1c8c9 |
226 | { method => $remote_method, |
e8ad6491 |
227 | args => [ $local_relname, |
228 | $local_class, |
229 | \%rev_cond, |
230 | ], |
231 | } |
232 | ); |
996be9ee |
233 | } |
234 | |
235 | return $all_code; |
236 | } |
237 | |
238 | 1; |