Commit | Line | Data |
876f6525 |
1 | package DBIx::Class::ResultSource::MultipleTableInheritance; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use parent qw(DBIx::Class::ResultSource::View); |
876f6525 |
6 | use Method::Signatures::Simple; |
7 | use Carp::Clan qw/^DBIx::Class/; |
ca79850d |
8 | use aliased 'DBIx::Class::ResultSource::Table'; |
7abe3af2 |
9 | use aliased 'DBIx::Class::ResultClass::HashRefInflator'; |
05fd2477 |
10 | use String::TT qw(strip tt); |
92ebfc06 |
11 | use Scalar::Util qw(blessed); |
ca79850d |
12 | use namespace::autoclean; |
70d56286 |
13 | |
14 | # how this works: |
15 | # |
16 | # On construction, we hook $self->result_class->result_source_instance |
17 | # if present to get the superclass' source object |
18 | # |
19 | # When attached to a schema, we need to add sources to that schema with |
20 | # appropriate relationships for the foreign keys so the concrete tables |
21 | # get generated |
22 | # |
23 | # We also generate our own view definition using this class' concrete table |
24 | # and the view for the superclass, and stored procedures for the insert, |
25 | # update and delete operations on this view. |
26 | # |
27 | # deploying the postgres rules through SQLT may be a pain though. |
28 | |
803ffff2 |
29 | __PACKAGE__->mk_group_accessors(simple => qw(parent_source additional_parents)); |
876f6525 |
30 | |
31 | method new ($class: @args) { |
32 | my $new = $class->next::method(@args); |
33 | my $rc = $new->result_class; |
34 | if (my $meth = $rc->can('result_source_instance')) { |
7abe3af2 |
35 | my $source = $rc->$meth; |
36 | if ($source->result_class ne $new->result_class |
37 | && $new->result_class->isa($source->result_class)) { |
38 | $new->parent_source($source); |
39 | } |
876f6525 |
40 | } |
41 | return $new; |
42 | } |
43 | |
803ffff2 |
44 | method add_additional_parent ($source) { |
45 | my ($our_pk, $their_pk) = map { |
46 | join('|',sort $_->primary_columns) |
47 | } ($self, $source); |
48 | |
49 | confess "Can't attach additional parent ${\$source->name} - it has different PKs ($their_pk versus our $our_pk)" |
50 | unless $their_pk eq $our_pk; |
51 | $self->additional_parents([ |
52 | @{$self->additional_parents||[]}, $source |
53 | ]); |
54 | $self->add_columns( |
55 | map { |
56 | $_ => # put the extra key first to default it |
57 | { originally_defined_in => $source->name, %{$source->column_info($_)}, }, |
58 | } grep !$self->has_column($_), $source->columns |
59 | ); |
60 | foreach my $rel ($source->relationships) { |
61 | my $rel_info = $source->relationship_info($rel); |
62 | $self->add_relationship( |
63 | $rel, $rel_info->{source}, $rel_info->{cond}, |
64 | # extra key first to default it |
65 | {originally_defined_in => $source->name, %{$rel_info->{attrs}}}, |
66 | ); |
67 | } |
68 | } |
69 | |
7abe3af2 |
70 | method schema (@args) { |
71 | my $ret = $self->next::method(@args); |
72 | if (@args) { |
c73d582b |
73 | if ($self->parent_source) { |
74 | my $schema = $self->schema; |
75 | my $parent_name = $self->parent_source->name; |
76 | my ($parent) = |
77 | grep { $_->name eq $parent_name } |
78 | map $schema->source($_), $schema->sources; |
79 | confess "Couldn't find attached source for parent $parent_name - did you use load_classes? This module is only compatible with load_namespaces" |
80 | unless $parent; |
81 | $self->parent_source($parent); # so our parent is the one in this schema |
82 | } |
7abe3af2 |
83 | } |
84 | return $ret; |
85 | } |
86 | |
c73d582b |
87 | method attach_additional_sources () { |
4d88a8d7 |
88 | my $raw_name = $self->raw_source_name; |
ca79850d |
89 | my $schema = $self->schema; |
90 | |
91 | # if the raw source is already present we can assume we're done |
92 | return if grep { $_ eq $raw_name } $schema->sources; |
4d88a8d7 |
93 | |
ca79850d |
94 | # our parent should've been registered already actually due to DBIC |
95 | # attaching subclass sources later in load_namespaces |
4d88a8d7 |
96 | |
ca79850d |
97 | my $parent; |
98 | if ($self->parent_source) { |
99 | my $parent_name = $self->parent_source->name; |
100 | ($parent) = |
101 | grep { $_->name eq $parent_name } |
102 | map $schema->source($_), $schema->sources; |
103 | confess "Couldn't find attached source for parent $parent_name - did you use load_classes? This module is only compatible with load_namespaces" |
104 | unless $parent; |
05fd2477 |
105 | $self->parent_source($parent); # so our parent is the one in this schema |
ca79850d |
106 | } |
4d88a8d7 |
107 | |
108 | # create the raw table source |
109 | |
110 | my $table = Table->new({ name => $self->raw_table_name }); |
111 | |
ca79850d |
112 | # we don't need to add the PK cols explicitly if we're the root table |
4d88a8d7 |
113 | # since they'll get added below |
114 | |
803ffff2 |
115 | my %pk_join; |
116 | |
ca79850d |
117 | if ($parent) { |
ca79850d |
118 | foreach my $pri ($self->primary_columns) { |
119 | my %info = %{$self->column_info($pri)}; |
120 | delete @info{qw(is_auto_increment sequence auto_nextval)}; |
7abe3af2 |
121 | $table->add_column($pri => \%info); |
803ffff2 |
122 | $pk_join{"foreign.${pri}"} = "self.${pri}"; |
ca79850d |
123 | } |
4d88a8d7 |
124 | # have to use source name lookups rather than result class here |
125 | # because we don't actually have a result class on the raw sources |
803ffff2 |
126 | $table->add_relationship('parent', $parent->raw_source_name, \%pk_join); |
127 | $self->depends_on->{$parent->source_name} = 1; |
128 | } |
129 | |
130 | foreach my $add (@{$self->additional_parents||[]}) { |
131 | $table->add_relationship( |
132 | 'parent_'.$add->name, $add->source_name, \%pk_join |
133 | ); |
134 | $self->depends_on->{$add->source_name} = 1; |
ca79850d |
135 | } |
4d88a8d7 |
136 | |
137 | # add every column that's actually a concrete part of us |
138 | |
139 | $table->add_columns( |
140 | map { ($_ => { %{$self->column_info($_)} }) } |
141 | grep { $self->column_info($_)->{originally_defined_in} eq $self->name } |
142 | $self->columns |
143 | ); |
ca79850d |
144 | $table->set_primary_key($self->primary_columns); |
490d5481 |
145 | |
146 | # we need to copy our rels to the raw object as well |
147 | # note that ->add_relationship on a source object doesn't create an |
148 | # accessor so we can leave that part in the attributes |
149 | |
150 | # if the other side is a table then we need to copy any rels it has |
151 | # back to us, as well, so that they point at the raw table. if the |
152 | # other side is an MTI view then we need to create the rels to it to |
153 | # point at -its- raw table; we don't need to worry about backrels because |
154 | # it's going to run this method too (and its raw source might not exist |
155 | # yet so we can't, anyway) |
156 | |
157 | foreach my $rel ($self->relationships) { |
158 | my $rel_info = $self->relationship_info($rel); |
159 | |
803ffff2 |
160 | # if we got this from the superclass, -its- raw table will nail this. |
161 | # if we got it from an additional parent, it's its problem. |
162 | next unless $rel_info->{attrs}{originally_defined_in} eq $self->name; |
163 | |
490d5481 |
164 | my $f_source = $schema->source($rel_info->{source}); |
165 | |
166 | # __PACKAGE__ is correct here because subclasses should be caught |
167 | |
168 | my $one_of_us = $f_source->isa(__PACKAGE__); |
169 | |
170 | my $f_source_name = $f_source->${\ |
171 | ($one_of_us ? 'raw_source_name' : 'source_name') |
172 | }; |
173 | |
174 | $table->add_relationship( |
175 | '_'.$rel, $f_source_name, @{$rel_info}{qw(cond attrs)} |
176 | ); |
177 | |
178 | unless ($one_of_us) { |
179 | my $reverse = do { |
180 | # we haven't been registered yet, so reverse_ cries |
181 | # XXX this is evil and will probably break eventually |
182 | local @{$schema->source_registrations} |
183 | {map $self->$_, qw(source_name result_class)} |
184 | = ($self, $self); |
185 | $self->reverse_relationship_info($rel); |
186 | }; |
187 | foreach my $rev_rel (keys %$reverse) { |
188 | $f_source->add_relationship( |
189 | '_raw_'.$rev_rel, $raw_name, @{$reverse->{$rev_rel}}{qw(cond attrs)} |
190 | ); |
191 | } |
192 | } |
193 | } |
194 | |
ca79850d |
195 | $schema->register_source($raw_name => $table); |
196 | } |
197 | |
198 | method set_primary_key (@args) { |
199 | if ($self->parent_source) { |
200 | confess "Can't set primary key on a subclass"; |
201 | } |
202 | return $self->next::method(@args); |
876f6525 |
203 | } |
204 | |
4d88a8d7 |
205 | method raw_source_name () { |
876f6525 |
206 | my $base = $self->source_name; |
05fd2477 |
207 | confess "Can't generate raw source name for ${\$self->name} when we don't have a source_name" |
876f6525 |
208 | unless $base; |
209 | return 'Raw::'.$base; |
210 | } |
70d56286 |
211 | |
4d88a8d7 |
212 | method raw_table_name () { |
213 | return '_'.$self->name; |
214 | } |
215 | |
876f6525 |
216 | method add_columns (@args) { |
217 | my $ret = $self->next::method(@args); |
218 | $_->{originally_defined_in} ||= $self->name for values %{$self->_columns}; |
219 | return $ret; |
70d56286 |
220 | } |
221 | |
803ffff2 |
222 | method add_relationship ($name, $f_source, $cond, $attrs) { |
223 | $self->next::method( |
224 | $name, $f_source, $cond, |
225 | { originally_defined_in => $self->name, %{$attrs||{}}, } |
226 | ); |
227 | } |
228 | |
487f4489 |
229 | BEGIN { |
230 | |
231 | # helper routines, constructed as anon subs so autoclean nukes them |
232 | |
233 | use signatures; |
234 | |
235 | *argify = sub (@names) { |
236 | map '_'.$_, @names; |
237 | }; |
238 | |
239 | *qualify_with = sub ($source, @names) { |
92ebfc06 |
240 | my $name = blessed($source) ? $source->name : $source; |
241 | map join('.', $name, $_), @names; |
487f4489 |
242 | }; |
243 | |
244 | *body_cols = sub ($source) { |
245 | my %pk; @pk{$source->primary_columns} = (); |
246 | map +{ %{$source->column_info($_)}, name => $_ }, |
247 | grep !exists $pk{$_}, $source->columns; |
248 | }; |
249 | |
250 | *pk_cols = sub ($source) { |
251 | map +{ %{$source->column_info($_)}, name => $_ }, |
252 | $source->primary_columns; |
253 | }; |
254 | |
92ebfc06 |
255 | *names_of = sub (@cols) { map $_->{name}, @cols }; |
487f4489 |
256 | |
05fd2477 |
257 | *function_body = sub ($name, $args, $body_parts) { |
258 | my $arglist = join( |
259 | ', ', |
388d83fc |
260 | map "_${\$_->{name}} ${\uc($_->{data_type})}", |
05fd2477 |
261 | @$args |
262 | ); |
263 | my $body = join("\n", '', map " $_;", @$body_parts); |
264 | return strip tt q{ |
265 | CREATE OR REPLACE FUNCTION [% name %] |
266 | ([% arglist %]) |
267 | RETURNS VOID AS $function$ |
268 | BEGIN |
269 | [%- body %] |
270 | END; |
271 | $function$ LANGUAGE plpgsql; |
272 | }; |
487f4489 |
273 | }; |
487f4489 |
274 | } |
275 | |
05fd2477 |
276 | BEGIN { |
277 | |
278 | use signatures; |
279 | |
280 | *arg_hash = sub ($source) { |
281 | map +($_ => \(argify $_)), names_of body_cols $source; |
282 | }; |
92ebfc06 |
283 | |
284 | *rule_body = sub ($on, $to, $oldlist, $newlist) { |
285 | my $arglist = join(', ', |
286 | (qualify_with 'OLD', names_of @$oldlist), |
287 | (qualify_with 'NEW', names_of @$newlist), |
288 | ); |
289 | $to = $to->name if blessed($to); |
290 | return strip tt q{ |
291 | CREATE RULE _[% to %]_[% on %]_rule AS |
292 | ON [% on | upper %] TO [% to %] |
293 | DO INSTEAD ( |
3c259cfb |
294 | SELECT [% to %]_[% on %]([% arglist %]) |
92ebfc06 |
295 | ); |
296 | }; |
297 | }; |
05fd2477 |
298 | } |
299 | |
300 | method root_table () { |
301 | $self->parent_source |
302 | ? $self->parent_source->root_table |
303 | : $self->schema->source($self->raw_source_name) |
304 | } |
305 | |
487f4489 |
306 | method view_definition () { |
307 | my $schema = $self->schema; |
308 | confess "Can't generate view without connected schema, sorry" |
309 | unless $schema && $schema->storage; |
310 | my $sqla = $schema->storage->sql_maker; |
311 | my @sources = my $table = $self->schema->source($self->raw_source_name); |
312 | my $super_view = $self->parent_source; |
313 | push(@sources, $super_view) if defined($super_view); |
314 | my @body_cols = map body_cols($_), @sources; |
315 | my @pk_cols = pk_cols $self; |
92ebfc06 |
316 | |
317 | # SELECT statement |
318 | |
487f4489 |
319 | my $select = $sqla->select( |
320 | ($super_view |
321 | ? ([ # FROM _tbl _tbl |
322 | { $table->name => $table->name }, |
323 | [ # JOIN view view |
324 | { $super_view->name => $super_view->name }, |
325 | # ON _tbl.id = view.id |
326 | { map +(qualify_with($super_view, $_), qualify_with($table, $_)), |
327 | names_of @pk_cols } |
328 | ] |
329 | ]) |
330 | : ($table->name)), |
331 | [ (qualify_with $table, names_of @pk_cols), names_of @body_cols ], |
05fd2477 |
332 | ).';'; |
92ebfc06 |
333 | |
05fd2477 |
334 | my ($now, $next) = grep defined, $super_view, $table; |
92ebfc06 |
335 | |
336 | # INSERT function |
337 | |
05fd2477 |
338 | # NOTE: this assumes a single PK col called id with a sequence somewhere |
339 | # but nothing else -should- so fixing this should make everything work |
340 | my $insert_func = |
341 | function_body |
342 | $self->name.'_insert', |
343 | \@body_cols, |
344 | [ |
345 | $sqla->insert( # INSERT INTO _tbl (foo, ...) VALUES (_foo, ...) |
346 | $now->name, |
347 | { arg_hash $now }, |
348 | ), |
349 | ($next |
350 | ? $sqla->insert( # INSERT INTO super_view (id, ...) |
351 | # VALUES (currval('_root_tbl_id_seq'), ...) |
352 | $next->name, |
353 | { |
354 | (arg_hash $next), |
355 | id => \"currval('${\$self->root_table->name}_id_seq')", |
356 | } |
357 | ) |
358 | : () |
359 | ) |
360 | ]; |
92ebfc06 |
361 | |
05fd2477 |
362 | # note - similar to arg_hash but not quite enough to share code sanely |
363 | my $pk_where = { # id = _id AND id2 = _id2 ... |
364 | map +($_ => \"= ${\argify $_}"), names_of @pk_cols |
365 | }; |
92ebfc06 |
366 | |
367 | # UPDATE function |
368 | |
05fd2477 |
369 | my $update_func = |
370 | function_body |
371 | $self->name.'_update', |
372 | [ @pk_cols, @body_cols ], |
373 | [ map $sqla->update( |
374 | $_->name, # UPDATE foo |
375 | { arg_hash $_ }, # SET a = _a |
376 | $pk_where, |
377 | ), @sources |
378 | ]; |
92ebfc06 |
379 | |
380 | # DELETE function |
381 | |
05fd2477 |
382 | my $delete_func = |
383 | function_body |
384 | $self->name.'_delete', |
385 | [ @pk_cols ], |
386 | [ map $sqla->delete($_->name, $pk_where), @sources ]; |
92ebfc06 |
387 | |
388 | my @rules = ( |
389 | (rule_body insert => $self, [], \@body_cols), |
390 | (rule_body update => $self, \@pk_cols, \@body_cols), |
391 | (rule_body delete => $self, \@pk_cols, []), |
392 | ); |
393 | return join("\n\n", $select, $insert_func, $update_func, $delete_func, @rules); |
487f4489 |
394 | } |
395 | |
70d56286 |
396 | 1; |