1 package DBIx::Class::ResultSource::MultipleTableInheritance;
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 String::TT qw(strip tt);
11 use namespace::autoclean;
15 # On construction, we hook $self->result_class->result_source_instance
16 # if present to get the superclass' source object
18 # When attached to a schema, we need to add sources to that schema with
19 # appropriate relationships for the foreign keys so the concrete tables
22 # We also generate our own view definition using this class' concrete table
23 # and the view for the superclass, and stored procedures for the insert,
24 # update and delete operations on this view.
26 # deploying the postgres rules through SQLT may be a pain though.
28 __PACKAGE__->mk_group_accessors(simple => qw(parent_source));
30 method new ($class: @args) {
31 my $new = $class->next::method(@args);
32 my $rc = $new->result_class;
33 if (my $meth = $rc->can('result_source_instance')) {
34 my $source = $rc->$meth;
35 if ($source->result_class ne $new->result_class
36 && $new->result_class->isa($source->result_class)) {
37 $new->parent_source($source);
43 method schema (@args) {
44 my $ret = $self->next::method(@args);
46 $self->_attach_additional_sources;
51 method _attach_additional_sources () {
52 my $raw_name = $self->raw_source_name;
53 my $schema = $self->schema;
55 # if the raw source is already present we can assume we're done
56 return if grep { $_ eq $raw_name } $schema->sources;
58 # our parent should've been registered already actually due to DBIC
59 # attaching subclass sources later in load_namespaces
62 if ($self->parent_source) {
63 my $parent_name = $self->parent_source->name;
65 grep { $_->name eq $parent_name }
66 map $schema->source($_), $schema->sources;
67 confess "Couldn't find attached source for parent $parent_name - did you use load_classes? This module is only compatible with load_namespaces"
69 $self->parent_source($parent); # so our parent is the one in this schema
72 # create the raw table source
74 my $table = Table->new({ name => $self->raw_table_name });
76 # we don't need to add the PK cols explicitly if we're the root table
77 # since they'll get added below
81 foreach my $pri ($self->primary_columns) {
82 my %info = %{$self->column_info($pri)};
83 delete @info{qw(is_auto_increment sequence auto_nextval)};
84 $table->add_column($pri => \%info);
85 $join{"foreign.${pri}"} = "self.${pri}";
87 # have to use source name lookups rather than result class here
88 # because we don't actually have a result class on the raw sources
89 $table->add_relationship('parent', $parent->raw_source_name, \%join);
92 # add every column that's actually a concrete part of us
95 map { ($_ => { %{$self->column_info($_)} }) }
96 grep { $self->column_info($_)->{originally_defined_in} eq $self->name }
99 $table->set_primary_key($self->primary_columns);
100 $schema->register_source($raw_name => $table);
103 method set_primary_key (@args) {
104 if ($self->parent_source) {
105 confess "Can't set primary key on a subclass";
107 return $self->next::method(@args);
110 method raw_source_name () {
111 my $base = $self->source_name;
112 confess "Can't generate raw source name for ${\$self->name} when we don't have a source_name"
114 return 'Raw::'.$base;
117 method raw_table_name () {
118 return '_'.$self->name;
121 method add_columns (@args) {
122 my $ret = $self->next::method(@args);
123 $_->{originally_defined_in} ||= $self->name for values %{$self->_columns};
129 # helper routines, constructed as anon subs so autoclean nukes them
133 *argify = sub (@names) {
137 *qualify_with = sub ($source, @names) {
138 map join('.', $source->name, $_), @names;
141 *body_cols = sub ($source) {
142 my %pk; @pk{$source->primary_columns} = ();
143 map +{ %{$source->column_info($_)}, name => $_ },
144 grep !exists $pk{$_}, $source->columns;
147 *pk_cols = sub ($source) {
148 map +{ %{$source->column_info($_)}, name => $_ },
149 $source->primary_columns;
152 *name_of = *names_of = sub (@cols) { map $_->{name}, @cols };
154 *function_body = sub ($name, $args, $body_parts) {
157 map '_'.join(' ', @{$_}{qw(name data_type)}),
160 my $body = join("\n", '', map " $_;", @$body_parts);
162 CREATE OR REPLACE FUNCTION [% name %]
164 RETURNS VOID AS $function$
168 $function$ LANGUAGE plpgsql;
178 *arg_hash = sub ($source) {
179 map +($_ => \(argify $_)), names_of body_cols $source;
183 method root_table () {
185 ? $self->parent_source->root_table
186 : $self->schema->source($self->raw_source_name)
189 method view_definition () {
190 my $schema = $self->schema;
191 confess "Can't generate view without connected schema, sorry"
192 unless $schema && $schema->storage;
193 my $sqla = $schema->storage->sql_maker;
194 my @sources = my $table = $self->schema->source($self->raw_source_name);
195 my $super_view = $self->parent_source;
196 push(@sources, $super_view) if defined($super_view);
197 my @body_cols = map body_cols($_), @sources;
198 my @pk_cols = pk_cols $self;
199 my $select = $sqla->select(
201 ? ([ # FROM _tbl _tbl
202 { $table->name => $table->name },
204 { $super_view->name => $super_view->name },
205 # ON _tbl.id = view.id
206 { map +(qualify_with($super_view, $_), qualify_with($table, $_)),
211 [ (qualify_with $table, names_of @pk_cols), names_of @body_cols ],
213 my ($now, $next) = grep defined, $super_view, $table;
214 # NOTE: this assumes a single PK col called id with a sequence somewhere
215 # but nothing else -should- so fixing this should make everything work
218 $self->name.'_insert',
221 $sqla->insert( # INSERT INTO _tbl (foo, ...) VALUES (_foo, ...)
226 ? $sqla->insert( # INSERT INTO super_view (id, ...)
227 # VALUES (currval('_root_tbl_id_seq'), ...)
231 id => \"currval('${\$self->root_table->name}_id_seq')",
237 # note - similar to arg_hash but not quite enough to share code sanely
238 my $pk_where = { # id = _id AND id2 = _id2 ...
239 map +($_ => \"= ${\argify $_}"), names_of @pk_cols
243 $self->name.'_update',
244 [ @pk_cols, @body_cols ],
246 $_->name, # UPDATE foo
247 { arg_hash $_ }, # SET a = _a
253 $self->name.'_delete',
255 [ map $sqla->delete($_->name, $pk_where), @sources ];
256 return join("\n\n", $select, $insert_func, $update_func, $delete_func);