diff options
author | Byron Jones <bjones@mozilla.com> | 2013-09-25 18:17:52 +0200 |
---|---|---|
committer | Byron Jones <bjones@mozilla.com> | 2013-09-25 18:17:52 +0200 |
commit | 3d49752f959fd6c2b5e88eef8333b15705614f68 (patch) | |
tree | 6ce6213a7ed74b77a3c8f59463d7d907e9acac65 | |
parent | ac5fe08d4f2390f54888a7a8806e829b68b27ec5 (diff) | |
download | bugzilla-3d49752f959fd6c2b5e88eef8333b15705614f68.tar.gz bugzilla-3d49752f959fd6c2b5e88eef8333b15705614f68.tar.xz |
Bug 920060: Bug.search API doesn't return total results
-rw-r--r-- | Bugzilla/Search.pm | 13 | ||||
-rw-r--r-- | Bugzilla/WebService/Bug.pm | 3 | ||||
-rw-r--r-- | extensions/Ember/lib/WebService.pm | 63 |
3 files changed, 76 insertions, 3 deletions
diff --git a/Bugzilla/Search.pm b/Bugzilla/Search.pm index aff189c43..f51a12372 100644 --- a/Bugzilla/Search.pm +++ b/Bugzilla/Search.pm @@ -753,6 +753,9 @@ sub data { # Restore the original 'fields' argument, just in case. $self->{fields} = \@orig_fields unless $all_in_bugs_table; + # BMO if the caller only wants the count, that's all we need to return + return $bug_ids->[0]->[0] if $self->_params->{count_only}; + # If there are no bugs found, or all fields are in the 'bugs' table, # there is no need for another query. if (!scalar @$bug_ids || $all_in_bugs_table) { @@ -811,7 +814,15 @@ sub _sql { ? "\nORDER BY " . join(', ', $self->_sql_order_by) : ''; my $limit = $self->_sql_limit; $limit = "\n$limit" if $limit; - + + # BMO allow fetching just the number of matching bugs + if ($self->_params->{count_only}) { + $select = 'COUNT(*) AS count'; + $group_by = ''; + $order_by = ''; + $limit = ''; + } + my $query = <<END; SELECT $select FROM $from diff --git a/Bugzilla/WebService/Bug.pm b/Bugzilla/WebService/Bug.pm index 8461b3d97..a107da923 100644 --- a/Bugzilla/WebService/Bug.pm +++ b/Bugzilla/WebService/Bug.pm @@ -524,6 +524,9 @@ sub search { my $search = new Bugzilla::Search(%options); my ($data) = $search->data; + # BMO if the caller only wants the count, that's all we need to return + return $data if $params->{count_only}; + if (!scalar @$data) { return { bugs => [] }; } diff --git a/extensions/Ember/lib/WebService.pm b/extensions/Ember/lib/WebService.pm index 423c8de34..57ce2fe36 100644 --- a/extensions/Ember/lib/WebService.pm +++ b/extensions/Ember/lib/WebService.pm @@ -25,7 +25,7 @@ use Bugzilla::Util qw(trick_taint); use Bugzilla::Extension::Ember::FakeBug; use Scalar::Util qw(blessed); -use Data::Dumper; +use Storable qw(dclone); use constant FIELD_TYPE_MAP => { 0 => 'unknown', @@ -203,6 +203,23 @@ sub show { return $data; } +sub search { + my ($self, $params) = @_; + + my $total; + if (exists $params->{offset} && exists $params->{limit}) { + my $count_params = dclone($params); + delete $count_params->{offset}; + delete $count_params->{limit}; + $count_params->{count_only} = 1; + $total = $self->SUPER::search($count_params); + } + + my $result = $self->SUPER::search($params); + $result->{total} = defined $total ? $total : scalar(@{ $result->{bugs} }); + return $result; +} + ################### # Private Methods # ################### @@ -529,7 +546,14 @@ sub rest_resources { GET => { method => 'show' } - } + }, + # search - wrapper around SUPER::search which also includes the total + # number of bugs when using pagination + qr{^/ember/search$}, { + GET => { + method => 'search', + }, + }, ]; }; @@ -636,3 +660,38 @@ You pass a field called C<id> that is the current bug id. =back =back + +=head2 search + +B<UNSTABLE> + +=over + +=item B<Description> + +A wrapper around Bugzilla's C<search> method which also returns the total of +bugs matching a query, even if the limit and offset parameters are supplied. + +=item B<Params> + +As per Bugzilla::WebService::Bug::search() + +=item B<Returns> + +=over + +=back + +=item B<Errors> + +=over + +=back + +=item B<History> + +=over + +=back + +=back |