[Bast-commits] r5478 - in DBIx-Class/0.08/branches/subquery: lib/DBIx/Class lib/DBIx/Class/Manual t/resultset

robkinyon at dev.catalyst.perl.org robkinyon at dev.catalyst.perl.org
Sun Feb 15 16:23:47 GMT 2009


Author: robkinyon
Date: 2009-02-15 16:23:46 +0000 (Sun, 15 Feb 2009)
New Revision: 5478

Modified:
   DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/Manual/Cookbook.pod
   DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/ResultSet.pm
   DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/ResultSetColumn.pm
   DBIx-Class/0.08/branches/subquery/t/resultset/as_query.t
Log:
Subqueries are done

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-15 15:39:26 UTC (rev 5477)
+++ DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/Manual/Cookbook.pod	2009-02-15 16:23:46 UTC (rev 5478)
@@ -295,6 +295,27 @@
 are in any way unsure about the use of the attributes above (C< join
 >, C< select >, C< as > and C< group_by >).
 
+=head2 Subqueries
+
+You can write subqueries relatively easily in DBIC.
+
+  my $inside_rs = $schema->resultset('Artist')->search({
+    name => [ 'Billy Joel', 'Brittany Spears' ],
+  });
+
+  my $rs = $schema->resulset('CD')->search({
+    artist_id => { 'IN' => $inside_rs->get_column('id')->as_query },
+  });
+
+The usual operators ( =, !=, IN, NOT IN, etc) are supported.
+
+B<NOTE>: You have to explicitly use '=' when doing an equality comparison.
+The following will B<not> work:
+
+  my $rs = $schema->resulset('CD')->search({
+    artist_id => $inside_rs->get_column('id')->as_query,
+  });
+
 =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/ResultSet.pm
===================================================================
--- DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/ResultSet.pm	2009-02-15 15:39:26 UTC (rev 5477)
+++ DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/ResultSet.pm	2009-02-15 16:23:46 UTC (rev 5478)
@@ -1721,57 +1721,12 @@
 
 Returns the SQL query and bind vars associated with the invocant.
 
+This is generally used as the RHS for a subquery.
+
 =cut
 
 sub as_query { return shift->cursor->as_query(@_) }
 
-=head2 as_subselect
-
-=over 4
-
-=item Arguments: none
-
-=item Return Value: \[ $sql, @bind ]
-
-=back
-
-Returns the SQL query and bind vars associated with the invocant.
-
-The SQL will be wrapped in parentheses, ready for use as a subselect.
-
-=cut
-
-sub as_subselect {
-  my $self = shift;
-  my $arr = ${$self->as_query(@_)};
-  $arr->[0] = '( ' . $arr->[0] . ' )';
-  return \$arr;
-}
-
-=head2 as_query
-
-=over 4
-
-=item Arguments: none
-
-=item Return Value: $sql
-
-=back
-
-Returns the SQL query associated with the invocant. All bind vars
-will have been bound using C<< DBI->quote() >>.
-
-=cut
-
-sub as_sql {
-  my $self = shift;
-  my $arr = ${$self->as_query(@_)};
-  my $sql = shift @$arr;
-  my $dbh = $self->result_source->schema->storage->dbh;
-  $sql =~ s/\?/$dbh->quote((shift @$arr)->[1])/eg;
-  return $sql
-}
-
 =head2 find_or_new
 
 =over 4

Modified: DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/ResultSetColumn.pm
===================================================================
--- DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/ResultSetColumn.pm	2009-02-15 15:39:26 UTC (rev 5477)
+++ DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/ResultSetColumn.pm	2009-02-15 16:23:46 UTC (rev 5478)
@@ -66,57 +66,12 @@
 
 Returns the SQL query and bind vars associated with the invocant.
 
+This is generally used as the RHS for a subquery.
+
 =cut
 
 sub as_query { return shift->_resultset->as_query }
 
