]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 453743: Decrease the number of calls to the DB about flags when viewing a bug...
authorlpsolit%gmail.com <>
Mon, 8 Sep 2008 21:23:46 +0000 (21:23 +0000)
committerlpsolit%gmail.com <>
Mon, 8 Sep 2008 21:23:46 +0000 (21:23 +0000)
Bugzilla/Bug.pm

index 37e4ff5bc2eedf2ea687d01b2e88ba5b66ccb9e1..5ba9d9a4629f8eabc5857404128dfd3528eb8b33 100644 (file)
@@ -2228,8 +2228,19 @@ sub attachments {
     return $self->{'attachments'} if exists $self->{'attachments'};
     return [] if $self->{'error'};
 
-    $self->{'attachments'} =
-        Bugzilla::Attachment->get_attachments_by_bug($self->bug_id);
+    my $attachments = Bugzilla::Attachment->get_attachments_by_bug($self->bug_id);
+    $_->{'flags'} = [] foreach @$attachments;
+    my %att = map { $_->id => $_ } @$attachments;
+
+    # Retrieve all attachment flags at once for this bug, and group them
+    # by attachment. We populate attachment flags here to avoid querying
+    # the DB for each attachment individually later.
+    my $flags = Bugzilla::Flag->match({ 'bug_id'      => $self->bug_id,
+                                        'target_type' => 'attachment' });
+
+    push(@{$att{$_->attach_id}->{'flags'}}, $_) foreach @$flags;
+
+    $self->{'attachments'} = [sort {$a->id <=> $b->id} values %att];
     return $self->{'attachments'};
 }
 
@@ -2343,14 +2354,20 @@ sub flag_types {
          'product_id'   => $self->{'product_id'}, 
          'component_id' => $self->{'component_id'} });
 
-    foreach my $flag_type (@$flag_types) {
-        $flag_type->{'flags'} = Bugzilla::Flag->match(
-            { 'bug_id'      => $self->bug_id,
-              'type_id'     => $flag_type->{'id'},
-              'target_type' => 'bug' });
-    }
+    $_->{'flags'} = [] foreach @$flag_types;
+    my %flagtypes = map { $_->id => $_ } @$flag_types;
+
+    # Retrieve all bug flags at once for this bug and group them
+    # by flag types.
+    my $flags = Bugzilla::Flag->match({ 'bug_id'      => $self->bug_id,
+                                        'target_type' => 'bug' });
+
+    # Call the internal 'type_id' variable instead of the method
+    # to not create a flagtype object.
+    push(@{$flagtypes{$_->{'type_id'}}->{'flags'}}, $_) foreach @$flags;
 
-    $self->{'flag_types'} = $flag_types;
+    $self->{'flag_types'} =
+      [sort {$a->sortkey <=> $b->sortkey || $a->name cmp $b->name} values %flagtypes];
 
     return $self->{'flag_types'};
 }