From: Rob Kinyon Date: Sun, 15 Feb 2009 16:23:46 +0000 (+0000) Subject: Subqueries are done X-Git-Tag: v0.08240~63^2~27 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=03834f775f371dd68f314f2648236700e7eb4404;p=dbsrgits%2FDBIx-Class.git Subqueries are done --- diff --git a/lib/DBIx/Class/Manual/Cookbook.pod b/lib/DBIx/Class/Manual/Cookbook.pod index 216d71b..0e9ed68 100644 --- a/lib/DBIx/Class/Manual/Cookbook.pod +++ b/lib/DBIx/Class/Manual/Cookbook.pod @@ -295,6 +295,27 @@ Please see L documentation if you 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: You have to explicitly use '=' when doing an equality comparison. +The following will B 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 class by inheriting from it diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index f717435..1a9530e 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -1721,56 +1721,11 @@ sub _remove_alias { Returns the SQL query and bind vars associated with the invocant. -=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. +This is generally used as the RHS for a subquery. =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 -} +sub as_query { return shift->cursor->as_query(@_) } =head2 find_or_new diff --git a/lib/DBIx/Class/ResultSetColumn.pm b/lib/DBIx/Class/ResultSetColumn.pm index 5e8e727..2a7ce97 100644 --- a/lib/DBIx/Class/ResultSetColumn.pm +++ b/lib/DBIx/Class/ResultSetColumn.pm @@ -66,56 +66,11 @@ sub new { Returns the SQL query and bind vars associated with the invocant. -=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. +This is generally used as the RHS for a subquery. =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 -} +sub as_query { return shift->_resultset->as_query } =head2 next diff --git a/t/resultset/as_query.t b/t/resultset/as_query.t index 2929e7c..8df7e7d 100644 --- a/t/resultset/as_query.t +++ b/t/resultset/as_query.t @@ -42,12 +42,12 @@ $art_rs = $art_rs->search({ name => 'Billy Joel' }); $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 @@ $art_rs = $art_rs->search({ rank => 2 }); 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__