Changeset View
Changeset View
Standalone View
Standalone View
src/applications/harbormaster/query/HarbormasterBuildItemQuery.php
<?php | <?php | ||||
final class HarbormasterBuildItemQuery | final class HarbormasterBuildItemQuery | ||||
extends PhabricatorCursorPagedPolicyAwareQuery { | extends PhabricatorCursorPagedPolicyAwareQuery { | ||||
private $ids; | private $ids; | ||||
private $phids; | private $phids; | ||||
private $buildTargetPHIDs; | |||||
private $types; | |||||
public function withIDs(array $ids) { | public function withIDs(array $ids) { | ||||
$this->ids = $ids; | $this->ids = $ids; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withPHIDs(array $phids) { | public function withPHIDs(array $phids) { | ||||
$this->phids = $phids; | $this->phids = $phids; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withBuildTargetPHIDs(array $phids) { | |||||
$this->buildTargetPHIDs = $phids; | |||||
return $this; | |||||
} | |||||
public function withTypes(array $types) { | |||||
$this->types = $types; | |||||
return $this; | |||||
} | |||||
protected function loadPage() { | protected function loadPage() { | ||||
$table = new HarbormasterBuildItem(); | $table = new HarbormasterBuildItem(); | ||||
$conn_r = $table->establishConnection('r'); | $conn_r = $table->establishConnection('r'); | ||||
$data = queryfx_all( | $data = queryfx_all( | ||||
$conn_r, | $conn_r, | ||||
'SELECT * FROM %T %Q %Q %Q', | 'SELECT * FROM %T %Q %Q %Q', | ||||
$table->getTableName(), | $table->getTableName(), | ||||
$this->buildWhereClause($conn_r), | $this->buildWhereClause($conn_r), | ||||
$this->buildOrderClause($conn_r), | $this->buildOrderClause($conn_r), | ||||
$this->buildLimitClause($conn_r)); | $this->buildLimitClause($conn_r)); | ||||
return $table->loadAllFromArray($data); | return $table->loadAllFromArray($data); | ||||
} | } | ||||
protected function willFilterPage(array $page) { | |||||
$build_targets = array(); | |||||
$build_target_phids = array_filter(mpull($page, 'getBuildTargetPHID')); | |||||
if ($build_target_phids) { | |||||
$build_targets = id(new HarbormasterBuildTargetQuery()) | |||||
->setViewer($this->getViewer()) | |||||
->withPHIDs($build_target_phids) | |||||
->setParentQuery($this) | |||||
->execute(); | |||||
$build_targets = mpull($build_targets, null, 'getPHID'); | |||||
} | |||||
foreach ($page as $key => $build_log) { | |||||
$build_target_phid = $build_log->getBuildTargetPHID(); | |||||
if (empty($build_targets[$build_target_phid])) { | |||||
unset($page[$key]); | |||||
continue; | |||||
} | |||||
$build_log->attachBuildTarget($build_targets[$build_target_phid]); | |||||
} | |||||
return $page; | |||||
} | |||||
private function buildWhereClause(AphrontDatabaseConnection $conn_r) { | private function buildWhereClause(AphrontDatabaseConnection $conn_r) { | ||||
$where = array(); | $where = array(); | ||||
if ($this->ids) { | if ($this->ids) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn_r, | $conn_r, | ||||
'id IN (%Ld)', | 'id IN (%Ld)', | ||||
$this->ids); | $this->ids); | ||||
} | } | ||||
if ($this->phids) { | if ($this->phids) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn_r, | $conn_r, | ||||
'phid in (%Ls)', | 'phid in (%Ls)', | ||||
$this->phids); | $this->phids); | ||||
} | } | ||||
if ($this->buildTargetPHIDs) { | |||||
$where[] = qsprintf( | |||||
$conn_r, | |||||
'buildTargetPHID in (%Ls)', | |||||
$this->buildTargetPHIDs); | |||||
} | |||||
if ($this->types) { | |||||
$where[] = qsprintf( | |||||
$conn_r, | |||||
'type in (%Ls)', | |||||
$this->types); | |||||
} | |||||
$where[] = $this->buildPagingClause($conn_r); | $where[] = $this->buildPagingClause($conn_r); | ||||
return $this->formatWhereClause($where); | return $this->formatWhereClause($where); | ||||
} | } | ||||
public function getQueryApplicationClass() { | public function getQueryApplicationClass() { | ||||
return 'PhabricatorHarbormasterApplication'; | return 'PhabricatorHarbormasterApplication'; | ||||
} | } | ||||
} | } |