| 1 | package Test2::Harness::UI::Queries; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.000150'; | ||
| 6 | |||
| 7 | use Carp qw/croak/; | ||
| 8 | |||
| 9 | use Test2::Harness::UI::Util::HashBase qw/-config/; | ||
| 10 | |||
| 11 | use Test2::Harness::UI::UUID qw/uuid_deflate/; | ||
| 12 | |||
| 13 | sub init { | ||
| 14 | my $self = shift; | ||
| 15 | |||
| 16 | croak "'config' is a required attribute" | ||
| 17 | unless $self->{+CONFIG}; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub projects { | ||
| 21 | my $self = shift; | ||
| 22 | |||
| 23 | my $dbh = $self->{+CONFIG}->connect; | ||
| 24 | |||
| 25 | my $sth = $dbh->prepare('SELECT name FROM projects ORDER BY name ASC'); | ||
| 26 | $sth->execute() or die $sth->errstr; | ||
| 27 | my $rows = $sth->fetchall_arrayref; | ||
| 28 | return [map { $_->[0] } @$rows]; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub versions { $_[0]->_from_project(version => $_[1]) } | ||
| 32 | sub categories { $_[0]->_from_project(category => $_[1]) } | ||
| 33 | sub tiers { $_[0]->_from_project(tier => $_[1]) } | ||
| 34 | sub builds { $_[0]->_from_project(build => $_[1]) } | ||
| 35 | |||
| 36 | sub _from_project { | ||
| 37 | my $self = shift; | ||
| 38 | my ($field, $project_name) = @_; | ||
| 39 | |||
| 40 | croak "project_name is required" | ||
| 41 | unless defined $project_name; | ||
| 42 | |||
| 43 | my %VALID_FIELDS = map { $_ => 1 } qw/version category tier build/; | ||
| 44 | croak "Invalid field: $field" unless $VALID_FIELDS{$field}; | ||
| 45 | |||
| 46 | my $dbh = $self->{+CONFIG}->connect; | ||
| 47 | |||
| 48 | my $schema = $self->{+CONFIG}->schema; | ||
| 49 | my $project = $schema->resultset('Project')->find({name => $project_name}) or return []; | ||
| 50 | |||
| 51 | my $sth = $dbh->prepare("SELECT distinct($field) FROM runs WHERE project_id = ? ORDER BY $field ASC"); | ||
| 52 | $sth->execute(uuid_deflate($project->project_id)) or die $sth->errstr; | ||
| 53 | my $rows = $sth->fetchall_arrayref; | ||
| 54 | return [map { $_->[0] } @$rows]; | ||
| 55 | } | ||
| 56 | |||
| 57 | 1; | ||
| 58 | |||
| 59 | __END__ |