- my $db = $dbh->selectall_hashref('SELECT * FROM ' . table_name() .
- ' WHERE distribution = ?',
- 'package', undef, $distribution);
+ my %options = @_;
+
+ my $q = 'SELECT * FROM ' . table_name()
+ . ' WHERE distribution = ? ';
+ my @args = ($distribution);
+ if ($options{state} && uc($options{state}) ne "ALL") {
+ $q .= ' AND upper(state) = ? ';
+ push @args, uc($options{state});
+ }
+
+ if ($options{user}) {
+ #this basically means "this user, or no user at all":
+ $q .= ' AND (builder = ? OR upper(state) = ?)';
+ push @args, $options{user};
+ push @args, "NEEDS-BUILD";
+ }
+
+ if ($options{category}) {
+ $q .= ' AND failed_category <> ? AND upper(state) = ? ';
+ push @args, $options{category};
+ push @args, "FAILED";
+ }
+
+ if ($options{list_min_age} > 0) {
+ $q .= ' AND age(state_change::timestamp) > ? ';
+ push @args, $options{list_min_age} . " days";
+ }
+
+ if ($options{list_min_age} < 0) {
+ $q .= ' AND age(state_change::timestamp) < ? days ';
+ push @args, -$options{list_min_age} . " days";
+ }
+
+ my $db = $dbh->selectall_hashref($q, 'package', undef, @args);