+=head2 get_bugs_by_db
+
+This routine uses the database to try to speed up
+searches.
+
+
+=cut
+
+state $_get_bugs_by_db_options =
+ {hash_slice(%_get_bugs_common_options,
+ (qw(package submitter severity tag archive),
+ qw(owner src maint bugs correspondent),
+ qw(affects usertags))
+ ),
+ schema => {type => OBJECT,
+ },
+ };
+sub get_bugs_by_db{
+ my %param = validate_with(params => \@_,
+ spec => $_get_bugs_by_db_options,
+ );
+ my %bugs = ();
+
+ my $keys = grep {$_ !~ $_non_search_key_regex} keys(%param);
+ die "Need at least 1 key to search by" unless $keys;
+ my $rs = $param{schema}->resultset('Bug');
+ if (exists $param{package}) {
+ $rs = $rs->search({-or => {map 'bin_package.'}})
+ }
+ if (exists $param{severity}) {
+ $rs = $rs->search({'severity.severity' =>
+ [make_list($param{severity})],
+ },
+ {join => 'severity'},
+ );
+ }
+ for my $key (qw(owner submitter done)) {
+ if (exists $param{$key}) {
+ $rs = $rs->search({"${key}.addr" =>
+ [make_list($param{$key})],
+ },
+ {join => $key},
+ );
+ }
+ }
+ if (exists $param{correspondent}) {
+ $rs = $rs->search({'message_correspondents.addr' =>
+ [make_list($param{correspondent})],
+ },
+ {join => {correspondent =>
+ {bug_messages =>
+ {message => 'message_correspondents'}}}},
+ );
+ }
+ if (exists $param{affects}) {
+ my @aff_list = make_list($param{affects});
+ $rs = $rs->search({-or => {'bin_pkg.pkg' =>
+ [@aff_list],
+ 'src_pkg.pkg' =>
+ [@aff_list],
+ },
+ },
+ {join => [{bug_affects_binpackages => 'bin_pkg'},
+ {bug_affects_srcpackages => 'src_pkg'},
+ ],
+ },
+ );
+ }
+ if (exists $param{package}) {
+ $rs = $rs->search({'bin_pkg.pkg' =>
+ [make_list($param{package})],
+ },
+ {join => {bug_binpackages => 'bin_pkg'}});
+ }
+ if (exists $param{maintainer}) {
+ my @maint_list =
+ map {$_ eq '' ? undef : $_}
+ make_list($param{maintainer});
+ $rs = $rs->search({-or => {correspondent => [@maint_list],
+ correspondent2 => [@maint_list],
+ },
+ },
+ {join => {bug_affects_binpackage =>
+ {bin_pkg =>
+ {bin_ver =>
+ {src_ver =>
+ {maintainer => 'correspondent'}
+ }}},
+ {bug_affects_srcpackage =>
+ {src_pkg =>
+ {src_ver =>
+ {maintainer => 'correspondent'}
+ }}}}
+ }
+ );
+ }
+ if (exists $param{src}) {
+ $rs = $rs->search({-or => {map {('src_pkg.pkg' => $_)}
+ make_list($param{src})},
+ },
+ {join => {bug_srcpackages => 'src_pkg'}});
+ }
+ # tags are very odd, because we must handle usertags.
+ if (exists $param{tag}) {
+ # bugs from usertags which matter
+ my %bugs_matching_usertags;
+ for my $bug (make_list(grep {defined $_ }
+ @{$param{usertags}}{make_list($param{tag})})) {
+ $bugs_matching_usertags{$bug} = 1;
+ }
+ # we want all bugs which either match the tag name given in
+ # param, or have a usertag set which matches one of the tag
+ # names given in param.
+ $rs = $rs->search({-or => {map {('tag.tag' => $_)}
+ make_list($param{tag}),
+ map {('me.id' => $_)}
+ keys %bugs_matching_usertags
+ },
+ },
+ {join => {bug_tags => 'tag'}});
+ }
+ if (exists $param{bugs}) {
+ $rs = $rs->search({-or => {map {('me.id' => $_)}
+ make_list($param{bugs})}
+ });
+ }
+ # handle archive
+ if (defined $param{archive} and $param{archive} ne 'both') {
+ $rs = $rs->search({'me.archived' => $param{archive}});
+ }
+ return $rs->get_column('id')->all();
+}
+
+