From: Matt S Trout Date: Sun, 7 Aug 2005 00:52:15 +0000 (+0000) Subject: Fixed search/count_related to use Abstract (though not for _convert yet) X-Git-Tag: v0.03001~60 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=944f30bf240a360b7963247945916c6388321b3d;p=dbsrgits%2FDBIx-Class.git Fixed search/count_related to use Abstract (though not for _convert yet) --- diff --git a/Build.PL b/Build.PL index 3da7a5f..125de09 100644 --- a/Build.PL +++ b/Build.PL @@ -8,6 +8,7 @@ my %arguments = ( requires => { 'DBI' => 0, 'NEXT' => 0, + 'SQL::Abstract' => 1.19, 'DBD::SQLite' => 1.08, 'Tie::IxHash' => 0, }, diff --git a/lib/DBIx/Class/Relationship.pm b/lib/DBIx/Class/Relationship.pm index ac717d8..10807dd 100644 --- a/lib/DBIx/Class/Relationship.pm +++ b/lib/DBIx/Class/Relationship.pm @@ -104,18 +104,17 @@ sub _cond_value { sub search_related { my $self = shift; - return $self->_literal_related('search', @_); + return $self->_query_related('search', @_); } sub count_related { my $self = shift; - return $self->_literal_related('count', @_); + return $self->_query_related('count', @_); } -sub _literal_related { +sub _query_related { my $self = shift; - my $op = shift; - my $meth = "${op}_literal"; + my $meth = shift; my $rel = shift; my $attrs = { }; if (@_ > 1 && ref $_[$#_] eq 'HASH') { @@ -124,19 +123,19 @@ sub _literal_related { my $rel_obj = $self->_relationships->{$rel}; $self->throw( "No such relationship ${rel}" ) unless $rel_obj; $attrs = { %{$rel_obj->{attrs} || {}}, %{$attrs || {}} }; - my $s_cond; - if (@_) { - $self->throw( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1)); - my $query = ((@_ > 1) ? {@_} : shift); - $s_cond = $self->_cond_resolve($query, $attrs); - } + + $self->throw( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1)); + my $query = ((@_ > 1) ? {@_} : shift); + $attrs->{_action} = 'convert'; # shouldn't we resolve the cond to something # to merge into the AST really? my ($cond) = $self->_cond_resolve($rel_obj->{cond}, $attrs); - $cond = "${s_cond} AND ${cond}" if $s_cond; + $query = ($query ? { '-and' => [ \$cond, $query ] } : \$cond); + #use Data::Dumper; warn Dumper($query); #warn $rel_obj->{class}." $meth $cond ".join(', ', @{$attrs->{bind}}); + delete $attrs->{_action}; return $self->resolve_class($rel_obj->{class} - )->$meth($cond, @{$attrs->{bind} || []}, $attrs); + )->$meth($query, $attrs); } sub create_related { diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index d49d5c5..c0b4d82 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -90,9 +90,9 @@ sub rollback { $_[0]->dbh->rollback; } sub _execute { my ($self, $op, $extra_bind, $ident, @args) = @_; my ($sql, @bind) = $self->sql_maker->$op($ident, @args); + unshift(@bind, @$extra_bind) if $extra_bind; warn "$sql: @bind" if $self->debug; my $sth = $self->sth($sql); - unshift(@bind, @$extra_bind) if $extra_bind; @bind = map { ref $_ ? ''.$_ : $_ } @bind; my $rv = $sth->execute(@bind); # stringify args return (wantarray ? ($rv, $sth, @bind) : $rv); diff --git a/lib/DBIx/Class/Table.pm b/lib/DBIx/Class/Table.pm index 2a491e4..aa85c7c 100644 --- a/lib/DBIx/Class/Table.pm +++ b/lib/DBIx/Class/Table.pm @@ -289,7 +289,7 @@ sub count { if (@_ > 1 && ref $_[$#_] eq 'HASH') { $attrs = { %{ pop(@_) } }; } - my $query = ref $_[0] eq "HASH" || (@_ == 1) ? shift: {@_}; + my $query = (@_ == 1 || ref $_[0] eq "HASH" ? shift: {@_}); my @cols = 'COUNT(*)'; my $cursor = $class->storage->select($class->_table_name, \@cols, $query, $attrs);