2012-12-17 13:53:32 -08:00
|
|
|
<?php
|
|
|
|
|
2013-12-26 10:41:36 -08:00
|
|
|
final class DrydockLeaseQuery
|
|
|
|
extends PhabricatorCursorPagedPolicyAwareQuery {
|
2012-12-17 13:53:32 -08:00
|
|
|
|
|
|
|
private $ids;
|
2013-12-26 12:29:58 -08:00
|
|
|
private $phids;
|
2012-12-17 13:53:32 -08:00
|
|
|
private $resourceIDs;
|
2013-12-26 10:42:00 -08:00
|
|
|
private $statuses;
|
2012-12-17 13:53:32 -08:00
|
|
|
|
|
|
|
public function withIDs(array $ids) {
|
|
|
|
$this->ids = $ids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-12-26 12:29:58 -08:00
|
|
|
public function withPHIDs(array $phids) {
|
|
|
|
$this->phids = $phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-12-26 10:41:36 -08:00
|
|
|
public function withResourceIDs(array $ids) {
|
|
|
|
$this->resourceIDs = $ids;
|
2012-12-17 13:53:32 -08:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-12-26 10:42:00 -08:00
|
|
|
public function withStatuses(array $statuses) {
|
|
|
|
$this->statuses = $statuses;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-12-26 10:41:36 -08:00
|
|
|
public function loadPage() {
|
2012-12-17 13:53:32 -08:00
|
|
|
$table = new DrydockLease();
|
|
|
|
$conn_r = $table->establishConnection('r');
|
|
|
|
|
|
|
|
$data = queryfx_all(
|
|
|
|
$conn_r,
|
|
|
|
'SELECT lease.* FROM %T lease %Q %Q %Q',
|
|
|
|
$table->getTableName(),
|
|
|
|
$this->buildWhereClause($conn_r),
|
|
|
|
$this->buildOrderClause($conn_r),
|
|
|
|
$this->buildLimitClause($conn_r));
|
|
|
|
|
2013-12-26 10:41:36 -08:00
|
|
|
return $table->loadAllFromArray($data);
|
|
|
|
}
|
2012-12-17 13:53:32 -08:00
|
|
|
|
2013-12-26 10:41:36 -08:00
|
|
|
public function willFilterPage(array $leases) {
|
|
|
|
$resources = id(new DrydockResourceQuery())
|
|
|
|
->setParentQuery($this)
|
|
|
|
->setViewer($this->getViewer())
|
|
|
|
->withIDs(mpull($leases, 'getResourceID'))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
foreach ($leases as $key => $lease) {
|
|
|
|
$resource = idx($resources, $lease->getResourceID());
|
|
|
|
if (!$resource) {
|
|
|
|
unset($leases[$key]);
|
|
|
|
continue;
|
2012-12-17 13:53:32 -08:00
|
|
|
}
|
2013-12-26 10:41:36 -08:00
|
|
|
$lease->attachResource($resource);
|
2012-12-17 13:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $leases;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
|
|
|
|
$where = array();
|
|
|
|
|
|
|
|
if ($this->resourceIDs) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'resourceID IN (%Ld)',
|
|
|
|
$this->resourceIDs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->ids) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'id IN (%Ld)',
|
|
|
|
$this->ids);
|
|
|
|
}
|
|
|
|
|
2013-12-26 12:29:58 -08:00
|
|
|
if ($this->phids) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'phid IN (%Ls)',
|
|
|
|
$this->phids);
|
|
|
|
}
|
|
|
|
|
2013-12-26 10:42:00 -08:00
|
|
|
if ($this->statuses) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'status IN (%Ld)',
|
|
|
|
$this->statuses);
|
|
|
|
}
|
|
|
|
|
2013-12-26 10:41:36 -08:00
|
|
|
$where[] = $this->buildPagingClause($conn_r);
|
|
|
|
|
2012-12-17 13:53:32 -08:00
|
|
|
return $this->formatWhereClause($where);
|
|
|
|
}
|
|
|
|
|
2013-12-26 10:41:36 -08:00
|
|
|
public function getQueryApplicationClass() {
|
|
|
|
return 'PhabricatorApplicationDrydock';
|
2012-12-17 13:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|