-=head2 as_subselect
-
-=over 4
-
-=item Arguments: none
-
-=item Return Value: \[ $sql, @bind ]
-
-=back
-
-Returns the SQL query and bind vars associated with the invocant.
-
-The SQL will be wrapped in parentheses, ready for use as a subselect.
-
-=cut
-
-sub as_subselect {
-  my $self = shift;
-  my $arr = ${$self->as_query(@_)};
-  $arr->[0] = '( ' . $arr->[0] . ' )';
-  return \$arr;
-}
-
-=head2 as_query
-
-=over 4
-
-=item Arguments: none
-
-=item Return Value: $sql
-
-=back
-
-Returns the SQL query associated with the invocant. All bind vars
-will have been bound using C<< DBI->quote() >>.
-
-=cut
-
-sub as_sql {
-  my $self = shift;
-  my $arr = ${$self->as_query(@_)};
-  my $sql = shift @$arr;
-  my $dbh = $self->_resultset->result_source->schema->storage->dbh;
-  $sql =~ s/\?/$dbh->quote((shift @$arr)->[1])/eg;
-  return $sql
-}
-
 =head2 next
 
 =over 4

Modified: DBIx-Class/0.08/branches/subquery/t/resultset/as_query.t
===================================================================
--- DBIx-Class/0.08/branches/subquery/t/resultset/as_query.t	2009-02-15 15:39:26 UTC (rev 5477)
+++ DBIx-Class/0.08/branches/subquery/t/resultset/as_query.t	2009-02-15 16:23:46 UTC (rev 5478)
@@ -42,12 +42,12 @@
 $art_rs = $art_rs->search({ rank => 2 });
 
 {
-  my $arr = $art_rs->as_subselect;
+  my $arr = $art_rs->as_query;
   my ($query, @bind) = @{$$arr};
 
   is_same_sql_bind(
     $query, \@bind,
-    "( SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me WHERE ( ( rank = ? ) AND ( name = ? ) ) )",
+    "SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me WHERE ( ( rank = ? ) AND ( name = ? ) )",
     [ [ rank => 2 ], [ name => 'Billy Joel' ] ],
   );
 }
@@ -55,29 +55,29 @@
 my $rscol = $art_rs->get_column( 'charfield' );
 
 {
-  my $arr = $rscol->as_subselect;
+  my $arr = $rscol->as_query;
   my ($query, @bind) = @{$$arr};
 
   is_same_sql_bind(
     $query, \@bind,
-    "( SELECT me.charfield FROM artist me WHERE ( ( ( rank = ? ) AND ( name = ? ) ) ) )",
+    "SELECT me.charfield FROM artist me WHERE ( ( ( rank = ? ) AND ( name = ? ) ) )",
     [ [ rank => 2 ], [ name => 'Billy Joel' ] ],
   );
 }
 
+# This is an actual subquery.
 {
   my $cdrs2 = $cdrs->search({
-    artist_id => { '=' => $art_rs->search({}, { rows => 1 })->get_column( 'id' )->as_subselect },
+    artist_id => { 'in' => $art_rs->search({}, { rows => 1 })->get_column( 'id' )->as_query },
   });
 
   my $arr = $cdrs2->as_query;
   my ($query, @bind) = @{$$arr};
   is_same_sql_bind(
     $query, \@bind,
-    "SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track FROM cd me WHERE artist_id = ( SELECT id FROM artist me WHERE ( rank = ? ) AND ( name = ? ) LIMIT 1 )",
+    "SELECT me.cdid,me.artist,me.title,me.year,me.genreid,me.single_track FROM cd me WHERE artist_id IN ( SELECT id FROM artist me WHERE ( rank = ? ) AND ( name = ? ) LIMIT 1 )",
     [ [ rank => 2 ], [ name => 'Billy Joel' ] ],
   );
-warn Dumper $cdrs2->as_sql;
 }
 
 __END__




More information about the Bast-commits mailing list