f27eab6726a58c0351d535eceb145c5b5e3fe876
[dbsrgits/DBIx-Class-ResultSource-MultipleTableInheritance.git] / lib / DBIx / Class / ResultSource / MultipleTableInheritance.pm
1 package DBIx::Class::ResultSource::MultipleTableInheritance;
2
3 use strict;
4 use warnings;
5 use parent qw(DBIx::Class::ResultSource::View);
6 use Method::Signatures::Simple;
7 use Carp::Clan qw/^DBIx::Class/;
8 use aliased 'DBIx::Class::ResultSource::Table';
9 use aliased 'DBIx::Class::ResultClass::HashRefInflator';
10 use namespace::autoclean;
11
12 # how this works:
13 #
14 # On construction, we hook $self->result_class->result_source_instance
15 # if present to get the superclass' source object
16
17 # When attached to a schema, we need to add sources to that schema with
18 # appropriate relationships for the foreign keys so the concrete tables
19 # get generated
20 #
21 # We also generate our own view definition using this class' concrete table
22 # and the view for the superclass, and stored procedures for the insert,
23 # update and delete operations on this view.
24 #
25 # deploying the postgres rules through SQLT may be a pain though.
26
27 __PACKAGE__->mk_group_accessors(simple => qw(parent_source));
28
29 method new ($class: @args) {
30   my $new = $class->next::method(@args);
31   my $rc = $new->result_class;
32   if (my $meth = $rc->can('result_source_instance')) {
33     my $source = $rc->$meth;
34     if ($source->result_class ne $new->result_class
35         && $new->result_class->isa($source->result_class)) {
36       $new->parent_source($source);
37     }
38   }
39   return $new;
40 }
41
42 method schema (@args) {
43   my $ret = $self->next::method(@args);
44   if (@args) {
45     $self->_attach_additional_sources;
46   }
47   return $ret;
48 }
49
50 method _attach_additional_sources () {
51   my $raw_name = $self->_raw_source_name;
52   my $schema = $self->schema;
53
54   # if the raw source is already present we can assume we're done
55   return if grep { $_ eq $raw_name } $schema->sources;
56   # our parent should've been registered already actually due to DBIC
57   # attaching subclass sources later in load_namespaces
58   my $parent;
59   if ($self->parent_source) {
60       my $parent_name = $self->parent_source->name;
61     ($parent) = 
62       grep { $_->name eq $parent_name }
63         map $schema->source($_), $schema->sources;
64     confess "Couldn't find attached source for parent $parent_name - did you use load_classes? This module is only compatible with load_namespaces"
65       unless $parent;
66   }
67   my $table = Table->new({ name => '_'.$self->name });
68   $table->add_columns(
69     map { ($_ => { %{$self->column_info($_)} }) }
70       grep { $self->column_info($_)->{originally_defined_in} eq $self->name }
71         $self->columns
72   );
73   # we don't need to add the PK cols explicitly if we're the root table
74   # since they already got added above
75   if ($parent) {
76     my %join;
77     foreach my $pri ($self->primary_columns) {
78       my %info = %{$self->column_info($pri)};
79       delete @info{qw(is_auto_increment sequence auto_nextval)};
80       $table->add_column($pri => \%info);
81       $join{"foreign.${pri}"} = "self.${pri}";
82     }
83     $table->add_relationship('parent', $parent->source_name, \%join);
84   }
85   $table->set_primary_key($self->primary_columns);
86   $schema->register_source($raw_name => $table);
87 }
88
89 method set_primary_key (@args) {
90   if ($self->parent_source) {
91     confess "Can't set primary key on a subclass";
92   }
93   return $self->next::method(@args);
94 }
95
96 method _raw_source_name () {
97   my $base = $self->source_name;
98   confess "Can't generate raw source name when we don't have a source_name"
99     unless $base;
100   return 'Raw::'.$base;
101 }
102
103 method add_columns (@args) {
104   my $ret = $self->next::method(@args);
105   $_->{originally_defined_in} ||= $self->name for values %{$self->_columns};
106   return $ret;
107 }
108
109 1;