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') {
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 {
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);
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);