[Bast-commits] r5617 - in DBIx-Class/0.08/branches/subquery: lib/DBIx/Class/Manual lib/DBIx/Class/Storage t/search

robkinyon at dev.catalyst.perl.org robkinyon at dev.catalyst.perl.org
Sun Feb 22 03:07:15 GMT 2009


Author: robkinyon
Date: 2009-02-22 03:07:14 +0000 (Sun, 22 Feb 2009)
New Revision: 5617

Modified:
   DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/Manual/Cookbook.pod
   DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/Storage/DBI.pm
   DBIx-Class/0.08/branches/subquery/t/search/subquery.t
Log:
Added support for subqueries in the select and +select sections

Modified: DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/Manual/Cookbook.pod
===================================================================
--- DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/Manual/Cookbook.pod	2009-02-22 01:25:19 UTC (rev 5616)
+++ DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/Manual/Cookbook.pod	2009-02-22 03:07:14 UTC (rev 5617)
@@ -316,6 +316,11 @@
     artist_id => $inside_rs->get_column('id')->as_query,
   });
 
+=head3 Support
+
+Subqueries are supported in the where clause (first hashref), and in the
+from, select, and +select attributes.
+
 =head3 Correlated subqueries
 
   my $cdrs = $schema->resultset('CD');
@@ -338,12 +343,6 @@
        WHERE artistid = me.artistid
       )
 
-=head2 Where subqueries will work
-
-Currently, subqueries will B<only> work in the where-clause of a search. In
-other words, in the first hashref of a search() method. Work is being done
-to make them work as part of the second hashref (from, select, +select, etc).
-
 =head2 Predefined searches
 
 You can write your own L<DBIx::Class::ResultSet> class by inheriting from it

Modified: DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/Storage/DBI.pm
===================================================================
--- DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/Storage/DBI.pm	2009-02-22 01:25:19 UTC (rev 5616)
+++ DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/Storage/DBI.pm	2009-02-22 03:07:14 UTC (rev 5617)
@@ -171,6 +171,13 @@
         .'( '.$self->_recurse_fields($fields->{$func}).' )';
     }
   }
+  # Is the second check absolutely necessary?
+  elsif ( $ref eq 'REF' and ref($$fields) eq 'ARRAY' ) {
+    return $self->_bind_to_sql( $fields );
+  }
+  else {
+    Carp::croak($ref . qq{ unexpected in _recurse_fields()})
+  }
 }
 
 sub _order_by {
@@ -1409,7 +1416,6 @@
     $attrs->{rows} = 2**48 if not defined $attrs->{rows} and defined $attrs->{offset};
     push @args, $attrs->{rows}, $attrs->{offset};
   }
-
   return @args;
 }
 

Modified: DBIx-Class/0.08/branches/subquery/t/search/subquery.t
===================================================================
--- DBIx-Class/0.08/branches/subquery/t/search/subquery.t	2009-02-22 01:25:19 UTC (rev 5616)
+++ DBIx-Class/0.08/branches/subquery/t/search/subquery.t	2009-02-22 03:07:14 UTC (rev 5617)
@@ -11,7 +11,7 @@
     eval "use SQL::Abstract 1.49";
     plan $@
         ? ( skip_all => "Needs SQLA 1.49+" )
-        : ( tests => 6 );
+        : ( tests => 7 );
 }
 
 use lib qw(t/lib);
@@ -36,17 +36,32 @@
   );
 }
 
-TODO: {
-  local $TODO = "'+select' doesn't work with as_query yet.";
+{
   my $rs = $art_rs->search(
     {},
     {
+      'select' => [
+        $cdrs->search({}, { rows => 1 })->get_column('id')->as_query,
+      ],
+    },
+  );
+
+  my $arr = $rs->as_query;
+  my ($query, @bind) = @{$$arr};
+  is_same_sql_bind(
+    $query, \@bind,
+    "SELECT (SELECT id FROM cd me LIMIT 1) FROM artist me",
+    [],
+  );
+}
+
+{
+  my $rs = $art_rs->search(
+    {},
+    {
       '+select' => [
         $cdrs->search({}, { rows => 1 })->get_column('id')->as_query,
       ],
-      '+as' => [
-        'cdid',
-      ],
     },
   );
 
@@ -54,7 +69,7 @@
   my ($query, @bind) = @{$$arr};
   is_same_sql_bind(
     $query, \@bind,
-    "SELECT me.artistid, me.name, me.rank, me.charfield, (SELECT id FROM cds LIMIT 1) AS cdid FROM artist me",
+    "SELECT me.artistid, me.name, me.rank, me.charfield, (SELECT id FROM cd me LIMIT 1) FROM artist me",
     [],
   );
 }




More information about the Bast-commits mailing list