SELECT statement generation
[dbsrgits/DBIx-Class-ResultSource-MultipleTableInheritance.git] / lib / DBIx / Class / ResultSource / MultipleTableInheritance.pm
index f27eab6..8f5fe21 100644 (file)
@@ -48,13 +48,15 @@ method schema (@args) {
 }
 
 method _attach_additional_sources () {
-  my $raw_name = $self->_raw_source_name;
+  my $raw_name = $self->raw_source_name;
   my $schema = $self->schema;
 
   # if the raw source is already present we can assume we're done
   return if grep { $_ eq $raw_name } $schema->sources;
+
   # our parent should've been registered already actually due to DBIC
   # attaching subclass sources later in load_namespaces
+
   my $parent;
   if ($self->parent_source) {
       my $parent_name = $self->parent_source->name;
@@ -64,14 +66,14 @@ method _attach_additional_sources () {
     confess "Couldn't find attached source for parent $parent_name - did you use load_classes? This module is only compatible with load_namespaces"
       unless $parent;
   }
-  my $table = Table->new({ name => '_'.$self->name });
-  $table->add_columns(
-    map { ($_ => { %{$self->column_info($_)} }) }
-      grep { $self->column_info($_)->{originally_defined_in} eq $self->name }
-        $self->columns
-  );
+
+  # create the raw table source
+
+  my $table = Table->new({ name => $self->raw_table_name });
+
   # we don't need to add the PK cols explicitly if we're the root table
-  # since they already got added above
+  # since they'll get added below
+
   if ($parent) {
     my %join;
     foreach my $pri ($self->primary_columns) {
@@ -80,8 +82,18 @@ method _attach_additional_sources () {
       $table->add_column($pri => \%info);
       $join{"foreign.${pri}"} = "self.${pri}";
     }
-    $table->add_relationship('parent', $parent->source_name, \%join);
+    # 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);
   }
+
+  # add every column that's actually a concrete part of us
+
+  $table->add_columns(
+    map { ($_ => { %{$self->column_info($_)} }) }
+      grep { $self->column_info($_)->{originally_defined_in} eq $self->name }
+        $self->columns
+  );
   $table->set_primary_key($self->primary_columns);
   $schema->register_source($raw_name => $table);
 }
@@ -93,17 +105,81 @@ method set_primary_key (@args) {
   return $self->next::method(@args);
 }
 
-method _raw_source_name () {
+method raw_source_name () {
   my $base = $self->source_name;
   confess "Can't generate raw source name when we don't have a source_name"
     unless $base;
   return 'Raw::'.$base;
 }
 
+method raw_table_name () {
+  return '_'.$self->name;
+}
+
 method add_columns (@args) {
   my $ret = $self->next::method(@args);
   $_->{originally_defined_in} ||= $self->name for values %{$self->_columns};
   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;