Merge 'trunk' into 'view_support'
Peter Rabbitson [Mon, 25 Aug 2008 10:19:10 +0000 (06:19 -0400)]
r21901@martha (orig r4767):  ribasushi | 2008-08-25 05:19:10 -0400
Force on_delete/on_update arguments to upper case for consistency with the rest of SQLT

lib/DBIx/Class/ResultSource.pm
lib/DBIx/Class/ResultSource/View.pm [new file with mode: 0644]
lib/SQL/Translator/Parser/DBIx/Class.pm
t/99dbic_sqlt_parser.t
t/lib/DBICTest/Schema.pm
t/lib/DBICTest/Schema/Year2000CDs.pm [new file with mode: 0644]

index a026339..1734d52 100644 (file)
@@ -71,6 +71,18 @@ and don't actually accomplish anything on their own:
     "_engine" => 'InnoDB',
   });
 
+=cut
+
+sub is_virtual { 0 }
+
+=pod
+
+=head2 is_virtual
+
+Returns true if the resultsource is a virtual result source. This stub
+method returns false by default, see L<DBIx::Class::ResultSource::View>
+for more information.
+
 =head2 add_columns
 
   $table->add_columns(qw/col1 col2 col3/);
diff --git a/lib/DBIx/Class/ResultSource/View.pm b/lib/DBIx/Class/ResultSource/View.pm
new file mode 100644 (file)
index 0000000..7f6bec7
--- /dev/null
@@ -0,0 +1,58 @@
+package DBIx::Class::ResultSource::View;
+
+use strict;
+use warnings;
+
+use DBIx::Class::ResultSet;
+
+use base qw/DBIx::Class/;
+__PACKAGE__->load_components(qw/ResultSource/);
+__PACKAGE__->mk_group_accessors(
+  'simple' => qw(is_virtual view_definition)
+);
+
+=head1 NAME
+
+DBIx::Class::ResultSource::Table - Table object
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+Table object that inherits from L<DBIx::Class::ResultSource>
+
+=head1 METHODS
+
+=head2 is_virtual
+
+Attribute to declare a view as virtual.
+
+=head2 from
+
+Returns the FROM entry for the table (i.e. the view name)
+or the definition if this is a virtual view.
+
+=cut
+
+sub from {
+  my $self = shift;
+  return \"(${\$self->view_definition})" if $self->is_virtual;
+  return $self->name;
+}
+
+1;
+
+=head1 AUTHORS
+
+Matt S. Trout <mst@shadowcatsystems.co.uk>
+
+With Contributions from:
+
+Guillermo Roditi E<lt>groditi@cpan.orgE<gt>
+
+=head1 LICENSE
+
+You may distribute this code under the same terms as Perl itself.
+
+=cut
+
index d4499aa..3e915bc 100644 (file)
@@ -65,8 +65,18 @@ sub parse {
         @monikers = grep { $sources->{$_} } @monikers;
     }
 
+    my(@table_monikers, @view_monikers);
+    for my $moniker (@monikers){
+      my $source = $dbicschema->source($moniker);
+      next if $source->is_virtual;
+       if ( $source->isa('DBIx::Class::ResultSource::Table') ) {
+         push(@table_monikers, $moniker);
+      } elsif( $source->isa('DBIx::Class::ResultSource::View') ){
+         push(@view_monikers, $moniker);
+      }
+    }
 
-    foreach my $moniker (sort @monikers)
+    foreach my $moniker (sort @table_monikers)
     {
         my $source = $dbicschema->source($moniker);
         
@@ -219,6 +229,26 @@ sub parse {
         }
     }
 
+    foreach my $moniker (sort @view_monikers)
+    {
+        my $source = $dbicschema->source($moniker);
+        # Skip custom query sources
+        next if ref($source->name);
+
+        # Its possible to have multiple DBIC source using same table
+        next if $seen_tables{$source->name}++;
+
+        my $view = $schema->add_view(
+          name => $source->name,
+          fields => [ $source->columns ],
+          ($source->view_definition ? $source->view_definition : ())
+        );
+        if ($source->result_class->can('sqlt_deploy_hook')) {
+          $source->result_class->sqlt_deploy_hook($view);
+        }
+    }
+
+
     if ($dbicschema->can('sqlt_deploy_hook')) {
       $dbicschema->sqlt_deploy_hook($schema);
     }
index 34547db..42eeb92 100644 (file)
@@ -9,7 +9,7 @@ BEGIN {
     eval "use DBD::mysql; use SQL::Translator 0.09;";
     plan $@
         ? ( skip_all => 'needs SQL::Translator 0.09 for testing' )
-        : ( tests => 99 );
+        : ( tests => 102 );
 }
 
 my $schema = DBICTest->init_schema();
index 22eddff..857c18c 100644 (file)
@@ -16,6 +16,7 @@ __PACKAGE__->load_classes(qw/
   #dummy
   Track
   Tag
+  Year2000CDs
   /,
   { 'DBICTest::Schema' => [qw/
     LinerNotes
diff --git a/t/lib/DBICTest/Schema/Year2000CDs.pm b/t/lib/DBICTest/Schema/Year2000CDs.pm
new file mode 100644 (file)
index 0000000..2a2c85b
--- /dev/null
@@ -0,0 +1,30 @@
+package # hide from PAUSE 
+    DBICTest::Schema::Year2000CDs;
+
+use base 'DBIx::Class::Core';
+use DBIx::Class::ResultSource::View;
+
+__PACKAGE__->table_class('DBIx::Class::ResultSource::View');
+
+__PACKAGE__->table('cd');
+__PACKAGE__->result_source_instance->view_definition(
+  "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
+);
+__PACKAGE__->add_columns(
+  'cdid' => {
+    data_type => 'integer',
+    is_auto_increment => 1,
+  },
+  'artist' => {
+    data_type => 'integer',
+  },
+  'title' => {
+    data_type => 'varchar',
+    size      => 100,
+  },
+
+);
+__PACKAGE__->set_primary_key('cdid');
+__PACKAGE__->add_unique_constraint([ qw/artist title/ ]);
+
+1;