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