Merge 'prefetch-group_by' into 'trunk'
Peter Rabbitson [Mon, 16 Nov 2009 18:15:45 +0000 (18:15 +0000)]
Changes
lib/DBIx/Class/Storage/DBI/Sybase.pm
lib/SQL/Translator/Parser/DBIx/Class.pm
t/104view.t
t/lib/DBICTest/Schema/Track.pm
t/lib/DBICTest/Schema/Year1999CDs.pm
t/lib/DBICTest/Schema/Year2000CDs.pm
t/lib/sqlite.sql

diff --git a/Changes b/Changes
index 8fe5fa2..dafb2d5 100644 (file)
--- a/Changes
+++ b/Changes
@@ -2,6 +2,7 @@ Revision history for DBIx::Class
 
         - Fix distinct => 1 with non-selecting order_by (the columns
           in order_by also need to be aded to the resulting group_by)
+        - Do not attempt to deploy FK constraints pointing to a View
 
 0.08114 2009-11-14 17:45:00 (UTC)
         - Preliminary support for MSSQL via DBD::ADO
index 8cb5f5f..5ade896 100644 (file)
@@ -191,6 +191,8 @@ sub _populate_dbh {
 
   $self->next::method(@_);
 
+  return unless $self->_driver_determined; # otherwise we screw up MSSQL
+
   if ($self->_is_bulk_storage) {
 # this should be cleared on every reconnect
     $self->_began_bulk_work(0);
index b570d60..bb40c91 100644 (file)
@@ -86,7 +86,7 @@ sub parse {
         # support quoting properly to be signaled about this
         $table_name = $$table_name if ref $table_name eq 'SCALAR';
 
-        # Its possible to have multiple DBIC sources using the same table
+        # It's possible to have multiple DBIC sources using the same table
         next if $tables{$table_name};
 
         $tables{$table_name}{source} = $source;
@@ -141,6 +141,7 @@ sub parse {
             next unless ref $rel_info->{cond} eq 'HASH';
 
             my $othertable = $source->related_source($rel);
+            next if $othertable->isa('DBIx::Class::ResultSource::View');  # can't define constraints referencing a view
             my $rel_table = $othertable->name;
 
             # FIXME - this isn't the right way to do it, but sqlt does not
@@ -200,7 +201,7 @@ sub parse {
                 next unless $fk_constraint;
 
                 # Make sure we dont create the same foreign key constraint twice
-                my $key_test = join("\x00", @keys);
+                my $key_test = join("\x00", sort @keys);
                 next if $created_FK_rels{$rel_table}->{$key_test};
 
                 if (scalar(@keys)) {
@@ -214,7 +215,6 @@ sub parse {
                   if (! $is_deferrable and $rel_table ne $table_name) {
                     $tables{$table_name}{foreign_table_deps}{$rel_table}++;
                   }
-
                   $table->add_constraint(
                                     type             => 'foreign_key',
                                     name             => join('_', $table_name, 'fk', @keys),
index 3efdcf1..e7eb46a 100644 (file)
@@ -1,5 +1,5 @@
 use strict;
-use warnings;  
+use warnings;
 
 use Test::More;
 use Test::Exception;
@@ -8,8 +8,6 @@ use DBICTest;
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 2;
-
 ## Real view
 my $cds_rs_2000 = $schema->resultset('CD')->search( { year => 2000 });
 my $year2kcds_rs = $schema->resultset('Year2000CDs');
@@ -24,5 +22,50 @@ my $year1999cds_rs = $schema->resultset('Year1999CDs');
 is($cds_rs_1999->count, $year1999cds_rs->count, 'View Year1999CDs sees all CDs in year 1999');
 
 
+# Test if relationships work correctly
+is_deeply (
+  [
+    $schema->resultset('Year1999CDs')->search (
+      {},
+      {
+        result_class => 'DBIx::Class::ResultClass::HashRefInflator',
+        prefetch => ['artist', { tracks => [qw/cd year1999cd year2000cd/] } ],
+      },
+    )->all
+  ],
+  [
+    $schema->resultset('CD')->search (
+      { 'me.year' => '1999'},
+      {
+        result_class => 'DBIx::Class::ResultClass::HashRefInflator',
+        prefetch => ['artist', { tracks => [qw/cd year1999cd year2000cd/] } ],
+        columns => [qw/cdid single_track title/],   # to match the columns retrieved by the virtview
+      },
+    )->all
+  ],
+  'Prefetch over virtual view gives expected result',
+);
 
+is_deeply (
+  [
+    $schema->resultset('Year2000CDs')->search (
+      {},
+      {
+        result_class => 'DBIx::Class::ResultClass::HashRefInflator',
+        prefetch => ['artist', { tracks => [qw/cd year1999cd year2000cd/] } ],
+      },
+    )->all
+  ],
+  [
+    $schema->resultset('CD')->search (
+      { 'me.year' => '2000'},
+      {
+        result_class => 'DBIx::Class::ResultClass::HashRefInflator',
+        prefetch => ['artist', { tracks => [qw/cd year1999cd year2000cd/] } ],
+      },
+    )->all
+  ],
+  'Prefetch over regular view gives expected result',
+);
 
+done_testing;
index d7ba952..12f7296 100644 (file)
@@ -50,4 +50,17 @@ __PACKAGE__->belongs_to( disc => 'DBICTest::Schema::CD' => 'cd');
 __PACKAGE__->might_have( cd_single => 'DBICTest::Schema::CD', 'single_track' );
 __PACKAGE__->might_have( lyrics => 'DBICTest::Schema::Lyrics', 'track_id' );
 
+__PACKAGE__->belongs_to(
+    "year1999cd",
+    "DBICTest::Schema::Year1999CDs",
+    { "foreign.cdid" => "self.cd" },
+    { join_type => 'left' },  # the relationship is of course optional
+);
+__PACKAGE__->belongs_to(
+    "year2000cd",
+    "DBICTest::Schema::Year2000CDs",
+    { "foreign.cdid" => "self.cd" },
+    { join_type => 'left' },
+);
+
 1;
index ee1c9ec..76606d4 100644 (file)
@@ -9,7 +9,7 @@ __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
 __PACKAGE__->table('year1999cds');
 __PACKAGE__->result_source_instance->is_virtual(1);
 __PACKAGE__->result_source_instance->view_definition(
-  "SELECT cdid, artist, title FROM cd WHERE year ='1999'"
+  "SELECT cdid, artist, title, single_track FROM cd WHERE year ='1999'"
 );
 __PACKAGE__->add_columns(
   'cdid' => {
@@ -23,9 +23,17 @@ __PACKAGE__->add_columns(
     data_type => 'varchar',
     size      => 100,
   },
-
+  'single_track' => {
+    data_type => 'integer',
+    is_nullable => 1,
+    is_foreign_key => 1,
+  },
 );
 __PACKAGE__->set_primary_key('cdid');
 __PACKAGE__->add_unique_constraint([ qw/artist title/ ]);
 
+__PACKAGE__->belongs_to( artist => 'DBICTest::Schema::Artist' );
+__PACKAGE__->has_many( tracks => 'DBICTest::Schema::Track',
+    { "foreign.cd" => "self.cdid" });
+
 1;
index 7f75f5f..2fc30aa 100644 (file)
@@ -1,30 +1,19 @@
-package # hide from PAUSE 
+package # hide from PAUSE
     DBICTest::Schema::Year2000CDs;
-## Used in 104view.t
 
-use base qw/DBICTest::BaseResult/;
+use base qw/DBICTest::Schema::CD/;
 
 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
-
 __PACKAGE__->table('year2000cds');
-__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/ ]);
+# need to operate on the instance for things to work
+__PACKAGE__->result_source_instance->view_definition( sprintf (
+  'SELECT %s FROM cd WHERE year = "2000"',
+  join (', ', __PACKAGE__->columns),
+));
+
+__PACKAGE__->belongs_to( artist => 'DBICTest::Schema::Artist' );
+__PACKAGE__->has_many( tracks => 'DBICTest::Schema::Track',
+    { "foreign.cd" => "self.cdid" });
 
 1;
index 888ccd0..cfb4e33 100644 (file)
@@ -1,4 +1,6 @@
--- Created on Tue Aug 25 12:34:34 2009
+-- 
+-- Created by SQL::Translator::Producer::SQLite
+-- Created on Sun Nov 15 14:13:02 2009
 -- 
 
 
@@ -451,6 +453,6 @@ CREATE INDEX fourkeys_to_twokeys_idx_t_artist_t_cd ON fourkeys_to_twokeys (t_art
 -- View: year2000cds
 --
 CREATE VIEW year2000cds AS
-    SELECT cdid, artist, title FROM cd WHERE year ='2000';
+    SELECT cdid, artist, title, year, genreid, single_track FROM cd WHERE year = "2000";
 
 COMMIT;