[Bast-commits] r5469 - in DBIx-Class/0.08/branches/subquery:
lib/DBIx/Class t/resultset
robkinyon at dev.catalyst.perl.org
robkinyon at dev.catalyst.perl.org
Fri Feb 13 19:43:12 GMT 2009
Author: robkinyon
Date: 2009-02-13 19:43:12 +0000 (Fri, 13 Feb 2009)
New Revision: 5469
Modified:
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:
Added as_sql and as_subselect as wrappers around as_query
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-13 14:47:56 UTC (rev 5468)
+++ DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/ResultSet.pm 2009-02-13 19:43:12 UTC (rev 5469)
@@ -1723,8 +1723,55 @@
=cut
-sub as_query { return shift->cursor->as_query }
+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-13 14:47:56 UTC (rev 5468)
+++ DBIx-Class/0.08/branches/subquery/lib/DBIx/Class/ResultSetColumn.pm 2009-02-13 19:43:12 UTC (rev 5469)
@@ -70,6 +70,53 @@
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-13 14:47:56 UTC (rev 5468)
+++ DBIx-Class/0.08/branches/subquery/t/resultset/as_query.t 2009-02-13 19:43:12 UTC (rev 5469)
@@ -52,23 +52,24 @@
);
}
+{my $sql = $art_rs->as_sql; warn "$sql\n";}
+
my $rscol = $art_rs->get_column( 'charfield' );
{
- my $arr = $rscol->as_query;
+ my $arr = $rscol->as_subselect;
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' ] ],
);
}
+{my $sql = $rscol->as_sql; warn "$sql\n";}
my $cdrs2 = $cdrs->search({
- artist_id => {
- -in => $art_rs->get_column( 'id' )->as_query,
- },
+ artist_id => $art_rs->get_column( 'id' )->as_query,
});
warn Dumper $cdrs2->as_query;
__END__
More information about the Bast-commits
mailing list