From: Matt S Trout Date: Sat, 1 Aug 2009 20:35:37 +0000 (-0400) Subject: sketch of view generation code X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d7749fb3699ece8287a1232cf357b58b012d609e;p=dbsrgits%2FDBIx-Class-ResultSource-MultipleTableInheritance.git sketch of view generation code --- diff --git a/vdef b/vdef new file mode 100644 index 0000000..b097bcc --- /dev/null +++ b/vdef @@ -0,0 +1,58 @@ +sub argify (@cols) { + map $_->new(%$_, name => '_'.$_->name), @cols; +} + +sub body_cols ($source) { + grep $_->name ne 'id', $source->all_cols; +} + +my @pk_col = ($table->col('id')); + +my @sources = grep defined, $table, $super_view; + +my @body_cols = map body_cols($_), @sources; + +CREATE VIEW $view_name => + SELECT { + (map $_->qualify, @pk_col), + @body_cols, + } FROM { + $super_view ? ($table->join($super_view)->using(@pk_col)) : $table + }; + +my ($now, $next) = grep defined, $super_view, $table; + +CREATE FUNCTION "${view_name}_insert" => + (argify @body_cols) + => RETURNS VOID => AS { + INSERT INTO { $now } (body_cols $now) + => VALUES (argify body_cols $now); + if ($next) { + INSERT INTO { $next } ($next->all_cols) + => VALUES { + $root_table->col('id')->sequence->currval, + argify body_cols $next + }; + } + }; + +my $pk_eq = AND( map (expr { $_ == argify $_ }), @pk_col); + +CREATE FUNCTION "${view_name}_update" => + (argify @pk_col, @body_cols) + => RETURNS VOID => AS { + foreach my $s (@sources) { + UPDATE { $s } SET { map ($_ => argify $_), body_cols $s } + WHERE { $pk_eq }; + } + }; + +CREATE FUNCTION "${view_name}_delete" => + (argify @pk_col) + => RETURNS VOID => AS { + foreach my $s (@sources) { + DELETE FROM { $s } WHERE { $pk_eq }; + } + }; + +