af0f4a49c77249950fc4ed1fa54f6131a836d3c3
[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 namespace::autoclean;
7 use Method::Signatures::Simple;
8 use Carp::Clan qw/^DBIx::Class/;
9
10 # how this works:
11 #
12 # On construction, we hook $self->result_class->result_source_instance
13 # if present to get the superclass' source object
14
15 # When attached to a schema, we need to add sources to that schema with
16 # appropriate relationships for the foreign keys so the concrete tables
17 # get generated
18 #
19 # We also generate our own view definition using this class' concrete table
20 # and the view for the superclass, and stored procedures for the insert,
21 # update and delete operations on this view.
22 #
23 # deploying the postgres rules through SQLT may be a pain though.
24
25 __PACKAGE__->mk_group_accessors(simple => qw(parent_source));
26
27 method new ($class: @args) {
28   my $new = $class->next::method(@args);
29   my $rc = $new->result_class;
30   if (my $meth = $rc->can('result_source_instance')) {
31     $new->parent_source($rc->$meth);
32   }
33   if ($new->schema) {
34     $new->_attach_additional_sources;
35   }
36   return $new;
37 }
38
39 method _attach_additional_sources () {
40   my $raw_name = $self->_raw_source_name;
41 }
42
43 method _raw_source_name () {
44   my $base = $self->source_name;
45   confess "Can't generate raw source name when we don't have a source_name"
46     unless $base;
47   return 'Raw::'.$base;
48 }
49
50 method add_columns (@args) {
51   my $ret = $self->next::method(@args);
52   $_->{originally_defined_in} ||= $self->name for values %{$self->_columns};
53   return $ret;
54 }
55
56 1;