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
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
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
$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' ] ],
);
}
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__