Nailing down errors in deployment.
[dbsrgits/DBIx-Class-ResultSource-MultipleTableInheritance.git] / lib / DBIx / Class / ResultSource / MultipleTableInheritance.pm
CommitLineData
876f6525 1package DBIx::Class::ResultSource::MultipleTableInheritance;
2
3use strict;
4use warnings;
5use parent qw(DBIx::Class::ResultSource::View);
876f6525 6use Method::Signatures::Simple;
7use Carp::Clan qw/^DBIx::Class/;
ca79850d 8use aliased 'DBIx::Class::ResultSource::Table';
7abe3af2 9use aliased 'DBIx::Class::ResultClass::HashRefInflator';
05fd2477 10use String::TT qw(strip tt);
92ebfc06 11use Scalar::Util qw(blessed);
ca79850d 12use namespace::autoclean;
c8e085ba 13use Devel::Dwarn;
70d56286 14
146ec120 15our $VERSION = 0.01;
70d56286 16
803ffff2 17__PACKAGE__->mk_group_accessors(simple => qw(parent_source additional_parents));
876f6525 18
e7189506 19# how this works:
20#
21# On construction, we hook $self->result_class->result_source_instance
22# if present to get the superclass' source object
23#
24# When attached to a schema, we need to add sources to that schema with
25# appropriate relationships for the foreign keys so the concrete tables
26# get generated
27#
28# We also generate our own view definition using this class' concrete table
29# and the view for the superclass, and stored procedures for the insert,
30# update and delete operations on this view.
31#
32# deploying the postgres rules through SQLT may be a pain though.
33
876f6525 34method new ($class: @args) {
35 my $new = $class->next::method(@args);
36 my $rc = $new->result_class;
37 if (my $meth = $rc->can('result_source_instance')) {
7abe3af2 38 my $source = $rc->$meth;
39 if ($source->result_class ne $new->result_class
40 && $new->result_class->isa($source->result_class)) {
41 $new->parent_source($source);
42 }
876f6525 43 }
44 return $new;
45}
46
4e4f71e3 47method add_additional_parents (@classes) {
48 foreach my $class (@classes) {
49 Class::C3::Componentised->ensure_class_loaded($class);
50 $self->add_additional_parent(
51 $class->result_source_instance
52 );
53 }
54}
55
803ffff2 56method add_additional_parent ($source) {
57 my ($our_pk, $their_pk) = map {
58 join('|',sort $_->primary_columns)
59 } ($self, $source);
60
61 confess "Can't attach additional parent ${\$source->name} - it has different PKs ($their_pk versus our $our_pk)"
62 unless $their_pk eq $our_pk;
63 $self->additional_parents([
64 @{$self->additional_parents||[]}, $source
65 ]);
66 $self->add_columns(
67 map {
68 $_ => # put the extra key first to default it
69 { originally_defined_in => $source->name, %{$source->column_info($_)}, },
70 } grep !$self->has_column($_), $source->columns
71 );
72 foreach my $rel ($source->relationships) {
73 my $rel_info = $source->relationship_info($rel);
74 $self->add_relationship(
75 $rel, $rel_info->{source}, $rel_info->{cond},
76 # extra key first to default it
77 {originally_defined_in => $source->name, %{$rel_info->{attrs}}},
78 );
79 }
a010ebf9 80 { no strict 'refs';
81 push(@{$self->result_class.'::ISA'}, $source->result_class);
82 }
803ffff2 83}
84
8b229aa6 85method _source_by_name ($name) {
86 my $schema = $self->schema;
87 my ($source) =
88 grep { $_->name eq $name }
89 map $schema->source($_), $schema->sources;
90 confess "Couldn't find attached source for parent $name - did you use load_classes? This module is only compatible with load_namespaces"
91 unless $source;
92 return $source;
93}
94
7abe3af2 95method schema (@args) {
96 my $ret = $self->next::method(@args);
97 if (@args) {
c73d582b 98 if ($self->parent_source) {
c73d582b 99 my $parent_name = $self->parent_source->name;
8b229aa6 100 $self->parent_source($self->_source_by_name($parent_name));
c73d582b 101 }
8b229aa6 102 $self->additional_parents([
103 map { $self->_source_by_name($_->name) }
104 @{$self->additional_parents||[]}
105 ]);
7abe3af2 106 }
107 return $ret;
108}
109
c73d582b 110method attach_additional_sources () {
4d88a8d7 111 my $raw_name = $self->raw_source_name;
ca79850d 112 my $schema = $self->schema;
113
114 # if the raw source is already present we can assume we're done
115 return if grep { $_ eq $raw_name } $schema->sources;
4d88a8d7 116
ca79850d 117 # our parent should've been registered already actually due to DBIC
118 # attaching subclass sources later in load_namespaces
4d88a8d7 119
ca79850d 120 my $parent;
121 if ($self->parent_source) {
122 my $parent_name = $self->parent_source->name;
123 ($parent) =
124 grep { $_->name eq $parent_name }
125 map $schema->source($_), $schema->sources;
126 confess "Couldn't find attached source for parent $parent_name - did you use load_classes? This module is only compatible with load_namespaces"
127 unless $parent;
05fd2477 128 $self->parent_source($parent); # so our parent is the one in this schema
ca79850d 129 }
4d88a8d7 130
131 # create the raw table source
132
133 my $table = Table->new({ name => $self->raw_table_name });
134
ca79850d 135 # we don't need to add the PK cols explicitly if we're the root table
4d88a8d7 136 # since they'll get added below
137
803ffff2 138 my %pk_join;
139
ca79850d 140 if ($parent) {
ca79850d 141 foreach my $pri ($self->primary_columns) {
142 my %info = %{$self->column_info($pri)};
143 delete @info{qw(is_auto_increment sequence auto_nextval)};
7abe3af2 144 $table->add_column($pri => \%info);
803ffff2 145 $pk_join{"foreign.${pri}"} = "self.${pri}";
ca79850d 146 }
4d88a8d7 147 # have to use source name lookups rather than result class here
148 # because we don't actually have a result class on the raw sources
803ffff2 149 $table->add_relationship('parent', $parent->raw_source_name, \%pk_join);
c8e085ba 150 $self->deploy_depends_on->{$parent->result_class} = 1;
803ffff2 151 }
152
153 foreach my $add (@{$self->additional_parents||[]}) {
154 $table->add_relationship(
155 'parent_'.$add->name, $add->source_name, \%pk_join
156 );
c8e085ba 157 $self->deploy_depends_on->{$add->result_class} = 1;
ca79850d 158 }
4d88a8d7 159
160 # add every column that's actually a concrete part of us
161
162 $table->add_columns(
163 map { ($_ => { %{$self->column_info($_)} }) }
164 grep { $self->column_info($_)->{originally_defined_in} eq $self->name }
165 $self->columns
166 );
ca79850d 167 $table->set_primary_key($self->primary_columns);
490d5481 168
169 # we need to copy our rels to the raw object as well
170 # note that ->add_relationship on a source object doesn't create an
171 # accessor so we can leave that part in the attributes
172
173 # if the other side is a table then we need to copy any rels it has
174 # back to us, as well, so that they point at the raw table. if the
175 # other side is an MTI view then we need to create the rels to it to
176 # point at -its- raw table; we don't need to worry about backrels because
177 # it's going to run this method too (and its raw source might not exist
178 # yet so we can't, anyway)
179
180 foreach my $rel ($self->relationships) {
181 my $rel_info = $self->relationship_info($rel);
182
803ffff2 183 # if we got this from the superclass, -its- raw table will nail this.
184 # if we got it from an additional parent, it's its problem.
185 next unless $rel_info->{attrs}{originally_defined_in} eq $self->name;
186
490d5481 187 my $f_source = $schema->source($rel_info->{source});
188
189 # __PACKAGE__ is correct here because subclasses should be caught
190
191 my $one_of_us = $f_source->isa(__PACKAGE__);
192
193 my $f_source_name = $f_source->${\
194 ($one_of_us ? 'raw_source_name' : 'source_name')
195 };
196
197 $table->add_relationship(
198 '_'.$rel, $f_source_name, @{$rel_info}{qw(cond attrs)}
199 );
200
201 unless ($one_of_us) {
202 my $reverse = do {
203 # we haven't been registered yet, so reverse_ cries
204 # XXX this is evil and will probably break eventually
205 local @{$schema->source_registrations}
206 {map $self->$_, qw(source_name result_class)}
207 = ($self, $self);
208 $self->reverse_relationship_info($rel);
209 };
210 foreach my $rev_rel (keys %$reverse) {
211 $f_source->add_relationship(
212 '_raw_'.$rev_rel, $raw_name, @{$reverse->{$rev_rel}}{qw(cond attrs)}
213 );
214 }
215 }
216 }
217
ca79850d 218 $schema->register_source($raw_name => $table);
219}
220
221method set_primary_key (@args) {
222 if ($self->parent_source) {
223 confess "Can't set primary key on a subclass";
224 }
225 return $self->next::method(@args);
876f6525 226}
227
4d88a8d7 228method raw_source_name () {
876f6525 229 my $base = $self->source_name;
05fd2477 230 confess "Can't generate raw source name for ${\$self->name} when we don't have a source_name"
876f6525 231 unless $base;
232 return 'Raw::'.$base;
233}
70d56286 234
4d88a8d7 235method raw_table_name () {
236 return '_'.$self->name;
237}
238
876f6525 239method add_columns (@args) {
240 my $ret = $self->next::method(@args);
241 $_->{originally_defined_in} ||= $self->name for values %{$self->_columns};
242 return $ret;
70d56286 243}
244
803ffff2 245method add_relationship ($name, $f_source, $cond, $attrs) {
246 $self->next::method(
247 $name, $f_source, $cond,
248 { originally_defined_in => $self->name, %{$attrs||{}}, }
249 );
250}
251
487f4489 252BEGIN {
253
254 # helper routines, constructed as anon subs so autoclean nukes them
255
256 use signatures;
257
258 *argify = sub (@names) {
259 map '_'.$_, @names;
260 };
261
262 *qualify_with = sub ($source, @names) {
92ebfc06 263 my $name = blessed($source) ? $source->name : $source;
264 map join('.', $name, $_), @names;
487f4489 265 };
266
267 *body_cols = sub ($source) {
268 my %pk; @pk{$source->primary_columns} = ();
269 map +{ %{$source->column_info($_)}, name => $_ },
270 grep !exists $pk{$_}, $source->columns;
271 };
272
273 *pk_cols = sub ($source) {
274 map +{ %{$source->column_info($_)}, name => $_ },
275 $source->primary_columns;
276 };
277
92ebfc06 278 *names_of = sub (@cols) { map $_->{name}, @cols };
487f4489 279
c8e085ba 280 *function_body = sub {
281 my ($name,$args,$body_parts) = @_;
05fd2477 282 my $arglist = join(
283 ', ',
388d83fc 284 map "_${\$_->{name}} ${\uc($_->{data_type})}",
05fd2477 285 @$args
286 );
287 my $body = join("\n", '', map " $_;", @$body_parts);
288 return strip tt q{
289 CREATE OR REPLACE FUNCTION [% name %]
290 ([% arglist %])
291 RETURNS VOID AS $function$
292 BEGIN
293 [%- body %]
294 END;
295 $function$ LANGUAGE plpgsql;
296 };
487f4489 297 };
c8e085ba 298 #*function_body = sub ($name,$args,$body_parts) {
299 #my $arglist = join(
300 #', ',
301 #map "_${\$_->{name}} ${\uc($_->{data_type})}",
302 #@$args
303 #);
304 #my $body = join("\n", '', map " $_;", @$body_parts);
305 #return strip tt q{
306 #CREATE OR REPLACE FUNCTION [% name %]
307 #([% arglist %])
308 #RETURNS VOID AS $function$
309 #BEGIN
310 #[%- body %]
311 #END;
312 #$function$ LANGUAGE plpgsql;
313 #};
314 #};
487f4489 315}
316
05fd2477 317BEGIN {
318
319 use signatures;
320
321 *arg_hash = sub ($source) {
322 map +($_ => \(argify $_)), names_of body_cols $source;
323 };
92ebfc06 324
325 *rule_body = sub ($on, $to, $oldlist, $newlist) {
326 my $arglist = join(', ',
327 (qualify_with 'OLD', names_of @$oldlist),
328 (qualify_with 'NEW', names_of @$newlist),
329 );
330 $to = $to->name if blessed($to);
331 return strip tt q{
332 CREATE RULE _[% to %]_[% on %]_rule AS
333 ON [% on | upper %] TO [% to %]
334 DO INSTEAD (
3c259cfb 335 SELECT [% to %]_[% on %]([% arglist %])
92ebfc06 336 );
337 };
338 };
05fd2477 339}
340
341method root_table () {
342 $self->parent_source
343 ? $self->parent_source->root_table
344 : $self->schema->source($self->raw_source_name)
345}
346
487f4489 347method view_definition () {
348 my $schema = $self->schema;
349 confess "Can't generate view without connected schema, sorry"
350 unless $schema && $schema->storage;
351 my $sqla = $schema->storage->sql_maker;
2816c8ed 352 my $table = $self->schema->source($self->raw_source_name);
487f4489 353 my $super_view = $self->parent_source;
2816c8ed 354 my @all_parents = my @other_parents = @{$self->additional_parents||[]};
355 push(@all_parents, $super_view) if defined($super_view);
356 my @sources = ($table, @all_parents);
487f4489 357 my @body_cols = map body_cols($_), @sources;
358 my @pk_cols = pk_cols $self;
92ebfc06 359
360 # SELECT statement
361
2816c8ed 362 my $am_root = !($super_view || @other_parents);
363
487f4489 364 my $select = $sqla->select(
2816c8ed 365 ($am_root
366 ? ($table->name)
367 : ([ # FROM _tbl _tbl
487f4489 368 { $table->name => $table->name },
2816c8ed 369 map {
370 my $parent = $_;
371 [ # JOIN view view
372 { $parent->name => $parent->name },
373 # ON _tbl.id = view.id
374 { map +(qualify_with($parent, $_), qualify_with($table, $_)),
375 names_of @pk_cols }
376 ]
377 } @all_parents
487f4489 378 ])
2816c8ed 379 ),
487f4489 380 [ (qualify_with $table, names_of @pk_cols), names_of @body_cols ],
05fd2477 381 ).';';
92ebfc06 382
2816c8ed 383 my ($now, @next) = grep defined, $super_view, $table, @other_parents;
92ebfc06 384
385 # INSERT function
386
05fd2477 387 # NOTE: this assumes a single PK col called id with a sequence somewhere
388 # but nothing else -should- so fixing this should make everything work
389 my $insert_func =
c8e085ba 390 function_body
05fd2477 391 $self->name.'_insert',
392 \@body_cols,
393 [
2816c8ed 394 $sqla->insert( # INSERT INTO tbl/super_view (foo, ...) VALUES (_foo, ...)
05fd2477 395 $now->name,
396 { arg_hash $now },
397 ),
2816c8ed 398 (map {
399 $sqla->insert( # INSERT INTO parent (id, ...)
400 # VALUES (currval('_root_tbl_id_seq'), ...)
401 $_->name,
402 {
403 (arg_hash $_),
404 id => \"currval('${\$self->root_table->name}_id_seq')",
405 }
406 )
407 } @next)
05fd2477 408 ];
92ebfc06 409
05fd2477 410 # note - similar to arg_hash but not quite enough to share code sanely
411 my $pk_where = { # id = _id AND id2 = _id2 ...
412 map +($_ => \"= ${\argify $_}"), names_of @pk_cols
413 };
92ebfc06 414
415 # UPDATE function
416
05fd2477 417 my $update_func =
c8e085ba 418 function_body
05fd2477 419 $self->name.'_update',
420 [ @pk_cols, @body_cols ],
421 [ map $sqla->update(
422 $_->name, # UPDATE foo
423 { arg_hash $_ }, # SET a = _a
424 $pk_where,
425 ), @sources
426 ];
92ebfc06 427
428 # DELETE function
429
05fd2477 430 my $delete_func =
c8e085ba 431 function_body
05fd2477 432 $self->name.'_delete',
433 [ @pk_cols ],
434 [ map $sqla->delete($_->name, $pk_where), @sources ];
92ebfc06 435
436 my @rules = (
437 (rule_body insert => $self, [], \@body_cols),
438 (rule_body update => $self, \@pk_cols, \@body_cols),
439 (rule_body delete => $self, \@pk_cols, []),
440 );
441 return join("\n\n", $select, $insert_func, $update_func, $delete_func, @rules);
487f4489 442}
443
70d56286 4441;
146ec120 445
446__END__
f5c54951 447
146ec120 448=head1 NAME
449
f5c54951 450DBIx::Class::ResultSource::MultipleTableInheritance
451Use multiple tables to define your classes
452
453=head1 NOTICE
454
455This only works with PostgreSQL for the moment.
146ec120 456
457=head1 SYNOPSIS
458
146ec120 459 {
f8864134 460 package Cafe::Result::Coffee;
146ec120 461
f8864134 462 use strict;
463 use warnings;
464 use parent 'DBIx::Class::Core';
465 use aliased 'DBIx::Class::ResultSource::MultipleTableInheritance'
466 => 'MTI';
467
468 __PACKAGE__->table_class(MTI);
146ec120 469 __PACKAGE__->table('coffee');
470 __PACKAGE__->add_columns(
f8864134 471 "id", { data_type => "integer" },
472 "flavor", {
473 data_type => "text",
474 default_value => "good" },
146ec120 475 );
476
477 __PACKAGE__->set_primary_key("id");
478
479 1;
480 }
481
482 {
f8864134 483 package Cafe::Result::Sumatra;
146ec120 484
f8864134 485 use parent 'Cafe::Result::Coffee';
146ec120 486
487 __PACKAGE__->table('sumatra');
488
f8864134 489 __PACKAGE__->add_columns( "aroma",
490 { data_type => "text" }
146ec120 491 );
492
493 1;
494 }
495
496 ...
497
f8864134 498 my $schema = Cafe->connect($dsn,$user,$pass);
146ec120 499
f8864134 500 my $cup = $schema->resultset('Sumatra');
146ec120 501
f8864134 502 print STDERR Dwarn $cup->result_source->columns;
146ec120 503
f8864134 504 "id"
505 "flavor"
506 "aroma"
507 ..
146ec120 508
f5c54951 509Inherit from this package and you can make a resultset class from a view, but
510that's more than a little bit misleading: the result is B<transparently
511writable>.
146ec120 512
f5c54951 513This is accomplished through the use of stored procedures that map changes
514written to the view to changes to the underlying concrete tables.
146ec120 515
516=head1 WHY?
517
f5c54951 518In many applications, many classes are subclasses of others. Let's say you
519have this schema:
146ec120 520
521 # Conceptual domain model
522
523 class User {
f5c54951 524 has id,
525 has name,
526 has password
146ec120 527 }
528
529 class Investor {
530 has id,
531 has name,
532 has password,
533 has dollars
534 }
535
536That's redundant. Hold on a sec...
537
538 class User {
f5c54951 539 has id,
540 has name,
541 has password
146ec120 542 }
543
e7189506 544 class Investor extends User {
146ec120 545 has dollars
546 }
547
548Good idea, but how to put this into code?
549
f5c54951 550One far-too common and absolutely horrendous solution is to have a "checkbox"
551in your database: a nullable "investor" column, which entails a nullable
552"dollars" column, in the user table.
146ec120 553
554 create table "user" (
555 "id" integer not null primary key autoincrement,
556 "name" text not null,
557 "password" text not null,
558 "investor" tinyint(1),
559 "dollars" integer
560 );
561
562Let's not discuss that further.
563
f5c54951 564A second, better, solution is to break out the two tables into user and
565investor:
146ec120 566
567 create table "user" (
568 "id" integer not null primary key autoincrement,
569 "name" text not null,
570 "password" text not null
571 );
572
573 create table "investor" (
574 "id" integer not null references user("id"),
575 "dollars" integer
576 );
577
f5c54951 578So that investor's PK is just an FK to the user. We can clearly see the class
579hierarchy here, in which investor is a subclass of user. In DBIx::Class
580applications, this second strategy looks like:
146ec120 581
582 my $user_rs = $schema->resultset('User');
583 my $new_user = $user_rs->create(
584 name => $args->{name},
585 password => $args->{password},
586 );
587
588 ...
589
590 my $new_investor = $schema->resultset('Investor')->create(
591 id => $new_user->id,
592 dollars => $args->{dollars},
593 );
594
f5c54951 595One can cope well with the second strategy, and it seems to be the most popular
596smart choice.
e7189506 597
146ec120 598=head1 HOW?
599
f5c54951 600There is a third strategy implemented here. Make the database do more of the
601work: hide the nasty bits so we don't have to handle them unless we really want
602to. It'll save us some typing and it'll make for more expressive code. What if
603we could do this:
146ec120 604
605 my $new_investor = $schema->resultset('Investor')->create(
606 name => $args->{name},
607 password => $args->{password},
608 dollars => $args->{dollars},
609 );
610
e7189506 611And have it Just Work? The user...
612
613 {
614 name => $args->{name},
615 password => $args->{password},
616 }
617
f5c54951 618should be created behind the scenes, and the use of either user or investor
619in your code should require no special handling. Deleting and updating
620$new_investor should also delete or update the user row.
146ec120 621
f5c54951 622It does. User and investor are both views, their concrete tables abstracted
623away behind a set of rules and triggers. You would expect the above DBIC
624create statement to look like this in SQL:
146ec120 625
626 INSERT INTO investor ("name","password","dollars") VALUES (...);
627
628But using MTI, it is really this:
629
630 INSERT INTO _user_table ("username","password") VALUES (...);
631 INSERT INTO _investor_table ("id","dollars") VALUES (currval('_user_table_id_seq',...) );
632
f5c54951 633For deletes, the triggers fire in reverse, to preserve referential integrity
634(foreign key constraints). For instance:
146ec120 635
636 my $investor = $schema->resultset('Investor')->find({id => $args->{id}});
637 $investor->delete;
638
639Becomes:
640
641 DELETE FROM _investor_table WHERE ("id" = ?);
642 DELETE FROM _user_table WHERE ("id" = ?);
643
644
e7189506 645=head1 METHODS
646
647=over
648
649=item new
650
651
f5c54951 652MTI find the parents, if any, of your resultset class and adds them to the
653list of parent_sources for the table.
e7189506 654
655
656=item add_additional_parents
657
658
659Continuing with coffee:
660
661 __PACKAGE__->result_source_instance->add_additional_parents(
662 qw/
663 MyApp::Schema::Result::Beverage
664 MyApp::Schema::Result::Liquid
665 /
666 );
667
668This just lets you manually add additional parents beyond the ones MTI finds.
669
670=item add_additional_parent
671
672 __PACKAGE__->result_source_instance->add_additional_parent(
673 MyApp::Schema::Result::Beverage
674 );
675
676You can also add just one.
677
678=item attach_additional_sources
679
f5c54951 680MTI takes the parents' sources and relationships, creates a new
681DBIx::Class::Table object from them, and registers this as a new, raw, source
682in the schema, e.g.,
e7189506 683
684 use MyApp::Schema;
685
686 print STDERR map { "$_\n" } MyApp::Schema->sources;
687
688 # Coffee
689 # Beverage
690 # Liquid
691 # Sumatra
692 # Raw::Sumatra
146ec120 693
e7189506 694Raw::Sumatra will be used to generate the view.
146ec120 695
e7189506 696=item view_definition
146ec120 697
e7189506 698This takes the raw table and generates the view (and stored procedures) you will use.
146ec120 699
e7189506 700=back
146ec120 701
702=head1 AUTHOR
703
704Matt S. Trout, E<lt>mst@shadowcatsystems.co.ukE<gt>
705
706=head2 CONTRIBUTORS
707
f5c54951 708Amiri Barksdale, E<lt>amiri@metalabel.comE<gt>
709
710=head1 COPYRIGHT
711
712Copyright (c) 2010 the DBIx::Class::ResultSource::MultipleTableInheritance
713L</AUTHOR> and L</CONTRIBUTORS> as listed above.
146ec120 714
715=head1 LICENSE
716
717This library is free software; you can redistribute it and/or modify
718it under the same terms as Perl itself.
719
720=head1 SEE ALSO
721
722L<DBIx::Class>
723L<DBIx::Class::ResultSource>
724
725=cut