Revision 605

Date:
2016/10/24 19:28:15
Author:
ahitrov
Revision Log:
Limit and offset

Files:

Legend:

 
Added
 
Removed
 
Modified
  • utf8/plugins/sphinx/lib/sphinx/Keeper.pm

     
    78 78 my $count = delete $opts{count};
    79 79 my $limit = delete $opts{limit};
    80 80 return if $limit && ($limit =~ /\D/ || $limit < 0);
    81 my $offset = delete $opts{offset};
    82 return if $offset && ($offset =~ /\D/ || $offset < 0);
    81 83 my $no_limit = delete $opts{no_limit};
    82 84 unless ( $no_limit ) {
    83 85 $limit ||= 1000;
    84 86 }
    85 my $offset = delete $opts{offset};
    86 return if $offset && ($offset =~ /\D/ || $offset < 0);
    87 if ( $count ) {
    88 $limit = 0;
    89 $offset = 0;
    90 }
    87 91 my $return_value = delete $opts{return_value} || 'array_ref';
    88 92 my $hash_by = delete $opts{hash_by} || 'object_id';
    89 93
    90 while ( my ($key, $val) = each %opts ) {
    91 if ( ref $val eq 'ARRAY' ) {
    92 push @wheres, "$key in (".join(',', map { '?' } @$val).")";
    93 push @values, @$val;
    94 } else {
    95 push @wheres, "$key = ?";
    96 push @values, $val;
    97 }
    94 if ( exists $opts{object_class} ) {
    95 push @wheres, "object_class in (".join( ',', map { '?' } ref $opts{object_class} eq 'ARRAY' ? @{$opts{object_class}} : ($opts{object_class}) ).")";
    96 push @values, ref $opts{object_class} ? @{$opts{object_class}} : $opts{object_class};
    98 97 }
    98
    99 99 my $query = "select ".($count ? 'count(*) as cnt' : '*, weight() as weight')." from $db_table where ".join( ' and ', @wheres );
    100 if ( $limit ) {
    101 $query .= " limit $limit ";
    100 if ( $limit && $offset ) {
    101 $query .= " limit $offset, $limit ";
    102 } elsif ( $limit ) {
    103 $query .= " limit 0, $limit ";
    102 104 }
    103 if ( $offset ) {
    104 $query .= " offset $offset ";
    105 }
    106 105 warn "SEARCH QUERY: $query\n" if $DEBUG;
    107 106 warn "SEARCH VALUES: ".Dumper( \@values ) if $DEBUG;
    108 my $sth = $self->SQL->prepare_cached( $query );
    107 my $sth = $self->SQL->prepare( $query );
    109 108 $sth->execute( @values );
    110 109 if ( $count ) {
    111 110 $result = $sth->fetchrow_arrayref;
    111 warn "COUNT: ". Dumper( $result ) if $DEBUG;
    112 112 $result = $result->[0];
    113 113 } else {
    114 114 $result = [];