return $ret;
}
+BEGIN {
+
+ # helper routines, constructed as anon subs so autoclean nukes them
+
+ use signatures;
+
+ *argify = sub (@names) {
+ map '_'.$_, @names;
+ };
+
+ *qualify_with = sub ($source, @names) {
+ map join('.', $source->name, $_), @names;
+ };
+
+ *body_cols = sub ($source) {
+ my %pk; @pk{$source->primary_columns} = ();
+ map +{ %{$source->column_info($_)}, name => $_ },
+ grep !exists $pk{$_}, $source->columns;
+ };
+
+ *pk_cols = sub ($source) {
+ map +{ %{$source->column_info($_)}, name => $_ },
+ $source->primary_columns;
+ };
+
+ *names_of = sub (@cols) { map $_->{name}, @cols };
+
+ *arglist = sub (@cols) {
+ map join(' ', @{$_}{qw(name data_type)}), @cols;
+ };
+
+}
+
+method view_definition () {
+ my $schema = $self->schema;
+ 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 $super_view = $self->parent_source;
+ push(@sources, $super_view) if defined($super_view);
+ my @body_cols = map body_cols($_), @sources;
+ my @pk_cols = pk_cols $self;
+ my $select = $sqla->select(
+ ($super_view
+ ? ([ # 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 }
+ ]
+ ])
+ : ($table->name)),
+ [ (qualify_with $table, names_of @pk_cols), names_of @body_cols ],
+ );
+ return $select;
+}
+
1;
--- /dev/null
+use strict;
+use warnings;
+use lib 't/lib';
+use Test::More qw(no_plan);
+use Test::Exception;
+use Data::Dumper; $Data::Dumper::Indent = 1;
+
+BEGIN { use_ok 'MTITest'; }
+
+dies_ok { MTITest->source('Foo')->view_definition }
+ "Can't generate view def without connected schema";
+
+my $schema = MTITest->connect('dbi:SQLite::memory:');
+
+warn $schema->source($_)->view_definition for qw(Foo Bar);