Path: blob/master/src/applications/almanac/query/AlmanacInterfaceQuery.php
12256 views
<?php12final class AlmanacInterfaceQuery3extends AlmanacQuery {45private $ids;6private $phids;7private $networkPHIDs;8private $devicePHIDs;9private $addresses;1011public function withIDs(array $ids) {12$this->ids = $ids;13return $this;14}1516public function withPHIDs(array $phids) {17$this->phids = $phids;18return $this;19}2021public function withNetworkPHIDs(array $phids) {22$this->networkPHIDs = $phids;23return $this;24}2526public function withDevicePHIDs(array $phids) {27$this->devicePHIDs = $phids;28return $this;29}3031public function withAddresses(array $addresses) {32$this->addresses = $addresses;33return $this;34}3536public function newResultObject() {37return new AlmanacInterface();38}3940protected function willFilterPage(array $interfaces) {41$network_phids = mpull($interfaces, 'getNetworkPHID');42$device_phids = mpull($interfaces, 'getDevicePHID');4344$networks = id(new AlmanacNetworkQuery())45->setParentQuery($this)46->setViewer($this->getViewer())47->withPHIDs($network_phids)48->needProperties($this->getNeedProperties())49->execute();50$networks = mpull($networks, null, 'getPHID');5152$devices = id(new AlmanacDeviceQuery())53->setParentQuery($this)54->setViewer($this->getViewer())55->withPHIDs($device_phids)56->needProperties($this->getNeedProperties())57->execute();58$devices = mpull($devices, null, 'getPHID');5960foreach ($interfaces as $key => $interface) {61$network = idx($networks, $interface->getNetworkPHID());62$device = idx($devices, $interface->getDevicePHID());63if (!$network || !$device) {64$this->didRejectResult($interface);65unset($interfaces[$key]);66continue;67}6869$interface->attachNetwork($network);70$interface->attachDevice($device);71}7273return $interfaces;74}7576protected function buildSelectClauseParts(AphrontDatabaseConnection $conn) {77$select = parent::buildSelectClauseParts($conn);7879if ($this->shouldJoinDeviceTable()) {80$select[] = qsprintf($conn, 'device.name');81}8283return $select;84}8586protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {87$where = parent::buildWhereClauseParts($conn);8889if ($this->ids !== null) {90$where[] = qsprintf(91$conn,92'interface.id IN (%Ld)',93$this->ids);94}9596if ($this->phids !== null) {97$where[] = qsprintf(98$conn,99'interface.phid IN (%Ls)',100$this->phids);101}102103if ($this->networkPHIDs !== null) {104$where[] = qsprintf(105$conn,106'interface.networkPHID IN (%Ls)',107$this->networkPHIDs);108}109110if ($this->devicePHIDs !== null) {111$where[] = qsprintf(112$conn,113'interface.devicePHID IN (%Ls)',114$this->devicePHIDs);115}116117if ($this->addresses !== null) {118$parts = array();119foreach ($this->addresses as $address) {120$parts[] = qsprintf(121$conn,122'(interface.networkPHID = %s '.123'AND interface.address = %s '.124'AND interface.port = %d)',125$address->getNetworkPHID(),126$address->getAddress(),127$address->getPort());128}129$where[] = qsprintf($conn, '%LO', $parts);130}131132return $where;133}134135protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {136$joins = parent::buildJoinClauseParts($conn);137138if ($this->shouldJoinDeviceTable()) {139$joins[] = qsprintf(140$conn,141'JOIN %T device ON device.phid = interface.devicePHID',142id(new AlmanacDevice())->getTableName());143}144145return $joins;146}147148protected function shouldGroupQueryResultRows() {149if ($this->shouldJoinDeviceTable()) {150return true;151}152153return parent::shouldGroupQueryResultRows();154}155156private function shouldJoinDeviceTable() {157$vector = $this->getOrderVector();158159if ($vector->containsKey('name')) {160return true;161}162163return false;164}165166protected function getPrimaryTableAlias() {167return 'interface';168}169170public function getQueryApplicationClass() {171return 'PhabricatorAlmanacApplication';172}173174public function getBuiltinOrders() {175return array(176'name' => array(177'vector' => array('name', 'id'),178'name' => pht('Device Name'),179),180) + parent::getBuiltinOrders();181}182183public function getOrderableColumns() {184return parent::getOrderableColumns() + array(185'name' => array(186'table' => 'device',187'column' => 'name',188'type' => 'string',189'reverse' => true,190),191);192}193194protected function newPagingMapFromCursorObject(195PhabricatorQueryCursor $cursor,196array $keys) {197198$interface = $cursor->getObject();199200return array(201'id' => (int)$interface->getID(),202'name' => $cursor->getRawRowProperty('device.name'),203);204}205206}207208209