From: Rob Kinyon Date: Tue, 10 Feb 2009 20:10:10 +0000 (+0000) Subject: Added as_query to ResultSet with a couple tests X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=59af6677aa817effed8a69dcafddb96c84d13203;p=dbsrgits%2FDBIx-Class-Historic.git Added as_query to ResultSet with a couple tests --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 472de94..72af68e 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -1709,6 +1709,14 @@ sub _remove_alias { return \%unaliased; } +=head2 as_query + +Returns the SQL query and bind vars associated with the invocant. + +=cut + +sub as_query { return shift->cursor->as_query } + =head2 find_or_new =over 4 diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 91e44e3..b542632 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -1135,11 +1135,15 @@ sub txn_rollback { sub _prep_for_execute { my ($self, $op, $extra_bind, $ident, $args) = @_; + if( blessed($ident) && $ident->isa("DBIx::Class::ResultSource") ) { + $ident = $ident->from(); + } + my ($sql, @bind) = $self->sql_maker->$op($ident, @$args); + unshift(@bind, map { ref $_ eq 'ARRAY' ? $_ : [ '!!dummy', $_ ] } @$extra_bind) if $extra_bind; - return ($sql, \@bind); } @@ -1181,10 +1185,6 @@ sub _query_end { sub _dbh_execute { my ($self, $dbh, $op, $extra_bind, $ident, $bind_attributes, @args) = @_; - if( blessed($ident) && $ident->isa("DBIx::Class::ResultSource") ) { - $ident = $ident->from(); - } - my ($sql, $bind) = $self->_prep_for_execute($op, $extra_bind, $ident, \@args); $self->_query_start( $sql, @$bind ); @@ -1319,6 +1319,13 @@ sub delete { } sub _select { + my $self = shift; + my $sql_maker = $self->sql_maker; + local $sql_maker->{for}; + return $self->_execute($self->_select_args(@_)); +} + +sub _select_args { my ($self, $ident, $select, $condition, $attrs) = @_; my $order = $attrs->{order_by}; @@ -1355,7 +1362,7 @@ sub _select { push @args, $attrs->{rows}, $attrs->{offset}; } - return $self->_execute(@args); + return @args; } sub source_bind_attributes { diff --git a/lib/DBIx/Class/Storage/DBI/Cursor.pm b/lib/DBIx/Class/Storage/DBI/Cursor.pm index 426f72e..e067408 100644 --- a/lib/DBIx/Class/Storage/DBI/Cursor.pm +++ b/lib/DBIx/Class/Storage/DBI/Cursor.pm @@ -49,6 +49,24 @@ sub new { return bless ($new, $class); } +=head2 as_query + +Returns the SQL statement and bind vars associated with the invocant. + +=cut + +sub as_query { + my $self = shift; + + my $storage = $self->{storage}; + my $sql_maker = $storage->sql_maker; + local $sql_maker->{for}; + + my @args = $storage->_select_args(@{$self->{args}}); + my ($sql, $bind) = $storage->_prep_for_execute(@args[0 .. 2], [@args[4 .. $#args]]); + return [ $sql, @$bind ]; +} + =head2 next =over 4 diff --git a/t/resultset/as_query.t b/t/resultset/as_query.t new file mode 100644 index 0000000..d5baabf --- /dev/null +++ b/t/resultset/as_query.t @@ -0,0 +1,43 @@ +#!/usr/bin/perl + +use strict; +use warnings FATAL => 'all'; + +use Test::More; +use lib qw(t/lib); +use DBICTest; + +plan tests => 6; + +my $schema = DBICTest->init_schema(); +my $art_rs = $schema->resultset('Artist'); + +{ + my $arr = $art_rs->as_query; + my ($query, @bind) = @$arr; + + is( $query, "SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me" ); + is_deeply( \@bind, [] ); +} + +$art_rs = $art_rs->search({ name => 'Billy Joel' }); + +{ + my $arr = $art_rs->as_query; + my ($query, @bind) = @$arr; + + is( $query, "SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me WHERE ( name = ? )" ); + is_deeply( \@bind, [ [ name => 'Billy Joel' ] ] ); +} + +$art_rs = $art_rs->search({ rank => 2 }); + +{ + my $arr = $art_rs->as_query; + my ($query, @bind) = @$arr; + + is( $query, "SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me WHERE ( ( ( rank = ? ) AND ( name = ? ) ) )" ); + is_deeply( \@bind, [ [ rank => 2 ], [ name => 'Billy Joel' ] ] ); +} + +__END__