From 4dd427ea99673391d923db9682836d344f178b54 Mon Sep 17 00:00:00 2001 From: "lpsolit%gmail.com" <> Date: Mon, 8 Sep 2008 21:21:24 +0000 Subject: Bug 453743: Decrease the number of calls to the DB about flags when viewing a bug - Patch by Frédéric Buclin r/a=mkanat MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bugzilla/Attachment.pm | 60 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) (limited to 'Bugzilla/Attachment.pm') diff --git a/Bugzilla/Attachment.pm b/Bugzilla/Attachment.pm index 314227c87..fcaf38c9f 100644 --- a/Bugzilla/Attachment.pm +++ b/Bugzilla/Attachment.pm @@ -139,8 +139,6 @@ the ID of the bug to which the attachment is attached =cut -# XXX Once Bug.pm slims down sufficiently this should become a reference -# to a bug object. sub bug_id { my $self = shift; return $self->{bug_id}; @@ -148,6 +146,24 @@ sub bug_id { =over +=item C + +the bug object to which the attachment is attached + +=back + +=cut + +sub bug { + my $self = shift; + + require Bugzilla::Bug; + $self->{bug} = Bugzilla::Bug->new($self->bug_id); + return $self->{bug}; +} + +=over + =item C user-provided text describing the attachment @@ -430,6 +446,30 @@ sub flags { return $self->{flags}; } +=over + +=item C + +Return all flag types available for this attachment as well as flags +already set, grouped by flag type. + +=back + +=cut + +sub flag_types { + my $self = shift; + return $self->{flag_types} if exists $self->{flag_types}; + + my $vars = { target_type => 'attachment', + product_id => $self->bug->product_id, + component_id => $self->bug->component_id, + attach_id => $self->id }; + + $self->{flag_types} = Bugzilla::Flag::_flag_types($vars); + return $self->{flag_types}; +} + # Instance methods; no POD documentation here yet because the only ones so far # are private. @@ -538,7 +578,7 @@ Returns: a reference to an array of attachment objects. =cut sub get_attachments_by_bug { - my ($class, $bug_id) = @_; + my ($class, $bug_id, $vars) = @_; my $user = Bugzilla->user; my $dbh = Bugzilla->dbh; @@ -556,6 +596,20 @@ sub get_attachments_by_bug { WHERE bug_id = ? $and_restriction", undef, @values); my $attachments = Bugzilla::Attachment->get_list($attach_ids); + + # To avoid $attachment->flags to run SQL queries itself for each + # attachment listed here, we collect all the data at once and + # populate $attachment->{flags} ourselves. + if ($vars->{preload}) { + $_->{flags} = [] foreach @$attachments; + my %att = map { $_->id => $_ } @$attachments; + + my $flags = Bugzilla::Flag->match({ bug_id => $bug_id, + target_type => 'attachment' }); + + push(@{$att{$_->attach_id}->{flags}}, $_) foreach @$flags; + $attachments = [sort {$a->id <=> $b->id} values %att]; + } return $attachments; } -- cgit v1.2.3-24-g4f1b