rework generation to hopefully handle multiple parents
Matt S Trout [Tue, 18 Aug 2009 04:42:45 +0000 (05:42 +0100)]
lib/DBIx/Class/ResultSource/MultipleTableInheritance.pm

index 832a51f..0a45aa8 100644 (file)
@@ -308,30 +308,38 @@ method view_definition () {
   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
 
@@ -342,21 +350,20 @@ method view_definition () {
       $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