Changeset View
Changeset View
Standalone View
Standalone View
src/applications/drydock/query/DrydockBlueprintQuery.php
<?php | <?php | ||||
final class DrydockBlueprintQuery extends DrydockQuery { | final class DrydockBlueprintQuery extends DrydockQuery { | ||||
private $ids; | private $ids; | ||||
private $phids; | private $phids; | ||||
private $blueprintClasses; | private $blueprintClasses; | ||||
private $datasourceQuery; | private $datasourceQuery; | ||||
private $disabled; | private $disabled; | ||||
private $authorizedPHIDs; | private $authorizedPHIDs; | ||||
private $identifiers; | |||||
private $identifierIDs; | |||||
private $identifierPHIDs; | |||||
private $identifierMap; | |||||
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; | ||||
Show All 20 Lines | final class DrydockBlueprintQuery extends DrydockQuery { | ||||
} | } | ||||
public function withNameNgrams($ngrams) { | public function withNameNgrams($ngrams) { | ||||
return $this->withNgramsConstraint( | return $this->withNgramsConstraint( | ||||
new DrydockBlueprintNameNgrams(), | new DrydockBlueprintNameNgrams(), | ||||
$ngrams); | $ngrams); | ||||
} | } | ||||
public function withIdentifiers(array $identifiers) { | |||||
if (!$identifiers) { | |||||
throw new Exception( | |||||
pht( | |||||
'Can not issue a query with an empty identifier list.')); | |||||
} | |||||
$this->identifiers = $identifiers; | |||||
$ids = array(); | |||||
$phids = array(); | |||||
foreach ($identifiers as $identifier) { | |||||
if (ctype_digit($identifier)) { | |||||
$ids[] = $identifier; | |||||
} else { | |||||
$phids[] = $identifier; | |||||
} | |||||
} | |||||
$this->identifierIDs = $ids; | |||||
$this->identifierPHIDs = $phids; | |||||
return $this; | |||||
} | |||||
public function getIdentifierMap() { | |||||
if ($this->identifierMap === null) { | |||||
throw new Exception( | |||||
pht( | |||||
'Execute a query with identifiers before getting the '. | |||||
'identifier map.')); | |||||
} | |||||
return $this->identifierMap; | |||||
} | |||||
public function newResultObject() { | public function newResultObject() { | ||||
return new DrydockBlueprint(); | return new DrydockBlueprint(); | ||||
} | } | ||||
protected function getPrimaryTableAlias() { | protected function getPrimaryTableAlias() { | ||||
return 'blueprint'; | return 'blueprint'; | ||||
} | } | ||||
protected function loadPage() { | protected function loadPage() { | ||||
return $this->loadStandardPage($this->newResultObject()); | return $this->loadStandardPage($this->newResultObject()); | ||||
} | } | ||||
protected function willExecute() { | |||||
if ($this->identifiers) { | |||||
$this->identifierMap = array(); | |||||
} else { | |||||
$this->identifierMap = null; | |||||
} | |||||
} | |||||
protected function willFilterPage(array $blueprints) { | protected function willFilterPage(array $blueprints) { | ||||
$impls = DrydockBlueprintImplementation::getAllBlueprintImplementations(); | $impls = DrydockBlueprintImplementation::getAllBlueprintImplementations(); | ||||
foreach ($blueprints as $key => $blueprint) { | foreach ($blueprints as $key => $blueprint) { | ||||
$impl = idx($impls, $blueprint->getClassName()); | $impl = idx($impls, $blueprint->getClassName()); | ||||
if (!$impl) { | if (!$impl) { | ||||
$this->didRejectResult($blueprint); | $this->didRejectResult($blueprint); | ||||
unset($blueprints[$key]); | unset($blueprints[$key]); | ||||
continue; | continue; | ||||
} | } | ||||
$impl = clone $impl; | $impl = clone $impl; | ||||
$blueprint->attachImplementation($impl); | $blueprint->attachImplementation($impl); | ||||
} | } | ||||
if ($this->identifiers) { | |||||
$id_map = mpull($blueprints, null, 'getID'); | |||||
$phid_map = mpull($blueprints, null, 'getPHID'); | |||||
$map = $this->identifierMap; | |||||
foreach ($this->identifierIDs as $id) { | |||||
if (isset($id_map[$id])) { | |||||
$map[$id] = $id_map[$id]; | |||||
} | |||||
} | |||||
foreach ($this->identifierPHIDs as $phid) { | |||||
if (isset($phid_map[$phid])) { | |||||
$map[$phid] = $phid_map[$phid]; | |||||
} | |||||
} | |||||
// Just for consistency, reorder the map to match input order. | |||||
$map = array_select_keys($map, $this->identifiers); | |||||
$this->identifierMap = $map; | |||||
} | |||||
return $blueprints; | return $blueprints; | ||||
} | } | ||||
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { | protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { | ||||
$where = parent::buildWhereClauseParts($conn); | $where = parent::buildWhereClauseParts($conn); | ||||
if ($this->ids !== null) { | if ($this->ids !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
Show All 25 Lines | protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { | ||||
if ($this->disabled !== null) { | if ($this->disabled !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn, | $conn, | ||||
'blueprint.isDisabled = %d', | 'blueprint.isDisabled = %d', | ||||
(int)$this->disabled); | (int)$this->disabled); | ||||
} | } | ||||
if ($this->identifiers !== null) { | |||||
$parts = array(); | |||||
if ($this->identifierIDs) { | |||||
$parts[] = qsprintf( | |||||
$conn, | |||||
'blueprint.id IN (%Ld)', | |||||
$this->identifierIDs); | |||||
} | |||||
if ($this->identifierPHIDs) { | |||||
$parts[] = qsprintf( | |||||
$conn, | |||||
'blueprint.phid IN (%Ls)', | |||||
$this->identifierPHIDs); | |||||
} | |||||
$where[] = qsprintf( | |||||
$conn, | |||||
'%LO', | |||||
$parts); | |||||
} | |||||
return $where; | return $where; | ||||
} | } | ||||
protected function shouldGroupQueryResultRows() { | protected function shouldGroupQueryResultRows() { | ||||
if ($this->authorizedPHIDs !== null) { | if ($this->authorizedPHIDs !== null) { | ||||
return true; | return true; | ||||
} | } | ||||
return parent::shouldGroupQueryResultRows(); | return parent::shouldGroupQueryResultRows(); | ||||
Show All 23 Lines |
Content licensed under Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) unless otherwise noted; code licensed under Apache 2.0 or other open source licenses. · CC BY-SA 4.0 · Apache 2.0