#
# deploying the postgres rules through SQLT may be a pain though.
-__PACKAGE__->mk_group_accessors(simple => qw(parent_source));
+__PACKAGE__->mk_group_accessors(simple => qw(parent_source additional_parents));
method new ($class: @args) {
my $new = $class->next::method(@args);
return $new;
}
+method add_additional_parents (@classes) {
+ foreach my $class (@classes) {
+ Class::C3::Componentised->ensure_class_loaded($class);
+ $self->add_additional_parent(
+ $class->result_source_instance
+ );
+ }
+}
+
+method add_additional_parent ($source) {
+ my ($our_pk, $their_pk) = map {
+ join('|',sort $_->primary_columns)
+ } ($self, $source);
+
+ confess "Can't attach additional parent ${\$source->name} - it has different PKs ($their_pk versus our $our_pk)"
+ unless $their_pk eq $our_pk;
+ $self->additional_parents([
+ @{$self->additional_parents||[]}, $source
+ ]);
+ $self->add_columns(
+ map {
+ $_ => # put the extra key first to default it
+ { originally_defined_in => $source->name, %{$source->column_info($_)}, },
+ } grep !$self->has_column($_), $source->columns
+ );
+ foreach my $rel ($source->relationships) {
+ my $rel_info = $source->relationship_info($rel);
+ $self->add_relationship(
+ $rel, $rel_info->{source}, $rel_info->{cond},
+ # extra key first to default it
+ {originally_defined_in => $source->name, %{$rel_info->{attrs}}},
+ );
+ }
+ { no strict 'refs';
+ push(@{$self->result_class.'::ISA'}, $source->result_class);
+ }
+}
+
+method _source_by_name ($name) {
+ my $schema = $self->schema;
+ my ($source) =
+ grep { $_->name eq $name }
+ map $schema->source($_), $schema->sources;
+ confess "Couldn't find attached source for parent $name - did you use load_classes? This module is only compatible with load_namespaces"
+ unless $source;
+ return $source;
+}
+
method schema (@args) {
my $ret = $self->next::method(@args);
if (@args) {
- $self->_attach_additional_sources;
+ if ($self->parent_source) {
+ my $parent_name = $self->parent_source->name;
+ $self->parent_source($self->_source_by_name($parent_name));
+ }
+ $self->additional_parents([
+ map { $self->_source_by_name($_->name) }
+ @{$self->additional_parents||[]}
+ ]);
}
return $ret;
}
-method _attach_additional_sources () {
+method attach_additional_sources () {
my $raw_name = $self->raw_source_name;
my $schema = $self->schema;
# we don't need to add the PK cols explicitly if we're the root table
# since they'll get added below
+ my %pk_join;
+
if ($parent) {
- my %join;
foreach my $pri ($self->primary_columns) {
my %info = %{$self->column_info($pri)};
delete @info{qw(is_auto_increment sequence auto_nextval)};
$table->add_column($pri => \%info);
- $join{"foreign.${pri}"} = "self.${pri}";
+ $pk_join{"foreign.${pri}"} = "self.${pri}";
}
# have to use source name lookups rather than result class here
# because we don't actually have a result class on the raw sources
- $table->add_relationship('parent', $parent->raw_source_name, \%join);
+ $table->add_relationship('parent', $parent->raw_source_name, \%pk_join);
+ $self->depends_on->{$parent->source_name} = 1;
+ }
+
+ foreach my $add (@{$self->additional_parents||[]}) {
+ $table->add_relationship(
+ 'parent_'.$add->name, $add->source_name, \%pk_join
+ );
+ $self->depends_on->{$add->source_name} = 1;
}
# add every column that's actually a concrete part of us
foreach my $rel ($self->relationships) {
my $rel_info = $self->relationship_info($rel);
+ # if we got this from the superclass, -its- raw table will nail this.
+ # if we got it from an additional parent, it's its problem.
+ next unless $rel_info->{attrs}{originally_defined_in} eq $self->name;
+
my $f_source = $schema->source($rel_info->{source});
# __PACKAGE__ is correct here because subclasses should be caught
return $ret;
}
+method add_relationship ($name, $f_source, $cond, $attrs) {
+ $self->next::method(
+ $name, $f_source, $cond,
+ { originally_defined_in => $self->name, %{$attrs||{}}, }
+ );
+}
+
BEGIN {
# helper routines, constructed as anon subs so autoclean nukes them
CREATE RULE _[% to %]_[% on %]_rule AS
ON [% on | upper %] TO [% to %]
DO INSTEAD (
- SELECT _[% to %]_[% on %]([% arglist %])
+ SELECT [% to %]_[% on %]([% arglist %])
);
};
};
confess "Can't generate view without connected schema, sorry"
unless $schema && $schema->storage;
my $sqla = $schema->storage->sql_maker;
- my @sources = my $table = $self->schema->source($self->raw_source_name);
+ my $table = $self->schema->source($self->raw_source_name);
my $super_view = $self->parent_source;
- push(@sources, $super_view) if defined($super_view);
+ my @all_parents = my @other_parents = @{$self->additional_parents||[]};
+ push(@all_parents, $super_view) if defined($super_view);
+ my @sources = ($table, @all_parents);
my @body_cols = map body_cols($_), @sources;
my @pk_cols = pk_cols $self;
# SELECT statement
+ my $am_root = !($super_view || @other_parents);
+
my $select = $sqla->select(
- ($super_view
- ? ([ # FROM _tbl _tbl
+ ($am_root
+ ? ($table->name)
+ : ([ # FROM _tbl _tbl
{ $table->name => $table->name },
- [ # JOIN view view
- { $super_view->name => $super_view->name },
- # ON _tbl.id = view.id
- { map +(qualify_with($super_view, $_), qualify_with($table, $_)),
- names_of @pk_cols }
- ]
+ map {
+ my $parent = $_;
+ [ # JOIN view view
+ { $parent->name => $parent->name },
+ # ON _tbl.id = view.id
+ { map +(qualify_with($parent, $_), qualify_with($table, $_)),
+ names_of @pk_cols }
+ ]
+ } @all_parents
])
- : ($table->name)),
+ ),
[ (qualify_with $table, names_of @pk_cols), names_of @body_cols ],
).';';
- my ($now, $next) = grep defined, $super_view, $table;
+ my ($now, @next) = grep defined, $super_view, $table, @other_parents;
# INSERT function
$self->name.'_insert',
\@body_cols,
[
- $sqla->insert( # INSERT INTO _tbl (foo, ...) VALUES (_foo, ...)
+ $sqla->insert( # INSERT INTO tbl/super_view (foo, ...) VALUES (_foo, ...)
$now->name,
{ arg_hash $now },
),
- ($next
- ? $sqla->insert( # INSERT INTO super_view (id, ...)
- # VALUES (currval('_root_tbl_id_seq'), ...)
- $next->name,
- {
- (arg_hash $next),
- id => \"currval('${\$self->root_table->name}_id_seq')",
- }
- )
- : ()
- )
+ (map {
+ $sqla->insert( # INSERT INTO parent (id, ...)
+ # VALUES (currval('_root_tbl_id_seq'), ...)
+ $_->name,
+ {
+ (arg_hash $_),
+ id => \"currval('${\$self->root_table->name}_id_seq')",
+ }
+ )
+ } @next)
];
# note - similar to arg_hash but not quite enough to share code sanely