2012-12-17 22:53:32 +01:00
|
|
|
<?php
|
|
|
|
|
2013-12-26 19:41:36 +01:00
|
|
|
final class DrydockLeaseQuery
|
|
|
|
extends PhabricatorCursorPagedPolicyAwareQuery {
|
2012-12-17 22:53:32 +01:00
|
|
|
|
|
|
|
private $ids;
|
|
|
|
private $resourceIDs;
|
2013-12-26 19:42:00 +01:00
|
|
|
private $statuses;
|
2012-12-17 22:53:32 +01:00
|
|
|
|
|
|
|
public function withIDs(array $ids) {
|
|
|
|
$this->ids = $ids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-12-26 19:41:36 +01:00
|
|
|
public function withResourceIDs(array $ids) {
|
|
|
|
$this->resourceIDs = $ids;
|
2012-12-17 22:53:32 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-12-26 19:42:00 +01:00
|
|
|
public function withStatuses(array $statuses) {
|
|
|
|
$this->statuses = $statuses;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-12-26 19:41:36 +01:00
|
|
|
public function loadPage() {
|
2012-12-17 22:53:32 +01: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 19:41:36 +01:00
|
|
|
return $table->loadAllFromArray($data);
|
|
|
|
}
|
2012-12-17 22:53:32 +01:00
|
|
|
|
2013-12-26 19:41:36 +01: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 22:53:32 +01:00
|
|
|
}
|
2013-12-26 19:41:36 +01:00
|
|
|
$lease->attachResource($resource);
|
2012-12-17 22:53:32 +01: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 19:42:00 +01:00
|
|
|
if ($this->statuses) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'status IN (%Ld)',
|
|
|
|
$this->statuses);
|
|
|
|
}
|
|
|
|
|
2013-12-26 19:41:36 +01:00
|
|
|
$where[] = $this->buildPagingClause($conn_r);
|
|
|
|
|
2012-12-17 22:53:32 +01:00
|
|
|
return $this->formatWhereClause($where);
|
|
|
|
}
|
|
|
|
|
2013-12-26 19:41:36 +01:00
|
|
|
public function getQueryApplicationClass() {
|
|
|
|
return 'PhabricatorApplicationDrydock';
|
2012-12-17 22:53:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|